1/* Boost.MultiIndex test for range().
2 *
3 * Copyright 2003-2020 Joaquin M Lopez Munoz.
4 * Distributed under the Boost Software License, Version 1.0.
5 * (See accompanying file LICENSE_1_0.txt or copy at
6 * http://www.boost.org/LICENSE_1_0.txt)
7 *
8 * See http://www.boost.org/libs/multi_index for library home page.
9 */
10
11#include "test_range.hpp"
12
13#include <boost/config.hpp> /* keep it first to prevent nasty warns in MSVC */
14#include <algorithm>
15#include <boost/bind/bind.hpp>
16#include <boost/detail/lightweight_test.hpp>
17#include "pre_multi_index.hpp"
18#include <boost/multi_index_container.hpp>
19#include <boost/multi_index/identity.hpp>
20#include <boost/multi_index/ordered_index.hpp>
21#include <boost/preprocessor/seq/enum.hpp>
22#include <functional>
23
24using namespace boost::multi_index;
25
26typedef multi_index_container<int> int_set;
27typedef int_set::iterator int_set_iterator;
28
29#undef CHECK_RANGE
30#define CHECK_RANGE(p,check_seq) \
31{\
32 int v[]={BOOST_PP_SEQ_ENUM(check_seq)};\
33 std::size_t size_v=sizeof(v)/sizeof(int);\
34 BOOST_TEST(std::size_t(std::distance((p).first,(p).second))==size_v);\
35 BOOST_TEST(std::equal((p).first,(p).second,&v[0]));\
36}
37
38#undef CHECK_VOID_RANGE
39#define CHECK_VOID_RANGE(p) BOOST_TEST((p).first==(p).second)
40
41#undef BIND1ST
42#define BIND1ST(f,x) ::boost::bind<bool>(f,x,::boost::arg<1>())
43
44#undef BIND2ND
45#define BIND2ND(f,x) ::boost::bind<bool>(f,::boost::arg<1>(),x)
46
47void test_range()
48{
49 int_set is;
50
51 for(int i=1;i<=10;++i)is.insert(x: i);
52
53 std::pair<int_set::iterator,int_set::iterator> p;
54
55 p=is.range(lower: unbounded,upper: unbounded);
56 CHECK_RANGE(p,(1)(2)(3)(4)(5)(6)(7)(8)(9)(10));
57
58 p=is.range(
59 BIND1ST(std::less<int>(),5), /* 5 < x */
60 upper: unbounded);
61 CHECK_RANGE(p,(6)(7)(8)(9)(10));
62
63 p=is.range(
64 BIND1ST(std::less_equal<int>(),8), /* 8 <= x */
65 upper: unbounded);
66 CHECK_RANGE(p,(8)(9)(10));
67
68 p=is.range(
69 BIND1ST(std::less_equal<int>(),11), /* 11 <= x */
70 upper: unbounded);
71 CHECK_VOID_RANGE(p);
72
73 p=is.range(
74 lower: unbounded,
75 BIND2ND(std::less<int>(),8)); /* x < 8 */
76 CHECK_RANGE(p,(1)(2)(3)(4)(5)(6)(7));
77
78 p=is.range(
79 lower: unbounded,
80 BIND2ND(std::less_equal<int>(),4)); /* x <= 4 */
81 CHECK_RANGE(p,(1)(2)(3)(4));
82
83 p=is.range(
84 lower: unbounded,
85 BIND2ND(std::less_equal<int>(),0)); /* x <= 0 */
86 CHECK_VOID_RANGE(p);
87
88 p=is.range(
89 BIND1ST(std::less<int>(),6), /* 6 < x */
90 BIND2ND(std::less_equal<int>(),9)); /* x <= 9 */
91 CHECK_RANGE(p,(7)(8)(9));
92
93 p=is.range(
94 BIND1ST(std::less_equal<int>(),4), /* 4 <= x */
95 BIND2ND(std::less<int>(),5)); /* x < 5 */
96 CHECK_RANGE(p,(4));
97
98 p=is.range(
99 BIND1ST(std::less_equal<int>(),10), /* 10 <= x */
100 BIND2ND(std::less_equal<int>(),10)); /* x <= 10 */
101 CHECK_RANGE(p,(10));
102
103 p=is.range(
104 BIND1ST(std::less<int>(),0), /* 0 < x */
105 BIND2ND(std::less<int>(),11)); /* x < 11 */
106 CHECK_RANGE(p,(1)(2)(3)(4)(5)(6)(7)(8)(9)(10));
107
108 p=is.range(
109 BIND1ST(std::less<int>(),7), /* 7 < x */
110 BIND2ND(std::less_equal<int>(),7)); /* x <= 7 */
111 CHECK_VOID_RANGE(p);
112 BOOST_TEST(p.first==is.upper_bound(7));
113
114 p=is.range(
115 BIND1ST(std::less_equal<int>(),8), /* 8 <= x */
116 BIND2ND(std::less<int>(),2)); /* x < 2 */
117 CHECK_VOID_RANGE(p);
118 BOOST_TEST(p.first==is.lower_bound(8));
119
120 p=is.range(
121 BIND1ST(std::less<int>(),4), /* 4 < x */
122 BIND2ND(std::less<int>(),5)); /* x < 5 */
123 CHECK_VOID_RANGE(p);
124 BOOST_TEST(p.first!=is.end());
125}
126

source code of boost/libs/multi_index/test/test_range.cpp