]>
Commit | Line | Data |
---|---|---|
8d9254fc | 1 | // Copyright (C) 2016-2020 Free Software Foundation, Inc. |
2dbe56bd JW |
2 | // |
3 | // This file is part of the GNU ISO C++ Library. This library is free | |
4 | // software; you can redistribute it and/or modify it under the | |
5 | // terms of the GNU General Public License as published by the | |
6 | // Free Software Foundation; either version 3, or (at your option) | |
7 | // any later version. | |
8 | ||
9 | // This library is distributed in the hope that it will be useful, | |
10 | // but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 | // GNU General Public License for more details. | |
13 | ||
14 | // You should have received a copy of the GNU General Public License along | |
15 | // with this library; see the file COPYING3. If not see | |
16 | // <http://www.gnu.org/licenses/>. | |
17 | ||
18 | // { dg-options "-std=gnu++17" } | |
6458742a | 19 | // { dg-do run { target c++17 } } |
2dbe56bd JW |
20 | |
21 | #include <unordered_map> | |
22 | #include <algorithm> | |
23 | #include <testsuite_hooks.h> | |
24 | ||
25 | using test_type = std::unordered_map<int, int>; | |
26 | ||
27 | struct hash { | |
28 | auto operator()(int i) const noexcept { return ~std::hash<int>()(i); } | |
29 | }; | |
30 | struct equal : std::equal_to<> { }; | |
31 | ||
32 | template<typename C1, typename C2> | |
33 | bool equal_elements(const C1& c1, const C2& c2) | |
34 | { | |
35 | if (c2.size() != c1.size()) | |
36 | return false; | |
37 | for (auto& i : c1) | |
38 | if (c2.count(i.first) != c1.count(i.first)) | |
39 | return false; | |
40 | return true; | |
41 | } | |
42 | ||
43 | void | |
44 | test01() | |
45 | { | |
2dbe56bd JW |
46 | const test_type c0{ {1, 10}, {2, 20}, {3, 30} }; |
47 | test_type c1 = c0, c2 = c0; | |
48 | ||
49 | c1.merge(c2); | |
50 | VERIFY( c1 == c0 ); | |
51 | VERIFY( c2 == c0 ); | |
52 | ||
53 | c1.clear(); | |
54 | c1.merge(c2); | |
55 | VERIFY( c1 == c0 ); | |
56 | VERIFY( c2.empty() ); | |
57 | ||
58 | c2.merge(std::move(c1)); | |
59 | VERIFY( c1.empty() ); | |
60 | VERIFY( c2 == c0 ); | |
61 | } | |
62 | ||
63 | void | |
64 | test02() | |
65 | { | |
2dbe56bd JW |
66 | const test_type c0{ {1, 10}, {2, 20}, {3, 30} }; |
67 | test_type c1 = c0; | |
68 | std::unordered_map<int, int, hash, equal> c2( c0.begin(), c0.end() ); | |
69 | ||
70 | c1.merge(c2); | |
71 | VERIFY( c1 == c0 ); | |
72 | VERIFY( equal_elements(c2, c0) ); | |
73 | ||
74 | c1.clear(); | |
75 | c1.merge(c2); | |
76 | VERIFY( c1 == c0 ); | |
77 | VERIFY( c2.empty() ); | |
78 | ||
79 | c2.merge(c1); | |
80 | VERIFY( c1.empty() ); | |
81 | VERIFY( equal_elements(c2, c0) ); | |
82 | ||
83 | c1.merge(std::move(c2)); | |
84 | VERIFY( c2.empty() ); | |
85 | VERIFY( c1 == c0 ); | |
86 | } | |
87 | ||
88 | void | |
89 | test03() | |
90 | { | |
2dbe56bd JW |
91 | const test_type c0{ {1, 10}, {2, 20}, {3, 30} }; |
92 | test_type c1 = c0; | |
93 | std::unordered_multimap<int, int, hash, equal> c2( c0.begin(), c0.end() ); | |
94 | c1.merge(c2); | |
95 | VERIFY( c1 == c0 ); | |
96 | VERIFY( equal_elements(c2, c0) ); | |
97 | ||
98 | c1.clear(); | |
99 | c1.merge(c2); | |
100 | VERIFY( c1 == c0 ); | |
101 | VERIFY( c2.empty() ); | |
102 | ||
103 | c2.merge(c1); | |
104 | VERIFY( c1.empty() ); | |
105 | VERIFY( equal_elements(c2, c0) ); | |
106 | ||
107 | c1 = c0; | |
108 | c2.merge(c1); | |
109 | VERIFY( c1.empty() ); | |
110 | VERIFY( c2.size() == (2 * c0.size()) ); | |
111 | VERIFY( c2.count(1) == 2 ); | |
112 | VERIFY( c2.count(2) == 2 ); | |
113 | VERIFY( c2.count(3) == 2 ); | |
114 | ||
115 | c1.merge(c2); | |
116 | VERIFY( c1 == c0 ); | |
117 | VERIFY( equal_elements(c2, c0) ); | |
118 | ||
119 | c1.merge(std::move(c2)); | |
120 | VERIFY( c1 == c0 ); | |
121 | VERIFY( equal_elements(c2, c0) ); | |
122 | ||
123 | c1.clear(); | |
124 | c1.merge(std::move(c2)); | |
125 | VERIFY( c1 == c0 ); | |
126 | VERIFY( c2.empty() ); | |
127 | } | |
128 | ||
129 | int | |
130 | main() | |
131 | { | |
132 | test01(); | |
133 | test02(); | |
134 | test03(); | |
135 | } |