| 1 | |
| 2 | // Copyright 2006-2009 Daniel James. |
| 3 | // Distributed under the Boost Software License, Version 1.0. (See accompanying |
| 4 | // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) |
| 5 | |
| 6 | // This header contains metafunctions/functions to get the equivalent |
| 7 | // associative container for an unordered container, and compare the contents. |
| 8 | |
| 9 | #if !defined(BOOST_UNORDERED_TEST_HELPERS_TRACKER_HEADER) |
| 10 | #define |
| 11 | |
| 12 | #include "../objects/fwd.hpp" |
| 13 | #include "./equivalent.hpp" |
| 14 | #include "./helpers.hpp" |
| 15 | #include "./list.hpp" |
| 16 | #include "./metafunctions.hpp" |
| 17 | #include <algorithm> |
| 18 | #include <iterator> |
| 19 | #include <map> |
| 20 | #include <set> |
| 21 | |
| 22 | namespace test { |
| 23 | template <typename X> struct equals_to_compare |
| 24 | { |
| 25 | typedef std::less<typename X::first_argument_type> type; |
| 26 | }; |
| 27 | |
| 28 | template <> struct equals_to_compare<test::equal_to> |
| 29 | { |
| 30 | typedef test::less type; |
| 31 | }; |
| 32 | |
| 33 | template<class T> struct equals_to_compare< std::equal_to<T> > |
| 34 | { |
| 35 | typedef std::less<T> type; |
| 36 | }; |
| 37 | |
| 38 | template <class X1, class X2> void compare_range(X1 const& x1, X2 const& x2) |
| 39 | { |
| 40 | typedef test::list<typename X1::value_type> value_list; |
| 41 | value_list values1(x1.begin(), x1.end()); |
| 42 | value_list values2(x2.begin(), x2.end()); |
| 43 | values1.sort(); |
| 44 | values2.sort(); |
| 45 | BOOST_TEST_ALL_WITH(values1.begin(), values1.end(), values2.begin(), |
| 46 | values2.end(), test::equivalent); |
| 47 | } |
| 48 | |
| 49 | template <class X1, class X2, class T> |
| 50 | void compare_pairs(X1 const& x1, X2 const& x2, T*) |
| 51 | { |
| 52 | test::list<T> values1(x1.first, x1.second); |
| 53 | test::list<T> values2(x2.first, x2.second); |
| 54 | values1.sort(); |
| 55 | values2.sort(); |
| 56 | BOOST_TEST(values1.size() == values2.size() && |
| 57 | test::equal(values1.begin(), values1.end(), values2.begin(), |
| 58 | test::equivalent)); |
| 59 | } |
| 60 | |
| 61 | template <typename X, bool is_set = test::is_set<X>::value, |
| 62 | bool has_unique_keys = test::has_unique_keys<X>::value> |
| 63 | struct ordered_base; |
| 64 | |
| 65 | template <typename X> struct ordered_base<X, true, true> |
| 66 | { |
| 67 | typedef std::set<typename X::value_type, |
| 68 | typename equals_to_compare<typename X::key_equal>::type> |
| 69 | type; |
| 70 | }; |
| 71 | |
| 72 | template <typename X> struct ordered_base<X, true, false> |
| 73 | { |
| 74 | typedef std::multiset<typename X::value_type, |
| 75 | typename equals_to_compare<typename X::key_equal>::type> |
| 76 | type; |
| 77 | }; |
| 78 | |
| 79 | template <typename X> struct ordered_base<X, false, true> |
| 80 | { |
| 81 | typedef std::map<typename X::key_type, typename X::mapped_type, |
| 82 | typename equals_to_compare<typename X::key_equal>::type> |
| 83 | type; |
| 84 | }; |
| 85 | |
| 86 | template <typename X> struct ordered_base<X, false, false> |
| 87 | { |
| 88 | typedef std::multimap<typename X::key_type, typename X::mapped_type, |
| 89 | typename equals_to_compare<typename X::key_equal>::type> |
| 90 | type; |
| 91 | }; |
| 92 | |
| 93 | template <class X> class ordered : public ordered_base<X>::type |
| 94 | { |
| 95 | typedef typename ordered_base<X>::type base; |
| 96 | |
| 97 | public: |
| 98 | typedef typename base::key_compare key_compare; |
| 99 | |
| 100 | ordered() : base() {} |
| 101 | |
| 102 | explicit ordered(key_compare const& kc) : base(kc) {} |
| 103 | |
| 104 | void compare(X const& x) { compare_range(x, *this); } |
| 105 | |
| 106 | void compare_key(X const& x, typename X::value_type const& val) |
| 107 | { |
| 108 | compare_pairs(x.equal_range(get_key<X>(val)), |
| 109 | this->equal_range(get_key<X>(val)), (typename X::value_type*)0); |
| 110 | } |
| 111 | |
| 112 | template <class It> void insert_range(It b, It e) |
| 113 | { |
| 114 | while (b != e) { |
| 115 | this->insert(*b); |
| 116 | ++b; |
| 117 | } |
| 118 | } |
| 119 | }; |
| 120 | |
| 121 | template <class Equals> |
| 122 | typename equals_to_compare<Equals>::type create_compare(Equals const&) |
| 123 | { |
| 124 | typename equals_to_compare<Equals>::type x; |
| 125 | return x; |
| 126 | } |
| 127 | |
| 128 | template <class X> ordered<X> create_ordered(X const& container) |
| 129 | { |
| 130 | return ordered<X>(create_compare(container.key_eq())); |
| 131 | } |
| 132 | |
| 133 | template <class X1, class X2> |
| 134 | void check_container(X1 const& container, X2 const& values) |
| 135 | { |
| 136 | ordered<X1> tracker = create_ordered(container); |
| 137 | tracker.insert_range(values.begin(), values.end()); |
| 138 | tracker.compare(container); |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | #endif |
| 143 | |