1 //===----------------------------------------------------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is dual licensed under the MIT and the University of Illinois Open
6 // Source Licenses. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // UNSUPPORTED: c++98, c++03, c++11, c++14
14 // class unordered_map
16 // template <class H2, class P2>
17 // void merge(unordered_map<key_type, value_type, H2, P2, allocator_type>& source);
18 // template <class H2, class P2>
19 // void merge(unordered_map<key_type, value_type, H2, P2, allocator_type>&& source);
20 // template <class H2, class P2>
21 // void merge(unordered_multimap<key_type, value_type, H2, P2, allocator_type>& source);
22 // template <class H2, class P2>
23 // void merge(unordered_multimap<key_type, value_type, H2, P2, allocator_type>&& source);
25 #include <unordered_map>
27 #include "test_macros.h"
31 bool map_equal(const Map& map, Map other)
36 #ifndef TEST_HAS_NO_EXCEPTIONS
42 throw_hasher(bool& should_throw) : should_throw_(should_throw) {}
44 size_t operator()(const T& p) const
48 return std::hash<T>()(p);
56 std::unordered_map<int, int> src{{1, 0}, {3, 0}, {5, 0}};
57 std::unordered_map<int, int> dst{{2, 0}, {4, 0}, {5, 0}};
59 assert(map_equal(src, {{5,0}}));
60 assert(map_equal(dst, {{1, 0}, {2, 0}, {3, 0}, {4, 0}, {5, 0}}));
63 #ifndef TEST_HAS_NO_EXCEPTIONS
65 bool do_throw = false;
66 typedef std::unordered_map<Counter<int>, int, throw_hasher<Counter<int>>> map_type;
67 map_type src({{1, 0}, {3, 0}, {5, 0}}, 0, throw_hasher<Counter<int>>(do_throw));
68 map_type dst({{2, 0}, {4, 0}, {5, 0}}, 0, throw_hasher<Counter<int>>(do_throw));
70 assert(Counter_base::gConstructed == 6);
82 assert(map_equal(src, map_type({{1, 0}, {3, 0}, {5, 0}}, 0, throw_hasher<Counter<int>>(do_throw))));
83 assert(map_equal(dst, map_type({{2, 0}, {4, 0}, {5, 0}}, 0, throw_hasher<Counter<int>>(do_throw))));
86 assert(Counter_base::gConstructed == 0);
91 bool operator()(const Counter<int>& lhs, const Counter<int>& rhs) const
99 size_t operator()(const Counter<int>& p) const
101 return std::hash<Counter<int>>()(p);
105 typedef std::unordered_map<Counter<int>, int, std::hash<Counter<int>>, std::equal_to<Counter<int>>> first_map_type;
106 typedef std::unordered_map<Counter<int>, int, hasher, equal> second_map_type;
107 typedef std::unordered_multimap<Counter<int>, int, hasher, equal> third_map_type;
110 first_map_type first{{1, 0}, {2, 0}, {3, 0}};
111 second_map_type second{{2, 0}, {3, 0}, {4, 0}};
112 third_map_type third{{1, 0}, {3, 0}};
114 assert(Counter_base::gConstructed == 8);
119 assert(map_equal(first, {{1, 0}, {2, 0}, {3, 0}, {4, 0}}));
120 assert(map_equal(second, {{2, 0}, {3, 0}}));
121 assert(map_equal(third, {{1, 0}, {3, 0}}));
123 assert(Counter_base::gConstructed == 8);
125 assert(Counter_base::gConstructed == 0);
127 first_map_type first{{1, 0}, {2, 0}, {3, 0}};
128 second_map_type second{{2, 0}, {3, 0}, {4, 0}};
129 third_map_type third{{1, 0}, {3, 0}};
131 assert(Counter_base::gConstructed == 8);
133 first.merge(std::move(second));
134 first.merge(std::move(third));
136 assert(map_equal(first, {{1, 0}, {2, 0}, {3, 0}, {4, 0}}));
137 assert(map_equal(second, {{2, 0}, {3, 0}}));
138 assert(map_equal(third, {{1, 0}, {3, 0}}));
140 assert(Counter_base::gConstructed == 8);
142 assert(Counter_base::gConstructed == 0);
145 std::unordered_map<int, int> first;
147 std::unordered_map<int, int> second;
149 first.merge(std::move(second));
152 std::unordered_multimap<int, int> second;
154 first.merge(std::move(second));