1/*
2 Copyright (c) T. Zachary Laine 2018.
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#include <iostream>
10
11#include <boost/algorithm/find_not.hpp>
12
13#define BOOST_TEST_MAIN
14#include <boost/test/unit_test.hpp>
15
16#include <vector>
17#include <list>
18
19
20namespace ba = boost::algorithm;
21
22template <typename Container>
23struct dist_t
24{
25 dist_t(Container & cont) : cont_(cont) {}
26 template<typename Iter>
27 std::ptrdiff_t operator()(Iter it) const
28 {
29 return std::distance(cont_.begin(), it);
30 }
31
32 Container & cont_;
33};
34
35BOOST_CXX14_CONSTEXPR bool check_constexpr()
36{
37 int in_data[] = {2, 2, 3, 4, 5};
38 bool res = true;
39
40 const int* from = in_data;
41 const int* to = in_data + 5;
42
43 const int* start = ba::find_not(first: from, last: to, x: 1); // stops on first
44 res = (res && start == from);
45
46 start = ba::find_not(r&: in_data, x: 1); // stops on first
47 res = (res && start == from);
48
49 int in_data_2[] = {6, 6, 6, 6, 6};
50 const int* end = ba::find_not(first: in_data_2, last: in_data_2 + 5, x: 6); // stops on the end
51 res = (res && end == in_data_2 + 5);
52
53 end = ba::find_not(r&: in_data_2, x: 6); // stops on the end
54 res = (res && end == in_data_2 + 5);
55
56 const int* three = ba::find_not(first: from, last: to, x: 2); // stops on third element
57 res = (res && three == in_data + 2);
58
59 three = ba::find_not(r&: in_data, x: 2); // stops on third element
60 res = (res && three == in_data + 2);
61
62 return res;
63}
64
65void test_sequence()
66{
67 {
68 std::vector<int> v1;
69 const dist_t<std::vector<int> > dist(v1);
70
71 for (int i = 5; i < 15; ++i)
72 v1.push_back(x: i);
73 BOOST_CHECK_EQUAL(dist(ba::find_not(v1.begin(), v1.end(), 0)), 0);
74 BOOST_CHECK_EQUAL(
75 dist(ba::find_not(v1.begin(), v1.end(), v1.back())), 0);
76 BOOST_CHECK_EQUAL(
77 dist(ba::find_not(v1.begin(), v1.end(), v1.front())), 1);
78
79 BOOST_CHECK_EQUAL(dist(ba::find_not(v1, 0)), 0);
80 BOOST_CHECK_EQUAL(dist(ba::find_not(v1, v1.back())), 0);
81 BOOST_CHECK_EQUAL(dist(ba::find_not(v1, v1.front())), 1);
82
83 v1 = std::vector<int>(10, 2);
84 BOOST_CHECK_EQUAL(dist(ba::find_not(v1.begin(), v1.end(), 0)), 0);
85 BOOST_CHECK_EQUAL(
86 dist(ba::find_not(v1.begin(), v1.end(), v1.back())), v1.size());
87 BOOST_CHECK_EQUAL(
88 dist(ba::find_not(v1.begin(), v1.end(), v1.front())), v1.size());
89
90 BOOST_CHECK_EQUAL(dist(ba::find_not(v1, 0)), 0);
91 BOOST_CHECK_EQUAL(dist(ba::find_not(v1, v1.back())), v1.size());
92 BOOST_CHECK_EQUAL(dist(ba::find_not(v1, v1.front())), v1.size());
93 }
94
95 // With bidirectional iterators.
96 {
97 std::list<int> l1;
98 const dist_t<std::list<int> > dist(l1);
99
100 for (int i = 5; i < 15; ++i)
101 l1.push_back(x: i);
102 BOOST_CHECK_EQUAL(dist(ba::find_not(l1.begin(), l1.end(), 0)), 0);
103 BOOST_CHECK_EQUAL(
104 dist(ba::find_not(l1.begin(), l1.end(), l1.back())), 0);
105 BOOST_CHECK_EQUAL(
106 dist(ba::find_not(l1.begin(), l1.end(), l1.front())), 1);
107
108 BOOST_CHECK_EQUAL(dist(ba::find_not(l1, 0)), 0);
109 BOOST_CHECK_EQUAL(dist(ba::find_not(l1, l1.back())), 0);
110 BOOST_CHECK_EQUAL(dist(ba::find_not(l1, l1.front())), 1);
111
112 l1.clear();
113 for (int i = 0; i < 10; ++i)
114 l1.push_back(x: 2);
115 BOOST_CHECK_EQUAL(dist(ba::find_not(l1.begin(), l1.end(), 0)), 0);
116 BOOST_CHECK_EQUAL(
117 dist(ba::find_not(l1.begin(), l1.end(), l1.back())), l1.size());
118 BOOST_CHECK_EQUAL(
119 dist(ba::find_not(l1.begin(), l1.end(), l1.front())), l1.size());
120
121 BOOST_CHECK_EQUAL(dist(ba::find_not(l1, 0)), 0);
122 BOOST_CHECK_EQUAL(dist(ba::find_not(l1, l1.back())), l1.size());
123 BOOST_CHECK_EQUAL(dist(ba::find_not(l1, l1.front())), l1.size());
124 }
125
126 BOOST_CXX14_CONSTEXPR bool ce_result = check_constexpr();
127 BOOST_CHECK(ce_result);
128}
129
130
131BOOST_AUTO_TEST_CASE(test_main)
132{
133 test_sequence();
134}
135

source code of boost/libs/algorithm/test/find_not_test.cpp