Aleph-w 7.10.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
dyndlist.cc
Go to the documentation of this file.
1
2/*
3 Aleph_w
4
5 Data structures & Algorithms
6 version 2.0.0b
7 https://github.com/lrleon/Aleph-w
8
9 This file is part of Aleph-w library
10
11 Copyright (c) 2002-2026 Leandro Rabindranath Leon
12
13 Permission is hereby granted, free of charge, to any person obtaining a copy
14 of this software and associated documentation files (the "Software"), to deal
15 in the Software without restriction, including without limitation the rights
16 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
17 copies of the Software, and to permit persons to whom the Software is
18 furnished to do so, subject to the following conditions:
19
20 The above copyright notice and this permission notice shall be included in all
21 copies or substantial portions of the Software.
22
23 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
24 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
26 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
28 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
29 SOFTWARE.
30*/
31
32
33
38# include <gtest/gtest.h>
39
40# include <stdexcept>
41# include <type_traits>
42# include <utility>
43
44# include <tpl_dynDlist.H>
45# include <ah-unique.H>
46# include <ah-convert.H>
47
48using namespace std;
49using namespace testing;
50using namespace Aleph;
51
52# define Declare_list_n_items(num) \
53 struct List_of_##num##_items : public Test \
54 { \
55 size_t n = 0; \
56 DynDlist<int> list##num; \
57 DynDlist<int> rlist##num; \
58 List_of_##num##_items() \
59 { \
60 for (size_t i = 0; i < num; ++i, ++n) \
61 list##num.append(i + 1); \
62 \
63 const DynDlist<int> l = list##num; \
64 rlist##num = l.rev(); \
65 } \
66 };
67
70
71TEST(DynDlist, Basic_operations)
72{
73 DynDlist<int> list;
74 EXPECT_TRUE(list.is_empty());
75 EXPECT_FALSE(list.is_unitarian());
76 EXPECT_TRUE(list.is_unitarian_or_empty());
77
78 list.append(2);
79 EXPECT_FALSE(list.is_empty());
80 EXPECT_TRUE(list.is_unitarian());
81 EXPECT_TRUE(list.is_unitarian_or_empty());
82 EXPECT_EQ(list.get_first(), list.get_last());
83
84 list.insert(1);
85 EXPECT_FALSE(list.is_empty());
86 EXPECT_FALSE(list.is_unitarian());
87 EXPECT_FALSE(list.is_unitarian_or_empty());
88
89 EXPECT_EQ(list.get_first(), 1);
90 EXPECT_EQ(list.get_last(), 2);
91
92 list.insert(0);
93 list.append(3);
94 EXPECT_EQ(list.get_first(), 0);
95 EXPECT_EQ(list.get_last(), 3);
96
97 EXPECT_EQ(list.remove_first(), 0);
98 EXPECT_EQ(list.size(), 3);
99 EXPECT_EQ(list.get_last(), 3);
100
101 EXPECT_EQ(list.remove_first(), 1);
102 EXPECT_EQ(list.size(), 2);
103 EXPECT_EQ(list.get_last(), 3);
104
105 EXPECT_EQ(list.remove_first(), 2);
106 EXPECT_EQ(list.size(), 1);
107 EXPECT_EQ(list.get_last(), 3);
108
109 EXPECT_TRUE(list.is_unitarian());
110 EXPECT_TRUE(list.is_unitarian_or_empty());
111
112 EXPECT_EQ(list.remove_first(), 3);
113 EXPECT_TRUE(list.is_empty());
114 EXPECT_EQ(list.size(), 0);
115
116 EXPECT_THROW(list.rotate_left(1), std::domain_error);
117 EXPECT_NO_THROW(list.rotate_left(0));
118}
119
120TEST(DynDlist, Simple_append_and_insert_of_list)
121{
122 { // by copy
123 DynDlist<int> laux, list;
124 laux.insert(2); // laux = { 2 }
125 list.append(laux); // list = { 2 }
126
127 EXPECT_FALSE(laux.is_empty());
128 EXPECT_FALSE(list.is_empty());
129 EXPECT_EQ(list.get_first(), 2);
130 EXPECT_EQ(list.get_first(), laux.get_first());
131 EXPECT_TRUE(list.is_unitarian());
132
133 laux.insert(1); // laux = { 1, 2 }
134 list.insert(laux); // list = { 1, 2, 2 }
135 EXPECT_FALSE(laux.is_empty());
136 EXPECT_EQ(laux.get_first(), 1);
137 EXPECT_EQ(laux.get_last(), 2);
138 EXPECT_EQ(list.size(), 3);
139 EXPECT_EQ(list.get_first(), 1);
140 EXPECT_EQ(list.get_last(), 2);
141 }
142
143 { // by move
144 DynDlist<int> laux, list;
145 laux.insert(2);
146 list.append(move(laux));
147
148 EXPECT_TRUE(laux.is_empty());
149 EXPECT_FALSE(list.is_empty());
150 EXPECT_EQ(list.get_first(), 2);
151 EXPECT_EQ(list.get_last(), 2);
152 EXPECT_TRUE(list.is_unitarian());
153 EXPECT_TRUE(list.is_unitarian_or_empty());
154
155 laux.insert(1);
156 list.insert(move(laux));
157 EXPECT_TRUE(laux.is_empty());
158 EXPECT_EQ(list.size(), 2);
159 EXPECT_EQ(list.get_first(), 1);
160 EXPECT_EQ(list.get_last(), 2);
161 }
162}
163
164TEST(DynDlistAlgorithms, InPlaceUnique)
165{
166 DynDlist<int> list;
167 list.append(1);
168 list.append(2);
169 list.append(1);
170 list.append(3);
171 list.append(2);
172 list.append(4);
173 list.append(4);
174
175 in_place_unique(list);
176
177 ASSERT_EQ(list.size(), 4u);
178 auto it = list.get_it();
179 ASSERT_TRUE(it.has_curr()); EXPECT_EQ(it.get_curr(), 1); it.next();
180 ASSERT_TRUE(it.has_curr()); EXPECT_EQ(it.get_curr(), 2); it.next();
181 ASSERT_TRUE(it.has_curr()); EXPECT_EQ(it.get_curr(), 3); it.next();
182 ASSERT_TRUE(it.has_curr()); EXPECT_EQ(it.get_curr(), 4); it.next();
183 EXPECT_FALSE(it.has_curr());
184}
185
186TEST_F(List_of_10_items, copy_and_assignment)
187{
188 { // copy constructor
189 auto tmp = list10;
190
191 EXPECT_EQ(tmp.size(), list10.size());
192 EXPECT_EQ(tmp.size(), 10);
193
194 auto it1 = list10.get_it();
195 auto it2 = tmp.get_it();
196 for (; it1.has_curr() and it2.has_curr(); it1.next(), it2.next())
197 ASSERT_EQ(it1.get_curr(), it2.get_curr());
198
199 EXPECT_FALSE(it1.has_curr());
200 EXPECT_FALSE(it2.has_curr());
201 }
202
203 EXPECT_FALSE(list10.is_empty());
204
205 { // move constructor
206 auto tmp = move(list10);
207
208 EXPECT_TRUE(list10.is_empty());
209 EXPECT_EQ(list10.size(), 0);
210 EXPECT_EQ(tmp.size(), 10);
211 int i = 1;
212 for (auto it = tmp.get_it(); it.has_curr(); it.next(), ++i)
213 ASSERT_EQ(it.get_curr(), i);
214 EXPECT_EQ(i, 11);
215 list10.swap(tmp);
216 }
217
218 EXPECT_FALSE(list10.is_empty());
219 DynDlist<int> aux;
220 aux = list10;
221 EXPECT_EQ(aux.size(), 10);
222 int i = 1;
223 for (auto it = aux.get_it(); it.has_curr(); it.next(), ++i)
224 ASSERT_EQ(it.get_curr(), i);
225 EXPECT_EQ(i, 11);
226
227 aux.empty();
228 EXPECT_TRUE(aux.is_empty());
229
230 aux = move(list10);
231 EXPECT_TRUE(list10.is_empty());
232 EXPECT_EQ(aux.size(), 10);
233
234 i = 1;
235 for (auto it = aux.get_it(); it.has_curr(); it.next(), ++i)
236 ASSERT_EQ(it.get_curr(), i);
237 EXPECT_EQ(i, 11);
238
239 list10 = move(aux);
240
241 EXPECT_TRUE(aux.is_empty());
242 EXPECT_EQ(list10.size(), 10);
243 i = 1;
244 for (auto it = list10.get_it(); it.has_curr(); it.next(), ++i)
245 ASSERT_EQ(it.get_curr(), i);
246 EXPECT_EQ(i, 11);
247}
248
249TEST_F(List_of_25_items, Basic_operations)
250{
251 EXPECT_EQ(list25.get_first(), 1);
252 EXPECT_EQ(list25.get_last(), 25);
253 EXPECT_EQ(list25.size(), 25);
254 EXPECT_FALSE(list25.is_empty());
255 EXPECT_FALSE(list25.is_unitarian());
256 EXPECT_FALSE(list25.is_unitarian_or_empty());
257}
258
259TEST_F(List_of_25_items, Iterator_operations)
260{
261 list25.for_each([] (auto i) { cout << i << " "; }); cout << endl;
262 int i = 1;
263 auto it = list25.get_it();
264 for (; it.has_curr(); it.next(), ++i)
265 {
266 EXPECT_EQ(it.get_curr(), i);
267 EXPECT_EQ(it.get_pos(), i - 1);
268 }
269 EXPECT_EQ(i, 26);
270
271 i = 25;
272 it.reset_last();
273 for (; it.has_curr(); it.prev(), --i)
274 {
275 EXPECT_EQ(it.get_curr(), i);
276 EXPECT_EQ(it.get_pos(), i - 1);
277 }
278 EXPECT_EQ(i, 0);
279}
280
281TEST_F(List_of_25_items, Split_and_concat)
282{
284 list25.split(l, r);
285
286 EXPECT_TRUE(list25.is_empty());
287 EXPECT_EQ(l.size(), 13);
288 EXPECT_EQ(r.size(), 12);
289 EXPECT_EQ(l.get_first(), 1);
290 EXPECT_EQ(l.get_last(), 13);
291 EXPECT_EQ(r.get_first(), 14);
292 EXPECT_EQ(r.get_last(), 25);
293
294 int i = 1;
295 for (auto it = l.get_it(); it.has_curr(); it.next(), ++i)
296 EXPECT_EQ(it.get_curr(), i);
297
298 for (auto it = r.get_it(); it.has_curr(); it.next(), ++i)
299 EXPECT_EQ(it.get_curr(), i);
300
301 list25.append(r);
302 list25.insert(l);
303 i = 1;
304 for (auto it = l.get_it(); it.has_curr(); it.next(), ++i)
305 EXPECT_EQ(it.get_curr(), i);
306}
307
309{
310 DynDlist<int> laux;
311 laux.swap(list25);
312
313 EXPECT_TRUE(list25.is_empty());
314 EXPECT_EQ(list25.size(), 0);
315 EXPECT_FALSE(laux.is_empty());
316
317 int i = 1;
318 for (auto it = laux.get_it(); it.has_curr(); it.next(), ++i)
319 EXPECT_EQ(it.get_curr(), i);
320}
321
323{
324 list25.reverse();
325 int i = 25;
326 for (auto it = list25.get_it(); it.has_curr(); it.next(), --i)
327 EXPECT_EQ(it.get_curr(), i);
328
329 list25.reverse();
330
331 i = 1;
332 for (auto it = list25.get_it(); it.has_curr(); it.next(), ++i)
333 EXPECT_EQ(it.get_curr(), i);
334
336 list25.split(l, r);
337
338 EXPECT_TRUE(list25.is_empty());
339
340 l.reverse();
341 r.reverse();
342 list25.insert(l);
343 list25.insert(r);
344
345 list25.reverse();
346 i = 1;
347 for (auto it = list25.get_it(); it.has_curr(); it.next(), ++i)
348 EXPECT_EQ(it.get_curr(), i);
349}
350
352{
353 list25.rotate_left(3);
354
355 {
356 auto it = list25.get_it();
357 for (int i = 0, n = 4; i < 3; ++i, ++n, it.next())
358 EXPECT_EQ(it.get_curr(), n);
359 }
360
361 list25.rotate_left(22);
362 int i = 1;
363 for (auto it = list25.get_it(); it.has_curr(); it.next(), ++i)
364 EXPECT_EQ(it.get_curr(), i);
365}
366
368{
369 list25.rotate_right(3);
370
371 {
372 auto it = list25.get_it();
373 for (int i = 0, n = 23; i < 3; ++i, ++n, it.next())
374 EXPECT_EQ(it.get_curr(), n);
375 }
376
377 list25.rotate_right(22);
378 int i = 1;
379 for (auto it = list25.get_it(); it.has_curr(); it.next(), ++i)
380 EXPECT_EQ(it.get_curr(), i);
381}
382
383TEST_F(List_of_25_items, append_and_insert_by_moving)
384{
385 DynDlist<int> ll = { 0, -1, -2, -3, -4, -5, -6, -7, -8, -9 };
386 DynDlist<int> lg = { 26, 27, 28, 29, 30, 31, 32, 33, 34, 35 };
387
388 ll.reverse();
389
390 list25.insert(std::move(ll));
391 list25.append(std::move(lg));
392
393 EXPECT_TRUE(ll.is_empty());
394 EXPECT_TRUE(lg.is_empty());
395
396 int i = -9;
397 for (auto it = list25.get_it(); it.has_curr(); it.next(), ++i)
398 EXPECT_EQ(it.get_curr(), i);
399}
400
401TEST(DynDlist, traverse_on_empty_container)
402{
404 size_t n = 0;
405 auto ret = m.traverse([&n] (int) { ++n; return true; });
406 EXPECT_TRUE(ret);
407 EXPECT_EQ(n, 0);
408}
409
411{
412 EXPECT_TRUE(list25.size() > 0);
413 EXPECT_EQ(list25.size(), n);
414 int N = 0;
415 auto ret = list25.traverse([&N, this] (int i) { ++N; return i < n/2; });
416 EXPECT_FALSE(ret);
417 EXPECT_EQ(N, n/2);
418}
419
420TEST(DynDlist, RemoveAndEraseByReference)
421{
422 DynDlist<int> list;
423 for (int i = 1; i <= 4; ++i)
424 list.append(i);
425
426 int &middle = list[2];
427 list.remove(middle);
428 ASSERT_EQ(list.size(), 3u);
429 int expected_after_remove[] = {1, 2, 4};
430 int idx = 0;
431 for (auto it = list.get_it(); it.has_curr(); it.next(), ++idx)
432 EXPECT_EQ(it.get_curr(), expected_after_remove[idx]);
433
434 int &first = list.get_first();
435 list.erase(first);
436 EXPECT_EQ(list.size(), 2u);
437 EXPECT_EQ(list.get_first(), 2);
438 EXPECT_EQ(list.get_last(), 4);
439}
440
441TEST(DynDlist, OperatorIndexEnforcesBounds)
442{
443 DynDlist<int> list;
444 for (int i = 0; i < 5; ++i)
445 list.append(i);
446
447 list[2] = 99;
448 EXPECT_EQ(list[2], 99);
449
450 const DynDlist<int> &clist = list;
451 EXPECT_EQ(clist[0], 0);
452
453 EXPECT_THROW(list[5], std::out_of_range);
454 EXPECT_THROW(list[10], std::out_of_range);
455
456 DynDlist<int> empty;
457 EXPECT_THROW(empty[0], std::out_of_range);
458}
459
460TEST_F(List_of_10_items, ConstReverseReturnsIndependentCopy)
461{
462 const DynDlist<int> &cref = list10;
463 auto reversed = cref.reverse();
464
465 int expected_desc = 10;
466 for (auto it = reversed.get_it(); it.has_curr(); it.next(), --expected_desc)
467 EXPECT_EQ(it.get_curr(), expected_desc);
468 EXPECT_EQ(expected_desc, 0);
469
470 int expected_orig = 1;
471 for (auto it = cref.get_it(); it.has_curr(); it.next(), ++expected_orig)
472 EXPECT_EQ(it.get_curr(), expected_orig);
473 EXPECT_EQ(expected_orig, 11);
474
475 auto reversed_alias = cref.rev();
476 int expected_desc_alias = 10;
477 for (auto it = reversed_alias.get_it(); it.has_curr(); it.next(), --expected_desc_alias)
478 EXPECT_EQ(it.get_curr(), expected_desc_alias);
479 EXPECT_EQ(expected_desc_alias, 0);
480}
481
482TEST(DynDlist, IteratorInsertAndAppendOperations)
483{
484 DynDlist<int> list = {1, 3};
485 auto it = list.get_it();
486 ASSERT_TRUE(it.has_curr());
487
488 it.insert(2);
489 int expected_after_insert[] = {1, 2, 3};
490 int idx = 0;
491 for (auto iter = list.get_it(); iter.has_curr(); iter.next(), ++idx)
492 EXPECT_EQ(iter.get_curr(), expected_after_insert[idx]);
493
494 it.reset_first();
495 it.append(0);
496 int expected_after_append[] = {0, 1, 2, 3};
497 idx = 0;
498 for (auto iter = list.get_it(); iter.has_curr(); iter.next(), ++idx)
499 EXPECT_EQ(iter.get_curr(), expected_after_append[idx]);
500
501 auto at_end = list.get_it();
502 at_end.end();
503 EXPECT_THROW(at_end.insert(42), std::overflow_error);
504 EXPECT_THROW(at_end.append(42), std::overflow_error);
505}
506
507TEST(DynDlist, IteratorListOperations)
508{
509 DynDlist<int> base = {1, 4};
510 DynDlist<int> middle = {2, 3};
511 DynDlist<int> head = {-1, 0};
512
513 auto it = base.get_it();
514 ASSERT_TRUE(it.has_curr());
515 it.insert_list(middle);
516 EXPECT_TRUE(middle.is_empty());
517
518 int expected_after_insert_list[] = {1, 2, 3, 4};
519 int idx = 0;
520 for (auto iter = base.get_it(); iter.has_curr(); iter.next(), ++idx)
521 EXPECT_EQ(iter.get_curr(), expected_after_insert_list[idx]);
522
523 it.reset_first();
524 it.append_list(head);
525 EXPECT_TRUE(head.is_empty());
526
527 int expected_after_append_list[] = {-1, 0, 1, 2, 3, 4};
528 idx = 0;
529 for (auto iter = base.get_it(); iter.has_curr(); iter.next(), ++idx)
530 EXPECT_EQ(iter.get_curr(), expected_after_append_list[idx]);
531}
532
533TEST(DynDlist, IteratorListOperationsRequireCurrent)
534{
535 DynDlist<int> base = {1, 2, 3};
536 DynDlist<int> extra = {4, 5};
537
538 auto it = base.get_it();
539 it.end();
540 EXPECT_THROW(it.insert_list(extra), std::overflow_error);
541 EXPECT_THROW(it.append_list(extra), std::overflow_error);
542
543 // ni la lista base ni la extra deberían alterarse tras los errores
544 int expected_base[] = {1, 2, 3};
545 int idx = 0;
546 for (auto iter = base.get_it(); iter.has_curr(); iter.next(), ++idx)
547 EXPECT_EQ(iter.get_curr(), expected_base[idx]);
548 EXPECT_FALSE(extra.is_empty());
549 EXPECT_EQ(extra.size(), 2u);
550}
551
552// The sizes of the halves are those of the items that they take, the first
553// n - n / 2 and the others, in their order; the sizes stay right as items
554// come and go
555TEST(DynDlist, SplitGivesTheSizesOfTheHalves)
556{
557 for (int n = 0; n <= 9; ++n)
558 {
559 DynDlist<int> list, l, r;
560 for (int i = 0; i < n; ++i)
561 list.append(i);
562
563 if (n % 2 == 0)
564 list.split(l, r);
565 else
566 list.split_list_ne(l, r);
567
568 EXPECT_TRUE(list.is_empty());
569 EXPECT_EQ(list.size(), 0u);
570 ASSERT_EQ(l.size(), size_t(n - n / 2)) << "n = " << n;
571 ASSERT_EQ(r.size(), size_t(n / 2)) << "n = " << n;
572
573 int i = 0;
574 for (auto it = l.get_it(); it.has_curr(); it.next_ne(), ++i)
575 EXPECT_EQ(it.get_curr(), i);
576 EXPECT_EQ(i, n - n / 2);
577 for (auto it = r.get_it(); it.has_curr(); it.next_ne(), ++i)
578 EXPECT_EQ(it.get_curr(), i);
579 EXPECT_EQ(i, n);
580
581 l.append(n);
582 r.insert(-1);
583 EXPECT_EQ(l.size(), size_t(n - n / 2 + 1));
584 EXPECT_EQ(r.size(), size_t(n / 2 + 1));
585 while (not l.is_empty())
586 l.remove_first();
587 while (not r.is_empty())
588 r.remove_first();
589 EXPECT_EQ(l.size(), 0u);
590 EXPECT_EQ(r.size(), 0u);
591 }
592}
593
594TEST(DynDlist, SplitRequiresEmptyDestination)
595{
596 DynDlist<int> source = {1, 2, 3};
597 DynDlist<int> left;
598 DynDlist<int> right;
599
600 left.append(42);
601 EXPECT_THROW(source.split(left, right), std::domain_error);
602
603 left.empty();
604 right.append(99);
605 EXPECT_THROW(source.split(left, right), std::domain_error);
606}
607
608TEST(DynDlist, StackAndQueueAliases)
609{
610 DynDlist<int> list;
611 list.push(3);
612 list.push(2);
613 list.push(1);
614
615 EXPECT_EQ(list.top(), 1);
616 EXPECT_EQ(list.pop(), 1);
617 EXPECT_EQ(list.top(), 2);
618
619 list.put(99);
620 EXPECT_EQ(list.rear(), 99);
621 EXPECT_EQ(list.front(), 2);
622 EXPECT_EQ(list.get(), 2);
623 EXPECT_EQ(list.front(), 3);
624
625 list.pop();
626 list.pop();
627 EXPECT_TRUE(list.is_empty());
628 EXPECT_THROW(list.pop(), std::underflow_error);
629}
630
631TEST(DynDlistFunctional, ToArrayHelperPreservesOrder)
632{
633 DynDlist<int> list;
634 for (int v : {2, 4, 8})
635 list.append(v);
636
637 auto arr = to_array(list);
638
639 ASSERT_EQ(arr.size(), list.size());
640 size_t i = 0;
641 for (auto it = list.get_it(); it.has_curr(); it.next_ne(), ++i)
642 EXPECT_EQ(arr(i), it.get_curr());
643}
644
645namespace
646{
647 // Counts its copies; its move cannot throw
648 struct Copy_Counted
649 {
650 static inline int copies = 0;
651 int value = 0;
652
653 Copy_Counted(int v = 0) noexcept : value(v) {}
654 Copy_Counted(const Copy_Counted & c) : value(c.value) { ++copies; }
655 Copy_Counted(Copy_Counted && c) noexcept : value(c.value) {}
656 Copy_Counted & operator = (const Copy_Counted & c)
657 {
658 value = c.value;
659 ++copies;
660 return *this;
661 }
662 Copy_Counted & operator = (Copy_Counted && c) noexcept
663 {
664 value = c.value;
665 return *this;
666 }
667 };
668
669 // Its move throws while fail is true; moves counts them
670 struct Fragile_Move
671 {
672 static inline bool fail = false;
673 static inline int moves = 0;
674 int value = 0;
675
676 Fragile_Move(int v = 0) noexcept : value(v) {}
677 Fragile_Move(const Fragile_Move & c) = default;
678 Fragile_Move(Fragile_Move && c) : value(c.value)
679 {
680 ++moves;
681 if (fail)
682 throw std::runtime_error("move failed");
683 }
684 Fragile_Move & operator = (const Fragile_Move & c) = default;
685 Fragile_Move & operator = (Fragile_Move && c) = default;
686 };
687} // namespace
688
689// They copied the item, in a function that was noexcept: a copy that threw
690// ended the program
691static_assert(noexcept(std::declval<DynDlist<int> &>().remove_first_ne()));
692static_assert(noexcept(std::declval<DynDlist<int> &>().remove_last_ne()));
693static_assert(not noexcept(std::declval<DynDlist<Fragile_Move> &>().remove_first_ne()));
694static_assert(not noexcept(std::declval<DynDlist<Fragile_Move> &>().remove_last_ne()));
695
696TEST(DynDlist, RemovingAnEndMovesTheItem)
697{
699 for (int i = 1; i <= 4; ++i)
700 list.append(Copy_Counted(i));
701
702 Copy_Counted::copies = 0;
703 EXPECT_EQ(list.remove_first_ne().value, 1);
704 EXPECT_EQ(list.remove_last_ne().value, 4);
705 EXPECT_EQ(list.remove_first().value, 2);
706 EXPECT_EQ(list.remove_last().value, 3);
707 EXPECT_EQ(Copy_Counted::copies, 0);
708 EXPECT_TRUE(list.is_empty());
709 EXPECT_EQ(list.size(), 0u);
710}
711
712// The item moves out before its node leaves the list
713TEST(DynDlist, AMoveThatThrowsLeavesTheItemInTheList)
714{
716 for (int i = 1; i <= 3; ++i)
717 list.append(Fragile_Move(i));
718
719 Fragile_Move::fail = true;
720 EXPECT_THROW((void) list.remove_first_ne(), std::runtime_error);
721 EXPECT_THROW((void) list.remove_last_ne(), std::runtime_error);
722 Fragile_Move::fail = false;
723
724 ASSERT_EQ(list.size(), 3u);
725 EXPECT_EQ(list.get_first().value, 1);
726 EXPECT_EQ(list.get_last().value, 3);
727 EXPECT_EQ(list.remove_first_ne().value, 1);
728 EXPECT_EQ(list.remove_last_ne().value, 3);
729 EXPECT_EQ(list.size(), 1u);
730}
731
732// The item moves once, straight into the result: a second move, as that of
733// a named result that the compiler does not elide, would come after the
734// node left, and if it threw the item would be lost
735TEST(DynDlist, ARemoveMovesTheItemOnce)
736{
738 for (int i = 1; i <= 4; ++i)
739 list.append(Fragile_Move(i));
740
741 Fragile_Move::moves = 0;
742 const Fragile_Move first = list.remove_first_ne();
743 EXPECT_EQ(Fragile_Move::moves, 1);
744 const Fragile_Move last = list.remove_last_ne();
745 EXPECT_EQ(Fragile_Move::moves, 2);
746 EXPECT_EQ(first.value, 1);
747 EXPECT_EQ(last.value, 4);
748 EXPECT_EQ(list.size(), 2u);
749}
750
751TEST(DynDlist, EqualityOperator)
752{
753 DynDlist<int> l1 = {1, 2, 3};
754 DynDlist<int> l2 = {1, 2, 3};
755 DynDlist<int> l3 = {1, 2, 4};
756 DynDlist<int> l4 = {1, 2};
757
758 EXPECT_TRUE(l1 == l2);
759 EXPECT_FALSE(l1 != l2);
760
761 EXPECT_FALSE(l1 == l3);
762 EXPECT_TRUE(l1 != l3);
763
764 EXPECT_FALSE(l1 == l4);
765 EXPECT_TRUE(l1 != l4);
766
767 DynDlist<int> empty1, empty2;
768 EXPECT_TRUE(empty1 == empty2);
769}
770
771namespace
772{
773 // Counts its default constructions
774 struct Default_Counted
775 {
776 static inline int defaults = 0;
777 int value = 0;
778
779 Default_Counted() { ++defaults; }
780 Default_Counted(int v) noexcept : value(v) {}
781 };
782} // namespace
783
784namespace
785{
786 // Its default construction may throw, and its move throws while fail is
787 // true
788 struct Fragile_Default_And_Move
789 {
790 static inline bool fail = false;
791 int value = 0;
792
793 Fragile_Default_And_Move() {}
794 Fragile_Default_And_Move(int v) noexcept : value(v) {}
795 Fragile_Default_And_Move(const Fragile_Default_And_Move &) = default;
796 Fragile_Default_And_Move(Fragile_Default_And_Move && c) : value(c.value)
797 {
798 if (fail)
799 throw std::runtime_error("move failed");
800 }
801 Fragile_Default_And_Move & operator = (const Fragile_Default_And_Move &) = default;
802 };
803} // namespace
804
805// The move built the header node of the new list with an item built by
806// default, in a constructor that was noexcept: an item whose default
807// construction throws, as one that allocates, ended the program. For such
808// an item the header node now takes the place and the item of that of the
809// other list, so the move is noexcept when that of the item is. When the
810// default construction cannot throw, the move builds the item as before
811static_assert(std::is_nothrow_move_constructible_v<DynDlist<int>>);
812static_assert(std::is_nothrow_move_constructible_v<DynDlist<Fragile_Move>>);
813static_assert(std::is_nothrow_move_constructible_v<DynDlist<Default_Counted>>);
814static_assert(not std::is_nothrow_move_constructible_v<DynDlist<Fragile_Default_And_Move>>);
815
816TEST(DynDlist, AMoveOfTheListBuildsNoItem)
817{
819 for (int i = 1; i <= 5; ++i)
820 source.append(Default_Counted(i));
821
822 Default_Counted::defaults = 0;
823 DynDlist<Default_Counted> target(std::move(source));
824 EXPECT_EQ(Default_Counted::defaults, 0);
825
826 EXPECT_TRUE(source.is_empty());
827 EXPECT_EQ(source.size(), 0u);
828 ASSERT_EQ(target.size(), 5u);
829 int expected = 1;
830 for (auto it = target.get_it(); it.has_curr(); it.next_ne())
831 EXPECT_EQ(it.get_curr().value, expected++);
832
833 // both lists go on as any other
834 source.append(Default_Counted(7));
835 source.insert(Default_Counted(6));
836 target.append(Default_Counted(6));
837 ASSERT_EQ(source.size(), 2u);
838 EXPECT_EQ(source.get_first().value, 6);
839 EXPECT_EQ(source.get_last().value, 7);
840 ASSERT_EQ(target.size(), 6u);
841 EXPECT_EQ(target.get_first().value, 1);
842 EXPECT_EQ(target.get_last().value, 6);
843}
844
845// The item of the header node moves before the nodes change hands
846TEST(DynDlist, AMoveOfTheListThatThrowsLeavesItAsItWas)
847{
848 using Item = Fragile_Default_And_Move;
849 DynDlist<Item> source;
850 for (int i = 1; i <= 3; ++i)
851 source.append(Item(i));
852
853 Item::fail = true;
854 EXPECT_THROW({ DynDlist<Item> target(std::move(source)); }, std::runtime_error);
855 Item::fail = false;
856
857 ASSERT_EQ(source.size(), 3u);
858 int expected = 1;
859 for (auto it = source.get_it(); it.has_curr(); it.next_ne())
860 EXPECT_EQ(it.get_curr().value, expected++);
861}
862
863namespace
864{
865 // An item that cannot be moved, and whose default construction may throw
866 struct Copy_Only
867 {
868 int value = 0;
869
870 Copy_Only() {}
871 Copy_Only(int v) : value(v) {}
872 Copy_Only(const Copy_Only &) = default;
873 Copy_Only(Copy_Only &&) = delete;
874 Copy_Only & operator = (const Copy_Only &) = default;
875 };
876}
877
878static_assert(not std::is_move_constructible_v<Copy_Only>);
879static_assert(std::is_move_constructible_v<DynDlist<Copy_Only>>);
880static_assert(not std::is_nothrow_move_constructible_v<DynDlist<Copy_Only>>);
881
882// A list of items that cannot be moved is moved all the same: the item of
883// its header node, which cannot be taken from that of the other list, is
884// built by default
885TEST(DynDlist, AListOfItemsThatCannotBeMovedIsMoved)
886{
888 DynDlist<Copy_Only> from_empty(std::move(empty));
889 EXPECT_TRUE(from_empty.is_empty());
890 EXPECT_TRUE(empty.is_empty());
891
892 DynDlist<Copy_Only> source;
893 for (int i = 1; i <= 3; ++i)
894 {
895 const Copy_Only item(i);
896 source.append(item);
897 }
898 DynDlist<Copy_Only> target(std::move(source));
899 EXPECT_TRUE(source.is_empty());
900 ASSERT_EQ(target.size(), 3u);
901 int expected = 1;
902 for (auto it = target.get_it(); it.has_curr(); it.next_ne())
903 EXPECT_EQ(it.get_curr().value, expected++);
904
905 const Copy_Only another(9); // the list that was moved from goes on
906 source.append(another);
907 ASSERT_EQ(source.size(), 1u);
908 EXPECT_EQ(source.get_first().value, 9);
909}
Conversion utilities between Aleph-w and STL container types.
Deduplicate sequential Aleph containers in-place.
size_t size_t int32_t value
Definition ca-c-api.h:116
Dynamic doubly linked list with O(1) size and bidirectional access.
const size_t & size() const noexcept
Return the number of elements (constant time)
T & get_first()
Return a modifiable reference to first item in the list.
DynDlist & reverse() noexcept
T & front()
If this was treated as a queue, the it returns the most oldlest inserted item.
T pop()
See remove_first().
T remove_first_ne() noexcept(std::is_nothrow_move_constructible_v< T >)
Remove the first item of the list, which must not be empty.
T remove_first()
Remove the first item of the list; return a copy of removed item.
void swap(DynDlist &l) noexcept
Swap in constant time all the items of this with all the items of l (very fast!)
void remove(T &data) noexcept
Assuming that data is a reference to the item in the list, it removes the item.
T remove_last_ne() noexcept(std::is_nothrow_move_constructible_v< T >)
Remove the last item of the list, which must not be empty.
T & append(const T &item)
Append a copied item at the end of the list.
T & get_last()
Return a modifiable reference to last item in the list.
T & push(const T &item)
See insert(const T & item).
T remove_last()
Remove the last item of the list; return a copy of removed item.
T & put(const T &item)
See append(const T & item).
void empty() noexcept
@brief Empties the container.
DynDlist & rev() noexcept
T & rear()
If this was treated as a queue, the it returns the most recently inserted item.
void split(DynDlist &l, DynDlist &r)
This is an overloaded member function, provided for convenience. It differs from the above function o...
T & top()
See get_first().
void erase(T &data) noexcept
See remove(T & data).
T & insert(const T &item)
Insert a copy of item at the beginning of the list.
T get()
See remove_first().
void split_list_ne(DynDlist &l, DynDlist &r) noexcept
Split the list in two.
T & get_first()
Return the first item of the list.
Definition htlist.H:1548
T & append(const T &item)
Definition htlist.H:1362
T & get_last()
Return the last item of the list.
Definition htlist.H:1529
T remove_first()
See remove().
Definition htlist.H:1473
size_t size() const noexcept
Return the number of items of the list, in constant time.
Definition htlist.H:1264
DynList & reverse() noexcept
Definition htlist.H:1728
std::uint32_t next() noexcept
Definition ah-random.H:203
constexpr bool is_empty() const noexcept
Definition htlist.H:434
Number Theoretic Transform over Z / MOD Z.
Definition ntt.H:115
Minimal std::expected-style result type for C++20.
auto get_it()
Return a properly initialized iterator positioned at the first item on the container.
Definition ah-dry.H:663
#define TEST(name)
TEST_F(List_of_10_items, copy_and_assignment)
Definition dyndlist.cc:186
#define Declare_list_n_items(num)
Definition dyndlist.cc:52
#define N
Definition fib.C:294
bool traverse(Node *root, Op op)
See infix_traverse().
Main namespace for Aleph-w library functions.
Array< typename C::Item_Type > to_array(const C &c)
Convenience wrapper identical to to_Array (copy contents).
Definition ah-convert.H:181
void in_place_unique(Container &c, Compare cmp={})
Remove duplicates in-place preserving first occurrence order.
Definition ah-unique.H:74
void reverse(Itor beg, Itor end)
Reverse elements in a range.
Definition ahAlgo.H:1094
T & swap(T &t1, T &t2)
Generic swap using object's swap method.
Definition ahTypes.H:123
void rotate_left(T *ptr, const size_t n, size_t m) noexcept
Rotate array elements left by m positions.
and
Check uniqueness with explicit hash + equality functors.
void rotate_right(T *ptr, const size_t n, size_t m) noexcept
Rotate array elements right by m positions.
STL namespace.
bool traverse(Operation &operation) noexcept(traverse_is_noexcept< Operation, typename Aleph::concepts_detail::traverse_arg< Container, Operation >::type, Container & >())
Traverse the container via its iterator and performs a conditioned operation on each item.
Definition ah-dry.H:411
FooMap m(5, fst_unit_pair_hash, snd_unit_pair_hash)
DynList< char > l3
DynList< int > l1
DynList< int > l2
Gsl_Compatible_Rng r
Dynamic doubly linked list implementation.
DynList< int > l