| 1 | /* Boost.MultiIndex test for range(). | 
|---|
| 2 |  * | 
|---|
| 3 |  * Copyright 2003-2007 Joaquín M López Muñoz. | 
|---|
| 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 <functional> | 
|---|
| 16 | #include "pre_multi_index.hpp" | 
|---|
| 17 | #include <boost/multi_index_container.hpp> | 
|---|
| 18 | #include <boost/multi_index/identity.hpp> | 
|---|
| 19 | #include <boost/multi_index/ordered_index.hpp> | 
|---|
| 20 | #include <boost/test/test_tools.hpp> | 
|---|
| 21 |  | 
|---|
| 22 | using namespace boost::multi_index; | 
|---|
| 23 |  | 
|---|
| 24 | typedef multi_index_container<int>  int_set; | 
|---|
| 25 | typedef int_set::iterator int_set_iterator; | 
|---|
| 26 |  | 
|---|
| 27 | #undef _ | 
|---|
| 28 | #define _ , | 
|---|
| 29 |  | 
|---|
| 30 | #undef CHECK_RANGE | 
|---|
| 31 | #define CHECK_RANGE(p,check_range) \ | 
|---|
| 32 | {\ | 
|---|
| 33 |   int v[]=check_range;\ | 
|---|
| 34 |   std::size_t size_v=sizeof(v)/sizeof(int);\ | 
|---|
| 35 |   BOOST_CHECK(std::size_t(std::distance((p).first,(p).second))==size_v);\ | 
|---|
| 36 |   BOOST_CHECK(std::equal((p).first,(p).second,&v[0]));\ | 
|---|
| 37 | } | 
|---|
| 38 |  | 
|---|
| 39 | #undef CHECK_VOID_RANGE | 
|---|
| 40 | #define CHECK_VOID_RANGE(p) BOOST_CHECK((p).first==(p).second) | 
|---|
| 41 |  | 
|---|
| 42 | void test_range() | 
|---|
| 43 | { | 
|---|
| 44 |   int_set is; | 
|---|
| 45 |  | 
|---|
| 46 |   for(int i=1;i<=10;++i)is.insert(i); | 
|---|
| 47 |  | 
|---|
| 48 |   std::pair<int_set::iterator,int_set::iterator> p; | 
|---|
| 49 |  | 
|---|
| 50 |   p=is.range(unbounded,unbounded); | 
|---|
| 51 |   CHECK_RANGE(p,{1 _ 2 _ 3 _ 4 _ 5 _ 6 _ 7 _ 8 _ 9 _ 10}); | 
|---|
| 52 |  | 
|---|
| 53 |   p=is.range( | 
|---|
| 54 |     std::bind1st(std::less<int>(),5), /* 5 < x */ | 
|---|
| 55 |     unbounded); | 
|---|
| 56 |   CHECK_RANGE(p,{6 _ 7 _ 8 _ 9 _ 10}); | 
|---|
| 57 |  | 
|---|
| 58 |   p=is.range( | 
|---|
| 59 |     std::bind1st(std::less_equal<int>(),8), /* 8 <= x */ | 
|---|
| 60 |     unbounded); | 
|---|
| 61 |   CHECK_RANGE(p,{8 _ 9 _ 10}); | 
|---|
| 62 |  | 
|---|
| 63 |   p=is.range( | 
|---|
| 64 |     std::bind1st(std::less_equal<int>(),11), /* 11 <= x */ | 
|---|
| 65 |     unbounded); | 
|---|
| 66 |   CHECK_VOID_RANGE(p); | 
|---|
| 67 |  | 
|---|
| 68 |   p=is.range( | 
|---|
| 69 |     unbounded, | 
|---|
| 70 |     std::bind2nd(std::less<int>(),8)); /* x < 8 */ | 
|---|
| 71 |   CHECK_RANGE(p,{1 _ 2 _ 3 _ 4 _ 5 _ 6 _ 7}); | 
|---|
| 72 |  | 
|---|
| 73 |   p=is.range( | 
|---|
| 74 |     unbounded, | 
|---|
| 75 |     std::bind2nd(std::less_equal<int>(),4)); /* x <= 4 */ | 
|---|
| 76 |   CHECK_RANGE(p,{1 _ 2 _ 3 _ 4}); | 
|---|
| 77 |  | 
|---|
| 78 |   p=is.range( | 
|---|
| 79 |     unbounded, | 
|---|
| 80 |     std::bind2nd(std::less_equal<int>(),0)); /* x <= 0 */ | 
|---|
| 81 |   CHECK_VOID_RANGE(p); | 
|---|
| 82 |  | 
|---|
| 83 |   p=is.range( | 
|---|
| 84 |     std::bind1st(std::less<int>(),6),        /* 6 <  x */ | 
|---|
| 85 |     std::bind2nd(std::less_equal<int>(),9)); /* x <= 9 */ | 
|---|
| 86 |   CHECK_RANGE(p,{7 _ 8 _ 9}); | 
|---|
| 87 |  | 
|---|
| 88 |   p=is.range( | 
|---|
| 89 |     std::bind1st(std::less_equal<int>(),4), /* 4 <= x */ | 
|---|
| 90 |     std::bind2nd(std::less<int>(),5));      /* x <  5 */ | 
|---|
| 91 |   CHECK_RANGE(p,{4}); | 
|---|
| 92 |  | 
|---|
| 93 |   p=is.range( | 
|---|
| 94 |     std::bind1st(std::less_equal<int>(),10),  /* 10 <=  x */ | 
|---|
| 95 |     std::bind2nd(std::less_equal<int>(),10)); /*  x <= 10 */ | 
|---|
| 96 |   CHECK_RANGE(p,{10}); | 
|---|
| 97 |  | 
|---|
| 98 |   p=is.range( | 
|---|
| 99 |     std::bind1st(std::less<int>(),0),   /* 0 <  x */ | 
|---|
| 100 |     std::bind2nd(std::less<int>(),11)); /* x < 11 */ | 
|---|
| 101 |   CHECK_RANGE(p,{1 _ 2 _ 3 _ 4 _ 5 _ 6 _ 7 _ 8 _ 9 _ 10}); | 
|---|
| 102 |  | 
|---|
| 103 |   p=is.range( | 
|---|
| 104 |     std::bind1st(std::less<int>(),7),        /* 7 <  x */ | 
|---|
| 105 |     std::bind2nd(std::less_equal<int>(),7)); /* x <= 7 */ | 
|---|
| 106 |   CHECK_VOID_RANGE(p); | 
|---|
| 107 |  | 
|---|
| 108 |   p=is.range( | 
|---|
| 109 |     std::bind1st(std::less_equal<int>(),8), /* 8 <= x */ | 
|---|
| 110 |     std::bind2nd(std::less<int>(),2));      /* x <  2 */ | 
|---|
| 111 |   CHECK_VOID_RANGE(p); | 
|---|
| 112 |  | 
|---|
| 113 |   p=is.range( | 
|---|
| 114 |     std::bind1st(std::less<int>(),4),  /* 4 < x */ | 
|---|
| 115 |     std::bind2nd(std::less<int>(),5)); /* x < 5 */ | 
|---|
| 116 |   CHECK_VOID_RANGE(p); | 
|---|
| 117 |   BOOST_CHECK(p.first!=is.end()&&p.second!=is.end()); | 
|---|
| 118 | } | 
|---|