test_bimap_operator_bracket.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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. // Boost.Bimap
  20. #include <boost/bimap/support/lambda.hpp>
  21. #include <boost/bimap/bimap.hpp>
  22. #include <boost/bimap/unordered_set_of.hpp>
  23. #include <boost/bimap/list_of.hpp>
  24. #include <boost/bimap/vector_of.hpp>
  25. #include <boost/bimap/unconstrained_set_of.hpp>
  26. void test_bimap_operator_bracket()
  27. {
  28. using namespace boost::bimaps;
  29. // Simple test
  30. {
  31. typedef bimap< int, std::string > bm;
  32. bm b;
  33. b.insert( bm::value_type(0,"0") );
  34. b.insert( bm::value_type(1,"1") );
  35. b.insert( bm::value_type(2,"2") );
  36. b.insert( bm::value_type(3,"3") );
  37. BOOST_CHECK( b.left.at(1) == "1" );
  38. // Out of range test
  39. {
  40. bool value_not_found_test_passed = false;
  41. b.clear();
  42. try
  43. {
  44. bool comp;
  45. comp = b.left.at(2) < "banana";
  46. }
  47. catch( std::out_of_range & )
  48. {
  49. value_not_found_test_passed = true;
  50. }
  51. BOOST_CHECK( value_not_found_test_passed );
  52. }
  53. }
  54. // Mutable data test (1)
  55. {
  56. typedef bimap<int, list_of<std::string> > bm;
  57. bm b;
  58. // Out of range test
  59. {
  60. bool value_not_found_test_passed = false;
  61. b.clear();
  62. try
  63. {
  64. bool comp;
  65. comp = b.left.at(1) < "banana";
  66. }
  67. catch( std::out_of_range & )
  68. {
  69. value_not_found_test_passed = true;
  70. }
  71. BOOST_CHECK( value_not_found_test_passed );
  72. }
  73. // Out of range test (const version)
  74. {
  75. bool value_not_found_test_passed = false;
  76. b.clear();
  77. try
  78. {
  79. const bm & cb(b);
  80. bool comp;
  81. comp = cb.left.at(1) < "banana";
  82. }
  83. catch( std::out_of_range & )
  84. {
  85. value_not_found_test_passed = true;
  86. }
  87. BOOST_CHECK( value_not_found_test_passed );
  88. }
  89. BOOST_CHECK( b.left[1] == "" );
  90. BOOST_CHECK( b.left.at(1) == "" );
  91. b.left[2] = "two";
  92. BOOST_CHECK( b.left.at(2) == "two" );
  93. b.left[2] = "<<two>>";
  94. BOOST_CHECK( b.left.at(2) == "<<two>>" );
  95. b.left.at(2) = "two";
  96. BOOST_CHECK( b.left.at(2) == "two" );
  97. }
  98. // Mutable data test (2)
  99. {
  100. typedef bimap< vector_of<int>, unordered_set_of<std::string> > bm;
  101. bm b;
  102. // Out of range test
  103. {
  104. bool value_not_found_test_passed = false;
  105. b.clear();
  106. try
  107. {
  108. bool comp;
  109. comp = b.right.at("banana") < 1;
  110. }
  111. catch( std::out_of_range & )
  112. {
  113. value_not_found_test_passed = true;
  114. }
  115. BOOST_CHECK( value_not_found_test_passed );
  116. }
  117. // Out of range test (const version)
  118. {
  119. bool value_not_found_test_passed = false;
  120. b.clear();
  121. try
  122. {
  123. const bm & cb(b);
  124. bool comp;
  125. comp = cb.right.at("banana") < 1;
  126. }
  127. catch( std::out_of_range & )
  128. {
  129. value_not_found_test_passed = true;
  130. }
  131. BOOST_CHECK( value_not_found_test_passed );
  132. }
  133. b.right["one"];
  134. BOOST_CHECK( b.size() == 1 );
  135. b.right["two"] = 2;
  136. BOOST_CHECK( b.right.at("two") == 2 );
  137. b.right["two"] = -2;
  138. BOOST_CHECK( b.right.at("two") == -2 );
  139. b.right.at("two") = 2;
  140. BOOST_CHECK( b.right.at("two") == 2 );
  141. }
  142. // Mutable data test (3)
  143. {
  144. typedef bimap< unconstrained_set_of<int>,
  145. unordered_set_of<std::string>,
  146. right_based > bm;
  147. bm b;
  148. b.right["one"];
  149. BOOST_CHECK( b.size() == 1 );
  150. b.right["two"] = 2;
  151. BOOST_CHECK( b.right.at("two") == 2 );
  152. b.right["two"] = -2;
  153. BOOST_CHECK( b.right.at("two") == -2 );
  154. b.right.at("two") = 2;
  155. BOOST_CHECK( b.right.at("two") == 2 );
  156. }
  157. }
  158. int test_main( int, char* [] )
  159. {
  160. test_bimap_operator_bracket();
  161. return 0;
  162. }