1 | #include <Common/PODArray.h> |
2 | #include <Core/Types.h> |
3 | #include <iostream> |
4 | |
5 | #define ASSERT_CHECK(cond, res) \ |
6 | do \ |
7 | { \ |
8 | if (!(cond)) \ |
9 | { \ |
10 | std::cerr << __FILE__ << ":" << __LINE__ << ":" \ |
11 | << "Assertion " << #cond << " failed.\n"; \ |
12 | if ((res)) { (res) = false; } \ |
13 | } \ |
14 | } \ |
15 | while (0) |
16 | |
17 | static void test1() |
18 | { |
19 | using namespace DB; |
20 | |
21 | static constexpr size_t initial_size = 8; |
22 | static constexpr size_t stack_threshold = 32; |
23 | using Array = PODArray<UInt64, initial_size, AllocatorWithStackMemory<Allocator<false>, stack_threshold>>; |
24 | |
25 | bool res = true; |
26 | |
27 | { |
28 | Array arr; |
29 | Array arr2; |
30 | arr2 = std::move(arr); |
31 | } |
32 | |
33 | { |
34 | Array arr; |
35 | |
36 | arr.push_back(1); |
37 | arr.push_back(2); |
38 | arr.push_back(3); |
39 | |
40 | Array arr2; |
41 | |
42 | arr2 = std::move(arr); |
43 | |
44 | ASSERT_CHECK((arr2.size() == 3), res); |
45 | ASSERT_CHECK((arr2[0] == 1), res); |
46 | ASSERT_CHECK((arr2[1] == 2), res); |
47 | ASSERT_CHECK((arr2[2] == 3), res); |
48 | |
49 | arr = std::move(arr2); |
50 | |
51 | ASSERT_CHECK((arr.size() == 3), res); |
52 | ASSERT_CHECK((arr[0] == 1), res); |
53 | ASSERT_CHECK((arr[1] == 2), res); |
54 | ASSERT_CHECK((arr[2] == 3), res); |
55 | } |
56 | |
57 | { |
58 | Array arr; |
59 | |
60 | arr.push_back(1); |
61 | arr.push_back(2); |
62 | arr.push_back(3); |
63 | arr.push_back(4); |
64 | arr.push_back(5); |
65 | |
66 | Array arr2; |
67 | |
68 | arr2 = std::move(arr); |
69 | |
70 | ASSERT_CHECK((arr2.size() == 5), res); |
71 | ASSERT_CHECK((arr2[0] == 1), res); |
72 | ASSERT_CHECK((arr2[1] == 2), res); |
73 | ASSERT_CHECK((arr2[2] == 3), res); |
74 | ASSERT_CHECK((arr2[3] == 4), res); |
75 | ASSERT_CHECK((arr2[4] == 5), res); |
76 | |
77 | arr = std::move(arr2); |
78 | |
79 | ASSERT_CHECK((arr.size() == 5), res); |
80 | ASSERT_CHECK((arr[0] == 1), res); |
81 | ASSERT_CHECK((arr[1] == 2), res); |
82 | ASSERT_CHECK((arr[2] == 3), res); |
83 | ASSERT_CHECK((arr[3] == 4), res); |
84 | ASSERT_CHECK((arr[4] == 5), res); |
85 | } |
86 | |
87 | { |
88 | Array arr; |
89 | |
90 | arr.push_back(1); |
91 | arr.push_back(2); |
92 | arr.push_back(3); |
93 | |
94 | Array arr2; |
95 | |
96 | arr2.push_back(4); |
97 | arr2.push_back(5); |
98 | arr2.push_back(6); |
99 | arr2.push_back(7); |
100 | |
101 | arr2 = std::move(arr); |
102 | |
103 | ASSERT_CHECK((arr2.size() == 3), res); |
104 | ASSERT_CHECK((arr2[0] == 1), res); |
105 | ASSERT_CHECK((arr2[1] == 2), res); |
106 | ASSERT_CHECK((arr2[2] == 3), res); |
107 | } |
108 | |
109 | { |
110 | Array arr; |
111 | |
112 | arr.push_back(1); |
113 | arr.push_back(2); |
114 | arr.push_back(3); |
115 | |
116 | Array arr2; |
117 | |
118 | arr2.push_back(4); |
119 | arr2.push_back(5); |
120 | arr2.push_back(6); |
121 | arr2.push_back(7); |
122 | arr2.push_back(8); |
123 | |
124 | arr = std::move(arr2); |
125 | |
126 | ASSERT_CHECK((arr.size() == 5), res); |
127 | ASSERT_CHECK((arr[0] == 4), res); |
128 | ASSERT_CHECK((arr[1] == 5), res); |
129 | ASSERT_CHECK((arr[2] == 6), res); |
130 | ASSERT_CHECK((arr[3] == 7), res); |
131 | ASSERT_CHECK((arr[4] == 8), res); |
132 | } |
133 | |
134 | if (!res) |
135 | std::cerr << "Some errors were found in test 1\n" ; |
136 | } |
137 | |
138 | static void test2() |
139 | { |
140 | using namespace DB; |
141 | |
142 | static constexpr size_t initial_size = 8; |
143 | static constexpr size_t stack_threshold = 32; |
144 | using Array = PODArray<UInt64, initial_size, AllocatorWithStackMemory<Allocator<false>, stack_threshold>>; |
145 | |
146 | bool res = true; |
147 | |
148 | { |
149 | Array arr; |
150 | Array arr2; |
151 | arr.swap(arr2); |
152 | arr2.swap(arr); |
153 | } |
154 | |
155 | { |
156 | Array arr; |
157 | |
158 | Array arr2; |
159 | |
160 | arr2.push_back(1); |
161 | arr2.push_back(2); |
162 | arr2.push_back(3); |
163 | |
164 | arr.swap(arr2); |
165 | |
166 | ASSERT_CHECK((arr.size() == 3), res); |
167 | ASSERT_CHECK((arr[0] == 1), res); |
168 | ASSERT_CHECK((arr[1] == 2), res); |
169 | ASSERT_CHECK((arr[2] == 3), res); |
170 | |
171 | ASSERT_CHECK((arr2.size() == 0), res); |
172 | |
173 | arr.swap(arr2); |
174 | |
175 | ASSERT_CHECK((arr.size() == 0), res); |
176 | |
177 | ASSERT_CHECK((arr2.size() == 3), res); |
178 | ASSERT_CHECK((arr2[0] == 1), res); |
179 | ASSERT_CHECK((arr2[1] == 2), res); |
180 | ASSERT_CHECK((arr2[2] == 3), res); |
181 | } |
182 | |
183 | { |
184 | Array arr; |
185 | |
186 | Array arr2; |
187 | |
188 | arr2.push_back(1); |
189 | arr2.push_back(2); |
190 | arr2.push_back(3); |
191 | arr2.push_back(4); |
192 | arr2.push_back(5); |
193 | |
194 | arr.swap(arr2); |
195 | |
196 | ASSERT_CHECK((arr.size() == 5), res); |
197 | ASSERT_CHECK((arr[0] == 1), res); |
198 | ASSERT_CHECK((arr[1] == 2), res); |
199 | ASSERT_CHECK((arr[2] == 3), res); |
200 | ASSERT_CHECK((arr[3] == 4), res); |
201 | ASSERT_CHECK((arr[4] == 5), res); |
202 | |
203 | ASSERT_CHECK((arr2.size() == 0), res); |
204 | |
205 | arr.swap(arr2); |
206 | |
207 | ASSERT_CHECK((arr.size() == 0), res); |
208 | |
209 | ASSERT_CHECK((arr2.size() == 5), res); |
210 | ASSERT_CHECK((arr2[0] == 1), res); |
211 | ASSERT_CHECK((arr2[1] == 2), res); |
212 | ASSERT_CHECK((arr2[2] == 3), res); |
213 | ASSERT_CHECK((arr2[3] == 4), res); |
214 | ASSERT_CHECK((arr2[4] == 5), res); |
215 | } |
216 | |
217 | { |
218 | Array arr; |
219 | |
220 | arr.push_back(1); |
221 | arr.push_back(2); |
222 | arr.push_back(3); |
223 | |
224 | Array arr2; |
225 | |
226 | arr2.push_back(4); |
227 | arr2.push_back(5); |
228 | arr2.push_back(6); |
229 | |
230 | arr.swap(arr2); |
231 | |
232 | ASSERT_CHECK((arr.size() == 3), res); |
233 | ASSERT_CHECK((arr[0] == 4), res); |
234 | ASSERT_CHECK((arr[1] == 5), res); |
235 | ASSERT_CHECK((arr[2] == 6), res); |
236 | |
237 | ASSERT_CHECK((arr2.size() == 3), res); |
238 | ASSERT_CHECK((arr2[0] == 1), res); |
239 | ASSERT_CHECK((arr2[1] == 2), res); |
240 | ASSERT_CHECK((arr2[2] == 3), res); |
241 | |
242 | arr.swap(arr2); |
243 | |
244 | ASSERT_CHECK((arr.size() == 3), res); |
245 | ASSERT_CHECK((arr[0] == 1), res); |
246 | ASSERT_CHECK((arr[1] == 2), res); |
247 | ASSERT_CHECK((arr[2] == 3), res); |
248 | |
249 | ASSERT_CHECK((arr2.size() == 3), res); |
250 | ASSERT_CHECK((arr2[0] == 4), res); |
251 | ASSERT_CHECK((arr2[1] == 5), res); |
252 | ASSERT_CHECK((arr2[2] == 6), res); |
253 | } |
254 | |
255 | { |
256 | Array arr; |
257 | |
258 | arr.push_back(1); |
259 | arr.push_back(2); |
260 | |
261 | Array arr2; |
262 | |
263 | arr2.push_back(3); |
264 | arr2.push_back(4); |
265 | arr2.push_back(5); |
266 | |
267 | arr.swap(arr2); |
268 | |
269 | ASSERT_CHECK((arr.size() == 3), res); |
270 | ASSERT_CHECK((arr[0] == 3), res); |
271 | ASSERT_CHECK((arr[1] == 4), res); |
272 | ASSERT_CHECK((arr[2] == 5), res); |
273 | |
274 | ASSERT_CHECK((arr2.size() == 2), res); |
275 | ASSERT_CHECK((arr2[0] == 1), res); |
276 | ASSERT_CHECK((arr2[1] == 2), res); |
277 | |
278 | arr.swap(arr2); |
279 | |
280 | ASSERT_CHECK((arr.size() == 2), res); |
281 | ASSERT_CHECK((arr[0] == 1), res); |
282 | ASSERT_CHECK((arr[1] == 2), res); |
283 | |
284 | ASSERT_CHECK((arr2.size() == 3), res); |
285 | ASSERT_CHECK((arr2[0] == 3), res); |
286 | ASSERT_CHECK((arr2[1] == 4), res); |
287 | ASSERT_CHECK((arr2[2] == 5), res); |
288 | } |
289 | |
290 | { |
291 | Array arr; |
292 | |
293 | arr.push_back(1); |
294 | arr.push_back(2); |
295 | arr.push_back(3); |
296 | |
297 | Array arr2; |
298 | |
299 | arr2.push_back(4); |
300 | arr2.push_back(5); |
301 | arr2.push_back(6); |
302 | arr2.push_back(7); |
303 | arr2.push_back(8); |
304 | |
305 | arr.swap(arr2); |
306 | |
307 | ASSERT_CHECK((arr.size() == 5), res); |
308 | ASSERT_CHECK((arr[0] == 4), res); |
309 | ASSERT_CHECK((arr[1] == 5), res); |
310 | ASSERT_CHECK((arr[2] == 6), res); |
311 | ASSERT_CHECK((arr[3] == 7), res); |
312 | ASSERT_CHECK((arr[4] == 8), res); |
313 | |
314 | ASSERT_CHECK((arr2.size() == 3), res); |
315 | ASSERT_CHECK((arr2[0] == 1), res); |
316 | ASSERT_CHECK((arr2[1] == 2), res); |
317 | ASSERT_CHECK((arr2[2] == 3), res); |
318 | |
319 | arr.swap(arr2); |
320 | |
321 | ASSERT_CHECK((arr.size() == 3), res); |
322 | ASSERT_CHECK((arr[0] == 1), res); |
323 | ASSERT_CHECK((arr[1] == 2), res); |
324 | ASSERT_CHECK((arr[2] == 3), res); |
325 | |
326 | ASSERT_CHECK((arr2.size() == 5), res); |
327 | ASSERT_CHECK((arr2[0] == 4), res); |
328 | ASSERT_CHECK((arr2[1] == 5), res); |
329 | ASSERT_CHECK((arr2[2] == 6), res); |
330 | ASSERT_CHECK((arr2[3] == 7), res); |
331 | ASSERT_CHECK((arr2[4] == 8), res); |
332 | } |
333 | |
334 | { |
335 | Array arr; |
336 | |
337 | arr.push_back(1); |
338 | arr.push_back(2); |
339 | arr.push_back(3); |
340 | arr.push_back(4); |
341 | arr.push_back(5); |
342 | |
343 | Array arr2; |
344 | |
345 | arr2.push_back(6); |
346 | arr2.push_back(7); |
347 | arr2.push_back(8); |
348 | arr2.push_back(9); |
349 | arr2.push_back(10); |
350 | |
351 | arr.swap(arr2); |
352 | |
353 | ASSERT_CHECK((arr.size() == 5), res); |
354 | ASSERT_CHECK((arr[0] == 6), res); |
355 | ASSERT_CHECK((arr[1] == 7), res); |
356 | ASSERT_CHECK((arr[2] == 8), res); |
357 | ASSERT_CHECK((arr[3] == 9), res); |
358 | ASSERT_CHECK((arr[4] == 10), res); |
359 | |
360 | ASSERT_CHECK((arr2.size() == 5), res); |
361 | ASSERT_CHECK((arr2[0] == 1), res); |
362 | ASSERT_CHECK((arr2[1] == 2), res); |
363 | ASSERT_CHECK((arr2[2] == 3), res); |
364 | ASSERT_CHECK((arr2[3] == 4), res); |
365 | ASSERT_CHECK((arr2[4] == 5), res); |
366 | |
367 | arr.swap(arr2); |
368 | |
369 | ASSERT_CHECK((arr.size() == 5), res); |
370 | ASSERT_CHECK((arr[0] == 1), res); |
371 | ASSERT_CHECK((arr[1] == 2), res); |
372 | ASSERT_CHECK((arr[2] == 3), res); |
373 | ASSERT_CHECK((arr[3] == 4), res); |
374 | ASSERT_CHECK((arr[4] == 5), res); |
375 | |
376 | ASSERT_CHECK((arr2.size() == 5), res); |
377 | ASSERT_CHECK((arr2[0] == 6), res); |
378 | ASSERT_CHECK((arr2[1] == 7), res); |
379 | ASSERT_CHECK((arr2[2] == 8), res); |
380 | ASSERT_CHECK((arr2[3] == 9), res); |
381 | ASSERT_CHECK((arr2[4] == 10), res); |
382 | } |
383 | |
384 | if (!res) |
385 | std::cerr << "Some errors were found in test 2\n" ; |
386 | } |
387 | |
388 | static void test3() |
389 | { |
390 | using namespace DB; |
391 | |
392 | static constexpr size_t initial_size = 8; |
393 | static constexpr size_t stack_threshold = 32; |
394 | using Array = PODArray<UInt64, initial_size, AllocatorWithStackMemory<Allocator<false>, stack_threshold>>; |
395 | |
396 | bool res = true; |
397 | |
398 | { |
399 | Array arr; |
400 | Array arr2{std::move(arr)}; |
401 | } |
402 | |
403 | { |
404 | Array arr; |
405 | |
406 | arr.push_back(1); |
407 | arr.push_back(2); |
408 | arr.push_back(3); |
409 | |
410 | Array arr2{std::move(arr)}; |
411 | |
412 | ASSERT_CHECK((arr.size() == 0), res); |
413 | |
414 | ASSERT_CHECK((arr2.size() == 3), res); |
415 | ASSERT_CHECK((arr2[0] == 1), res); |
416 | ASSERT_CHECK((arr2[1] == 2), res); |
417 | ASSERT_CHECK((arr2[2] == 3), res); |
418 | } |
419 | |
420 | { |
421 | Array arr; |
422 | |
423 | arr.push_back(1); |
424 | arr.push_back(2); |
425 | arr.push_back(3); |
426 | arr.push_back(4); |
427 | arr.push_back(5); |
428 | |
429 | Array arr2{std::move(arr)}; |
430 | |
431 | ASSERT_CHECK((arr.size() == 0), res); |
432 | |
433 | ASSERT_CHECK((arr2.size() == 5), res); |
434 | ASSERT_CHECK((arr2[0] == 1), res); |
435 | ASSERT_CHECK((arr2[1] == 2), res); |
436 | ASSERT_CHECK((arr2[2] == 3), res); |
437 | ASSERT_CHECK((arr2[3] == 4), res); |
438 | ASSERT_CHECK((arr2[4] == 5), res); |
439 | } |
440 | |
441 | if (!res) |
442 | std::cerr << "Some errors were found in test 3\n" ; |
443 | } |
444 | |
445 | int main() |
446 | { |
447 | std::cout << "test 1\n" ; |
448 | test1(); |
449 | std::cout << "test 2\n" ; |
450 | test2(); |
451 | std::cout << "test 3\n" ; |
452 | test3(); |
453 | |
454 | return 0; |
455 | } |
456 | |