Aleph-w 7.10.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
dynlist.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 <vector>
42
43# include <htlist.H>
44# include <ah-unique.H>
45# include <ah-convert.H>
46
47using namespace testing;
48using namespace Aleph;
49
50struct List_of_25_items : public Test
51{
52 size_t n = 0;
56 {
57 for (size_t i = 0; i < 25; ++i, ++n)
58 list.append(i + 1);
59
60 const DynList<int> l = list;
61 rlist = l.rev();
62 }
63};
64
65TEST(DynList, Basic_operations)
66{
67 DynList<int> list;
68 EXPECT_TRUE(list.is_empty());
69 EXPECT_FALSE(list.is_unitarian());
70 EXPECT_TRUE(list.is_unitarian_or_empty());
71
72 list.append(2);
73 EXPECT_FALSE(list.is_empty());
74 EXPECT_TRUE(list.is_unitarian());
75 EXPECT_TRUE(list.is_unitarian_or_empty());
76 EXPECT_EQ(list.get_first(), list.get_last());
77
78 list.insert(1);
79 EXPECT_FALSE(list.is_empty());
80 EXPECT_FALSE(list.is_unitarian());
81 EXPECT_FALSE(list.is_unitarian_or_empty());
82
83 EXPECT_EQ(list.get_first(), 1);
84 EXPECT_EQ(list.get_last(), 2);
85
86 list.insert(0);
87 list.append(3);
88 EXPECT_EQ(list.get_first(), 0);
89 EXPECT_EQ(list.get_last(), 3);
90
91 EXPECT_EQ(list.remove_first(), 0);
92 EXPECT_EQ(list.size(), 3);
93 EXPECT_EQ(list.get_last(), 3);
94
95 EXPECT_EQ(list.remove_first(), 1);
96 EXPECT_EQ(list.size(), 2);
97 EXPECT_EQ(list.get_last(), 3);
98
99 EXPECT_EQ(list.remove_first(), 2);
100 EXPECT_EQ(list.size(), 1);
101 EXPECT_EQ(list.get_last(), 3);
102
103 EXPECT_TRUE(list.is_unitarian());
104 EXPECT_TRUE(list.is_unitarian_or_empty());
105
106 EXPECT_EQ(list.remove_first(), 3);
107 EXPECT_TRUE(list.is_empty());
108 EXPECT_EQ(list.size(), 0);
109
110 EXPECT_THROW(list.rotate_left(1), std::domain_error);
111 EXPECT_NO_THROW(list.rotate_left(0));
112}
113
114TEST(DynList, Clear_operation)
115{
116 DynList<int> list;
117 list.append(1);
118 list.append(2);
119 list.append(3);
120 EXPECT_FALSE(list.is_empty());
121 EXPECT_EQ(list.size(), 3);
122
123 list.clear();
124 EXPECT_TRUE(list.is_empty());
125 EXPECT_EQ(list.size(), 0);
126
127 list.append(4);
128 EXPECT_FALSE(list.is_empty());
129 EXPECT_EQ(list.size(), 1);
130 EXPECT_EQ(list.get_first(), 4);
131
132 EXPECT_TRUE(list.contains(4));
133 EXPECT_FALSE(list.contains(1));
134}
135
136TEST(DynList, EqualityOperator)
137{
138 DynList<int> l1 = {1, 2, 3};
139 DynList<int> l2 = {1, 2, 3};
140 DynList<int> l3 = {1, 2, 4};
141 DynList<int> l4 = {1, 2};
142
143 EXPECT_TRUE(l1 == l2);
144 EXPECT_FALSE(l1 != l2);
145
146 EXPECT_FALSE(l1 == l3);
147 EXPECT_TRUE(l1 != l3);
148
149 EXPECT_FALSE(l1 == l4);
150 EXPECT_TRUE(l1 != l4);
151
152 DynList<int> empty1, empty2;
153 EXPECT_TRUE(empty1 == empty2);
154}
155
156TEST(DynList, Simple_append_and_insert_of_list)
157{
158 DynList<int> laux, list;
159 laux.insert(2);
160 list.append(std::move(laux));
161
162 EXPECT_TRUE(laux.is_empty());
163 EXPECT_TRUE(list.is_unitarian());
164
165 laux.insert(1);
166 list.insert(std::move(laux));
167 EXPECT_TRUE(laux.is_empty());
168 EXPECT_EQ(list.size(), 2);
169 EXPECT_EQ(list.get_first(), 1);
170 EXPECT_EQ(list.get_last(), 2);
171}
172
173TEST(DynListAlgorithms, InPlaceUnique)
174{
175 {
176 DynList<int> list;
177 list.append(3);
178 list.append(1);
179 list.append(2);
180 list.append(3);
181
182 in_place_unique(list);
183
184 ASSERT_EQ(list.size(), 3u);
185 auto it = list.get_it();
186 ASSERT_TRUE(it.has_curr()); EXPECT_EQ(it.get_curr(), 3); it.next();
187 ASSERT_TRUE(it.has_curr()); EXPECT_EQ(it.get_curr(), 1); it.next();
188 ASSERT_TRUE(it.has_curr()); EXPECT_EQ(it.get_curr(), 2); it.next();
189 EXPECT_FALSE(it.has_curr());
190 }
191
192 {
193 DynList<int> empty_list;
194 in_place_unique(empty_list);
195 EXPECT_EQ(empty_list.size(), 0u);
196 EXPECT_FALSE(empty_list.get_it().has_curr());
197 }
198
199 {
200 DynList<int> single_list;
201 single_list.append(42);
202 in_place_unique(single_list);
203 EXPECT_EQ(single_list.size(), 1u);
204 EXPECT_EQ(single_list.get_first(), 42);
205 }
206
207 {
208 DynList<int> all_equal;
209 all_equal.append(5);
210 all_equal.append(5);
211 all_equal.append(5);
212 in_place_unique(all_equal);
213 EXPECT_EQ(all_equal.size(), 1u);
214 EXPECT_EQ(all_equal.get_first(), 5);
215 }
216}
217
218TEST_F(List_of_25_items, Basic_operations)
219{
220 EXPECT_EQ(list.get_first(), 1);
221 EXPECT_EQ(list.get_last(), 25);
222 EXPECT_EQ(list.size(), 25);
223 EXPECT_FALSE(list.is_empty());
224 EXPECT_FALSE(list.is_unitarian());
225 EXPECT_FALSE(list.is_unitarian_or_empty());
226}
227
228TEST_F(List_of_25_items, Iterator_operations)
229{
230 int i = 1;
231 for (auto it = list.get_it(); it.has_curr(); it.next(), ++i)
232 {
233 EXPECT_EQ(it.get_curr(), i);
234 EXPECT_EQ(it.get_pos(), i - 1);
235 }
236}
237
238TEST_F(List_of_25_items, Split_and_concat)
239{
241 list.split(l, r);
242
243 EXPECT_TRUE(list.is_empty());
244 EXPECT_EQ(l.size(), 13);
245 EXPECT_EQ(r.size(), 12);
246 EXPECT_EQ(l.get_first(), 1);
247 EXPECT_EQ(l.get_last(), 13);
248 EXPECT_EQ(r.get_first(), 14);
249 EXPECT_EQ(r.get_last(), 25);
250
251 int i = 1;
252 for (auto it = l.get_it(); it.has_curr(); it.next(), ++i)
253 EXPECT_EQ(it.get_curr(), i);
254
255 for (auto it = r.get_it(); it.has_curr(); it.next(), ++i)
256 EXPECT_EQ(it.get_curr(), i);
257
258 list.append(r);
259 list.insert(l);
260 i = 1;
261 for (auto it = l.get_it(); it.has_curr(); it.next(), ++i)
262 EXPECT_EQ(it.get_curr(), i);
263}
264
266{
267 DynList<int> laux;
268 laux.swap(list);
269
270 EXPECT_TRUE(list.is_empty());
271 EXPECT_EQ(list.size(), 0);
272 EXPECT_FALSE(laux.is_empty());
273
274 int i = 1;
275 for (auto it = laux.get_it(); it.has_curr(); it.next(), ++i)
276 EXPECT_EQ(it.get_curr(), i);
277}
278
280{
281 list.reverse();
282 int i = 25;
283 for (auto it = list.get_it(); it.has_curr(); it.next(), --i)
284 EXPECT_EQ(it.get_curr(), i);
285
286 list.reverse();
287
288 i = 1;
289 for (auto it = list.get_it(); it.has_curr(); it.next(), ++i)
290 EXPECT_EQ(it.get_curr(), i);
291
292 const DynList<int> list_cpy = list;
293 const DynList<int> rlist_cpy = rlist;
294
295 EXPECT_TRUE(eq(list_cpy.rev(), rlist));
296 EXPECT_TRUE(eq(rlist_cpy.rev(), list));
297
299 list.split(l, r);
300
301 EXPECT_TRUE(list.is_empty());
302
303 l.reverse();
304 r.reverse();
305 list.insert(l);
306 list.insert(r);
307
308 list.reverse();
309 i = 1;
310 for (HTList::Iterator it = list; it.has_curr(); it.next(), ++i)
311 EXPECT_EQ(it.get_curr()->to_data<int>(), i);
312
313}
314
316{
317 list.rotate_left(3);
318
319 {
320 auto it = list.get_it();
321 for (int i = 0, n = 4; i < 3; ++i, ++n, it.next())
322 EXPECT_EQ(it.get_curr(), n);
323 }
324
325 list.rotate_left(22);
326 int i = 1;
327 for (auto it = list.get_it(); it.has_curr(); it.next(), ++i)
328 EXPECT_EQ(it.get_curr(), i);
329}
330
331TEST_F(List_of_25_items, append_and_insert_by_moving)
332{
333 DynList<int> ll = { 0, -1, -2, -3, -4, -5, -6, -7, -8, -9 };
334 DynList<int> lg = { 26, 27, 28, 29, 30, 31, 32, 33, 34, 35 };
335
336 ll.reverse();
337
338 list.insert(std::move(ll));
339 list.append(std::move(lg));
340
341 EXPECT_TRUE(ll.is_empty());
342 EXPECT_TRUE(lg.is_empty());
343
344 int i = -9;
345 for (auto it = list.get_it(); it.has_curr(); it.next(), ++i)
346 EXPECT_EQ(it.get_curr(), i);
347}
348
349TEST(DynList, traverse_on_empty_container)
350{
352 size_t n = 0;
353 auto ret = m.traverse([&n] (int) { ++n; return true; });
354 EXPECT_TRUE(ret);
355 EXPECT_EQ(n, 0);
356}
357
359{
360 EXPECT_TRUE(list.size() > 0);
361 EXPECT_EQ(list.size(), n);
362 int N = 0;
363 auto ret = list.traverse([&N, this] (int i) { ++N; return i < n/2; });
364 EXPECT_FALSE(ret);
365 EXPECT_EQ(N, n/2);
366}
367
368TEST(DynListFunctional, ToArrayHelperPreservesOrder)
369{
370 // empty list
371 {
372 DynList<int> empty;
373 auto arr = to_array(empty);
374 EXPECT_EQ(arr.size(), 0u);
375 }
376
377 // single element
378 {
379 DynList<int> one;
380 one.append(42);
381 auto arr = to_array(one);
382 ASSERT_EQ(arr.size(), 1u);
383 EXPECT_EQ(arr(0), 42);
384 }
385
386 // multiple elements — order preserved
387 DynList<int> list;
388 for (int v : {5, 8, 13})
389 list.append(v);
390
391 auto arr = to_array(list);
392
393 ASSERT_EQ(arr.size(), list.size());
394 size_t i = 0;
395 for (auto it = list.get_it(); it.has_curr(); it.next(), ++i)
396 EXPECT_EQ(arr(i), it.get_curr());
397}
398
399namespace
400{
401 // An item that counts the items alive, and whose copy throws when a
402 // budget of copies runs out
403 struct Counted_Copy
404 {
405 static inline int alive = 0;
406 static inline int copies_left = -1; // -1: no copy throws
407 int value = 0;
408
409 Counted_Copy(const int v = 0) : value(v) { ++alive; }
410
411 Counted_Copy(const Counted_Copy & other) : value(other.value)
412 {
413 if (copies_left == 0)
414 throw std::runtime_error("the copy of an item");
415 if (copies_left > 0)
416 --copies_left;
417 ++alive;
418 }
419
420 Counted_Copy & operator = (const Counted_Copy &) = default;
421
422 ~Counted_Copy() { --alive; }
423 };
424}
425
426// A constructor whose copy of an item throws frees the items that it had
427// appended, with their nodes, and the exception reaches the caller: the
428// copy, the range of iterators and the initializer list
429TEST(DynList, ConstructorsFreeWhatTheyAppendedWhenACopyThrows)
430{
431 const DynList<Counted_Copy> source = {1, 2, 3, 4, 5};
432 const std::vector<Counted_Copy> items = {1, 2, 3, 4, 5};
433 const int before = Counted_Copy::alive;
434 for (int k = 0; k < 5; ++k)
435 {
436 Counted_Copy::copies_left = k;
437 EXPECT_THROW([&] { const DynList<Counted_Copy> copy(source); }(), std::runtime_error);
438 EXPECT_EQ(Counted_Copy::alive, before) << "the copy, at the item " << k;
439
440 Counted_Copy::copies_left = k;
441 EXPECT_THROW([&] { const DynList<Counted_Copy> l(items.begin(), items.end()); }(),
442 std::runtime_error);
443 EXPECT_EQ(Counted_Copy::alive, before) << "the range, at the item " << k;
444
445 Counted_Copy::copies_left = k;
446 EXPECT_THROW(([&] { const DynList<Counted_Copy> l = {1, 2, 3, 4, 5}; }()),
447 std::runtime_error);
448 EXPECT_EQ(Counted_Copy::alive, before) << "the initializer list, at the item " << k;
449 }
450
451 // with copies enough, each one builds the whole list
452 Counted_Copy::copies_left = 5;
453 const DynList<Counted_Copy> copy(source);
454 EXPECT_EQ(copy.size(), 5u);
455 EXPECT_EQ(copy.get_last().value, 5);
456 Counted_Copy::copies_left = -1;
457}
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
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 & append(const T &item)
Definition htlist.H:1362
T & insert(const T &item)
Definition htlist.H:1308
T & get_last()
Return the last item of the list.
Definition htlist.H:1529
DynList & rev() noexcept
Definition htlist.H:1734
void clear() noexcept
Empties the container.
Definition htlist.H:1599
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
DynList & swap(DynList &l) noexcept
Definition htlist.H:1253
std::uint32_t next() noexcept
Definition ah-random.H:203
bool has_curr() const noexcept
Definition htlist.H:969
constexpr bool is_empty() const noexcept
Definition htlist.H:434
void rotate_left(size_t n)
Rotate to left the list n positions.
Definition htlist.H:1144
bool is_unitarian() const noexcept
Return true if the list contains exactly just one.
Definition htlist.H:441
bool is_unitarian_or_empty() const noexcept
Return true if list contains one element or is empty.
Definition htlist.H:448
Number Theoretic Transform over Z / MOD Z.
Definition ntt.H:115
auto get_it()
Return a properly initialized iterator positioned at the first item on the container.
Definition ah-dry.H:663
bool contains(const Type &item) const
Test if an item is present in the container using equality.
Definition ah-dry.H:1074
#define TEST(name)
TEST_F(List_of_25_items, Basic_operations)
Definition dynlist.cc:218
#define N
Definition fib.C:294
bool traverse(Node *root, Op op)
See infix_traverse().
Singly linked list implementations with head-tail access.
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
bool eq(const C1 &c1, const C2 &c2, Eq e=Eq())
Check equality of two containers using a predicate.
T & swap(T &t1, T &t2)
Generic swap using object's swap method.
Definition ahTypes.H:123
Itor2 copy(Itor1 sourceBeg, const Itor1 &sourceEnd, Itor2 destBeg)
Copy elements from one range to another.
Definition ahAlgo.H:584
void rotate_left(T *ptr, const size_t n, size_t m) noexcept
Rotate array elements left by m positions.
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
DynList< int > rlist
Definition dynlist.cc:54
DynList< int > list
Definition dynlist.cc:53
FooMap m(5, fst_unit_pair_hash, snd_unit_pair_hash)
DynList< char > l3
DynList< int > l1
DynList< int > l2
static int * k
Gsl_Compatible_Rng r
DynList< int > l