File: find_not_test.cpp

package info (click to toggle)
boost1.83 1.83.0-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 545,632 kB
  • sloc: cpp: 3,857,086; xml: 125,552; ansic: 34,414; python: 25,887; asm: 5,276; sh: 4,799; ada: 1,681; makefile: 1,629; perl: 1,212; pascal: 1,139; sql: 810; yacc: 478; ruby: 102; lisp: 24; csh: 6
file content (134 lines) | stat: -rw-r--r-- 4,117 bytes parent folder | download | duplicates (12)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
/* 
   Copyright (c) T. Zachary Laine 2018.

   Distributed under the Boost Software License, Version 1.0. (See accompanying
   file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)

    For more information, see http://www.boost.org
*/
#include <iostream>

#include <boost/algorithm/find_not.hpp>

#define BOOST_TEST_MAIN
#include <boost/test/unit_test.hpp>

#include <vector>
#include <list>


namespace ba = boost::algorithm;

template <typename Container>
struct dist_t
{
    dist_t(Container & cont) : cont_(cont) {}
    template<typename Iter>
    std::ptrdiff_t operator()(Iter it) const
    {
        return std::distance(cont_.begin(), it);
    }

    Container & cont_;
};

BOOST_CXX14_CONSTEXPR bool check_constexpr()
{
    int in_data[] = {2, 2, 3, 4, 5};
    bool res = true;

    const int* from = in_data;
    const int* to = in_data + 5;

    const int* start = ba::find_not(from, to, 1); // stops on first
    res = (res && start == from);

    start = ba::find_not(in_data, 1); // stops on first
    res = (res && start == from);

    int in_data_2[] = {6, 6, 6, 6, 6};
    const int* end = ba::find_not(in_data_2, in_data_2 + 5, 6); // stops on the end
    res = (res && end == in_data_2 + 5);

    end = ba::find_not(in_data_2, 6); // stops on the end
    res = (res && end == in_data_2 + 5);

    const int* three = ba::find_not(from, to, 2); // stops on third element
    res = (res && three == in_data + 2);

    three = ba::find_not(in_data, 2); // stops on third element
    res = (res && three == in_data + 2);

    return res;
}

void test_sequence()
{
    {
        std::vector<int> v1;
        const dist_t<std::vector<int> > dist(v1);

        for (int i = 5; i < 15; ++i)
            v1.push_back(i);
        BOOST_CHECK_EQUAL(dist(ba::find_not(v1.begin(), v1.end(), 0)), 0);
        BOOST_CHECK_EQUAL(
            dist(ba::find_not(v1.begin(), v1.end(), v1.back())), 0);
        BOOST_CHECK_EQUAL(
            dist(ba::find_not(v1.begin(), v1.end(), v1.front())), 1);

        BOOST_CHECK_EQUAL(dist(ba::find_not(v1, 0)), 0);
        BOOST_CHECK_EQUAL(dist(ba::find_not(v1, v1.back())), 0);
        BOOST_CHECK_EQUAL(dist(ba::find_not(v1, v1.front())), 1);

        v1 = std::vector<int>(10, 2);
        BOOST_CHECK_EQUAL(dist(ba::find_not(v1.begin(), v1.end(), 0)), 0);
        BOOST_CHECK_EQUAL(
            dist(ba::find_not(v1.begin(), v1.end(), v1.back())), v1.size());
        BOOST_CHECK_EQUAL(
            dist(ba::find_not(v1.begin(), v1.end(), v1.front())), v1.size());

        BOOST_CHECK_EQUAL(dist(ba::find_not(v1, 0)), 0);
        BOOST_CHECK_EQUAL(dist(ba::find_not(v1, v1.back())), v1.size());
        BOOST_CHECK_EQUAL(dist(ba::find_not(v1, v1.front())), v1.size());
    }

    //  With bidirectional iterators.
    {
        std::list<int> l1;
        const dist_t<std::list<int> > dist(l1);

        for (int i = 5; i < 15; ++i)
            l1.push_back(i);
        BOOST_CHECK_EQUAL(dist(ba::find_not(l1.begin(), l1.end(), 0)), 0);
        BOOST_CHECK_EQUAL(
            dist(ba::find_not(l1.begin(), l1.end(), l1.back())), 0);
        BOOST_CHECK_EQUAL(
            dist(ba::find_not(l1.begin(), l1.end(), l1.front())), 1);

        BOOST_CHECK_EQUAL(dist(ba::find_not(l1, 0)), 0);
        BOOST_CHECK_EQUAL(dist(ba::find_not(l1, l1.back())), 0);
        BOOST_CHECK_EQUAL(dist(ba::find_not(l1, l1.front())), 1);

        l1.clear();
        for (int i = 0; i < 10; ++i)
            l1.push_back(2);
        BOOST_CHECK_EQUAL(dist(ba::find_not(l1.begin(), l1.end(), 0)), 0);
        BOOST_CHECK_EQUAL(
            dist(ba::find_not(l1.begin(), l1.end(), l1.back())), l1.size());
        BOOST_CHECK_EQUAL(
            dist(ba::find_not(l1.begin(), l1.end(), l1.front())), l1.size());

        BOOST_CHECK_EQUAL(dist(ba::find_not(l1, 0)), 0);
        BOOST_CHECK_EQUAL(dist(ba::find_not(l1, l1.back())), l1.size());
        BOOST_CHECK_EQUAL(dist(ba::find_not(l1, l1.front())), l1.size());
    }

    BOOST_CXX14_CONSTEXPR bool ce_result = check_constexpr();
    BOOST_CHECK(ce_result);
}


BOOST_AUTO_TEST_CASE(test_main)
{
    test_sequence();
}