any_of.pass.cpp 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. //===----------------------------------------------------------------------===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is dual licensed under the MIT and the University of Illinois Open
  6. // Source Licenses. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. // <algorithm>
  10. // template <class InputIterator, class Predicate>
  11. // constpexr bool // constexpr after C++17
  12. // any_of(InputIterator first, InputIterator last, Predicate pred);
  13. #include <algorithm>
  14. #include <cassert>
  15. #include "test_macros.h"
  16. #include "test_iterators.h"
  17. struct test1
  18. {
  19. TEST_CONSTEXPR bool operator()(const int& i) const
  20. {
  21. return i % 2 == 0;
  22. }
  23. };
  24. #if TEST_STD_VER > 17
  25. TEST_CONSTEXPR bool test_constexpr() {
  26. int ia[] = {2, 4, 6, 8};
  27. int ib[] = {1, 3, 5, 7};
  28. return std::any_of(std::begin(ia), std::end(ia), test1())
  29. && !std::any_of(std::begin(ib), std::end(ib), test1())
  30. ;
  31. }
  32. #endif
  33. int main()
  34. {
  35. {
  36. int ia[] = {2, 4, 6, 8};
  37. const unsigned sa = sizeof(ia)/sizeof(ia[0]);
  38. assert(std::any_of(input_iterator<const int*>(ia),
  39. input_iterator<const int*>(ia + sa), test1()) == true);
  40. assert(std::any_of(input_iterator<const int*>(ia),
  41. input_iterator<const int*>(ia), test1()) == false);
  42. }
  43. {
  44. const int ia[] = {2, 4, 5, 8};
  45. const unsigned sa = sizeof(ia)/sizeof(ia[0]);
  46. assert(std::any_of(input_iterator<const int*>(ia),
  47. input_iterator<const int*>(ia + sa), test1()) == true);
  48. assert(std::any_of(input_iterator<const int*>(ia),
  49. input_iterator<const int*>(ia), test1()) == false);
  50. }
  51. {
  52. const int ia[] = {1, 3, 5, 7};
  53. const unsigned sa = sizeof(ia)/sizeof(ia[0]);
  54. assert(std::any_of(input_iterator<const int*>(ia),
  55. input_iterator<const int*>(ia + sa), test1()) == false);
  56. assert(std::any_of(input_iterator<const int*>(ia),
  57. input_iterator<const int*>(ia), test1()) == false);
  58. }
  59. #if TEST_STD_VER > 17
  60. static_assert(test_constexpr());
  61. #endif
  62. }