all_of.cpp 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. // Range v3 library
  2. //
  3. // Copyright Andrew Sutton 2014
  4. //
  5. // Use, modification and distribution is subject to the
  6. // Boost Software License, Version 1.0. (See accompanying
  7. // file LICENSE_1_0.txt or copy at
  8. // http://www.boost.org/LICENSE_1_0.txt)
  9. //
  10. // Project home: https://github.com/ericniebler/range-v3
  11. #include <vector>
  12. #include <range/v3/core.hpp>
  13. #include <range/v3/algorithm/all_of.hpp>
  14. #include "../simple_test.hpp"
  15. constexpr bool even(int n)
  16. {
  17. return n % 2 == 0;
  18. }
  19. struct S {
  20. S(bool p) : test(p) { }
  21. bool p() const { return test; }
  22. bool test;
  23. };
  24. constexpr bool test_constexpr(std::initializer_list<int> il)
  25. {
  26. return ranges::all_of(il, even);
  27. }
  28. int main()
  29. {
  30. std::vector<int> all_even { 0, 2, 4, 6 };
  31. std::vector<int> one_even { 1, 3, 4, 7 };
  32. std::vector<int> none_even { 1, 3, 5, 7 };
  33. CHECK(ranges::all_of(all_even.begin(), all_even.end(), even));
  34. CHECK(!ranges::all_of(one_even.begin(), one_even.end(), even));
  35. CHECK(!ranges::all_of(none_even.begin(), none_even.end(), even));
  36. CHECK(ranges::all_of(all_even, even));
  37. CHECK(!ranges::all_of(one_even, even));
  38. CHECK(!ranges::all_of(none_even, even));
  39. using ILI = std::initializer_list<int>;
  40. CHECK(ranges::all_of(ILI{0, 2, 4, 6}, [](int n) { return n % 2 == 0; }));
  41. CHECK(!ranges::all_of(ILI{1, 3, 4, 7}, [](int n) { return n % 2 == 0; }));
  42. CHECK(!ranges::all_of(ILI{1, 3, 5, 7}, [](int n) { return n % 2 == 0; }));
  43. std::vector<S> all_true { true, true, true };
  44. std::vector<S> one_true { false, false, true };
  45. std::vector<S> none_true { false, false, false };
  46. CHECK(ranges::all_of(all_true.begin(), all_true.end(), &S::p));
  47. CHECK(!ranges::all_of(one_true.begin(), one_true.end(), &S::p));
  48. CHECK(!ranges::all_of(none_true.begin(), none_true.end(), &S::p));
  49. CHECK(ranges::all_of(all_true, &S::p));
  50. CHECK(!ranges::all_of(one_true, &S::p));
  51. CHECK(!ranges::all_of(none_true, &S::p));
  52. using ILS = std::initializer_list<S>;
  53. CHECK(ranges::all_of(ILS{S(true), S(true), S(true)}, &S::p));
  54. CHECK(!ranges::all_of(ILS{S(false), S(true), S(false)}, &S::p));
  55. CHECK(!ranges::all_of(ILS{S(false), S(false), S(false)}, &S::p));
  56. STATIC_CHECK(test_constexpr({0, 2, 4, 6}));
  57. STATIC_CHECK(!test_constexpr({0, 2, 4, 5}));
  58. STATIC_CHECK(!test_constexpr({1, 3, 4, 7}));
  59. STATIC_CHECK(!test_constexpr({1, 3, 5, 7}));
  60. return ::test_result();
  61. }