source: mainline/uspace/lib/cpp/src/__bits/test/list.cpp

Last change on this file was 7452b155, checked in by Dzejrou <dzejrou@…>, 7 years ago

cpp: added the rest of list tests and fixed bugs found by them

  • Property mode set to 100644
File size: 9.2 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 <list>
32#include <utility>
33
34namespace std::test
35{
36 bool list_test::run(bool report)
37 {
38 report_ = report;
39 start();
40
41 test_construction_and_assignment();
42 test_modifiers();
43
44 return end();
45 }
46
47 const char* list_test::name()
48 {
49 return "list";
50 }
51
52 void list_test::test_construction_and_assignment()
53 {
54 auto check1 = {1, 1, 1, 1, 1, 1};
55 auto check2 = {1, 2, 3, 4, 5, 6};
56
57 std::list<int> l1(6U, 1);
58 test_eq(
59 "n*value initialization",
60 check1.begin(), check1.end(),
61 l1.begin(), l1.end()
62 );
63
64 std::list<int> l2{check2};
65 test_eq(
66 "initializer_list initialization",
67 check2.begin(), check2.end(),
68 l2.begin(), l2.end()
69 );
70
71 std::list<int> l3{check2.begin(), check2.end()};
72 test_eq(
73 "iterator range initialization",
74 check2.begin(), check2.end(),
75 l3.begin(), l3.end()
76 );
77
78 std::list<int> l4{l3};
79 test_eq(
80 "copy initialization",
81 check2.begin(), check2.end(),
82 l4.begin(), l4.end()
83 );
84 test_eq("size", l4.size(), 6U);
85 test_eq("not empty", l4.empty(), false);
86
87 std::list<int> l5{std::move(l4)};
88 test_eq(
89 "move initialization",
90 check2.begin(), check2.end(),
91 l5.begin(), l5.end()
92 );
93 test_eq("move initializaiton - origin empty pt1", l4.empty(), true);
94 test_eq("move initializaiton - origin empty pt2", l4.size(), 0U);
95
96 l4 = l5;
97 test_eq(
98 "copy assignment",
99 l5.begin(), l5.end(),
100 l4.begin(), l4.end()
101 );
102 test_eq("copy assignment size", l4.size(), l5.size());
103
104 l1 = std::move(l4);
105 test_eq(
106 "move assignment",
107 l5.begin(), l5.end(),
108 l1.begin(), l1.end()
109 );
110 test_eq("move assignment - origin empty", l4.empty(), true);
111
112 auto check3 = {5, 4, 3, 2, 1};
113 l4 = check3;
114 test_eq(
115 "initializer_list assignment pt1",
116 check3.begin(), check3.end(),
117 l4.begin(), l4.end()
118 );
119 test_eq("initializer_list assignment pt2", l4.size(), 5U);
120
121 l5.assign(check3.begin(), check3.end());
122 test_eq(
123 "iterator range assign() pt1",
124 check3.begin(), check3.end(),
125 l5.begin(), l5.end()
126 );
127 test_eq("iterator range assign() pt2", l5.size(), 5U);
128
129 l5.assign(6U, 1);
130 test_eq(
131 "n*value assign() pt1",
132 check1.begin(), check1.end(),
133 l5.begin(), l5.end()
134 );
135 test_eq("n*value assign() pt2", l5.size(), 6U);
136
137 l5.assign(check3);
138 test_eq(
139 "initializer_list assign() pt1",
140 check3.begin(), check3.end(),
141 l5.begin(), l5.end()
142 );
143 test_eq("initializer_list assign() pt2", l5.size(), 5U);
144
145 auto check4 = {1, 2, 3, 4, 5};
146 test_eq(
147 "reverse iterators",
148 check4.begin(), check4.end(),
149 l5.rbegin(), l5.rend()
150 );
151
152 test_eq("front", l5.front(), 5);
153 test_eq("back", l5.back(), 1);
154 }
155
156 void list_test::test_modifiers()
157 {
158 std::list<int> l1{};
159 test_eq("empty list", l1.empty(), true);
160
161 l1.push_back(1);
162 test_eq("empty list push_back pt1", l1.size(), 1U);
163 test_eq("empty list push_back pt2", l1.empty(), false);
164 test_eq("empty list push_back pt3", l1.front(), 1);
165 test_eq("empty list push_back pt4", l1.back(), 1);
166
167 l1.push_front(2);
168 test_eq("push_front pt1", l1.size(), 2U);
169 test_eq("push_front pt2", l1.front(), 2);
170 test_eq("push_front pt3", l1.back(), 1);
171
172 l1.pop_back();
173 test_eq("pop_back pt1", l1.size(), 1U);
174 test_eq("pop_back pt2", l1.back(), 2);
175
176 l1.push_front(3);
177 test_eq("size", l1.size(), 2U);
178
179 l1.pop_front();
180 test_eq("pop_front", l1.front(), 2);
181
182 auto check1 = {2, 42, 42, 42, 42, 42};
183 l1.insert(l1.begin(), 5U, 42);
184 test_eq(
185 "insert n*value",
186 check1.begin(), check1.end(),
187 l1.begin(), l1.end()
188 );
189
190 auto data1 = {33, 34};
191 auto check2 = {2, 42, 33, 34, 42, 42, 42, 42};
192 auto it1 = l1.begin();
193 std::advance(it1, 2);
194
195 l1.insert(it1, data1.begin(), data1.end());
196 test_eq(
197 "insert iterator range",
198 check2.begin(), check2.end(),
199 l1.begin(), l1.end()
200 );
201
202 auto check3 = {2, 42, 33, 34, 42, 33, 34, 42, 42, 42};
203 auto it2 = l1.begin();
204 std::advance(it2, 5);
205
206 l1.insert(it2, data1);
207 test_eq(
208 "insert initializer_list",
209 check3.begin(), check3.end(),
210 l1.begin(), l1.end()
211 );
212
213 auto check4 = {2, 42, 33, 34, 33, 34, 42, 42, 42};
214 auto it3 = l1.begin();
215 std::advance(it3, 4);
216
217 l1.erase(it3);
218 test_eq(
219 "erase iterator",
220 check4.begin(), check4.end(),
221 l1.begin(), l1.end()
222 );
223
224 auto check5 = {33, 34, 42, 42, 42};
225 auto it4 = l1.begin();
226 auto it5 = l1.begin();
227 std::advance(it5, 4);
228
229 l1.erase(it4, it5);
230 test_eq(
231 "erase iterator range",
232 check5.begin(), check5.end(),
233 l1.begin(), l1.end()
234 );
235
236 l1.clear();
237 test_eq("clear empty", l1.empty(), true);
238 test_eq("clear size", l1.size(), 0U);
239
240 std::list<int> l2{1, 2, 3, 4, 5};
241 std::list<int> l3{10, 20, 30, 40, 50};
242
243 auto check6 = {1, 2, 10, 20, 30, 40, 50, 3, 4, 5};
244 auto check7 = {1, 2, 10, 20, 30, 40, 50};
245 auto check8 = {3, 4, 5};
246
247 auto it6 = l2.begin();
248 std::advance(it6, 2);
249
250 l2.splice(it6, l3);
251 test_eq(
252 "splice pt1",
253 check6.begin(), check6.end(),
254 l2.begin(), l2.end()
255 );
256 test_eq("splice pt2", l3.empty(), true);
257
258 l3.splice(l3.begin(), l2, it6, l2.end());
259 test_eq(
260 "splice pt3",
261 check7.begin(), check7.end(),
262 l2.begin(), l2.end()
263 );
264 test_eq(
265 "splice pt4",
266 check8.begin(), check8.end(),
267 l3.begin(), l3.end()
268 );
269 test_eq("splice size pt1", l2.size(), 7U);
270 test_eq("splice size pt2", l3.size(), 3U);
271
272 auto check9 = {1, -1, 2, -2, 3, -3, 4, -4};
273 auto check10 = {1, 2, 3, 4};
274 std::list<int> l4{1, -1, 2, 5, -2, 5, 3, -3, 5, 4, -4};
275
276 l4.remove(5);
277 test_eq(
278 "remove",
279 check9.begin(), check9.end(),
280 l4.begin(), l4.end()
281 );
282 test_eq("remove size", l4.size(), 8U);
283
284 l4.remove_if([](auto x){ return x < 0; });
285 test_eq(
286 "remove_if",
287 check10.begin(), check10.end(),
288 l4.begin(), l4.end()
289 );
290 test_eq("remove_if size", l4.size(), 4U);
291
292 auto check11 = {1, 2, 3, 2, 4, 5};
293 std::list<int> l5{1, 1, 2, 3, 3, 2, 2, 4, 5, 5};
294
295 l5.unique();
296 test_eq(
297 "unique",
298 check11.begin(), check11.end(),
299 l5.begin(), l5.end()
300 );
301 test_eq("unique size", l5.size(), 6U);
302
303 auto check12 = {1, 3, 3, 5, 7, 9, 9};
304 std::list<int> l6{1, 3, 3, 4, 4, 5, 6, 6, 6, 7, 8, 9, 9};
305
306 l6.unique([](auto lhs, auto rhs){ return lhs == rhs + 1; });
307 test_eq(
308 "unique predicate",
309 check12.begin(), check12.end(),
310 l6.begin(), l6.end()
311 );
312 test_eq("unique predicate size", l6.size(), 7U);
313 }
314}
315
Note: See TracBrowser for help on using the repository browser.