File: unstable_remove_if.cpp

package info (click to toggle)
range-v3 0.12.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 5,652 kB
  • sloc: cpp: 76,839; xml: 226; sh: 89; python: 34; makefile: 19; perl: 15
file content (281 lines) | stat: -rw-r--r-- 7,558 bytes parent folder | download | duplicates (3)
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
// Range v3 library
//
//  Copyright Andrey Diduh 2019
//
//  Use, modification and distribution is subject to 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)
//
// Project home: https://github.com/ericniebler/range-v3

#include <vector>
#include <cstdlib>
#include <ctime>
#include <random>

#include <range/v3/action/unstable_remove_if.hpp>
#include <range/v3/action/remove_if.hpp>
#include <range/v3/action/sort.hpp>

#include "../array.hpp"
#include "../simple_test.hpp"
#include "../test_utils.hpp"

void logic_test()
{
    using namespace ranges;

    const auto make_vector = []() -> std::vector<int> {
        return {1,2,3,4,5};
    };

    // empty
    {
        std::vector<int> vec;
        vec |= actions::unstable_remove_if([](int) { return true; });
        CHECK(vec.empty());
    }

    // all stay
    {
        std::vector<int> vec = make_vector();
        vec |= actions::unstable_remove_if([](int) { return false; });
        check_equal(vec, {1,2,3,4,5});
    }

    // all remove
    {
        std::vector<int> vec = make_vector();
        vec |= actions::unstable_remove_if([](int) { return true; });
        CHECK(vec.empty());
    }

    // remove one in the middle
    {
        std::vector<int> vec = make_vector();
        vec |= actions::unstable_remove_if([](int i) { return i == 2; });
        check_equal(vec, {1,5,3,4});
    }

    // remove first
    {
        std::vector<int> vec = make_vector();
        vec |= actions::unstable_remove_if([](int i) { return i == 1; });
        check_equal(vec, {5,2,3,4});
    }

    // remove last
    {
        std::vector<int> vec = make_vector();
        vec |= actions::unstable_remove_if([](int i) { return i == 5; });
        check_equal(vec, {1,2,3,4});
    }

    // remove group in the middle
    {
        std::vector<int> vec = make_vector();
        vec |= actions::unstable_remove_if([](int i) { return i == 2 || i == 3 || i == 4; });
        check_equal(vec, {1,5});
    }

    // remove group in the begin
    {
        std::vector<int> vec = make_vector();
        vec |= actions::unstable_remove_if([](int i) { return i == 1 || i == 2 || i == 3; });
        check_equal(vec, {5,4});
    }

    // remove group in the end
    {
        std::vector<int> vec = make_vector();
        vec |= actions::unstable_remove_if([](int i) { return i == 3 || i == 4 || i == 5; });
        check_equal(vec, {1,2});
    }

    // remains one in the middle
    {
        std::vector<int> vec = make_vector();
        vec |= actions::unstable_remove_if([](int i) { return i != 3; });
        check_equal(vec, {3});
    }
    // remains group in the middle
    {
        std::vector<int> vec = make_vector();
        vec |= actions::unstable_remove_if([](int i) { return (i != 3) && (i != 4); });
        check_equal(vec, {4,3});
    }
}

void num_pred_calls_test()
{
    // std::ranges::remove_if requires:
    // "Exactly N applications of the corresponding predicate and any projection, where N = (last - first)"
    // https://en.cppreference.com/w/cpp/algorithm/ranges/remove
    // so expect the same of unstable_remove_if
    using namespace ranges;

    int pred_invocation_counter = 0;
    auto is_zero_count_invocations = [&pred_invocation_counter](int i) {
        ++pred_invocation_counter;
        return i == 0;
    };

    {
        std::vector<int> vec{0};
        pred_invocation_counter = 0;
        vec |= actions::unstable_remove_if(is_zero_count_invocations);
        check_equal(pred_invocation_counter, 1);
    }

    {
        std::vector<int> vec{1,1,1};
        pred_invocation_counter = 0;
        vec |= actions::unstable_remove_if(is_zero_count_invocations);
        check_equal(pred_invocation_counter, 3);
    }

    {
        std::vector<int> vec{1,0};
        pred_invocation_counter = 0;
        vec |= actions::unstable_remove_if(is_zero_count_invocations);
        check_equal(pred_invocation_counter, 2);
    }

    {
        std::vector<int> vec{1,2,0};
        pred_invocation_counter = 0;
        vec |= actions::unstable_remove_if(is_zero_count_invocations);
        check_equal(pred_invocation_counter, 3);
    }

    {
        std::vector<int> vec{0,0,0,0};
        pred_invocation_counter = 0;
        vec |= actions::unstable_remove_if(is_zero_count_invocations);
        check_equal(pred_invocation_counter, 4);
    }

    {
        std::vector<int> vec{1,2,3,0,0,0,0,4,5};
        pred_invocation_counter = 0;
        vec |= actions::unstable_remove_if(is_zero_count_invocations);
        check_equal(pred_invocation_counter, 9);
    }
}

class fuzzy_test_fn
{
    int size;
#if defined(__GLIBCXX__) && defined(RANGES_WORKAROUND_VALGRIND_RDRAND)
    std::random_device rd{"/dev/urandom"};
#else
    std::random_device rd;
#endif
    std::mt19937 eng{rd()};
    std::uniform_int_distribution<int> distr;

public:
    explicit fuzzy_test_fn(int sz)
      : size(sz)
      , distr{0, sz}
    {}

    void operator()()
    {
        struct Int
        {
            int value;

            explicit Int(int v)
              : value(v)
            {}
            Int(Int const &) = default;
            Int(Int&& other) noexcept
              : value(0)
            {
                *this = std::move(other);
            }

            Int &operator=(Int const &) = default;
            Int &operator=(Int&& other) noexcept
            {
                const int sentinel = -1;
                CHECK(other.value != sentinel);

                value = other.value;
                other.value = sentinel;
                return *this;
            }

            RANGES_DIAGNOSTIC_PUSH
            RANGES_DIAGNOSTIC_IGNORE_UNNEEDED_MEMBER
            bool operator==(Int const &other) const
            {
                return value == other.value;
            }
            bool operator!=(Int const &other) const
            {
                return value != other.value;
            }
            bool operator<(Int const &other) const
            {
                return value < other.value;
            }
            bool operator>(Int const &other) const
            {
                return value > other.value;
            }
            bool operator<=(Int const &other) const
            {
                return value <= other.value;
            }
            bool operator>=(Int const &other) const
            {
                return value >= other.value;
            }
            RANGES_DIAGNOSTIC_POP
        };

        using namespace ranges;
        std::vector<Int> ordered_list;
        std::vector<Int> unordered_list;

        // fill
        for(int i=0; i < size; ++i)
        {
            ordered_list.emplace_back(i);
            unordered_list.emplace_back(i);
        }

        // erase
        const int erase_count = distr(eng);
        for(int i=0; i < erase_count; ++i)
        {
            const int value = distr(eng);
            const auto pred = [value](Int j) { return j.value == value; };
            unordered_list |= actions::unstable_remove_if(pred);
            ordered_list |= actions::remove_if(pred);
        }

        // compare
        unordered_list |= actions::sort;
        CHECK(ordered_list == unordered_list);
    }
};

int main()
{
    logic_test();
    num_pred_calls_test();

    {
        const int size = 100;
        const int repeats = 1000;
        fuzzy_test_fn fuzzy_test(size);
        for(int i=0; i < repeats; ++i)
            fuzzy_test();
    }

    return ::test_result();
}