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
20template<typename T>
21struct is_ : public std::unary_function<T, bool> {
22 is_ ( T v ) : val_ ( v ) {}
23 ~is_ () {}
24 bool operator () ( T comp ) const { return val_ == comp; }
25private:
26 is_ (); // need a value
27
28 T val_;
29 };
30
31namespace ba = boost::algorithm;
32
33void test_one ()
34{
35// Note: The literal values here are tested against directly, careful if you change them:
36 int some_numbers[] = { 1, 1, 2, 3, 5 };
37 std::vector<int> vi(some_numbers, some_numbers + 5);
38 std::list<int> li(vi.begin(), vi.end ());
39
40 int some_letters[] = { 'a', 'q', 'n', 'y', 'n' };
41 std::vector<char> vc(some_letters, some_letters + 5);
42
43 BOOST_CHECK (!ba::one_of_equal ( vi, 1 ));
44 BOOST_CHECK (!ba::one_of ( vi, is_<int> ( 1 )));
45 BOOST_CHECK (!ba::one_of_equal ( vi.begin(), vi.end(), 1 ));
46 BOOST_CHECK (!ba::one_of ( vi.begin(), vi.end(), is_<int> ( 1 )));
47
48 BOOST_CHECK (!ba::one_of_equal ( vi, 0 ));
49 BOOST_CHECK (!ba::one_of ( vi, is_<int> ( 0 )));
50 BOOST_CHECK (!ba::one_of_equal ( vi.begin(), vi.end(), 0 ));
51 BOOST_CHECK (!ba::one_of ( vi.begin(), vi.end(), is_<int> ( 0 )));
52
53 BOOST_CHECK ( ba::one_of_equal ( vi, 2 ));
54 BOOST_CHECK ( ba::one_of ( vi, is_<int> ( 2 )));
55 BOOST_CHECK ( ba::one_of_equal ( vi.begin(), vi.end(), 2 ));
56 BOOST_CHECK ( ba::one_of ( vi.begin(), vi.end(), is_<int> ( 2 )));
57
58// Check for a match at the end
59 BOOST_CHECK ( ba::one_of_equal ( vi, 5 ));
60 BOOST_CHECK ( ba::one_of ( vi, is_<int> ( 5 )));
61 BOOST_CHECK ( ba::one_of_equal ( vi.begin(), vi.end(), 5 ));
62 BOOST_CHECK ( ba::one_of ( vi.begin(), vi.end(), is_<int> ( 5 )));
63
64 BOOST_CHECK ( ba::one_of_equal ( vi.begin() + 1, vi.end(), 1 ));
65 BOOST_CHECK ( ba::one_of ( vi.begin() + 1, vi.end(), is_<int> ( 1 )));
66
67 BOOST_CHECK ( ba::one_of_equal ( vc.begin() + 1, vc.begin() + 2, 'q' ));
68 BOOST_CHECK ( ba::one_of ( vc.begin() + 1, vc.begin() + 2, is_<char> ( 'q' )));
69
70 BOOST_CHECK (!ba::one_of_equal ( vc, '!' ));
71 BOOST_CHECK (!ba::one_of ( vc, is_<char> ( '!' )));
72
73 BOOST_CHECK (!ba::one_of_equal ( vc, 'n' ));
74 BOOST_CHECK (!ba::one_of ( vc, is_<char> ( 'n' )));
75
76// Empty range check
77 BOOST_CHECK (!ba::one_of_equal ( vi.begin(), vi.begin(), 1 ));
78 BOOST_CHECK (!ba::one_of_equal ( vc.begin(), vc.begin(), 'a' ));
79 BOOST_CHECK (!ba::one_of ( vi.begin(), vi.begin(), is_<int> ( 1 )));
80 BOOST_CHECK (!ba::one_of ( vc.begin(), vc.begin(), is_<char> ( 'a' )));
81
82 BOOST_CHECK (!ba::one_of_equal ( li, 1 ));
83 BOOST_CHECK (!ba::one_of ( li, is_<int> ( 1 )));
84 BOOST_CHECK (!ba::one_of_equal ( li.begin(), li.end(), 1 ));
85 BOOST_CHECK (!ba::one_of ( li.begin(), li.end(), is_<int> ( 1 )));
86
87 std::list<int>::iterator l_iter = li.begin ();
88 l_iter++; l_iter++; l_iter++;
89 BOOST_CHECK (!ba::one_of_equal ( li.begin(), l_iter, 1 ));
90 BOOST_CHECK (!ba::one_of ( li.begin(), l_iter, is_<int> ( 1 )));
91 BOOST_CHECK ( ba::one_of_equal ( li.begin(), l_iter, 2 ));
92 BOOST_CHECK ( ba::one_of ( li.begin(), l_iter, is_<int> ( 2 )));
93 BOOST_CHECK (!ba::one_of_equal ( li.begin(), l_iter, 3 ));
94 BOOST_CHECK (!ba::one_of ( li.begin(), l_iter, is_<int> ( 3 )));
95
96}
97
98
99BOOST_AUTO_TEST_CASE( test_main )
100{
101 test_one ();
102}
103