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/all_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_all ()
34{
35// Note: The literal values here are tested against directly, careful if you change them:
36 int some_numbers[] = { 1, 1, 1, 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
44 BOOST_CHECK (!ba::all_of_equal ( vi, 1 ));
45 BOOST_CHECK (!ba::all_of ( vi, is_<int> ( 1 )));
46 BOOST_CHECK (!ba::all_of_equal ( vi.begin(), vi.end(), 1 ));
47 BOOST_CHECK (!ba::all_of ( vi.begin(), vi.end(), is_<int> ( 1 )));
48
49 BOOST_CHECK (!ba::all_of_equal ( vi, 0 ));
50 BOOST_CHECK (!ba::all_of ( vi, is_<int> ( 0 )));
51 BOOST_CHECK (!ba::all_of_equal ( vi.begin(), vi.end(), 0 ));
52 BOOST_CHECK (!ba::all_of ( vi.begin(), vi.end(), is_<int> ( 0 )));
53
54 BOOST_CHECK ( ba::all_of_equal ( vi.end(), vi.end(), 0 ));
55 BOOST_CHECK ( ba::all_of ( vi.end(), vi.end(), is_<int> ( 0 )));
56
57 BOOST_CHECK ( ba::all_of_equal ( vi.begin(), vi.begin () + 3, 1 ));
58 BOOST_CHECK ( ba::all_of ( vi.begin(), vi.begin () + 3, is_<int> ( 1 )));
59
60 BOOST_CHECK ( ba::all_of_equal ( vc.begin() + 1, vc.begin() + 2, 'q' ));
61 BOOST_CHECK ( ba::all_of ( vc.begin() + 1, vc.begin() + 2, is_<char> ( 'q' )));
62
63 BOOST_CHECK (!ba::all_of_equal ( vc, '!' ));
64 BOOST_CHECK (!ba::all_of ( vc, is_<char> ( '!' )));
65
66 BOOST_CHECK ( ba::all_of_equal ( vi.begin(), vi.begin(), 1 ));
67 BOOST_CHECK ( ba::all_of_equal ( vc.begin(), vc.begin(), 'a' ));
68 BOOST_CHECK ( ba::all_of ( vi.begin(), vi.begin(), is_<int> ( 1 )));
69 BOOST_CHECK ( ba::all_of ( vc.begin(), vc.begin(), is_<char> ( 'a' )));
70
71 BOOST_CHECK (!ba::all_of_equal ( li, 1 ));
72 BOOST_CHECK (!ba::all_of ( li, is_<int> ( 1 )));
73 BOOST_CHECK (!ba::all_of_equal ( li.begin(), li.end(), 1 ));
74 BOOST_CHECK (!ba::all_of ( li.begin(), li.end(), is_<int> ( 1 )));
75
76 std::list<int>::iterator l_iter = li.begin ();
77 l_iter++; l_iter++; l_iter++;
78 BOOST_CHECK ( ba::all_of_equal ( li.begin(), l_iter, 1 ));
79 BOOST_CHECK ( ba::all_of ( li.begin(), l_iter, is_<int> ( 1 )));
80
81}
82
83
84BOOST_AUTO_TEST_CASE( test_main )
85{
86 test_all ();
87}
88