source: mainline/uspace/lib/cpp/src/__bits/test/set.cpp@ b57a3ee

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since b57a3ee was b57a3ee, checked in by Dzejrou <dzejrou@…>, 7 years ago

cpp: refactored the library layout, everything from the impl directory was moved to the bits directory for the sake of consistency, updated copyright notices and include guards

  • Property mode set to 100644
File size: 9.6 KB
Line 
1/*
2 * Copyright (c) 2018 Jaroslav Jindrak
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * - The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29#include <__bits/test/tests.hpp>
30#include <initializer_list>
31#include <set>
32#include <string>
33#include <utility>
34
35namespace std::test
36{
37 bool set_test::run(bool report)
38 {
39 report_ = report;
40 start();
41
42 test_constructors_and_assignment();
43 test_emplace_insert();
44 test_bounds_and_ranges();
45 test_multi();
46 test_reverse_iterators();
47 test_multi_bounds_and_ranges();
48
49 return end();
50 }
51
52 const char* set_test::name()
53 {
54 return "set";
55 }
56
57 void set_test::test_constructors_and_assignment()
58 {
59 auto check1 = {1, 2, 3, 4, 5, 6, 7};
60 auto src1 = {3, 1, 5, 2, 7, 6, 4};
61
62 std::set<int> s1{src1};
63 test_eq(
64 "initializer list initialization",
65 check1.begin(), check1.end(),
66 s1.begin(), s1.end()
67 );
68 test_eq("size", s1.size(), 7U);
69
70 std::set<int> s2{src1.begin(), src1.end()};
71 test_eq(
72 "iterator range initialization",
73 check1.begin(), check1.end(),
74 s2.begin(), s2.end()
75 );
76
77 std::set<int> s3{s1};
78 test_eq(
79 "copy initialization",
80 check1.begin(), check1.end(),
81 s3.begin(), s3.end()
82 );
83
84 std::set<int> s4{std::move(s1)};
85 test_eq(
86 "move initialization",
87 check1.begin(), check1.end(),
88 s4.begin(), s4.end()
89 );
90 test_eq("move initialization - origin empty", s1.size(), 0U);
91 test_eq("empty", s1.empty(), true);
92
93 s1 = s4;
94 test_eq(
95 "copy assignment",
96 check1.begin(), check1.end(),
97 s1.begin(), s1.end()
98 );
99
100 s4 = std::move(s1);
101 test_eq(
102 "move assignment",
103 check1.begin(), check1.end(),
104 s4.begin(), s4.end()
105 );
106 test_eq("move assignment - origin empty", s1.size(), 0U);
107
108 s1 = src1;
109 test_eq(
110 "initializer list assignment",
111 check1.begin(), check1.end(),
112 s1.begin(), s1.end()
113 );
114 }
115
116 void set_test::test_emplace_insert()
117 {
118 std::set<int> set1{};
119
120 auto res1 = set1.emplace(1);
121 test_eq("first emplace succession", res1.second, true);
122 test_eq("first emplace equivalence", *res1.first, 1);
123
124 auto res2 = set1.emplace(1);
125 test_eq("second emplace failure", res2.second, false);
126 test_eq("second emplace equivalence", *res2.first, 1);
127
128 auto res3 = set1.emplace_hint(set1.begin(), 2);
129 test_eq("first emplace_hint succession", (res3 != set1.end()), true);
130 test_eq("first emplace_hint equivalence", *res3, 2);
131
132 auto res4 = set1.emplace_hint(set1.begin(), 2);
133 test_eq("second emplace_hint failure", (res4 != set1.end()), true);
134 test_eq("second emplace_hint equivalence", *res4, 2);
135
136 std::set<std::string> set2{};
137 auto res5 = set2.insert("A");
138 test_eq("conversion insert succession", res5.second, true);
139 test_eq("conversion insert equivalence", *res5.first, std::string{"A"});
140
141 auto res6 = set2.insert(std::string{"B"});
142 test_eq("first insert succession", res6.second, true);
143 test_eq("first insert equivalence", *res6.first, std::string{"B"});
144
145 auto res7 = set2.insert(std::string{"B"});
146 test_eq("second insert failure", res7.second, false);
147 test_eq("second insert equivalence", *res7.first, std::string{"B"});
148
149 auto res10 = set1.erase(set1.find(2));
150 test_eq("erase", set1.find(2), set1.end());
151 test_eq("highest erased", res10, set1.end());
152
153 set2.insert(std::string{"G"});
154 set2.insert(std::string{"H"});
155 set2.insert(std::string{"K"});
156 auto res11 = set2.erase(std::string{"G"});
157 test_eq("erase by key pt1", res11, 1U);
158 auto res12 = set2.erase(std::string{"M"});
159 test_eq("erase by key pt2", res12, 0U);
160
161 std::set<int> set3{};
162 set3.insert(1);
163 auto res13 = set3.erase(1);
164 test_eq("erase root by key pt1", res13, 1U);
165 test_eq("erase root by key pt2", set3.empty(), true);
166
167 set3.insert(3);
168 auto res14 = set3.erase(set3.begin());
169 test_eq("erase root by iterator pt1", res14, set3.end());
170 test_eq("erase root by iterator pt2", set3.empty(), true);
171
172 set2.clear();
173 test_eq("clear", set2.empty(), true);
174
175 set3.insert(1);
176 auto res15 = set3.count(1);
177 test_eq("count", res15, 1U);
178 }
179
180 void set_test::test_bounds_and_ranges()
181 {
182 std::set<int> set{};
183 for (int i = 0; i < 10; ++i)
184 set.insert(i);
185 for (int i = 15; i < 20; ++i)
186 set.insert(i);
187
188 auto res1 = set.lower_bound(5);
189 test_eq("lower_bound of present key", *res1, 5);
190
191 auto res2 = set.lower_bound(13);
192 test_eq("lower_bound of absent key", *res2, 9);
193
194 auto res3 = set.upper_bound(7);
195 test_eq("upper_bound of present key", *res3, 8);
196
197 auto res4 = set.upper_bound(12);
198 test_eq("upper_bound of absent key", *res4, 15);
199
200 auto res5 = set.equal_range(4);
201 test_eq("equal_range of present key pt1", *res5.first, 4);
202 test_eq("equal_range of present key pt2", *res5.second, 5);
203
204 auto res6 = set.equal_range(14);
205 test_eq("equal_range of absent key pt1", *res6.first, 9);
206 test_eq("equal_range of absent key pt2", *res6.second, 15);
207 }
208
209 void set_test::test_multi()
210 {
211 auto check1 = {1, 2, 3, 3, 4, 5, 6, 6, 6, 7};
212 auto src1 = {3, 6, 1, 5, 6, 3, 2, 7, 6, 4};
213
214 std::multiset<int> mset{src1};
215 test_eq(
216 "multi construction",
217 check1.begin(), check1.end(),
218 mset.begin(), mset.end()
219 );
220
221 auto res1 = mset.count(6);
222 test_eq("multi count", res1, 3U);
223
224 auto res2 = mset.emplace(7);
225 test_eq("multi duplicit emplace pt1", *res2, 7);
226 test_eq("multi duplicit emplace pt2", mset.count(7), 2U);
227
228 auto res3 = mset.emplace(8);
229 test_eq("multi unique emplace pt1", *res3, 8);
230 test_eq("multi unique emplace pt2", mset.count(8), 1U);
231
232 auto res4 = mset.insert(8);
233 test_eq("multi duplicit insert pt1", *res4, 8);
234 test_eq("multi duplicit insert pt2", mset.count(8), 2U);
235
236 auto res5 = mset.insert(9);
237 test_eq("multi unique insert pt1", *res5, 9);
238 test_eq("multi unique insert pt2", mset.count(9), 1U);
239
240 auto res6 = mset.erase(8);
241 test_eq("multi erase by key pt1", res6, 2U);
242 test_eq("multi erase by key pt2", mset.count(8), 0U);
243
244 auto res7 = mset.erase(mset.find(7));
245 test_eq("multi erase by iterator pt1", *res7, 7);
246 test_eq("multi erase by iterator pt2", mset.count(7), 1U);
247 }
248
249 void set_test::test_reverse_iterators()
250 {
251 auto check1 = {7, 6, 6, 6, 5, 4, 3, 3, 2, 1};
252 auto src1 = {3, 6, 1, 5, 6, 3, 2, 7, 6, 4};
253
254 std::multiset<int> mset{src1};
255 test_eq(
256 "multi reverse iterators",
257 check1.begin(), check1.end(),
258 mset.rbegin(), mset.rend()
259 );
260
261 auto check2 = {7, 6, 5, 4, 3, 2, 1};
262 auto src2 = {3, 1, 5, 2, 7, 6, 4};
263
264 std::set<int> set{src2};
265 test_eq(
266 "reverse iterators",
267 check2.begin(), check2.end(),
268 set.rbegin(), set.rend()
269 );
270 }
271
272 void set_test::test_multi_bounds_and_ranges()
273 {
274 auto check1 = {1, 1};
275 auto check2 = {5, 5, 5};
276 auto check3 = {6};
277 auto src = {1, 1, 2, 3, 5, 5, 5, 6};
278
279 std::multiset<int> mset{src};
280 auto res1 = mset.equal_range(1);
281 test_eq(
282 "multi equal_range at the start",
283 check1.begin(), check1.end(),
284 res1.first, res1.second
285 );
286
287 auto res2 = mset.equal_range(5);
288 test_eq(
289 "multi equal_range in the middle",
290 check2.begin(), check2.end(),
291 res2.first, res2.second
292 );
293
294 auto res3 = mset.equal_range(6);
295 test_eq(
296 "multi equal_range at the end + single element range",
297 check3.begin(), check3.end(),
298 res3.first, res3.second
299 );
300 }
301}
Note: See TracBrowser for help on using the repository browser.