equivalent.hpp 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. // Copyright 2005-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. #if !defined(BOOST_UNORDERED_TESTS_EQUIVALENT_HEADER)
  5. #define BOOST_UNORDERED_TESTS_EQUIVALENT_HEADER
  6. #include "./fwd.hpp"
  7. #include "./list.hpp"
  8. #include "./metafunctions.hpp"
  9. #include <algorithm>
  10. #include <boost/core/lightweight_test.hpp>
  11. #include <boost/unordered_map.hpp>
  12. #include <boost/unordered_set.hpp>
  13. namespace test {
  14. template <class T1, class T2>
  15. bool equivalent_impl(T1 const& x, T2 const& y, base_type)
  16. {
  17. return x == y;
  18. }
  19. template <class T>
  20. bool equivalent_impl(
  21. boost::hash<T> const&, boost::hash<T> const&, derived_type)
  22. {
  23. return true;
  24. }
  25. template <class T>
  26. bool equivalent_impl(
  27. std::equal_to<T> const&, std::equal_to<T> const&, derived_type)
  28. {
  29. return true;
  30. }
  31. template <class T1, class T2, class T3, class T4>
  32. bool equivalent_impl(
  33. std::pair<T1, T2> const& x1, std::pair<T3, T4> const& x2, derived_type)
  34. {
  35. return equivalent_impl(x1.first, x2.first, derived) &&
  36. equivalent_impl(x1.second, x2.second, derived);
  37. }
  38. struct equivalent_type
  39. {
  40. equivalent_type() {}
  41. template <class T1, class T2>
  42. bool operator()(T1 const& x, T2 const& y) const
  43. {
  44. return equivalent_impl(x, y, derived);
  45. }
  46. };
  47. const equivalent_type equivalent;
  48. template <class Container> class unordered_equivalence_tester
  49. {
  50. typename Container::size_type size_;
  51. typename Container::hasher hasher_;
  52. typename Container::key_equal key_equal_;
  53. float max_load_factor_;
  54. typedef test::list<typename Container::value_type> value_list;
  55. value_list values_;
  56. public:
  57. unordered_equivalence_tester(Container const& x)
  58. : size_(x.size()), hasher_(x.hash_function()), key_equal_(x.key_eq()),
  59. max_load_factor_(x.max_load_factor()), values_(x.begin(), x.end())
  60. {
  61. values_.sort();
  62. }
  63. bool operator()(Container const& x) const
  64. {
  65. if (!((size_ == x.size()) &&
  66. (test::equivalent(hasher_, x.hash_function())) &&
  67. (test::equivalent(key_equal_, x.key_eq())) &&
  68. (max_load_factor_ == x.max_load_factor()) &&
  69. (values_.size() == x.size())))
  70. return false;
  71. value_list copy(x.begin(), x.end());
  72. copy.sort();
  73. return values_ == copy;
  74. }
  75. private:
  76. unordered_equivalence_tester();
  77. };
  78. }
  79. #endif