]> git.ipfire.org Git - thirdparty/gcc.git/blame - libstdc++-v3/testsuite/23_containers/unordered_map/modifiers/merge.cc
Update copyright years.
[thirdparty/gcc.git] / libstdc++-v3 / testsuite / 23_containers / unordered_map / modifiers / merge.cc
CommitLineData
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" }
19
20#include <unordered_map>
21#include <algorithm>
22#include <testsuite_hooks.h>
23
24using test_type = std::unordered_map<int, int>;
25
26struct hash {
27 auto operator()(int i) const noexcept { return ~std::hash<int>()(i); }
28};
29struct equal : std::equal_to<> { };
30
31template<typename C1, typename C2>
32bool equal_elements(const C1& c1, const C2& c2)
33{
34 if (c2.size() != c1.size())
35 return false;
36 for (auto& i : c1)
37 if (c2.count(i.first) != c1.count(i.first))
38 return false;
39 return true;
40}
41
42void
43test01()
44{
2dbe56bd
JW
45 const test_type c0{ {1, 10}, {2, 20}, {3, 30} };
46 test_type c1 = c0, c2 = c0;
47
48 c1.merge(c2);
49 VERIFY( c1 == c0 );
50 VERIFY( c2 == c0 );
51
52 c1.clear();
53 c1.merge(c2);
54 VERIFY( c1 == c0 );
55 VERIFY( c2.empty() );
56
57 c2.merge(std::move(c1));
58 VERIFY( c1.empty() );
59 VERIFY( c2 == c0 );
60}
61
62void
63test02()
64{
2dbe56bd
JW
65 const test_type c0{ {1, 10}, {2, 20}, {3, 30} };
66 test_type c1 = c0;
67 std::unordered_map<int, int, hash, equal> c2( c0.begin(), c0.end() );
68
69 c1.merge(c2);
70 VERIFY( c1 == c0 );
71 VERIFY( equal_elements(c2, c0) );
72
73 c1.clear();
74 c1.merge(c2);
75 VERIFY( c1 == c0 );
76 VERIFY( c2.empty() );
77
78 c2.merge(c1);
79 VERIFY( c1.empty() );
80 VERIFY( equal_elements(c2, c0) );
81
82 c1.merge(std::move(c2));
83 VERIFY( c2.empty() );
84 VERIFY( c1 == c0 );
85}
86
87void
88test03()
89{
2dbe56bd
JW
90 const test_type c0{ {1, 10}, {2, 20}, {3, 30} };
91 test_type c1 = c0;
92 std::unordered_multimap<int, int, hash, equal> c2( c0.begin(), c0.end() );
93 c1.merge(c2);
94 VERIFY( c1 == c0 );
95 VERIFY( equal_elements(c2, c0) );
96
97 c1.clear();
98 c1.merge(c2);
99 VERIFY( c1 == c0 );
100 VERIFY( c2.empty() );
101
102 c2.merge(c1);
103 VERIFY( c1.empty() );
104 VERIFY( equal_elements(c2, c0) );
105
106 c1 = c0;
107 c2.merge(c1);
108 VERIFY( c1.empty() );
109 VERIFY( c2.size() == (2 * c0.size()) );
110 VERIFY( c2.count(1) == 2 );
111 VERIFY( c2.count(2) == 2 );
112 VERIFY( c2.count(3) == 2 );
113
114 c1.merge(c2);
115 VERIFY( c1 == c0 );
116 VERIFY( equal_elements(c2, c0) );
117
118 c1.merge(std::move(c2));
119 VERIFY( c1 == c0 );
120 VERIFY( equal_elements(c2, c0) );
121
122 c1.clear();
123 c1.merge(std::move(c2));
124 VERIFY( c1 == c0 );
125 VERIFY( c2.empty() );
126}
127
128int
129main()
130{
131 test01();
132 test02();
133 test03();
134}