map.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. // Boost.Range library
  2. //
  3. // Copyright Neil Groves 2009. Use, modification and
  4. // distribution is subject to the Boost Software License, Version
  5. // 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  6. // http://www.boost.org/LICENSE_1_0.txt)
  7. //
  8. //
  9. // For more information, see http://www.boost.org/libs/range/
  10. //
  11. #include <boost/range/adaptor/map.hpp>
  12. #include <boost/test/test_tools.hpp>
  13. #include <boost/test/unit_test.hpp>
  14. #include <boost/array.hpp>
  15. #include <boost/assign.hpp>
  16. #include <boost/range/algorithm_ext.hpp>
  17. #include <algorithm>
  18. #include <map>
  19. #include <vector>
  20. namespace boost
  21. {
  22. namespace
  23. {
  24. template< class Container >
  25. void map_test_keys( Container& c )
  26. {
  27. using namespace boost::adaptors;
  28. std::vector<int> keys;
  29. boost::push_back(keys, c | map_keys);
  30. std::vector<int> keys2;
  31. boost::push_back(keys2, adaptors::keys(c));
  32. std::vector<int> reference_keys;
  33. typedef BOOST_DEDUCED_TYPENAME Container::const_iterator iter_t;
  34. for (iter_t it = c.begin(); it != c.end(); ++it)
  35. {
  36. reference_keys.push_back(it->first);
  37. }
  38. BOOST_CHECK_EQUAL_COLLECTIONS( reference_keys.begin(),
  39. reference_keys.end(),
  40. keys.begin(),
  41. keys.end() );
  42. BOOST_CHECK_EQUAL_COLLECTIONS( reference_keys.begin(),
  43. reference_keys.end(),
  44. keys2.begin(),
  45. keys2.end() );
  46. }
  47. template< class Container >
  48. void map_test_values( Container& c )
  49. {
  50. using namespace boost::adaptors;
  51. std::vector<int> values;
  52. boost::push_back(values, c | map_values);
  53. std::vector<int> values2;
  54. boost::push_back(values2, adaptors::values(c));
  55. std::vector<int> reference_values;
  56. typedef BOOST_DEDUCED_TYPENAME Container::const_iterator iter_t;
  57. for (iter_t it = c.begin(); it != c.end(); ++it)
  58. {
  59. reference_values.push_back(it->second);
  60. }
  61. BOOST_CHECK_EQUAL_COLLECTIONS( reference_values.begin(),
  62. reference_values.end(),
  63. values.begin(),
  64. values.end() );
  65. BOOST_CHECK_EQUAL_COLLECTIONS( reference_values.begin(),
  66. reference_values.end(),
  67. values2.begin(),
  68. values2.end() );
  69. }
  70. template< class Container >
  71. void map_test_impl()
  72. {
  73. using namespace boost::assign;
  74. Container c;
  75. // Test empty
  76. map_test_keys(c);
  77. map_test_values(c);
  78. // Test one element
  79. c.insert(std::make_pair(1,2));
  80. map_test_keys(c);
  81. map_test_values(c);
  82. // Test many elements
  83. for (int x = 2; x < 10; ++x)
  84. {
  85. c.insert(std::make_pair(x, x * 2));
  86. }
  87. map_test_keys(c);
  88. map_test_values(c);
  89. }
  90. void map_test()
  91. {
  92. map_test_impl< std::map<int,int> >();
  93. }
  94. void test_trac_item_4388()
  95. {
  96. typedef std::pair<int,char> pair_t;
  97. const boost::array<pair_t,3> ar = {{
  98. pair_t(3, 'a'),
  99. pair_t(1, 'b'),
  100. pair_t(4, 'c')
  101. }};
  102. const boost::array<int, 3> expected_keys = {{ 3, 1, 4 }};
  103. const boost::array<char, 3> expected_values = {{ 'a', 'b', 'c' }};
  104. {
  105. std::vector<int> test;
  106. boost::push_back(test, ar | boost::adaptors::map_keys);
  107. BOOST_CHECK_EQUAL_COLLECTIONS(
  108. expected_keys.begin(), expected_keys.end(),
  109. test.begin(), test.end()
  110. );
  111. }
  112. {
  113. std::vector<char> test;
  114. boost::push_back(test, ar | boost::adaptors::map_values);
  115. BOOST_CHECK_EQUAL_COLLECTIONS(
  116. expected_values.begin(), expected_values.end(),
  117. test.begin(), test.end()
  118. );
  119. }
  120. {
  121. std::vector<char> test;
  122. boost::array<std::pair<int, char>, 3> src(ar);
  123. boost::push_back(test, src | boost::adaptors::map_values);
  124. BOOST_CHECK_EQUAL_COLLECTIONS(
  125. expected_values.begin(), expected_values.end(),
  126. test.begin(), test.end()
  127. );
  128. }
  129. }
  130. }
  131. }
  132. boost::unit_test::test_suite*
  133. init_unit_test_suite(int argc, char* argv[])
  134. {
  135. boost::unit_test::test_suite* test
  136. = BOOST_TEST_SUITE( "RangeTestSuite.adaptor.map" );
  137. test->add( BOOST_TEST_CASE( &boost::map_test ) );
  138. test->add( BOOST_TEST_CASE( &boost::test_trac_item_4388 ) );
  139. return test;
  140. }