]> git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/testsuite/23_containers/unordered_multiset/modifiers/merge.cc
Update copyright years.
[thirdparty/gcc.git] / libstdc++-v3 / testsuite / 23_containers / unordered_multiset / modifiers / merge.cc
1 // Copyright (C) 2016-2024 Free Software Foundation, Inc.
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-do run { target c++17 } }
19
20 #include <string>
21 #include <unordered_set>
22 #include <algorithm>
23 #include <testsuite_hooks.h>
24
25 using test_type = std::unordered_multiset<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 {
34 test_type c0{ 1, 1, 2, 2, 3, 3 };
35 test_type c1 = c0, c2 = c0;
36
37 c1.merge(c2);
38 for (auto i : c1)
39 VERIFY( c1.count(i) == (2 * c0.count(i)) );
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 {
52 test_type c0{ 1, 1, 2, 2, 3, 3 };
53 test_type c1 = c0;
54 std::unordered_multiset<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) == (2 * c0.count(i)) );
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 {
72 const test_type c0{ 1, 1, 2, 2, 3, 3 };
73 test_type c1 = c0;
74 std::unordered_multiset<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) == (2 * c0.count(i)) );
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 {
92 const test_type c0{ 1, 1, 2, 2, 3, 3 };
93 test_type c1 = c0;
94 std::unordered_set<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) == (1.5 * c0.count(i)) );
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 void
110 test05()
111 {
112 const std::unordered_multiset<std::string> c0{ "abcd", "abcd", "efgh", "efgh", "ijkl", "ijkl" };
113 std::unordered_multiset<std::string> c1 = c0;
114 std::unordered_set<std::string> c2( c0.begin(), c0.end() );
115
116 c1.merge(c2);
117 VERIFY( c1.size() == (1.5 * c0.size()) );
118 for (auto& i : c1)
119 VERIFY( c1.count(i) == (1.5 * c0.count(i)) );
120 VERIFY( c2.empty() );
121
122 c1.clear();
123 c2.insert( c0.begin(), c0.end() );
124 c1.merge(std::move(c2));
125 VERIFY( c1.size() == (0.5 * c0.size()) );
126 VERIFY( c2.empty() );
127 }
128
129 int
130 main()
131 {
132 test01();
133 test02();
134 test03();
135 test04();
136 test05();
137 }