summaryrefslogtreecommitdiffstats
path: root/src/boost/libs/phoenix/test/algorithm/querying.cpp
blob: 02704d3ca97fc05c283ca0b35927be7fb1c6cbb3 (plain)
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
282
283
284
285
286
/*=============================================================================
    Copyright (c) 2005 Dan Marsden
    Copyright (c) 2005-2007 Joel de Guzman
    Copyright (c) 2007 Hartmut Kaiser
    Copyright (c) 2015 John Fletcher

    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)
==============================================================================*/

#include <boost/phoenix/core.hpp>
#include <boost/phoenix/stl/algorithm/querying.hpp>
#include <boost/detail/lightweight_test.hpp>
#include <boost/assign/list_of.hpp>

#include <boost/config.hpp>

#ifdef BOOST_PHOENIX_HAS_HASH
#define _GLIBCXX_PERMIT_BACKWARD_HASH
#include BOOST_PHOENIX_HASH_SET_HEADER
#include BOOST_PHOENIX_HASH_MAP_HEADER
#endif

#include <set>
#include <map>
#include <functional>

namespace
{
    struct even
    {
        bool operator()(const int i) const
        {
            return i % 2 == 0;
        }
    };

    struct mod_2_comparison
    {
        bool operator()(
            const int lhs,
            const int rhs)
        {
            return lhs % 2 == rhs % 2;
        }
    };

    void find_test()
    {
        using boost::phoenix::arg_names::arg1;
        int array[] = {1,2,3};
        BOOST_TEST(boost::phoenix::find(arg1,2)(array) == array + 1);

        std::set<int> s(array, array + 3);
        BOOST_TEST(boost::phoenix::find(arg1, 2)(s) == s.find(2));

#if !(defined(BOOST_MSVC) && (BOOST_MSVC >= 1900))
        std::map<int, int> m = boost::assign::map_list_of(0, 1)(2, 3)(4, 5);
        BOOST_TEST(boost::phoenix::find(arg1, 2)(m) == m.find(2));
#endif

#ifdef BOOST_PHOENIX_HAS_HASH

        BOOST_PHOENIX_HASH_NAMESPACE::hash_set<int> hs(array, array + 3);
        BOOST_TEST(boost::phoenix::find(arg1, 2)(hs) == hs.find(2));

        BOOST_PHOENIX_HASH_NAMESPACE::hash_map<int, int> hm = boost::assign::map_list_of(0, 1)(2, 3)(4, 5);
        BOOST_TEST(boost::phoenix::find(arg1, 2)(hm) == hm.find(2));

#endif

        return;
    }


    void find_if_test()
    {
        using boost::phoenix::arg_names::arg1;
        int array[] = {1,2,3};
        BOOST_TEST(boost::phoenix::find_if(arg1, even())(array) == array + 1);
        return;
    }

    void find_end_test()
    {
        using boost::phoenix::arg_names::arg1;
        using boost::phoenix::arg_names::arg2;
        int array[] = {1,2,3,1,2,3,1};
        int pattern[] = {1,2,3};
        BOOST_TEST(boost::phoenix::find_end(arg1, arg2)(array, pattern) == array + 3);
        int pattern2[] = {5,6,5};
        BOOST_TEST(boost::phoenix::find_end(arg1, arg2, mod_2_comparison())(array, pattern2) == array + 3);
        return;
    }

    void find_first_of_test()
    {
        using boost::phoenix::arg_names::arg1;
        using boost::phoenix::arg_names::arg2;
        int array[] = {1,2,3};
        int search_for[] = {2,3,4};
        BOOST_TEST(boost::phoenix::find_first_of(arg1, arg2)(array, search_for) == array + 1);

        int search_for2[] = {0};
        BOOST_TEST(boost::phoenix::find_first_of(arg1, arg2, mod_2_comparison())(array, search_for2) == array + 1);
        return;
    }

    void adjacent_find_test()
    {
        using boost::phoenix::arg_names::arg1;
        int array[] = {0,1,3,4,4};
        BOOST_TEST(boost::phoenix::adjacent_find(arg1)(array) == array + 3);
        BOOST_TEST(boost::phoenix::adjacent_find(arg1, mod_2_comparison())(array) == array + 1);
        return;
    }

    void count_test()
    {
        using boost::phoenix::arg_names::arg1;
        int array[] = {1,1,0,1,1};
        BOOST_TEST(boost::phoenix::count(arg1, 1)(array) == 4);
        return;
    }

    void count_if_test()
    {
        using boost::phoenix::arg_names::arg1;
        int array[] = {1,2,3,4,5};
        BOOST_TEST(boost::phoenix::count_if(arg1, even())(array) == 2);
        return;
    }

    void distance_test()
    {
        using boost::phoenix::arg_names::arg1;
        int array[] = {1,1,0,1,1};
        BOOST_TEST(boost::phoenix::distance(arg1)(array) == 5);
        return;
    }

    void mismatch_test()
    {
        using boost::phoenix::arg_names::arg1;
        using boost::phoenix::arg_names::arg2;
        int array[] = {1,2,3,4,5};
        int search[] = {1,2,4};

        BOOST_TEST(
            boost::phoenix::mismatch(arg1, arg2)(array, search) == 
            std::make_pair(array + 2, search + 2));
        int search2[] = {1,2,1,1};
        BOOST_TEST(
            boost::phoenix::mismatch(arg1, arg2, mod_2_comparison())(array, search2) 
            == std::make_pair(array + 3, search2 + 3));

        return;
    }

