1 /*
2 Copyright (c) Marshall Clow 2008-2012.
3
4 Distributed under the Boost Software License, Version 1.0. (See accompanying
5 file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6
7 For more information, see http://www.boost.org
8 */
9
10 #include <boost/config.hpp>
11 #include <boost/algorithm/cxx11/one_of.hpp>
12
13 #define BOOST_TEST_MAIN
14 #include <boost/test/unit_test.hpp>
15
16 #include <functional>
17 #include <vector>
18 #include <list>
19
20 template<typename T>
21 struct is_ {
is_is_22 BOOST_CXX14_CONSTEXPR is_ ( T v ) : val_ ( v ) {}
operator ()is_23 BOOST_CXX14_CONSTEXPR bool operator () ( T comp ) const { return val_ == comp; }
24 private:
25 is_ (); // need a value
26
27 T val_;
28 };
29
30 namespace ba = boost::algorithm;
31
test_one()32 void test_one ()
33 {
34 // Note: The literal values here are tested against directly, careful if you change them:
35 BOOST_CXX14_CONSTEXPR int some_numbers[] = { 1, 1, 2, 3, 5 };
36 std::vector<int> vi(some_numbers, some_numbers + 5);
37 std::list<int> li(vi.begin(), vi.end ());
38
39 int some_letters[] = { 'a', 'q', 'n', 'y', 'n' };
40 std::vector<char> vc(some_letters, some_letters + 5);
41
42 BOOST_CHECK (!ba::one_of_equal ( vi, 1 ));
43 BOOST_CHECK (!ba::one_of ( vi, is_<int> ( 1 )));
44 BOOST_CHECK (!ba::one_of_equal ( vi.begin(), vi.end(), 1 ));
45 BOOST_CHECK (!ba::one_of ( vi.begin(), vi.end(), is_<int> ( 1 )));
46
47 BOOST_CHECK (!ba::one_of_equal ( vi, 0 ));
48 BOOST_CHECK (!ba::one_of ( vi, is_<int> ( 0 )));
49 BOOST_CHECK (!ba::one_of_equal ( vi.begin(), vi.end(), 0 ));
50 BOOST_CHECK (!ba::one_of ( vi.begin(), vi.end(), is_<int> ( 0 )));
51
52 BOOST_CHECK ( ba::one_of_equal ( vi, 2 ));
53 BOOST_CHECK ( ba::one_of ( vi, is_<int> ( 2 )));
54 BOOST_CHECK ( ba::one_of_equal ( vi.begin(), vi.end(), 2 ));
55 BOOST_CHECK ( ba::one_of ( vi.begin(), vi.end(), is_<int> ( 2 )));
56
57 // Check for a match at the end
58 BOOST_CHECK ( ba::one_of_equal ( vi, 5 ));
59 BOOST_CHECK ( ba::one_of ( vi, is_<int> ( 5 )));
60 BOOST_CHECK ( ba::one_of_equal ( vi.begin(), vi.end(), 5 ));
61 BOOST_CHECK ( ba::one_of ( vi.begin(), vi.end(), is_<int> ( 5 )));
62
63 BOOST_CHECK ( ba::one_of_equal ( vi.begin() + 1, vi.end(), 1 ));
64 BOOST_CHECK ( ba::one_of ( vi.begin() + 1, vi.end(), is_<int> ( 1 )));
65
66 BOOST_CHECK ( ba::one_of_equal ( vc.begin() + 1, vc.begin() + 2, 'q' ));
67 BOOST_CHECK ( ba::one_of ( vc.begin() + 1, vc.begin() + 2, is_<char> ( 'q' )));
68
69 BOOST_CHECK (!ba::one_of_equal ( vc, '!' ));
70 BOOST_CHECK (!ba::one_of ( vc, is_<char> ( '!' )));
71
72 BOOST_CHECK (!ba::one_of_equal ( vc, 'n' ));
73 BOOST_CHECK (!ba::one_of ( vc, is_<char> ( 'n' )));
74
75 // Empty range check
76 BOOST_CHECK (!ba::one_of_equal ( vi.begin(), vi.begin(), 1 ));
77 BOOST_CHECK (!ba::one_of_equal ( vc.begin(), vc.begin(), 'a' ));
78 BOOST_CHECK (!ba::one_of ( vi.begin(), vi.begin(), is_<int> ( 1 )));
79 BOOST_CHECK (!ba::one_of ( vc.begin(), vc.begin(), is_<char> ( 'a' )));
80
81 BOOST_CHECK (!ba::one_of_equal ( li, 1 ));
82 BOOST_CHECK (!ba::one_of ( li, is_<int> ( 1 )));
83 BOOST_CHECK (!ba::one_of_equal ( li.begin(), li.end(), 1 ));
84 BOOST_CHECK (!ba::one_of ( li.begin(), li.end(), is_<int> ( 1 )));
85
86 std::list<int>::iterator l_iter = li.begin ();
87 l_iter++; l_iter++; l_iter++;
88 BOOST_CHECK (!ba::one_of_equal ( li.begin(), l_iter, 1 ));
89 BOOST_CHECK (!ba::one_of ( li.begin(), l_iter, is_<int> ( 1 )));
90 BOOST_CHECK ( ba::one_of_equal ( li.begin(), l_iter, 2 ));
91 BOOST_CHECK ( ba::one_of ( li.begin(), l_iter, is_<int> ( 2 )));
92 BOOST_CHECK (!ba::one_of_equal ( li.begin(), l_iter, 3 ));
93 BOOST_CHECK (!ba::one_of ( li.begin(), l_iter, is_<int> ( 3 )));
94
95 BOOST_CXX14_CONSTEXPR bool constexpr_res =
96 !ba::one_of ( some_numbers, is_<int> ( 6 )) &&
97 ba::one_of ( some_numbers + 1, some_numbers + 3, is_<int> ( 1 )) &&
98 true;
99
100 BOOST_CHECK ( constexpr_res );
101 }
102
103
BOOST_AUTO_TEST_CASE(test_main)104 BOOST_AUTO_TEST_CASE( test_main )
105 {
106 test_one ();
107 }
108