]>
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_multimap<int, int>; | |
26 | struct hash { | |
27 | auto operator()(int i) const noexcept { return ~std::hash<int>()(i); } | |
28 | }; | |
29 | struct equal : std::equal_to<> { }; | |
30 | ||
31 | void | |
32 | test01() | |
33 | { | |
2dbe56bd JW |
34 | const test_type c0{ {1, 10}, {1, 11}, {2, 20}, {2, 21}, {3, 30}, {3, 31} }; |
35 | test_type c1 = c0, c2 = c0; | |
36 | ||
37 | c1.merge(c2); | |
38 | for (auto& i : c1) | |
39 | VERIFY( c1.count(i.first) == (2 * c0.count(i.first)) ); | |
40 | VERIFY( c2.empty() ); | |
41 | ||
42 | c1.clear(); | |
43 | c2 = c0; | |
44 | c1.merge(std::move(c2)); | |
45 | VERIFY( c1 == c0 ); | |
46 | VERIFY( c2.empty() ); | |
47 | } | |
48 | ||
49 | void | |
50 | test02() | |
51 | { | |
2dbe56bd JW |
52 | const test_type c0{ {1, 10}, {1, 11}, {2, 20}, {2, 21}, {3, 30}, {3, 31} }; |
53 | test_type c1 = c0; | |
54 | std::unordered_multimap<int, int, hash, equal> c2( c0.begin(), c0.end() ); | |
55 | ||
56 | c1.merge(c2); | |
57 | VERIFY( c1.size() == (2 * c0.size()) ); | |
58 | for (auto& i : c1) | |
59 | VERIFY( c1.count(i.first) == (2 * c0.count(i.first)) ); | |
60 | VERIFY( c2.empty() ); | |
61 | ||
62 | c1.clear(); | |
63 | c2.insert( c0.begin(), c0.end() ); | |
64 | c1.merge(std::move(c2)); | |
65 | VERIFY( c1 == c0 ); | |
66 | VERIFY( c2.empty() ); | |
67 | } | |
68 | ||
69 | void | |
70 | test03() | |
71 | { | |
2dbe56bd JW |
72 | const test_type c0{ {1, 10}, {1, 11}, {2, 20}, {2, 21}, {3, 30}, {3, 31} }; |
73 | test_type c1 = c0; | |
74 | std::unordered_multimap<int, int, hash, equal> c2( c0.begin(), c0.end() ); | |
75 | ||
76 | c1.merge(c2); | |
77 | VERIFY( c1.size() == (2 * c0.size()) ); | |
78 | for (auto& i : c1) | |
79 | VERIFY( c1.count(i.first) == (2 * c0.count(i.first)) ); | |
80 | VERIFY( c2.empty() ); | |
81 | ||
82 | c1.clear(); | |
83 | c2.insert( c0.begin(), c0.end() ); | |
84 | c1.merge(std::move(c2)); | |
85 | VERIFY( c1 == c0 ); | |
86 | VERIFY( c2.empty() ); | |
87 | } | |
88 | ||
89 | void | |
90 | test04() | |
91 | { | |
2dbe56bd JW |
92 | const test_type c0{ {1, 10}, {1, 11}, {2, 20}, {2, 21}, {3, 30}, {3, 31} }; |
93 | test_type c1 = c0; | |
94 | std::unordered_map<int, int, hash, equal> c2( c0.begin(), c0.end() ); | |
95 | ||
96 | c1.merge(c2); | |
97 | VERIFY( c1.size() == (1.5 * c0.size()) ); | |
98 | for (auto& i : c1) | |
99 | VERIFY( c1.count(i.first) == (1.5 * c0.count(i.first)) ); | |
100 | VERIFY( c2.empty() ); | |
101 | ||
102 | c1.clear(); | |
103 | c2.insert( c0.begin(), c0.end() ); | |
104 | c1.merge(std::move(c2)); | |
105 | VERIFY( c1.size() == (0.5 * c0.size()) ); | |
106 | VERIFY( c2.empty() ); | |
107 | } | |
108 | ||
109 | int | |
110 | main() | |
111 | { | |
112 | test01(); | |
113 | test02(); | |
114 | test03(); | |
115 | test04(); | |
116 | } |