invariants.hpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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. // This header contains metafunctions/functions to get the equivalent
  5. // associative container for an unordered container, and compare the contents.
  6. #if !defined(BOOST_UNORDERED_TEST_HELPERS_INVARIANT_HEADER)
  7. #define BOOST_UNORDERED_TEST_HELPERS_INVARIANT_HEADER
  8. #include "./helpers.hpp"
  9. #include "./metafunctions.hpp"
  10. #include <cmath>
  11. #include <set>
  12. #if defined(BOOST_MSVC)
  13. #pragma warning(push)
  14. #pragma warning(disable : 4127) // conditional expression is constant
  15. #pragma warning(disable : 4267) // conversion from 'size_t' to 'unsigned int',
  16. // possible loss of data
  17. #endif
  18. namespace test {
  19. template <class X> void check_equivalent_keys(X const& x1)
  20. {
  21. typename X::key_equal eq = x1.key_eq();
  22. typedef typename X::key_type key_type;
  23. std::set<key_type, std::less<key_type> > found_;
  24. typename X::const_iterator it = x1.begin(), end = x1.end();
  25. typename X::size_type size = 0;
  26. while (it != end) {
  27. // First test that the current key has not occurred before, required
  28. // to test either that keys are unique or that equivalent keys are
  29. // adjacent. (6.3.1/6)
  30. key_type key = get_key<X>(*it);
  31. if (!found_.insert(key).second)
  32. BOOST_ERROR("Elements with equivalent keys aren't adjacent.");
  33. // Iterate over equivalent keys, counting them.
  34. unsigned int count = 0;
  35. do {
  36. ++it;
  37. ++count;
  38. ++size;
  39. } while (it != end && eq(get_key<X>(*it), key));
  40. // If the container has unique keys, test that there's only one.
  41. // Since the previous test makes sure that all equivalent keys are
  42. // adjacent, this is all the equivalent keys - so the test is
  43. // sufficient. (6.3.1/6 again).
  44. if (test::has_unique_keys<X>::value && count != 1)
  45. BOOST_ERROR("Non-unique key.");
  46. if (x1.count(key) != count) {
  47. BOOST_ERROR("Incorrect output of count.");
  48. std::cerr << x1.count(key) << "," << count << "\n";
  49. }
  50. // Check that the keys are in the correct bucket and are
  51. // adjacent in the bucket.
  52. typename X::size_type bucket = x1.bucket(key);
  53. typename X::const_local_iterator lit = x1.begin(bucket),
  54. lend = x1.end(bucket);
  55. unsigned int count_checked = 0;
  56. for (; lit != lend && !eq(get_key<X>(*lit), key); ++lit) {
  57. ++count_checked;
  58. }
  59. if (lit == lend) {
  60. BOOST_ERROR("Unable to find element with a local_iterator");
  61. std::cerr << "Checked: " << count_checked << " elements" << std::endl;
  62. } else {
  63. unsigned int count2 = 0;
  64. for (; lit != lend && eq(get_key<X>(*lit), key); ++lit)
  65. ++count2;
  66. if (count != count2)
  67. BOOST_ERROR("Element count doesn't match local_iterator.");
  68. for (; lit != lend; ++lit) {
  69. if (eq(get_key<X>(*lit), key)) {
  70. BOOST_ERROR("Non-adjacent element with equivalent key "
  71. "in bucket.");
  72. break;
  73. }
  74. }
  75. }
  76. };
  77. // Check that size matches up.
  78. if (x1.size() != size) {
  79. BOOST_ERROR("x1.size() doesn't match actual size.");
  80. std::cout << x1.size() << "/" << size << std::endl;
  81. }
  82. // Check the load factor.
  83. float load_factor = size == 0 ? 0
  84. : static_cast<float>(size) /
  85. static_cast<float>(x1.bucket_count());
  86. using namespace std;
  87. if (fabs(x1.load_factor() - load_factor) > x1.load_factor() / 64)
  88. BOOST_ERROR("x1.load_factor() doesn't match actual load_factor.");
  89. // Check that size in the buckets matches up.
  90. typename X::size_type bucket_size = 0;
  91. for (typename X::size_type i = 0; i < x1.bucket_count(); ++i) {
  92. for (typename X::const_local_iterator begin2 = x1.begin(i),
  93. end2 = x1.end(i);
  94. begin2 != end2; ++begin2) {
  95. ++bucket_size;
  96. }
  97. }
  98. if (x1.size() != bucket_size) {
  99. BOOST_ERROR("x1.size() doesn't match bucket size.");
  100. std::cout << x1.size() << "/" << bucket_size << std::endl;
  101. }
  102. }
  103. }
  104. #if defined(BOOST_MSVC)
  105. #pragma warning(pop)
  106. #endif
  107. #endif