]> git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/testsuite/23_containers/map/modifiers/merge.cc
326104e1283d853f5e1204cd3622d1ea7154558d
[thirdparty/gcc.git] / libstdc++-v3 / testsuite / 23_containers / map / modifiers / merge.cc
1 // Copyright (C) 2016-2021 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 // { dg-do run { target c++17 } }
20
21 #include <map>
22 #include <algorithm>
23 #include <testsuite_hooks.h>
24
25 using test_type = std::map<int, int>;
26
27 void
28 test01()
29 {
30 const test_type c0{ {1, 10}, {2, 20}, {3, 30} };
31 test_type c1 = c0, c2 = c0;
32
33 c1.merge(c2);
34 VERIFY( c1 == c0 );
35 VERIFY( c2 == c0 );
36
37 c1.clear();
38 c1.merge(c2);
39 VERIFY( c1 == c0 );
40 VERIFY( c2.empty() );
41
42 c2.merge(std::move(c1));
43 VERIFY( c1.empty() );
44 VERIFY( c2 == c0 );
45 }
46
47 void
48 test02()
49 {
50 const test_type c0{ {1, 10}, {2, 20}, {3, 30} };
51 test_type c1 = c0;
52 std::map<int, int, std::less<>> c2( c0.begin(), c0.end() );
53
54 c1.merge(c2);
55 VERIFY( c1 == c0 );
56 VERIFY( std::equal(c2.begin(), c2.end(), c0.begin(), c0.end()) );
57
58 c1.clear();
59 c1.merge(c2);
60 VERIFY( c1 == c0 );
61 VERIFY( c2.empty() );
62
63 c2.merge(std::move(c1));
64 VERIFY( c1.empty() );
65 VERIFY( std::equal(c2.begin(), c2.end(), c0.begin(), c0.end()) );
66 }
67
68 void
69 test03()
70 {
71 const test_type c0{ {1, 10}, {2, 20}, {3, 30} };
72 test_type c1 = c0;
73 std::map<int, int, std::greater<>> c2( c0.begin(), c0.end() );
74
75 c1.merge(c2);
76 VERIFY( c1 == c0 );
77 VERIFY( std::equal(c2.rbegin(), c2.rend(), c0.begin(), c0.end()) );
78
79 c1.clear();
80 c1.merge(c2);
81 VERIFY( c1 == c0 );
82 VERIFY( c2.empty() );
83
84 c2.merge(c1);
85 VERIFY( c1.empty() );
86 VERIFY( std::equal(c2.rbegin(), c2.rend(), c0.begin(), c0.end()) );
87
88 c1.merge(std::move(c2));
89 VERIFY( c1 == c0 );
90 VERIFY( c2.empty() );
91 }
92
93 void
94 test04()
95 {
96 const test_type c0{ {1, 10}, {2, 20}, {3, 30} };
97 test_type c1 = c0;
98 std::multimap<int, int, std::greater<>> c2( c0.begin(), c0.end() );
99
100 c1.merge(c2);
101 VERIFY( c1 == c0 );
102 VERIFY( std::equal(c2.rbegin(), c2.rend(), c0.begin(), c0.end()) );
103
104 c1.clear();
105 c1.merge(c2);
106 VERIFY( c1 == c0 );
107 VERIFY( c2.empty() );
108
109 c2.merge(c1);
110 VERIFY( c1.empty() );
111 VERIFY( std::equal(c2.rbegin(), c2.rend(), c0.begin(), c0.end()) );
112
113 c1 = c0;
114 c2.merge(c1);
115 VERIFY( c1.empty() );
116 VERIFY( c2.size() == (2 * c0.size()) );
117 VERIFY( std::is_sorted(c2.begin(), c2.end(), c2.value_comp()) );
118
119 c1.merge(c2);
120 VERIFY( c1 == c0 );
121 VERIFY( std::equal(c2.rbegin(), c2.rend(), c0.begin(), c0.end()) );
122
123 c1.merge(std::move(c2));
124 VERIFY( c1 == c0 );
125 VERIFY( std::equal(c2.rbegin(), c2.rend(), c0.begin(), c0.end()) );
126
127 c1.clear();
128 c1.merge(std::move(c2));
129 VERIFY( c1 == c0 );
130 VERIFY( c2.empty() );
131 }
132
133 int
134 main()
135 {
136 test01();
137 test02();
138 test03();
139 test04();
140 }