    void equal_test()
    {
        using boost::phoenix::arg_names::arg1;
        using boost::phoenix::arg_names::arg2;
        int array[] = {1,2,3};
        int array2[] = {1,2,3};
        int array3[] = {1,2,4};
        BOOST_TEST(
            boost::phoenix::equal(arg1, arg2)(array, array2));
        BOOST_TEST(
            !boost::phoenix::equal(arg1, arg2)(array, array3));

        BOOST_TEST(
            boost::phoenix::equal(arg1, arg2, mod_2_comparison())(array, array2));
        BOOST_TEST(
            !boost::phoenix::equal(arg1, arg2, mod_2_comparison())(array, array3));
        return;
    }

    void search_test()
    {
        using boost::phoenix::arg_names::arg1;
        using boost::phoenix::arg_names::arg2;
        int array[] = {1,2,3,1,2,3};
        int pattern[] = {2,3};
        BOOST_TEST(
            boost::phoenix::search(arg1, arg2)(array, pattern) == array + 1);
        int pattern2[] = {1,1};
        BOOST_TEST(
            boost::phoenix::search(arg1, arg2, mod_2_comparison())(array, pattern2) == array + 2);
        return;
    }

    void lower_bound_test()
    {
        using boost::phoenix::arg_names::arg1;
        int array[] = {1,2,3};
        const std::set<int> test_set(array, array + 3);
        BOOST_TEST(boost::phoenix::lower_bound(arg1, 2)(array) == array + 1);
        BOOST_TEST(boost::phoenix::lower_bound(arg1, 2)(test_set) == test_set.lower_bound(2));

        int array2[] = {3,2,1};
        const std::set<int, std::greater<int> > test_set2(array2, array2 + 3);
        BOOST_TEST(boost::phoenix::lower_bound(arg1, 2, std::greater<int>())(array2) ==
                   array2 + 1);
        BOOST_TEST(boost::phoenix::lower_bound(arg1, 2, std::greater<int>())(test_set2) ==
                   test_set2.lower_bound(2));
        return;
    }

    void upper_bound_test()
    {
        using boost::phoenix::arg_names::arg1;
        int array[] = {1,2,3};
        const std::set<int> test_set(array, array + 3);
        BOOST_TEST(upper_bound(arg1, 2)(array) == array + 2);
        BOOST_TEST(upper_bound(arg1, 2)(test_set) == test_set.upper_bound(2));

        int array2[] = {3,2,1};
        const std::set<int, std::greater<int> > test_set2(array2, array2 + 3);
        BOOST_TEST(boost::phoenix::upper_bound(arg1, 2, std::greater<int>())(array2) ==
                   array2 + 2);
        BOOST_TEST(boost::phoenix::upper_bound(arg1, 2, std::greater<int>())(test_set2) ==
                   test_set2.upper_bound(2));
        return;
    }

    void equal_range_test()
    {
        using boost::phoenix::arg_names::arg1;
        int array[] = {1,2,2,3};
        const std::set<int> test_set(array, array + 4);
        BOOST_TEST(boost::phoenix::equal_range(arg1, 2)(array).first == 
                   array + 1);
        BOOST_TEST(boost::phoenix::equal_range(arg1, 2)(array).second == 
                   array + 3);

        BOOST_TEST(boost::phoenix::equal_range(arg1, 2)(test_set).first == 
                   test_set.equal_range(2).first);
        BOOST_TEST(boost::phoenix::equal_range(arg1, 2)(test_set).second == 
                   test_set.equal_range(2).second);

        int array2[] = {3,2,2,1};
        const std::set<int, std::greater<int> > test_set2(array2, array2 + 4);
        BOOST_TEST(boost::phoenix::equal_range(arg1, 2, std::greater<int>())(array2).first == 
                   array2 + 1);
        BOOST_TEST(boost::phoenix::equal_range(arg1, 2, std::greater<int>())(array2).second == 
                   array2 + 3);

        BOOST_TEST(boost::phoenix::equal_range(arg1, 2, std::greater<int>())(test_set2).first == 
                   test_set2.equal_range(2).first);
        BOOST_TEST(boost::phoenix::equal_range(arg1, 2, std::greater<int>())(test_set2).second == 
                   test_set2.equal_range(2).second);

        return;
    }

    void binary_search_test()
    {
        using boost::phoenix::arg_names::arg1;
        int array[] = {1,2,3};
        BOOST_TEST(boost::phoenix::binary_search(arg1, 2)(array));
        BOOST_TEST(!boost::phoenix::binary_search(arg1, 4)(array));
        return;
    }

}

int main()
{
    find_test();
    find_if_test();
    find_end_test();
    find_first_of_test();
    adjacent_find_test();
    count_test();
    count_if_test();
    distance_test();
    mismatch_test();
    equal_test();
    search_test();
    lower_bound_test();
    upper_bound_test();
    equal_range_test();
    binary_search_test();
    return boost::report_errors();
}