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

http://hadesmem.googlecode.com/ · C++ · 151 lines · 119 code · 24 blank · 8 comment · 3 complexity · a349fa63ef9ff2af66960585c65b5640 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 "../objects/test.hpp"
  9. #include "../helpers/random_values.hpp"
  10. #include "../helpers/tracker.hpp"
  11. #include "../helpers/equivalent.hpp"
  12. #include "../helpers/invariants.hpp"
  13. test::seed_t seed(9063);
  14. namespace copy_tests
  15. {
  16. template <class T>
  17. void copy_construct_tests1(T*,
  18. test::random_generator const& generator = test::default_generator)
  19. {
  20. BOOST_DEDUCED_TYPENAME T::hasher hf;
  21. BOOST_DEDUCED_TYPENAME T::key_equal eq;
  22. BOOST_DEDUCED_TYPENAME T::allocator_type al;
  23. {
  24. T x;
  25. T y(x);
  26. BOOST_TEST(y.empty());
  27. BOOST_TEST(test::equivalent(y.hash_function(), hf));
  28. BOOST_TEST(test::equivalent(y.key_eq(), eq));
  29. BOOST_TEST(test::equivalent(y.get_allocator(), al));
  30. BOOST_TEST(x.max_load_factor() == y.max_load_factor());
  31. test::check_equivalent_keys(y);
  32. }
  33. {
  34. test::random_values<T> v(1000, generator);
  35. T x(v.begin(), v.end());
  36. T y(x);
  37. test::unordered_equivalence_tester<T> equivalent(x);
  38. BOOST_TEST(equivalent(y));
  39. test::check_equivalent_keys(y);
  40. }
  41. {
  42. // In this test I drop the original containers max load factor, so it
  43. // is much lower than the load factor. The hash table is not allowed
  44. // to rehash, but the destination container should probably allocate
  45. // enough buckets to decrease the load factor appropriately.
  46. test::random_values<T> v(1000, generator);
  47. T x(v.begin(), v.end());
  48. x.max_load_factor(x.load_factor() / 4);
  49. T y(x);
  50. test::unordered_equivalence_tester<T> equivalent(x);
  51. BOOST_TEST(equivalent(y));
  52. // This isn't guaranteed:
  53. BOOST_TEST(y.load_factor() < y.max_load_factor());
  54. test::check_equivalent_keys(y);
  55. }
  56. }
  57. template <class T>
  58. void copy_construct_tests2(T* ptr,
  59. test::random_generator const& generator = test::default_generator)
  60. {
  61. copy_construct_tests1(ptr);
  62. BOOST_DEDUCED_TYPENAME T::hasher hf(1);
  63. BOOST_DEDUCED_TYPENAME T::key_equal eq(1);
  64. BOOST_DEDUCED_TYPENAME T::allocator_type al(1);
  65. BOOST_DEDUCED_TYPENAME T::allocator_type al2(2);
  66. {
  67. T x(10000, hf, eq, al);
  68. T y(x);
  69. BOOST_TEST(y.empty());
  70. BOOST_TEST(test::equivalent(y.hash_function(), hf));
  71. BOOST_TEST(test::equivalent(y.key_eq(), eq));
  72. BOOST_TEST(test::equivalent(y.get_allocator(), al));
  73. BOOST_TEST(x.max_load_factor() == y.max_load_factor());
  74. test::check_equivalent_keys(y);
  75. }
  76. {
  77. T x(1000, hf, eq, al);
  78. T y(x, al2);
  79. BOOST_TEST(y.empty());
  80. BOOST_TEST(test::equivalent(y.hash_function(), hf));
  81. BOOST_TEST(test::equivalent(y.key_eq(), eq));
  82. BOOST_TEST(test::equivalent(y.get_allocator(), al2));
  83. BOOST_TEST(x.max_load_factor() == y.max_load_factor());
  84. test::check_equivalent_keys(y);
  85. }
  86. {
  87. test::random_values<T> v(1000, generator);
  88. T x(v.begin(), v.end(), 0, hf, eq, al);
  89. T y(x);
  90. test::unordered_equivalence_tester<T> equivalent(x);
  91. BOOST_TEST(equivalent(y));
  92. test::check_equivalent_keys(y);
  93. BOOST_TEST(test::equivalent(y.get_allocator(), al));
  94. }
  95. {
  96. test::random_values<T> v(500, generator);
  97. T x(v.begin(), v.end(), 0, hf, eq, al);
  98. T y(x, al2);
  99. test::unordered_equivalence_tester<T> equivalent(x);
  100. BOOST_TEST(equivalent(y));
  101. test::check_equivalent_keys(y);
  102. BOOST_TEST(test::equivalent(y.get_allocator(), al2));
  103. }
  104. }
  105. boost::unordered_set<test::object,
  106. test::hash, test::equal_to,
  107. test::allocator<test::object> >* test_set;
  108. boost::unordered_multiset<test::object,
  109. test::hash, test::equal_to,
  110. test::allocator<test::object> >* test_multiset;
  111. boost::unordered_map<test::object, test::object,
  112. test::hash, test::equal_to,
  113. test::allocator<test::object> >* test_map;
  114. boost::unordered_multimap<test::object, test::object,
  115. test::hash, test::equal_to,
  116. test::allocator<test::object> >* test_multimap;
  117. using test::default_generator;
  118. using test::generate_collisions;
  119. UNORDERED_TEST(copy_construct_tests1,
  120. ((test_set)(test_multiset)(test_map)(test_multimap))
  121. )
  122. UNORDERED_TEST(copy_construct_tests2,
  123. ((test_set)(test_multiset)(test_map)(test_multimap))
  124. ((default_generator)(generate_collisions))
  125. )
  126. }
  127. RUN_TESTS()