Aleph-w 7.10.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
htlist.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 <htlist.H>
41# include <stdexcept>
42# include <utility>
43
44using namespace std;
45using namespace testing;
46using namespace Aleph;
47
48struct List_of_25_nodes : public Test
49{
52 {
53 for (size_t i = 0; i < 25; ++i)
54 list.append(new Snodenc<int>(i + 1));
55 }
60};
61
62TEST(HTList, Basic_operations)
63{
64 HTList list;
65 EXPECT_TRUE(list.is_empty());
66 EXPECT_FALSE(list.is_unitarian());
67 EXPECT_TRUE(list.is_unitarian_or_empty());
68 EXPECT_EQ(list.get_head(), list.get_first());
69 EXPECT_EQ(list.get_tail(), list.get_last());
70
71 list.append(new Snodenc<int>(2));
72 EXPECT_FALSE(list.is_empty());
73 EXPECT_TRUE(list.is_unitarian());
74 EXPECT_TRUE(list.is_unitarian_or_empty());
75 EXPECT_EQ(list.get_head(), list.get_first());
76 EXPECT_EQ(list.get_tail(), list.get_last());
77 EXPECT_EQ(list.get_first(), list.get_last());
78
79 list.insert(new Snodenc<int>(1));
80 EXPECT_FALSE(list.is_empty());
81 EXPECT_FALSE(list.is_unitarian());
82 EXPECT_FALSE(list.is_unitarian_or_empty());
83 EXPECT_EQ(list.get_head(), list.get_first());
84 EXPECT_EQ(list.get_tail(), list.get_last());
85
86 // list = { 1, 2}
87
88 EXPECT_EQ(static_cast<Snodenc<int>*>(list.get_first())->get_data(), 1);
89 EXPECT_EQ(static_cast<Snodenc<int>*>(list.get_last())->get_data(), 2);
90
91 auto p1 = new Snodenc<int>(0);
92 auto p2 = new Snodenc<int>(3);
93 list.insert(p1);
94 list.append(p2);
95 EXPECT_EQ(list.get_first()->to_snodenc<int>()->get_data(), 0);
96 EXPECT_EQ(list.get_last()->to_snodenc<int>()->get_data(), 3);
97
98 // list = { 0, 1, 2, 3}
99
100 auto fst = list.remove_first(); // remove 0
101 EXPECT_EQ(fst, p1);
102 EXPECT_EQ(fst->to_snodenc<int>()->get_data(), 0);
103 delete fst;
104 EXPECT_EQ(list.size(), 3);
105 EXPECT_EQ(list.get_last()->to_data<int>(), 3);
106
107 fst = list.remove_first(); // remove 1
108 EXPECT_EQ(fst->to_data<int>(), 1);
109 delete fst;
110 EXPECT_EQ(list.size(), 2);
111 EXPECT_EQ(list.get_last()->to_data<int>(), 3);
112
113 fst = list.remove_first(); // remove 2
114 EXPECT_EQ(fst->to_data<int>(), 2);
115 delete fst;
116 EXPECT_EQ(list.size(), 1);
117 EXPECT_EQ(list.get_last()->to_data<int>(), 3);
118
119 //list = { 3 }
120
121 EXPECT_EQ(list.get_first()->to_data<int>(), 3);
122 EXPECT_EQ(list.get_last()->to_data<int>(), 3);
123 EXPECT_TRUE(list.is_unitarian());
124 EXPECT_TRUE(list.is_unitarian_or_empty());
125
126 fst = list.remove_first(); // remove 3
127 EXPECT_EQ(fst->to_data<int>(), 3);
128 delete fst;
129 EXPECT_TRUE(list.is_empty());
130 EXPECT_EQ(list.size(), 0);
131
132 EXPECT_THROW(list.rotate_left(1), std::domain_error);
133 EXPECT_NO_THROW(list.rotate_left(0));
134}
135
136TEST(HTList, Simple_append_and_insert_of_list)
137{
138 HTList laux, list;
139 laux.insert(new Snodenc<int>(2));
140 list.append(laux);
141
142 EXPECT_TRUE(laux.is_empty());
143 EXPECT_TRUE(list.is_unitarian());
144
145 laux.insert(new Snodenc<int>(1));
146 list.insert(laux);
147 EXPECT_TRUE(laux.is_empty());
148 EXPECT_EQ(list.size(), 2);
149 EXPECT_EQ(list.get_first()->to_data<int>(), 1);
150 EXPECT_EQ(list.get_last()->to_data<int>(), 2);
151
153}
154
155TEST_F(List_of_25_nodes, Basic_operations)
156{
157 EXPECT_EQ(static_cast<Snodenc<int>*>(list.get_first())->get_data(), 1);
158 EXPECT_EQ(static_cast<Snodenc<int>*>(list.get_last())->get_data(), 25);
159 EXPECT_EQ(list.get_first(), list.get_head());
160 EXPECT_EQ(list.get_last(), list.get_tail());
161 EXPECT_EQ(list.size(), 25);
162 EXPECT_FALSE(list.is_empty());
163 EXPECT_FALSE(list.is_unitarian());
164 EXPECT_FALSE(list.is_unitarian_or_empty());
165}
166
167TEST(HTList, Iterator_on_empty_list)
168{
169 HTList l;
171 ASSERT_EQ(it.has_curr(), false);
172 EXPECT_THROW(it.get_curr(), std::overflow_error);
173 EXPECT_FALSE(it.is_in_last());
174 EXPECT_FALSE(it.is_in_first());
175
176 it.reset_first();
177 EXPECT_FALSE(it.has_curr());
178 EXPECT_THROW(it.get_curr(), std::overflow_error);
179 EXPECT_FALSE(it.is_last());
180 EXPECT_FALSE(it.is_in_first());
181}
182
183TEST_F(List_of_25_nodes, Iterator_operations)
184{
185 int i = 1;
186 for (HTList::Iterator it = list; it.has_curr(); it.next(), ++i)
187 {
188 EXPECT_EQ(it.get_curr()->to_snodenc<int>()->get_data(), i);
189 EXPECT_EQ(it.get_pos(), i - 1);
190 }
191}
192
193TEST_F(List_of_25_nodes, Iterator_del_ne_removes_the_first_a_middle_and_the_last)
194{
195 HTList::Iterator it(list);
196 static_assert(noexcept(it.del_ne()));
197 Slinknc * p = it.del_ne(); // the first one, as remove_first_ne() does
198 EXPECT_EQ(p->to_data<int>(), 1);
199 delete p->to_snodenc<int>();
200 EXPECT_EQ(it.get_curr_ne()->to_data<int>(), 2);
201 EXPECT_EQ(list.get_first()->to_data<int>(), 2);
202
203 it.next_ne();
204 p = it.del_ne(); // a middle one
205 EXPECT_EQ(p->to_data<int>(), 3);
206 delete p->to_snodenc<int>();
207 EXPECT_EQ(it.get_curr_ne()->to_data<int>(), 4);
208
209 while (not it.is_last())
210 it.next_ne();
211 p = it.del_ne(); // the last one
212 EXPECT_EQ(p->to_data<int>(), 25);
213 delete p->to_snodenc<int>();
214 EXPECT_FALSE(it.has_curr());
215 EXPECT_EQ(list.get_last()->to_data<int>(), 24);
216 EXPECT_EQ(list.size(), 22u);
217
218 for (it.reset_first(); it.has_curr(); /* del_ne() moves forward */)
219 delete it.del_ne()->to_snodenc<int>();
220 EXPECT_TRUE(list.is_empty());
221}
222
223// del_ne() checks nothing, as the _ne variants promise: its precondition,
224// has_curr(), is asserted. Without a current item on an empty list, it
225// called remove_first(), which throws, inside noexcept: the program ended
226// with that exception, in every build.
227#ifndef NDEBUG
228TEST(HTListDeathTest, Iterator_del_ne_asserts_that_there_is_a_current_item)
229{
230 HTList empty;
231 HTList::Iterator on_empty(empty);
232 EXPECT_DEATH((void) on_empty.del_ne(), "has_curr");
233
234 HTList list;
235 Slinknc a, b;
236 list.append(&a);
237 list.append(&b);
238 HTList::Iterator past_the_end(list);
239 past_the_end.next_ne();
240 past_the_end.next_ne();
241 EXPECT_DEATH((void) past_the_end.del_ne(), "has_curr");
242}
243#endif
244
245TEST_F(List_of_25_nodes, Split_and_concat)
246{
247 HTList l, r;
248 list.split(l, r);
249
250 EXPECT_TRUE(list.is_empty());
251 EXPECT_EQ(l.size(), 13);
252 EXPECT_EQ(r.size(), 12);
253 EXPECT_EQ(l.get_first()->to_data<int>(), 1);
254 EXPECT_EQ(l.get_last()->to_data<int>(), 13);
255 EXPECT_EQ(r.get_first()->to_data<int>(), 14);
256 EXPECT_EQ(r.get_last()->to_data<int>(), 25);
257
258 int i = 1;
259 for (HTList::Iterator it = l; it.has_curr(); it.next(), ++i)
260 EXPECT_EQ(it.get_curr()->to_data<int>(), i);
261
262 for (HTList::Iterator it = r; it.has_curr(); it.next(), ++i)
263 EXPECT_EQ(it.get_curr()->to_data<int>(), i);
264
265 list.append(r);
266 list.insert(l);
267 i = 1;
268 for (HTList::Iterator it = l; it.has_curr(); it.next(), ++i)
269 EXPECT_EQ(it.get_curr()->to_data<int>(), i);
270}
271
273{
274 HTList laux;
275 laux.swap(list);
276
277 EXPECT_TRUE(list.is_empty());
278 EXPECT_EQ(list.size(), 0);
279 EXPECT_FALSE(laux.is_empty());
280
281 int i = 1;
282 for (HTList::Iterator it = laux; it.has_curr(); it.next(), ++i)
283 EXPECT_EQ(it.get_curr()->to_data<int>(), i);
284
286}
287
288TEST_F(List_of_25_nodes, cut_and_concat)
289{
290 HTList::Iterator it = list;
291 for (size_t i = 1; i < 13; ++i, it.next())
292 ;
293
294 HTList laux;
295 list.cut(it.get_curr(), laux);
296
297 EXPECT_FALSE(laux.is_empty());
298
299 int i = 1;
300 for (HTList::Iterator it = list; it.has_curr(); it.next(), ++i)
301 EXPECT_EQ(it.get_curr()->to_data<int>(), i);
302
303 for (HTList::Iterator it = laux; it.has_curr(); it.next(), ++i)
304 EXPECT_EQ(it.get_curr()->to_data<int>(), i);
305
306 list.concat(laux);
307 i = 1;
308 for (HTList::Iterator it = list; it.has_curr(); it.next(), ++i)
309 EXPECT_EQ(it.get_curr()->to_data<int>(), i);
310}
311
313{
314 EXPECT_EQ(list.reverse(), 25);
315 int i = 25;
316 for (HTList::Iterator it = list; it.has_curr(); it.next(), --i)
317 EXPECT_EQ(it.get_curr()->to_data<int>(), i);
318
319 EXPECT_EQ(list.reverse(), 25);
320
321 i = 1;
322 for (HTList::Iterator it = list; it.has_curr(); it.next(), ++i)
323 EXPECT_EQ(it.get_curr()->to_data<int>(), i);
324
325 HTList l, r;
326 list.split(l, r);
327
328 EXPECT_TRUE(list.is_empty());
329
330 l.reverse();
331 r.reverse();
332 list.insert(l);
333 list.insert(r);
334 EXPECT_TRUE(l.is_empty());
335 EXPECT_TRUE(r.is_empty());
336
337 list.reverse();
338 i = 1;
339 for (HTList::Iterator it = list; it.has_curr(); it.next(), ++i)
340 EXPECT_EQ(it.get_curr()->to_data<int>(), i);
341}
342
344{
345 list.rotate_left(3);
346
347 {
348 HTList::Iterator it = list;
349 for (int i = 0, n = 4; i < 3; ++i, ++n, it.next())
350 EXPECT_EQ(it.get_curr()->to_data<int>(), n);
351 }
352
353 list.rotate_left(22);
354 int i = 1;
355 for (HTList::Iterator it = list; it.has_curr(); it.next(), ++i)
356 EXPECT_EQ(it.get_curr()->to_data<int>(), i);
357}
358
359TEST(HTList, As_stack)
360{
361 HTList stack;
362 ASSERT_TRUE(stack.is_empty());
363 ASSERT_THROW(stack.top(), underflow_error);
364 ASSERT_THROW(stack.pop(), underflow_error);
365 ASSERT_TRUE(stack.is_unitarian_or_empty());
366 Slinknc n1, n2, n3;
367 stack.push(&n3);
368
369 ASSERT_FALSE(stack.is_empty());
370 ASSERT_TRUE(stack.is_unitarian());
371
372 stack.push(&n2);
373 stack.push(&n1);
374 ASSERT_EQ(stack.top(), &n1);
375 ASSERT_EQ(stack.pop(), &n1);
376 ASSERT_EQ(stack.top(), &n2);
377 ASSERT_EQ(stack.pop(), &n2);
378
379 ASSERT_TRUE(stack.is_unitarian());
380
381 ASSERT_EQ(stack.top(), &n3);
382 ASSERT_EQ(stack.pop(), &n3);
383 ASSERT_FALSE(stack.is_unitarian());
384 ASSERT_TRUE(stack.is_empty());
385}
386
387namespace
388{
389 // Its move throws while fail is true; moves counts them
390 struct Fragile_Move
391 {
392 static inline bool fail = false;
393 static inline int moves = 0;
394 int value = 0;
395
396 Fragile_Move(int v = 0) noexcept : value(v) {}
397 Fragile_Move(const Fragile_Move &) = default;
398 Fragile_Move(Fragile_Move && c) : value(c.value)
399 {
400 ++moves;
401 if (fail)
402 throw std::runtime_error("move failed");
403 }
404 Fragile_Move & operator = (const Fragile_Move &) = default;
405 Fragile_Move & operator = (Fragile_Move &&) = default;
406 };
407} // namespace
408
409// remove_ne() copied nothing, but it was noexcept, so that a move that threw
410// ended the program
411static_assert(noexcept(std::declval<DynList<int> &>().remove_ne()));
412static_assert(not noexcept(std::declval<DynList<Fragile_Move> &>().remove_ne()));
413
414// remove() unlinked the node before it moved the item out: a move that threw
415// lost the item and its node
416TEST(DynList, AMoveThatThrowsLeavesTheItemInTheList)
417{
419 for (int i = 1; i <= 3; ++i)
420 list.append(Fragile_Move(i));
421
422 Fragile_Move::fail = true;
423 EXPECT_THROW((void) list.remove_ne(), std::runtime_error);
424 EXPECT_THROW((void) list.remove(), std::runtime_error);
425 EXPECT_THROW((void) list.remove_first(), std::runtime_error);
426 EXPECT_THROW((void) list.pop(), std::runtime_error);
427 EXPECT_THROW((void) list.get(), std::runtime_error);
428 Fragile_Move::fail = false;
429
430 ASSERT_EQ(list.size(), 3u);
431 EXPECT_EQ(list.get_first().value, 1);
432 EXPECT_EQ(list.remove().value, 1);
433 EXPECT_EQ(list.size(), 2u);
434 EXPECT_THROW((void) DynList<Fragile_Move>().remove(), std::underflow_error);
435}
436
437// The item moves once, straight into the result: a second move, as that of
438// a named result that the compiler does not elide, would come after the
439// node left, and if it threw the item would be lost
440TEST(DynList, ARemoveMovesTheItemOnce)
441{
443 for (int i = 1; i <= 3; ++i)
444 list.append(Fragile_Move(i));
445
446 Fragile_Move::moves = 0;
447 const Fragile_Move first = list.remove_ne();
448 EXPECT_EQ(Fragile_Move::moves, 1);
449 const Fragile_Move second = list.remove();
450 EXPECT_EQ(Fragile_Move::moves, 2);
451 EXPECT_EQ(first.value, 1);
452 EXPECT_EQ(second.value, 2);
453 EXPECT_EQ(list.size(), 1u);
454}
455
456// Iterator::del() also unlinked the node before it moved the item out. Now a
457// move that throws leaves the item, and the iterator on it, on the first, a
458// middle and the last item
459TEST(DynList, AMoveThatThrowsInDelLeavesTheItemInTheList)
460{
461 for (int pos = 0; pos < 3; ++pos)
462 {
464 for (int i = 1; i <= 3; ++i)
465 list.append(Fragile_Move(i));
466
468 for (int k = 0; k < pos; ++k)
469 it.next();
470
471 Fragile_Move::fail = true;
472 EXPECT_THROW((void) it.del(), std::runtime_error);
473 Fragile_Move::fail = false;
474
475 ASSERT_EQ(list.size(), 3u);
476 ASSERT_TRUE(it.has_curr());
477 EXPECT_EQ(it.get_curr().value, pos + 1);
478
479 Fragile_Move::moves = 0;
480 EXPECT_EQ(it.del().value, pos + 1); // one move, straight into the result
481 EXPECT_EQ(Fragile_Move::moves, 1);
482 ASSERT_EQ(list.size(), 2u);
483 EXPECT_EQ(list.get_first().value, pos == 0 ? 2 : 1);
484 EXPECT_EQ(list.get_last().value, pos == 2 ? 2 : 3);
485 ASSERT_EQ(it.has_curr(), pos < 2);
486 if (pos < 2)
487 {
488 EXPECT_EQ(it.get_curr().value, pos + 2);
489 }
490 }
491
494 EXPECT_THROW((void) it.del(), std::overflow_error);
495}
size_t size_t int32_t value
Definition ca-c-api.h:116
Iterator on the items of list.
Definition htlist.H:1614
T & get_curr() const
Return the current item.
Definition htlist.H:1679
T del()
Remove the current item of the iterator.
Definition htlist.H:1695
Doubly-linked list (defined in tpl_dynList.H).
Definition htlist.H:1231
T & get_first()
Return the first item of the list.
Definition htlist.H:1548
T pop()
See remove().
Definition htlist.H:1479
T & append(const T &item)
Definition htlist.H:1362
T remove_ne() noexcept(std::is_nothrow_move_constructible_v< T >)
Remove the first item of the list, which must not be empty.
Definition htlist.H:1423
T get()
See remove().
Definition htlist.H:1485
T & get_last()
Return the last item of the list.
Definition htlist.H:1529
T remove()
Remove the first item of the list.
Definition htlist.H:1447
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
Slinknc * get_curr_ne() const noexcept
Return the current link guaranteeing no exception. Be careful.
Definition htlist.H:1005
void next_ne() noexcept
Move the iterator one position forward guaranteeing no exception.
Definition htlist.H:1012
void reset_first() noexcept
See reset().
Definition htlist.H:931
bool is_in_last() const noexcept
Return true if the iterator is positioned on the last item.
Definition htlist.H:986
bool is_last() const noexcept
Definition htlist.H:974
Slinknc * get_curr() const
Definition htlist.H:997
bool has_curr() const noexcept
Definition htlist.H:969
bool is_in_first() const noexcept
Return true if the iterator is positioned on the first item.
Definition htlist.H:980
Slinknc * del_ne() noexcept
Remove the current item and move the iterator one position forward, without checking that there is a ...
Definition htlist.H:1057
Single linked list of nodes.
Definition htlist.H:420
Slinknc * get_first() const noexcept
Definition htlist.H:463
void push(Slinknc *link) noexcept
Definition htlist.H:717
void remove_all_and_delete() noexcept
Definition htlist.H:879
Slinknc * top() const
Definition htlist.H:739
Slinknc * get_tail() const noexcept
Definition htlist.H:458
void cut(Slinknc *link, HTList &list) noexcept
It cuts 'this' over 'link' element and it puts all.
Definition htlist.H:849
constexpr bool is_empty() const noexcept
Definition htlist.H:434
Slinknc * get_head() const noexcept
Definition htlist.H:453
void rotate_left(size_t n)
Rotate to left the list n positions.
Definition htlist.H:1144
HTList & swap(HTList &l) noexcept
Definition htlist.H:488
Slinknc * pop()
Remove the first item of this.
Definition htlist.H:733
void append(Slinknc *link) noexcept
Definition htlist.H:518
Slinknc * remove_first()
Definition htlist.H:672
size_t size() const noexcept
Count the number of elements of the list.
Definition htlist.H:1119
void insert(Slinknc *link) noexcept
Definition htlist.H:499
bool is_unitarian() const noexcept
Return true if the list contains exactly just one.
Definition htlist.H:441
Slinknc * get_last() const noexcept
Return the last item of the list (nullptr if the list is empty)
Definition htlist.H:475
bool is_unitarian_or_empty() const noexcept
Return true if list contains one element or is empty.
Definition htlist.H:448
Link of a single linked list non-circular and without header node.
Definition htlist.H:95
T & to_data() noexcept
Return the item of the Snodenc<T> that this is.
Definition htlist.H:382
Snodenc< T > * to_snodenc() noexcept
Convert this to a Snodenc<T>.
Definition htlist.H:370
T & get_data() noexcept
Return a modifiable reference to the data.
Definition htlist.H:295
#define TEST(name)
Singly linked list implementations with head-tail access.
TEST_F(List_of_25_nodes, Basic_operations)
Definition htlist.cc:155
Main namespace for Aleph-w library functions.
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.
Fw_Itor remove(Fw_Itor __first, const Fw_Itor &__last, const T &__value)
Remove elements equal to a value.
Definition ahAlgo.H:962
STL namespace.
Dnode< int > Test
Definition testDnode.C:42
static int * k
Gsl_Compatible_Rng r
DynList< int > l