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