File: half_open_range_test.cpp

package info (click to toggle)
boost 1.27.0-3
  • links: PTS
  • area: main
  • in suites: woody
  • size: 19,908 kB
  • ctags: 26,546
  • sloc: cpp: 122,225; ansic: 10,956; python: 4,412; sh: 855; yacc: 803; makefile: 257; perl: 165; lex: 90; csh: 6
file content (366 lines) | stat: -rw-r--r-- 10,554 bytes parent folder | download
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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
// (C) Copyright David Abrahams 2001. Permission to copy, use, modify, sell and
// distribute this software is granted provided this copyright notice appears in
// all copies. This software is provided "as is" without express or implied
// warranty, and with no claim as to its suitability for any purpose.
//
//  See http://www.boost.org for most recent version including documentation.
//
// Revision History
// 11 Feb 2001  Compile with Borland, re-enable failing tests (David Abrahams)
// 29 Jan 2001  Initial revision (David Abrahams)

#include <boost/half_open_range.hpp>
#include <boost/utility.hpp>
#include <iterator>
#include <stdlib.h>
#include <vector>
#include <list>
#include <cassert>
#include <stdexcept>
#ifndef BOOST_NO_LIMITS
# include <limits>
#endif
#ifndef BOOST_NO_SLIST
# include <slist>
#endif

inline unsigned unsigned_random(unsigned max)
{
    return (max > 0) ? (unsigned)rand() % max : 0;
}

// Special tests for ranges supporting random access
template <class T>
void category_test_1(
    const boost::half_open_range<T>& r, std::random_access_iterator_tag)
{
    typedef boost::half_open_range<T> range;
    typedef typename range::size_type size_type;
    size_type size = r.size();

    // pick a random offset
    size_type offset = unsigned_random(size);

    typename range::value_type x = *(r.begin() + offset);
    // test contains(value_type)
    assert(r.contains(r.start()) == !r.empty());
    assert(!r.contains(r.finish()));
    assert(r.contains(x) == (offset != size));

    range::const_iterator p = r.find(x);
    assert((p == r.end()) == (x == r.finish()));
    assert(r.find(r.finish()) == r.end());

    if (offset != size)
    {
        assert(x == r[offset]);
        assert(x == r.at(offset));
    }

    bool caught_out_of_range = false;
    try {
        bool never_initialized = x == r.at(size);
        (void)never_initialized;
    }
    catch(std::out_of_range&)
    {
        caught_out_of_range = true;
    }
    catch(...)
    {
    }
    assert(caught_out_of_range);
}

// Those tests must be skipped for other ranges
template <class T>
void category_test_1(
    const boost::half_open_range<T>&, std::forward_iterator_tag)
{
}

unsigned indices[][2] = { {0,0},{0,1},{0,2},{0,3},
                                {1,1},{1,2},{1,3},
                                      {2,2},{2,3},
                                            {3,3}};

template <class Range>
void category_test_2(
    const std::vector<Range>& ranges, unsigned i, unsigned j, std::random_access_iterator_tag)
{
    typedef Range range;
    const range& ri = ranges[i];
    const range& rj = ranges[j];

    if (indices[i][0] <= indices[j][0] && indices[i][1] >= indices[j][1])
        assert(ri.contains(rj));

    if (ri.contains(rj))
        assert((ri & rj) == rj);
    assert(boost::intersects(ri, rj) == !(ri & rj).empty());

    range t1(ri);
    t1 &= rj;
    assert(t1 == range(indices[i][0] > indices[j][0] ? ri.start() : rj.start(),
                       indices[i][1] < indices[j][1] ? ri.finish() : rj.finish()));
    assert(t1 == (ri & rj));
    
    range t2(ri);
    t2 |= rj;
    
    if (ri.empty())
        assert(t2 == rj);
    else if (rj.empty())
        assert(t2 == ri);
    else
        assert(t2 == range(indices[i][0] < indices[j][0] ? ri.start() : rj.start(),
                           indices[i][1] > indices[j][1] ? ri.finish() : rj.finish()));
    assert(t2 == (ri | rj));
    if (i == j)
        assert(ri == rj);
    
    if (ri.empty() || rj.empty())
        assert((ri == rj) == (ri.empty() && rj.empty()));
    else
        assert((ri == rj) == (ri.start() == rj.start() && ri.finish() == rj.finish()));

    assert((ri == rj) == !(ri != rj));

    bool same = ri == rj;
    bool one_empty = ri.empty() != rj.empty();

    std::less<range> less;
    std::less_equal<range> less_equal;
    std::greater<range> greater;
    std::greater_equal<range> greater_equal;
    
    if (same)
    {
        assert(greater_equal(ri,rj));
        assert(less_equal(ri,rj));
        assert(!greater(ri,rj));
        assert(!less(ri,rj));
    }
    else if (one_empty)
    {
        const range& empty = ri.empty() ? ri : rj;
        const range& non_empty = rj.empty() ? ri : rj;
        
        assert(less(empty,non_empty));
        assert(less_equal(empty,non_empty));
        assert(!greater(empty,non_empty));
        assert(!greater_equal(empty,non_empty));
        assert(!less(non_empty,empty));
        assert(!less_equal(non_empty,empty));
        assert(greater(non_empty,empty));
        assert(greater_equal(non_empty,empty));
    }
    else {
        if (indices[i][0] < indices[j][0] ||
            indices[i][0] == indices[j][0] && indices[i][1] < indices[j][1])
        {
            assert(!greater_equal(ri,rj));
            assert(less(ri,rj));
        }

        if (indices[i][0] < indices[j][0] ||
            indices[i][0] == indices[j][0] && indices[i][1] <= indices[j][1])
        {
            assert(!greater(ri,rj));
            assert(less_equal(ri,rj));
        }

        if (indices[i][0] > indices[j][0] ||
            indices[i][0] == indices[j][0] && indices[i][1] > indices[j][1])
        {
            assert(!less_equal(ri,rj));
            assert(greater(ri,rj));
        }

        if (indices[i][0] > indices[j][0] ||
            indices[i][0] == indices[j][0] && indices[i][1] >= indices[j][1])
        {
            assert(!less(ri,rj));
            assert(greater_equal(ri,rj));
        }
    }
}


