test_bimap_range.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. // VC++ 8.0 warns on usage of certain Standard Library and API functions that
  9. // can be cause buffer overruns or other possible security issues if misused.
  10. // See https://web.archive.org/web/20071014014301/http://msdn.microsoft.com/msdnmag/issues/05/05/SafeCandC/default.aspx
  11. // But the wording of the warning is misleading and unsettling, there are no
  12. // portable alternative functions, and VC++ 8.0's own libraries use the
  13. // functions in question. So turn off the warnings.
  14. #define _CRT_SECURE_NO_DEPRECATE
  15. #define _SCL_SECURE_NO_DEPRECATE
  16. #include <boost/config.hpp>
  17. // Boost.Test
  18. #include <boost/test/minimal.hpp>
  19. #include <boost/config.hpp>
  20. #include <algorithm>
  21. #include <boost/range/functions.hpp>
  22. #include <boost/range/metafunctions.hpp>
  23. #include <boost/bimap/bimap.hpp>
  24. #include <boost/bimap/multiset_of.hpp>
  25. #include <boost/bimap/support/lambda.hpp>
  26. template< class ForwardReadableRange, class UnaryFunctor >
  27. UnaryFunctor for_each(const ForwardReadableRange & r, UnaryFunctor func)
  28. {
  29. typedef typename
  30. boost::range_const_iterator<ForwardReadableRange>::type const_iterator;
  31. for(const_iterator i= boost::begin(r), iend= boost::end(r); i!=iend; ++i )
  32. {
  33. func(*i);
  34. }
  35. return func;
  36. }
  37. struct do_something_with_a_pair
  38. {
  39. template< class Pair >
  40. void operator()(const Pair & p)
  41. {
  42. BOOST_CHECK( p.first && p.second );
  43. }
  44. };
  45. int test_bimap_range()
  46. {
  47. using namespace boost::bimaps;
  48. typedef bimap< double, multiset_of<int> > bm_type;
  49. bm_type bm;
  50. bm.insert( bm_type::value_type(1.1 , 1) );
  51. bm.insert( bm_type::value_type(2.2 , 2) );
  52. bm.insert( bm_type::value_type(3.3 , 3) );
  53. bm.insert( bm_type::value_type(4.4 , 4) );
  54. for_each( bm.left.range( 1.0 < _key, _key < 5.0 ),
  55. do_something_with_a_pair() );
  56. for_each( bm.right.range( unbounded, _key <= 2 ),
  57. do_something_with_a_pair() );
  58. // left range
  59. {
  60. bm_type::left_range_type r = bm.left.range( 2.0 < _key, _key < 4.0 );
  61. BOOST_CHECK( ! boost::empty(r) );
  62. BOOST_CHECK( boost::begin(r) == bm.left.upper_bound(2.0) );
  63. BOOST_CHECK( boost::end(r) == bm.left.lower_bound(4.0) );
  64. }
  65. // right range
  66. {
  67. bm_type::right_range_type r = bm.right.range( 2 <= _key, _key <= 3 );
  68. BOOST_CHECK( ! boost::empty(r) );
  69. BOOST_CHECK( boost::begin(r) == bm.right.lower_bound(2) );
  70. BOOST_CHECK( boost::end(r) == bm.right.upper_bound(3) );
  71. }
  72. // const range from range
  73. {
  74. bm_type:: left_const_range_type lr = bm. left.range( unbounded, _key < 4.0 );
  75. bm_type::right_const_range_type rr = bm.right.range( 2 < _key , unbounded );
  76. }
  77. const bm_type & cbm = bm;
  78. // left const range
  79. {
  80. bm_type:: left_const_range_type r = cbm.left.range( unbounded, unbounded );
  81. BOOST_CHECK( ! boost::empty(r) );
  82. BOOST_CHECK( boost::begin(r) == cbm.left.begin() );
  83. }
  84. // right const range
  85. {
  86. bm_type::right_const_range_type r = cbm.right.range( 1 < _key, _key < 1 );
  87. BOOST_CHECK( boost::empty(r) );
  88. }
  89. return 0;
  90. }
  91. //]
  92. int test_main( int, char* [] )
  93. {
  94. test_bimap_range();
  95. return 0;
  96. }