none_of.cpp 2.3 KB

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