Imported Upstream version 1.72.0
[platform/upstream/boost.git] / libs / bimap / test / test_bimap_range.cpp
1 // Boost.Bimap
2 //
3 // Copyright (c) 2006-2007 Matias Capeletto
4 //
5 // Distributed under the Boost Software License, Version 1.0.
6 // (See accompanying file LICENSE_1_0.txt or copy at
7 // http://www.boost.org/LICENSE_1_0.txt)
8
9 //  VC++ 8.0 warns on usage of certain Standard Library and API functions that
10 //  can be cause buffer overruns or other possible security issues if misused.
11 //  See https://web.archive.org/web/20071014014301/http://msdn.microsoft.com/msdnmag/issues/05/05/SafeCandC/default.aspx
12 //  But the wording of the warning is misleading and unsettling, there are no
13 //  portable alternative functions, and VC++ 8.0's own libraries use the
14 //  functions in question. So turn off the warnings.
15 #define _CRT_SECURE_NO_DEPRECATE
16 #define _SCL_SECURE_NO_DEPRECATE
17
18 #include <boost/config.hpp>
19
20 // Boost.Test
21 #include <boost/test/minimal.hpp>
22
23 #include <boost/config.hpp>
24
25 #include <algorithm>
26
27 #include <boost/range/functions.hpp>
28 #include <boost/range/metafunctions.hpp>
29
30 #include <boost/bimap/bimap.hpp>
31 #include <boost/bimap/multiset_of.hpp>
32 #include <boost/bimap/support/lambda.hpp>
33
34
35 template< class ForwardReadableRange, class UnaryFunctor >
36 UnaryFunctor for_each(const ForwardReadableRange & r, UnaryFunctor func)
37 {
38     typedef typename 
39     boost::range_const_iterator<ForwardReadableRange>::type const_iterator;
40
41     for(const_iterator i= boost::begin(r), iend= boost::end(r); i!=iend; ++i )
42     {
43         func(*i);
44     }
45
46     return func;
47 }
48
49 struct do_something_with_a_pair
50 {
51     template< class Pair >
52     void operator()(const Pair & p)
53     {
54         BOOST_CHECK( p.first && p.second );
55     }
56 };
57
58 int test_bimap_range()
59 {
60     using namespace boost::bimaps;
61
62     typedef bimap< double, multiset_of<int> > bm_type;
63
64
65     bm_type bm;
66     bm.insert( bm_type::value_type(1.1 , 1) );
67     bm.insert( bm_type::value_type(2.2 , 2) );
68     bm.insert( bm_type::value_type(3.3 , 3) );
69     bm.insert( bm_type::value_type(4.4 , 4) );
70
71
72     for_each( bm.left.range( 1.0 < _key, _key < 5.0 ),
73               do_something_with_a_pair() );
74
75     for_each( bm.right.range( unbounded, _key <= 2 ),
76               do_something_with_a_pair() );
77
78
79     // left range
80     {
81
82     bm_type::left_range_type r = bm.left.range( 2.0 < _key, _key < 4.0 );
83     BOOST_CHECK( ! boost::empty(r) );
84     BOOST_CHECK( boost::begin(r) == bm.left.upper_bound(2.0) );
85     BOOST_CHECK( boost::end(r)   == bm.left.lower_bound(4.0) );
86
87     }
88
89     // right range
90     {
91
92     bm_type::right_range_type r = bm.right.range( 2 <= _key, _key <= 3 );
93     BOOST_CHECK( ! boost::empty(r) );
94     BOOST_CHECK( boost::begin(r) == bm.right.lower_bound(2) );
95     BOOST_CHECK( boost::end(r)   == bm.right.upper_bound(3) );
96
97     }
98
99     // const range from range
100     {
101
102     bm_type:: left_const_range_type lr = bm. left.range( unbounded, _key < 4.0 );
103     bm_type::right_const_range_type rr = bm.right.range( 2 < _key ,  unbounded );
104
105     }
106
107     const bm_type & cbm = bm;
108
109     // left const range
110     {
111     bm_type:: left_const_range_type r = cbm.left.range( unbounded, unbounded );
112     BOOST_CHECK( ! boost::empty(r) );
113     BOOST_CHECK( boost::begin(r) == cbm.left.begin() );
114
115     }
116
117     // right const range
118     {
119
120     bm_type::right_const_range_type r = cbm.right.range( 1 < _key, _key < 1 );
121     BOOST_CHECK( boost::empty(r) );
122
123     }
124
125     return 0;
126 }
127 //]
128
129 int test_main( int, char* [] )
130 {
131     test_bimap_range();
132     return 0;
133 }
134