1//===----------------------------------------------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9// UNSUPPORTED: c++03, c++11, c++14
10
11// <unordered_map>
12
13// class unordered_multimap
14
15// template <class H2, class P2>
16// void merge(unordered_map<key_type, value_type, H2, P2, allocator_type>& source);
17// template <class H2, class P2>
18// void merge(unordered_map<key_type, value_type, H2, P2, allocator_type>&& source);
19// template <class H2, class P2>
20// void merge(unordered_multimap<key_type, value_type, H2, P2, allocator_type>& source);
21// template <class H2, class P2>
22// void merge(unordered_multimap<key_type, value_type, H2, P2, allocator_type>&& source);
23
24#include <unordered_map>
25#include <cassert>
26#include "test_macros.h"
27#include "Counter.h"
28
29template <class Map>
30bool map_equal(const Map& map, Map other) {
31 return map == other;
32}
33
34#ifndef TEST_HAS_NO_EXCEPTIONS
35template <class T>
36struct throw_hasher {
37 bool& should_throw_;
38
39 throw_hasher(bool& should_throw) : should_throw_(should_throw) {}
40
41 std::size_t operator()(const T& p) const {
42 if (should_throw_)
43 throw 0;
44 return std::hash<T>()(p);
45 }
46};
47#endif
48
49int main(int, char**) {
50 {
51 std::unordered_multimap<int, int> src{{1, 0}, {3, 0}, {5, 0}};
52 std::unordered_multimap<int, int> dst{{2, 0}, {4, 0}, {5, 0}};
53 dst.merge(source&: src);
54 assert(map_equal(src, {}));
55 assert(map_equal(dst, {{1, 0}, {2, 0}, {3, 0}, {4, 0}, {5, 0}, {5, 0}}));
56 }
57
58#ifndef TEST_HAS_NO_EXCEPTIONS
59 {
60 bool do_throw = false;
61 typedef std::unordered_multimap<Counter<int>, int, throw_hasher<Counter<int>>> map_type;
62 map_type src({{1, 0}, {3, 0}, {5, 0}}, 0, throw_hasher<Counter<int>>(do_throw));
63 map_type dst({{2, 0}, {4, 0}, {5, 0}}, 0, throw_hasher<Counter<int>>(do_throw));
64
65 assert(Counter_base::gConstructed == 6);
66
67 do_throw = true;
68 try {
69 dst.merge(src);
70 } catch (int) {
71 do_throw = false;
72 }
73 assert(!do_throw);
74 assert(map_equal(src, map_type({{1, 0}, {3, 0}, {5, 0}}, 0, throw_hasher<Counter<int>>(do_throw))));
75 assert(map_equal(dst, map_type({{2, 0}, {4, 0}, {5, 0}}, 0, throw_hasher<Counter<int>>(do_throw))));
76 }
77#endif
78 assert(Counter_base::gConstructed == 0);
79 struct equal {
80 equal() = default;
81
82 bool operator()(const Counter<int>& lhs, const Counter<int>& rhs) const { return lhs == rhs; }
83 };
84 struct hasher {
85 hasher() = default;
86 std::size_t operator()(const Counter<int>& p) const { return std::hash<Counter<int>>()(p); }
87 };
88 {
89 typedef std::unordered_multimap<Counter<int>, int, std::hash<Counter<int>>, std::equal_to<Counter<int>>>
90 first_map_type;
91 typedef std::unordered_multimap<Counter<int>, int, hasher, equal> second_map_type;
92 typedef std::unordered_map<Counter<int>, int, hasher, equal> third_map_type;
93
94 {
95 first_map_type first{{1, 0}, {2, 0}, {3, 0}};
96 second_map_type second{{2, 0}, {3, 0}, {4, 0}};
97 third_map_type third{{1, 0}, {3, 0}};
98
99 assert(Counter_base::gConstructed == 8);
100
101 first.merge(second);
102 first.merge(third);
103
104 assert(map_equal(first, {{1, 0}, {2, 0}, {3, 0}, {4, 0}, {2, 0}, {3, 0}, {1, 0}, {3, 0}}));
105 assert(map_equal(second, {}));
106 assert(map_equal(third, {}));
107
108 assert(Counter_base::gConstructed == 8);
109 }
110 assert(Counter_base::gConstructed == 0);
111 {
112 first_map_type first{{1, 0}, {2, 0}, {3, 0}};
113 second_map_type second{{2, 0}, {3, 0}, {4, 0}};
114 third_map_type third{{1, 0}, {3, 0}};
115
116 assert(Counter_base::gConstructed == 8);
117
118 first.merge(std::move(second));
119 first.merge(std::move(third));
120
121 assert(map_equal(first, {{1, 0}, {2, 0}, {3, 0}, {4, 0}, {2, 0}, {3, 0}, {1, 0}, {3, 0}}));
122 assert(map_equal(second, {}));
123 assert(map_equal(third, {}));
124
125 assert(Counter_base::gConstructed == 8);
126 }
127 assert(Counter_base::gConstructed == 0);
128 }
129 {
130 std::unordered_multimap<int, int> first;
131 {
132 std::unordered_multimap<int, int> second;
133 first.merge(source&: second);
134 first.merge(source: std::move(second));
135 }
136 {
137 std::unordered_map<int, int> second;
138 first.merge(source&: second);
139 first.merge(source: std::move(second));
140 }
141 }
142 return 0;
143}
144

source code of libcxx/test/std/containers/unord/unord.multimap/unord.multimap.modifiers/merge.pass.cpp