1 // Copyright Louis Dionne 2013-2017
2 // Distributed under the Boost Software License, Version 1.0.
3 // (See accompanying file LICENSE.md or copy at http://boost.org/LICENSE_1_0.txt)
5 #ifndef BOOST_HANA_TEST_AUTO_ALL_OF_HPP
6 #define BOOST_HANA_TEST_AUTO_ALL_OF_HPP
8 #include <boost/hana/all_of.hpp>
9 #include <boost/hana/assert.hpp>
10 #include <boost/hana/bool.hpp>
11 #include <boost/hana/equal.hpp>
12 #include <boost/hana/not.hpp>
14 #include "test_case.hpp"
15 #include <laws/base.hpp>
18 TestCase test_all_of{[]{
19 namespace hana = boost::hana;
20 using hana::test::ct_eq;
22 BOOST_HANA_CONSTANT_CHECK(hana::all_of(
24 [](auto) { return hana::false_c; }
27 BOOST_HANA_CONSTANT_CHECK(hana::all_of(
28 MAKE_TUPLE(ct_eq<0>{}),
29 [](auto) { return hana::true_c; }
31 BOOST_HANA_CONSTANT_CHECK(hana::not_(hana::all_of(
32 MAKE_TUPLE(ct_eq<0>{}),
33 [](auto) { return hana::false_c; }
35 BOOST_HANA_CONSTANT_CHECK(hana::all_of(
36 MAKE_TUPLE(ct_eq<0>{}),
37 hana::equal.to(ct_eq<0>{})
39 BOOST_HANA_CONSTANT_CHECK(hana::not_(hana::all_of(
40 MAKE_TUPLE(ct_eq<0>{}),
41 hana::equal.to(ct_eq<999>{})
44 BOOST_HANA_CONSTANT_CHECK(hana::all_of(
45 MAKE_TUPLE(ct_eq<0>{}, ct_eq<1>{}),
46 [](auto) { return hana::true_c; }
48 BOOST_HANA_CONSTANT_CHECK(hana::not_(hana::all_of(
49 MAKE_TUPLE(ct_eq<0>{}, ct_eq<1>{}),
50 [](auto) { return hana::false_c; }
52 BOOST_HANA_CONSTANT_CHECK(hana::all_of(
53 MAKE_TUPLE(ct_eq<0>{}, ct_eq<0>{}),
54 hana::equal.to(ct_eq<0>{})
56 BOOST_HANA_CONSTANT_CHECK(hana::not_(hana::all_of(
57 MAKE_TUPLE(ct_eq<0>{}, ct_eq<1>{}),
58 hana::equal.to(ct_eq<0>{})
61 BOOST_HANA_CONSTANT_CHECK(hana::all_of(
62 MAKE_TUPLE(ct_eq<0>{}, ct_eq<1>{}, ct_eq<2>{}),
63 [](auto) { return hana::true_c; }
65 BOOST_HANA_CONSTANT_CHECK(hana::not_(hana::all_of(
66 MAKE_TUPLE(ct_eq<0>{}, ct_eq<1>{}, ct_eq<2>{}),
67 [](auto) { return hana::false_c; }
69 BOOST_HANA_CONSTANT_CHECK(hana::all_of(
70 MAKE_TUPLE(ct_eq<0>{}, ct_eq<0>{}, ct_eq<0>{}),
71 hana::equal.to(ct_eq<0>{})
73 BOOST_HANA_CONSTANT_CHECK(hana::not_(hana::all_of(
74 MAKE_TUPLE(ct_eq<0>{}, ct_eq<1>{}, ct_eq<0>{}),
75 hana::equal.to(ct_eq<0>{})
78 BOOST_HANA_CONSTANT_CHECK(hana::all_of(
79 MAKE_TUPLE(ct_eq<0>{}, ct_eq<0>{}, ct_eq<0>{}, ct_eq<0>{}),
80 hana::equal.to(ct_eq<0>{})
82 BOOST_HANA_CONSTANT_CHECK(hana::not_(hana::all_of(
83 MAKE_TUPLE(ct_eq<999>{}, ct_eq<0>{}, ct_eq<0>{}, ct_eq<0>{}),
84 hana::equal.to(ct_eq<0>{})
86 BOOST_HANA_CONSTANT_CHECK(hana::not_(hana::all_of(
87 MAKE_TUPLE(ct_eq<0>{}, ct_eq<999>{}, ct_eq<0>{}, ct_eq<0>{}),
88 hana::equal.to(ct_eq<0>{})
90 BOOST_HANA_CONSTANT_CHECK(hana::not_(hana::all_of(
91 MAKE_TUPLE(ct_eq<0>{}, ct_eq<0>{}, ct_eq<999>{}, ct_eq<0>{}),
92 hana::equal.to(ct_eq<0>{})
94 BOOST_HANA_CONSTANT_CHECK(hana::not_(hana::all_of(
95 MAKE_TUPLE(ct_eq<0>{}, ct_eq<0>{}, ct_eq<0>{}, ct_eq<999>{}),
96 hana::equal.to(ct_eq<0>{})
99 // Make sure `all_of` short-circuits with runtime predicates
100 // See http://stackoverflow.com/q/42012512/627587
104 auto tuple = MAKE_TUPLE(ct_eq<0>{}, ct_eq<1>{});
105 hana::all_of(tuple, [&](auto) { ++counter; return false; });
106 BOOST_HANA_RUNTIME_CHECK(counter == 1);
110 auto tuple = MAKE_TUPLE(ct_eq<0>{}, ct_eq<999>{}, ct_eq<0>{});
111 hana::all_of(tuple, [&](auto x) -> bool {
113 return hana::equal(x, ct_eq<0>{});
115 BOOST_HANA_RUNTIME_CHECK(counter == 2);
120 #endif // !BOOST_HANA_TEST_AUTO_ALL_OF_HPP