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_set>
12
13// class unordered_multiset
14
15// template <class H2, class P2>
16// void merge(unordered_set<key_type, H2, P2, allocator_type>& source);
17// template <class H2, class P2>
18// void merge(unordered_set<key_type, H2, P2, allocator_type>&& source);
19// template <class H2, class P2>
20// void merge(unordered_multiset<key_type, H2, P2, allocator_type>& source);
21// template <class H2, class P2>
22// void merge(unordered_multiset<key_type, H2, P2, allocator_type>&& source);
23
24#include <unordered_set>
25#include <cassert>
26#include "test_macros.h"
27#include "Counter.h"
28
29template <class Set>
30bool set_equal(const Set& set, Set other) {
31 return set == 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_multiset<int> src{1, 3, 5};
52 std::unordered_multiset<int> dst{2, 4, 5};
53 dst.merge(source&: src);
54 assert(set_equal(src, {}));
55 assert(set_equal(dst, {1, 2, 3, 4, 5, 5}));
56 }
57
58#ifndef TEST_HAS_NO_EXCEPTIONS
59 {
60 bool do_throw = false;
61 typedef std::unordered_multiset<Counter<int>, throw_hasher<Counter<int>>> set_type;
62 set_type src({1, 3, 5}, 0, throw_hasher<Counter<int>>(do_throw));
63 set_type dst({2, 4, 5}, 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(set_equal(src, set_type({1, 3, 5}, 0, throw_hasher<Counter<int>>(do_throw))));
75 assert(set_equal(dst, set_type({2, 4, 5}, 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_multiset<Counter<int>, std::hash<Counter<int>>, std::equal_to<Counter<int>>> first_set_type;
90 typedef std::unordered_multiset<Counter<int>, hasher, equal> second_set_type;
91 typedef std::unordered_set<Counter<int>, hasher, equal> third_set_type;
92
93 {
94 first_set_type first{1, 2, 3};
95 second_set_type second{2, 3, 4};
96 third_set_type third{1, 3};
97
98 assert(Counter_base::gConstructed == 8);
99
100 first.merge(second);
101 first.merge(third);
102
103 assert(set_equal(first, {1, 2, 3, 4, 2, 3, 1, 3}));
104 assert(set_equal(second, {}));
105 assert(set_equal(third, {}));
106
107 assert(Counter_base::gConstructed == 8);
108 }
109 assert(Counter_base::gConstructed == 0);
110 {
111 first_set_type first{1, 2, 3};
112 second_set_type second{2, 3, 4};
113 third_set_type third{1, 3};
114
115 assert(Counter_base::gConstructed == 8);
116
117 first.merge(std::move(second));
118 first.merge(std::move(third));
119
120 assert(set_equal(first, {1, 2, 3, 4, 2, 3, 1, 3}));
121 assert(set_equal(second, {}));
122 assert(set_equal(third, {}));
123
124 assert(Counter_base::gConstructed == 8);
125 }
126 assert(Counter_base::gConstructed == 0);
127 }
128 {
129 std::unordered_multiset<int> first;
130 {
131 std::unordered_multiset<int> second;
132 first.merge(source&: second);
133 first.merge(source: std::move(second));
134 }
135 {
136 std::unordered_set<int> second;
137 first.merge(source&: second);
138 first.merge(source: std::move(second));
139 }
140 }
141 return 0;
142}
143

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