all_of.pass.cpp 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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. // all_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[] = {2, 4, 5, 8};
  28. return std::all_of(std::begin(ia), std::end(ia), test1())
  29. && !std::all_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::all_of(input_iterator<const int*>(ia),
  39. input_iterator<const int*>(ia + sa), test1()) == true);
  40. assert(std::all_of(input_iterator<const int*>(ia),
  41. input_iterator<const int*>(ia), test1()) == true);
  42. }
  43. {
  44. const int ia[] = {2, 4, 5, 8};
  45. const unsigned sa = sizeof(ia)/sizeof(ia[0]);
  46. assert(std::all_of(input_iterator<const int*>(ia),
  47. input_iterator<const int*>(ia + sa), test1()) == false);
  48. assert(std::all_of(input_iterator<const int*>(ia),
  49. input_iterator<const int*>(ia), test1()) == true);
  50. }
  51. #if TEST_STD_VER > 17
  52. static_assert(test_constexpr());
  53. #endif
  54. }