source: mainline/uspace/lib/cpp/src/__bits/test/deque.cpp@ 7bbf91e

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

cpp: changed internal to bits to avoid include space pollusion, fixed old std::hel:: bugs in files that weren't touched since

  • Property mode set to 100644
File size: 8.3 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 <deque>
30#include <initializer_list>
31#include <__bits/test/tests.hpp>
32#include <utility>
33
34namespace std::test
35{
36 bool deque_test::run(bool report)
37 {
38 report_ = report;
39 start();
40
41 test_constructors_and_assignment();
42 test_resizing();
43 test_push_pop();
44 test_operations();
45
46 return end();
47 }
48
49 const char* deque_test::name()
50 {
51 return "deque";
52 }
53
54 void deque_test::test_constructors_and_assignment()
55 {
56 auto check1 = {0, 0, 0, 0, 0};
57 std::deque<int> d1(5U);
58 test_eq(
59 "size construction",
60 check1.begin(), check1.end(),
61 d1.begin(), d1.end()
62 );
63
64 auto check2 = {1, 1, 1, 1, 1};
65 std::deque<int> d2(5U, 1);
66 test_eq(
67 "value construction",
68 check2.begin(), check2.end(),
69 d2.begin(), d2.end()
70 );
71
72 auto check3 = {1, 2, 3, 4, 5};
73 std::deque<int> d3{check3.begin(), check3.end()};
74 test_eq(
75 "iterator range construction",
76 check3.begin(), check3.end(),
77 d3.begin(), d3.end()
78 );
79
80 std::deque<int> d4{d3};
81 test_eq(
82 "copy construction",
83 check3.begin(), check3.end(),
84 d4.begin(), d4.end()
85 );
86
87 std::deque<int> d5{std::move(d4)};
88 test_eq(
89 "move construction",
90 check3.begin(), check3.end(),
91 d5.begin(), d5.end()
92 );
93 test_eq("move construction - origin empty", d4.empty(), true);
94
95 std::deque<int> d6{check3};
96 test_eq(
97 "inializer list construction",
98 check3.begin(), check3.end(),
99 d6.begin(), d6.end()
100 );
101
102 d4 = d6;
103 test_eq(
104 "copy assignment",
105 check3.begin(), check3.end(),
106 d4.begin(), d4.end()
107 );
108
109 d6 = std::move(d4);
110 test_eq(
111 "move assignment",
112 check3.begin(), check3.end(),
113 d6.begin(), d6.end()
114 );
115 test_eq("move assignment - origin empty", d4.empty(), true);
116
117 d4 = check3;
118 test_eq(
119 "initializer list assignment",
120 check3.begin(), check3.end(),
121 d4.begin(), d4.end()
122 );
123
124 std::deque<int> d7{};
125 d7.assign(check3.begin(), check3.end());
126 test_eq(
127 "iterator range assign()",
128 check3.begin(), check3.end(),
129 d7.begin(), d7.end()
130 );
131
132 d7.assign(5U, 1);
133 test_eq(
134 "value assign()",
135 check2.begin(), check2.end(),
136 d7.begin(), d7.end()
137 );
138
139 d7.assign(check3);
140 test_eq(
141 "initializer list assign()",
142 check3.begin(), check3.end(),
143 d7.begin(), d7.end()
144 );
145 }
146
147 void deque_test::test_resizing()
148 {
149 auto check1 = {1, 2, 3};
150 auto check2 = {1, 2, 3, 0, 0};
151 std::deque<int> d1{1, 2, 3, 4, 5};
152
153 d1.resize(3U);
154 test_eq(
155 "downsize",
156 check1.begin(), check1.end(),
157 d1.begin(), d1.end()
158 );
159
160 d1.resize(5U);
161 test_eq(
162 "upsize",
163 check2.begin(), check2.end(),
164 d1.begin(), d1.end()
165 );
166
167 auto check3 = {1, 2, 3, 9, 9};
168 std::deque<int> d2{1, 2, 3, 4, 5};
169
170 d2.resize(3U, 9);
171 test_eq(
172 "downsize with default value",
173 check1.begin(), check1.end(),
174 d2.begin(), d2.end()
175 );
176
177 d2.resize(5U, 9);
178 test_eq(
179 "upsize with default value",
180 check3.begin(), check3.end(),
181 d2.begin(), d2.end()
182 );
183
184 d2.resize(0U);
185 test_eq("resize to 0", d2.empty(), true);
186 }
187
188 void deque_test::test_push_pop()
189 {
190 std::deque<int> d1{};
191
192 d1.push_back(42);
193 test_eq("push_back to empty equivalence", d1[0], 42);
194 test_eq("push_back to empty size", d1.size(), 1U);
195
196 d1.push_front(21);
197 test_eq("push_front after push_back equivalence", d1[0], 21);
198 test_eq("push_front after push_back size", d1.size(), 2U);
199
200 for (int i = 0; i <= 100; ++i)
201 d1.push_back(i);
202 test_eq("back after bucket test", d1.back(), 100);
203
204 d1.pop_back();
205 test_eq("back after pop_back", d1.back(), 99);
206 d1.pop_front();
207 test_eq("front after pop_front", d1.back(), 99);
208
209
210 for (int i = 0; i <= 100; ++i)
211 d1.push_front(i);
212 test_eq("front after bucket test", d1.front(), 100);
213
214 std::deque<int> d2{};
215
216 d2.push_front(42);
217 test_eq("push_front to empty equivalence", d2[0], 42);
218 test_eq("push_front to empty size", d2.size(), 1U);
219
220 d2.push_back(21);
221 test_eq("push_back after push_front equivalence", d2[1], 21);
222 test_eq("push_back after push_front size", d2.size(), 2U);
223
224 d2.clear();
225 test_eq("clear() - empty()", d2.empty(), true);
226 test_eq("clear() - iterators", d2.begin(), d2.end());
227 }
228
229 void deque_test::test_operations()
230 {
231 auto check1 = {
232 1, 2, 3, 4, 11, 22, 33, 44, 55, 66, 77, 88,
233 5, 6, 7, 8, 9, 10, 11, 12
234 };
235 auto to_insert = {11, 22, 33, 44, 55, 66, 77, 88};
236
237 std::deque<int> d1{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12};
238 std::deque<int> d2{d1};
239 std::deque<int> d3{d1};
240 std::deque<int> d4{d1};
241
242 d1.insert(d1.begin() + 5, to_insert.begin(), to_insert.end());
243 test_eq(
244 "insert iterator range",
245 check1.begin(), check1.end(),
246 d1.begin(), d1.end()
247 );
248
249 d2.insert(d2.begin() + 5, to_insert);
250 test_eq(
251 "insert initializer list",
252 check1.begin(), check1.end(),
253 d2.begin(), d2.end()
254 );
255
256 auto check2 = {
257 1, 2, 3, 4, 99, 99, 99, 99, 99, 99, 99, 99,
258 5, 6, 7, 8, 9, 10, 11, 12
259 };
260 d3.insert(d3.begin() + 5, 8U, 99);
261 test_eq(
262 "insert value n times",
263 check2.begin(), check2.end(),
264 d3.begin(), d3.end()
265 );
266
267 auto check3 = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12};
268 d3.erase(d3.begin() + 4, d3.begin() + 12);
269 test_eq(
270 "erase iterator range",
271 check3.begin(), check3.end(),
272 d3.begin(), d3.end()
273 );
274
275 auto check4 = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 12};
276 d3.erase(d3.begin() + 10);
277 test_eq(
278 "erase",
279 check4.begin(), check4.end(),
280 d3.begin(), d3.end()
281 );
282
283 d2.swap(d3);
284 test_eq(
285 "swap1",
286 check1.begin(), check1.end(),
287 d3.begin(), d3.end()
288 );
289 test_eq(
290 "swap2",
291 check4.begin(), check4.end(),
292 d2.begin(), d2.end()
293 );
294 }
295}
Note: See TracBrowser for help on using the repository browser.