/Src/Dependencies/Boost/libs/unordered/test/unordered/erase_tests.cpp

http://hadesmem.googlecode.com/ · C++ · 205 lines · 174 code · 24 blank · 7 comment · 45 complexity · b5a9728fdc8cc358ac4501ec3dcef58c MD5 · raw file

  1. // Copyright 2006-2009 Daniel James.
  2. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  3. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  4. #include "../helpers/prefix.hpp"
  5. #include <boost/unordered_set.hpp>
  6. #include <boost/unordered_map.hpp>
  7. #include "../helpers/test.hpp"
  8. #include <boost/next_prior.hpp>
  9. #include "../objects/test.hpp"
  10. #include "../helpers/random_values.hpp"
  11. #include "../helpers/tracker.hpp"
  12. #include "../helpers/equivalent.hpp"
  13. #include "../helpers/helpers.hpp"
  14. #include <iostream>
  15. namespace erase_tests
  16. {
  17. test::seed_t seed(85638);
  18. template <class Container>
  19. void erase_tests1(Container*,
  20. test::random_generator generator = test::default_generator)
  21. {
  22. std::cerr<<"Erase by key.\n";
  23. {
  24. test::random_values<Container> v(1000, generator);
  25. Container x(v.begin(), v.end());
  26. for(BOOST_DEDUCED_TYPENAME test::random_values<Container>::iterator
  27. it = v.begin(); it != v.end(); ++it)
  28. {
  29. std::size_t count = x.count(test::get_key<Container>(*it));
  30. std::size_t old_size = x.size();
  31. BOOST_TEST(count == x.erase(test::get_key<Container>(*it)));
  32. BOOST_TEST(x.size() == old_size - count);
  33. BOOST_TEST(x.count(test::get_key<Container>(*it)) == 0);
  34. BOOST_TEST(x.find(test::get_key<Container>(*it)) == x.end());
  35. }
  36. }
  37. std::cerr<<"erase(begin()).\n";
  38. {
  39. test::random_values<Container> v(1000, generator);
  40. Container x(v.begin(), v.end());
  41. std::size_t size = x.size();
  42. while(size > 0 && !x.empty())
  43. {
  44. BOOST_DEDUCED_TYPENAME Container::key_type
  45. key = test::get_key<Container>(*x.begin());
  46. std::size_t count = x.count(key);
  47. BOOST_DEDUCED_TYPENAME Container::iterator
  48. pos = x.erase(x.begin());
  49. --size;
  50. BOOST_TEST(pos == x.begin());
  51. BOOST_TEST(x.count(key) == count - 1);
  52. BOOST_TEST(x.size() == size);
  53. }
  54. BOOST_TEST(x.empty());
  55. }
  56. std::cerr<<"erase(random position).\n";
  57. {
  58. test::random_values<Container> v(1000, generator);
  59. Container x(v.begin(), v.end());
  60. std::size_t size = x.size();
  61. while(size > 0 && !x.empty())
  62. {
  63. using namespace std;
  64. int index = rand() % (int) x.size();
  65. BOOST_DEDUCED_TYPENAME Container::const_iterator prev, pos, next;
  66. if(index == 0) {
  67. prev = pos = x.begin();
  68. }
  69. else {
  70. prev = boost::next(x.begin(), index - 1);
  71. pos = boost::next(prev);
  72. }
  73. next = boost::next(pos);
  74. BOOST_DEDUCED_TYPENAME Container::key_type
  75. key = test::get_key<Container>(*pos);
  76. std::size_t count = x.count(key);
  77. BOOST_TEST(next == x.erase(pos));
  78. --size;
  79. if(size > 0)
  80. BOOST_TEST(index == 0 ? next == x.begin() :
  81. next == boost::next(prev));
  82. BOOST_TEST(x.count(key) == count - 1);
  83. BOOST_TEST(x.size() == size);
  84. }
  85. BOOST_TEST(x.empty());
  86. }
  87. std::cerr<<"erase(ranges).\n";
  88. {
  89. test::random_values<Container> v(500, generator);
  90. Container x(v.begin(), v.end());
  91. std::size_t size = x.size();
  92. // I'm actually stretching it a little here, as the standard says it
  93. // returns 'the iterator immediately following the erase elements'
  94. // and if nothing is erased, then there's nothing to follow. But I
  95. // think this is the only sensible option...
  96. BOOST_TEST(x.erase(x.end(), x.end()) == x.end());
  97. BOOST_TEST(x.erase(x.begin(), x.begin()) == x.begin());
  98. BOOST_TEST(x.size() == size);
  99. BOOST_TEST(x.erase(x.begin(), x.end()) == x.end());
  100. BOOST_TEST(x.empty());
  101. BOOST_TEST(x.begin() == x.end());
  102. BOOST_TEST(x.erase(x.begin(), x.end()) == x.begin());
  103. }
  104. std::cerr<<"quick_erase(begin()).\n";
  105. {
  106. test::random_values<Container> v(1000, generator);
  107. Container x(v.begin(), v.end());
  108. std::size_t size = x.size();
  109. while(size > 0 && !x.empty())
  110. {
  111. BOOST_DEDUCED_TYPENAME Container::key_type
  112. key = test::get_key<Container>(*x.begin());
  113. std::size_t count = x.count(key);
  114. x.quick_erase(x.begin());
  115. --size;
  116. BOOST_TEST(x.count(key) == count - 1);
  117. BOOST_TEST(x.size() == size);
  118. }
  119. BOOST_TEST(x.empty());
  120. }
  121. std::cerr<<"quick_erase(random position).\n";
  122. {
  123. test::random_values<Container> v(1000, generator);
  124. Container x(v.begin(), v.end());
  125. std::size_t size = x.size();
  126. while(size > 0 && !x.empty())
  127. {
  128. using namespace std;
  129. int index = rand() % (int) x.size();
  130. BOOST_DEDUCED_TYPENAME Container::const_iterator prev, pos, next;
  131. if(index == 0) {
  132. prev = pos = x.begin();
  133. }
  134. else {
  135. prev = boost::next(x.begin(), index - 1);
  136. pos = boost::next(prev);
  137. }
  138. next = boost::next(pos);
  139. BOOST_DEDUCED_TYPENAME Container::key_type
  140. key = test::get_key<Container>(*pos);
  141. std::size_t count = x.count(key);
  142. x.quick_erase(pos);
  143. --size;
  144. if(size > 0)
  145. BOOST_TEST(index == 0 ? next == x.begin() :
  146. next == boost::next(prev));
  147. BOOST_TEST(x.count(key) == count - 1);
  148. BOOST_TEST(x.size() == size);
  149. }
  150. BOOST_TEST(x.empty());
  151. }
  152. std::cerr<<"clear().\n";
  153. {
  154. test::random_values<Container> v(500, generator);
  155. Container x(v.begin(), v.end());
  156. x.clear();
  157. BOOST_TEST(x.empty());
  158. BOOST_TEST(x.begin() == x.end());
  159. }
  160. std::cerr<<"\n";
  161. }
  162. boost::unordered_set<test::object,
  163. test::hash, test::equal_to,
  164. test::allocator<test::object> >* test_set;
  165. boost::unordered_multiset<test::object,
  166. test::hash, test::equal_to,
  167. test::allocator<test::object> >* test_multiset;
  168. boost::unordered_map<test::object, test::object,
  169. test::hash, test::equal_to,
  170. test::allocator<test::object> >* test_map;
  171. boost::unordered_multimap<test::object, test::object,
  172. test::hash, test::equal_to,
  173. test::allocator<test::object> >* test_multimap;
  174. using test::default_generator;
  175. using test::generate_collisions;
  176. UNORDERED_TEST(erase_tests1,
  177. ((test_set)(test_multiset)(test_map)(test_multimap))
  178. ((default_generator)(generate_collisions))
  179. )
  180. }
  181. RUN_TESTS()