]> git.ipfire.org Git - thirdparty/gcc.git/blame - libstdc++-v3/testsuite/23_containers/unordered_multimap/modifiers/merge.cc
Update copyright years.
[thirdparty/gcc.git] / libstdc++-v3 / testsuite / 23_containers / unordered_multimap / modifiers / merge.cc
CommitLineData
a945c346 1// Copyright (C) 2016-2024 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
6458742a 18// { dg-do run { target c++17 } }
2dbe56bd
JW
19
20#include <unordered_map>
21#include <algorithm>
22#include <testsuite_hooks.h>
23
24using test_type = std::unordered_multimap<int, int>;
25struct hash {
26 auto operator()(int i) const noexcept { return ~std::hash<int>()(i); }
27};
28struct equal : std::equal_to<> { };
29
30void
31test01()
32{
2dbe56bd
JW
33 const test_type c0{ {1, 10}, {1, 11}, {2, 20}, {2, 21}, {3, 30}, {3, 31} };
34 test_type c1 = c0, c2 = c0;
35
36 c1.merge(c2);
37 for (auto& i : c1)
38 VERIFY( c1.count(i.first) == (2 * c0.count(i.first)) );
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
48void
49test02()
50{
2dbe56bd
JW
51 const test_type c0{ {1, 10}, {1, 11}, {2, 20}, {2, 21}, {3, 30}, {3, 31} };
52 test_type c1 = c0;
53 std::unordered_multimap<int, 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.first) == (2 * c0.count(i.first)) );
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
68void
69test03()
70{
2dbe56bd
JW
71 const test_type c0{ {1, 10}, {1, 11}, {2, 20}, {2, 21}, {3, 30}, {3, 31} };
72 test_type c1 = c0;
73 std::unordered_multimap<int, 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.first) == (2 * c0.count(i.first)) );
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
88void
89test04()
90{
2dbe56bd
JW
91 const test_type c0{ {1, 10}, {1, 11}, {2, 20}, {2, 21}, {3, 30}, {3, 31} };
92 test_type c1 = c0;
93 std::unordered_map<int, 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.first) == (1.5 * c0.count(i.first)) );
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
108int
109main()
110{
111 test01();
112 test02();
113 test03();
114 test04();
115}