all_of.pass.cpp 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. //===----------------------------------------------------------------------===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. // <algorithm>
  9. // template <class InputIterator, class Predicate>
  10. // constpexr bool // constexpr after C++17
  11. // all_of(InputIterator first, InputIterator last, Predicate pred);
  12. #include <algorithm>
  13. #include <cassert>
  14. #include "test_macros.h"
  15. #include "test_iterators.h"
  16. struct test1
  17. {
  18. TEST_CONSTEXPR bool operator()(const int& i) const
  19. {
  20. return i % 2 == 0;
  21. }
  22. };
  23. #if TEST_STD_VER > 17
  24. TEST_CONSTEXPR bool test_constexpr() {
  25. int ia[] = {2, 4, 6, 8};
  26. int ib[] = {2, 4, 5, 8};
  27. return std::all_of(std::begin(ia), std::end(ia), test1())
  28. && !std::all_of(std::begin(ib), std::end(ib), test1())
  29. ;
  30. }
  31. #endif
  32. int main(int, char**)
  33. {
  34. {
  35. int ia[] = {2, 4, 6, 8};
  36. const unsigned sa = sizeof(ia)/sizeof(ia[0]);
  37. assert(std::all_of(input_iterator<const int*>(ia),
  38. input_iterator<const int*>(ia + sa), test1()) == true);
  39. assert(std::all_of(input_iterator<const int*>(ia),
  40. input_iterator<const int*>(ia), test1()) == true);
  41. }
  42. {
  43. const int ia[] = {2, 4, 5, 8};
  44. const unsigned sa = sizeof(ia)/sizeof(ia[0]);
  45. assert(std::all_of(input_iterator<const int*>(ia),
  46. input_iterator<const int*>(ia + sa), test1()) == false);
  47. assert(std::all_of(input_iterator<const int*>(ia),
  48. input_iterator<const int*>(ia), test1()) == true);
  49. }
  50. #if TEST_STD_VER > 17
  51. static_assert(test_constexpr());
  52. #endif
  53. return 0;
  54. }