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