1/*
2 Copyright (c) Marshall Clow 2010-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/any_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_any ()
34{
35// Note: The literal values here are tested against directly, careful if you change them:
36 int some_numbers[] = { 1, 5, 0, 18, 10 };
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::any_of_equal ( vi, 1 ));
44 BOOST_CHECK ( ba::any_of ( vi, is_<int> ( 1 )));
45 BOOST_CHECK ( ba::any_of_equal ( vi.begin(), vi.end(), 1 ));
46 BOOST_CHECK ( ba::any_of ( vi.begin(), vi.end(), is_<int> ( 1 )));
47
48 BOOST_CHECK (!ba::any_of_equal ( vi, 9 ));
49 BOOST_CHECK (!ba::any_of ( vi, is_<int> ( 9 )));
50 BOOST_CHECK (!ba::any_of_equal ( vi.begin(), vi.end(), 9 ));
51 BOOST_CHECK (!ba::any_of ( vi.begin(), vi.end(), is_<int> ( 9 )));
52
53 BOOST_CHECK ( ba::any_of_equal ( vi, 10 ));
54 BOOST_CHECK ( ba::any_of ( vi, is_<int> ( 10 )));
55 BOOST_CHECK (!ba::any_of_equal ( vi, 4 ));
56 BOOST_CHECK (!ba::any_of ( vi, is_<int> ( 4 )));
57
58 BOOST_CHECK (!ba::any_of_equal ( vi.end(), vi.end(), 0 ));
59 BOOST_CHECK (!ba::any_of ( vi.end(), vi.end(), is_<int> ( 0 )));
60
61// 5 is not in { 0, 18, 10 }, but 10 is
62 BOOST_CHECK ( ba::any_of_equal ( vi.begin() + 2, vi.end(), 10 ));
63 BOOST_CHECK ( ba::any_of ( vi.begin() + 2, vi.end(), is_<int> ( 10 )));
64
65 BOOST_CHECK (!ba::any_of_equal ( vi.begin() + 2, vi.end(), 5 ));
66 BOOST_CHECK (!ba::any_of ( vi.begin() + 2, vi.end(), is_<int> ( 5 )));
67
68// 18 is not in { 1, 5, 0 }, but 5 is
69 BOOST_CHECK ( ba::any_of_equal ( vi.begin(), vi.begin() + 3, 5 ));
70 BOOST_CHECK ( ba::any_of ( vi.begin(), vi.begin() + 3, is_<int> ( 5 )));
71
72 BOOST_CHECK (!ba::any_of_equal ( vi.begin(), vi.begin() + 3, 18 ));
73 BOOST_CHECK (!ba::any_of ( vi.begin(), vi.begin() + 3, is_<int> ( 18 )));
74
75 BOOST_CHECK ( ba::any_of_equal ( vc, 'q' ));
76 BOOST_CHECK ( ba::any_of ( vc, is_<char> ( 'q' )));
77
78 BOOST_CHECK (!ba::any_of_equal ( vc, '!' ));
79 BOOST_CHECK (!ba::any_of ( vc, is_<char> ( '!' )));
80
81 BOOST_CHECK ( ba::any_of_equal ( vc, 'n' ));
82 BOOST_CHECK ( ba::any_of ( vc, is_<char> ( 'n' )));
83
84 BOOST_CHECK (!ba::any_of_equal ( vi.begin(), vi.begin(), 1 ));
85 BOOST_CHECK (!ba::any_of_equal ( vc.begin(), vc.begin(), 'a' ));
86 BOOST_CHECK (!ba::any_of ( vi.begin(), vi.begin(), is_<int> ( 1 )));
87 BOOST_CHECK (!ba::any_of ( vc.begin(), vc.begin(), is_<char> ( 'a' )));
88
89 BOOST_CHECK ( ba::any_of_equal ( li, 1 ));
90 BOOST_CHECK ( ba::any_of ( li, is_<int> ( 1 )));
91 BOOST_CHECK ( ba::any_of_equal ( li.begin(), li.end(), 1 ));
92 BOOST_CHECK ( ba::any_of ( li.begin(), li.end(), is_<int> ( 1 )));
93
94 std::list<int>::iterator l_iter = li.begin ();
95 l_iter++; l_iter++; l_iter++;
96 BOOST_CHECK ( ba::any_of_equal ( li.begin(), l_iter, 5 ));
97 BOOST_CHECK ( ba::any_of ( li.begin(), l_iter, is_<int> ( 5 )));
98 BOOST_CHECK (!ba::any_of_equal ( li.begin(), l_iter, 18 ));
99 BOOST_CHECK (!ba::any_of ( li.begin(), l_iter, is_<int> ( 18 )));
100}
101
102
103BOOST_AUTO_TEST_CASE( test_main )
104{
105 test_any ();
106}
107