template <class Range>
void category_test_2(
    const std::vector<Range>&, unsigned, unsigned, std::forward_iterator_tag)
{
}

template <class T>
void category_test_2(
    const std::vector<boost::half_open_range<T> >&, unsigned, unsigned, std::bidirectional_iterator_tag)
{
}

template <class Range>
void test_back(Range& x, std::bidirectional_iterator_tag)
{
    assert(x.back() == boost::prior(x.finish()));
}

template <class Range>
void test_back(Range& x, std::forward_iterator_tag)
{
}

template <class T>
boost::half_open_range<T> range_identity(const boost::half_open_range<T>& x)
{
    return x;
}

template <class T>
void test(T x0, T x1, T x2, T x3)
{
    std::vector<boost::half_open_range<T> > ranges;
    typedef boost::half_open_range<T> range;

    T bounds[4] = { x0, x1, x2, x3 };

    const std::size_t num_ranges = sizeof(indices)/sizeof(*indices);
    // test construction
    for (std::size_t n = 0; n < num_ranges;++n)
    {
        T start = bounds[indices[n][0]];
        T finish = bounds[indices[n][1]];
        boost::half_open_range<T> r(start, finish);
        ranges.push_back(r);
    }
    
    // test implicit conversion from std::pair<T,T>
    range converted = std::pair<T,T>(x0,x0);
    (void)converted;

    // test assignment, equality and inequality
    range r00 = range(x0, x0);
    assert(r00 == range(x0,x0));
    assert(r00 == range(x1,x1)); // empty ranges are all equal
    if (x3 != x0)
        assert(r00 != range(x0, x3));
    r00 = range(x0, x3);
    assert(r00 == range(x0, x3));
    if (x3 != x0)
        assert(r00 != range(x0, x0));

    typedef typename range::iterator iterator;
    typedef typename iterator::iterator_category category;
    
    for (unsigned i = 0; i < num_ranges; ++i)
    {
        const range& r = ranges[i];
            
        // test begin(), end(), basic iteration.
        unsigned count = 0;
        for (range::const_iterator p = r.begin(), finish = r.end();
             p != finish;
             ++p, ++count)
        {
            assert(count < 2100);
        }

        // test size(), empty(), front(), back()
        assert((unsigned)r.size() == count);
        if (indices[i][0] == indices[i][1])
            assert(r.empty());
        if (r.empty())
            assert(r.size() == 0);
        if (!r.empty())
        {
            assert(r.front() == r.start());
            test_back(r, category());
        }

            // test swap
        range r1(r);
        range r2(x0,x3);
        const bool same = r1 == r2;
        r1.swap(r2);
        assert(r1 == range(x0,x3));
        assert(r2 == r);
        if (!same) {
            assert(r1 != r);
            assert(r2 != range(x0,x3));
        }

        // do individual tests for random-access iterators
        category_test_1(r, category());
    }

    for (unsigned j = 0; j < num_ranges; ++j) {
        for (unsigned k = 0; k < num_ranges; ++k) {
            category_test_2(ranges, j, k, category());
        }
    }
    
}

template <class Integer>
void test_integer(Integer* = 0) // default arg works around MSVC bug
{
    Integer a = 0;
    Integer b = a + unsigned_random(128 - a);
    Integer c = b + unsigned_random(128 - b);
    Integer d = c + unsigned_random(128 - c);

    test(a, b, c, d);
}

template <class Container>
void test_container(Container* = 0)  // default arg works around MSVC bug
{
    Container c(unsigned_random(1673));

    const typename Container::size_type offset1 = unsigned_random(c.size());
    const typename Container::size_type offset2 = unsigned_random(c.size() - offset1);
    typename Container::iterator internal1 = c.begin();
    std::advance(internal1, offset1);
    typename Container::iterator internal2 = internal1;
    std::advance(internal2, offset2);
    
    test(c.begin(), internal1, internal2, c.end());

    typedef typename Container::const_iterator const_iterator;
    test(const_iterator(c.begin()),
         const_iterator(internal1),
         const_iterator(internal2),
         const_iterator(c.end()));
}

int main()
{
    // Test the built-in integer types.
    test_integer<char>();
    test_integer<unsigned char>();
    test_integer<signed char>();
    test_integer<wchar_t>();
    test_integer<short>();
    test_integer<unsigned short>();
    test_integer<int>();
    test_integer<unsigned int>();
    test_integer<long>();
    test_integer<unsigned long>();
#if defined(ULLONG_MAX) || defined(ULONG_LONG_MAX)
    test_integer<long long>();
    test_integer<unsigned long long>();
#endif
    // Some tests on container iterators, to prove we handle a few different categories
    test_container<std::vector<int> >();
    test_container<std::list<int> >();
#ifndef BOOST_NO_SLIST
    test_container<BOOST_STD_EXTENSION_NAMESPACE::slist<int> >();
#endif
    // Also prove that we can handle raw pointers.
    int array[2000];
    const std::size_t a = 0;
    const std::size_t b = a + unsigned_random(2000 - a);
    const std::size_t c = b + unsigned_random(2000 - b);
    test(array, array+b, array+c, array+2000);
    return 0;
}