summaryrefslogtreecommitdiffstats
path: root/src/boost/libs/bimap/test/test_bimap_range.cpp
blob: 747b5934a8eb2358c542a6aa28083784dd2a34d6 (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
// Boost.Bimap
//
// Copyright (c) 2006-2007 Matias Capeletto
//
// 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)

//  VC++ 8.0 warns on usage of certain Standard Library and API functions that
//  can be cause buffer overruns or other possible security issues if misused.
//  See https://web.archive.org/web/20071014014301/http://msdn.microsoft.com/msdnmag/issues/05/05/SafeCandC/default.aspx
//  But the wording of the warning is misleading and unsettling, there are no
//  portable alternative functions, and VC++ 8.0's own libraries use the
//  functions in question. So turn off the warnings.
#define _CRT_SECURE_NO_DEPRECATE
#define _SCL_SECURE_NO_DEPRECATE

#include <boost/config.hpp>

// Boost.Test
#include <boost/test/minimal.hpp>

#include <boost/config.hpp>

#include <algorithm>

#include <boost/range/functions.hpp>
#include <boost/range/metafunctions.hpp>

#include <boost/bimap/bimap.hpp>
#include <boost/bimap/multiset_of.hpp>
#include <boost/bimap/support/lambda.hpp>


template< class ForwardReadableRange, class UnaryFunctor >
UnaryFunctor for_each(const ForwardReadableRange & r, UnaryFunctor func)
{
    typedef typename 
    boost::range_const_iterator<ForwardReadableRange>::type const_iterator;

    for(const_iterator i= boost::begin(r), iend= boost::end(r); i!=iend; ++i )
    {
        func(*i);
    }

    return func;
}

struct do_something_with_a_pair
{
    template< class Pair >
    void operator()(const Pair & p)
    {
        BOOST_CHECK( p.first && p.second );
    }
};

int test_bimap_range()
{
    using namespace boost::bimaps;

    typedef bimap< double, multiset_of<int> > bm_type;


    bm_type bm;
    bm.insert( bm_type::value_type(1.1 , 1) );
    bm.insert( bm_type::value_type(2.2 , 2) );
    bm.insert( bm_type::value_type(3.3 , 3) );
    bm.insert( bm_type::value_type(4.4 , 4) );


    for_each( bm.left.range( 1.0 < _key, _key < 5.0 ),
              do_something_with_a_pair() );

    for_each( bm.right.range( unbounded, _key <= 2 ),
              do_something_with_a_pair() );


    // left range
    {

    bm_type::left_range_type r = bm.left.range( 2.0 < _key, _key < 4.0 );
    BOOST_CHECK( ! boost::empty(r) );
    BOOST_CHECK( boost::begin(r) == bm.left.upper_bound(2.0) );
    BOOST_CHECK( boost::end(r)   == bm.left.lower_bound(4.0) );

    }

    // right range
    {

    bm_type::right_range_type r = bm.right.range( 2 <= _key, _key <= 3 );
    BOOST_CHECK( ! boost::empty(r) );
    BOOST_CHECK( boost::begin(r) == bm.right.lower_bound(2) );
    BOOST_CHECK( boost::end(r)   == bm.right.upper_bound(3) );

    }

    // const range from range
    {

    bm_type:: left_const_range_type lr = bm. left.range( unbounded, _key < 4.0 );
    bm_type::right_const_range_type rr = bm.right.range( 2 < _key ,  unbounded );

    }

    const bm_type & cbm = bm;

    // left const range
    {
    bm_type:: left_const_range_type r = cbm.left.range( unbounded, unbounded );
    BOOST_CHECK( ! boost::empty(r) );
    BOOST_CHECK( boost::begin(r) == cbm.left.begin() );

    }

    // right const range
    {

    bm_type::right_const_range_type r = cbm.right.range( 1 < _key, _key < 1 );
    BOOST_CHECK( boost::empty(r) );

    }

    return 0;
}
//]

int test_main( int, char* [] )
{
    test_bimap_range();
    return 0;
}