Aleph-w 7.10.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
ah_iterator_test.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
38#include <gtest/gtest.h>
39
40#include <algorithm>
41#include <type_traits>
42#include <utility>
43#include <vector>
44
45#include <ah-ranges.H>
46#include <array_it.H>
47#include <tpl_array.H>
48#include <tpl_dynArray.H>
49#include <tpl_dynArrayHeap.H>
50#include <tpl_dynDlist.H>
51#include <tpl_dynSetTree.H>
52
53using namespace Aleph;
54
55#if ALEPH_HAS_RANGES
56
57#include <concepts>
58#include <iterator>
59
60namespace
61{
62
63template <class C>
64concept HasBeginEnd = requires(C & c) {
65 c.begin();
66 c.end();
67};
68
69} // namespace
70
71TEST(AhIteratorConcepts, DynDlistIteratorsSatisfyForwardIterator)
72{
73 using Iter = DynDlist<int>::iterator;
75
76 static_assert(std::forward_iterator<Iter>);
77 static_assert(std::forward_iterator<CIter>);
78
79 static_assert(std::same_as<std::iter_reference_t<Iter>, std::iter_reference_t<const Iter>>);
80 static_assert(std::same_as<std::iter_reference_t<CIter>, std::iter_reference_t<const CIter>>);
81
82 SUCCEED();
83}
84
85TEST(AhIteratorConcepts, DynSetTreeIteratorsSatisfyForwardIterator)
86{
87 using Set = DynSetTree<int>;
88 using Iter = Set::iterator;
89 using CIter = Set::const_iterator;
90
91 static_assert(std::forward_iterator<Iter>);
92 static_assert(std::forward_iterator<CIter>);
93
94 static_assert(std::same_as<std::iter_reference_t<Iter>, std::iter_reference_t<const Iter>>);
95 static_assert(std::same_as<std::iter_reference_t<CIter>, std::iter_reference_t<const CIter>>);
96
97 using Ref = std::iter_reference_t<Iter>;
98 using Ptr = std::add_pointer_t<std::remove_reference_t<Ref>>;
99 static_assert(std::same_as<decltype(std::declval<const Iter &>().operator->()), Ptr>,
100 "operator-> must return a pointer consistent with operator*");
101
102 SUCCEED();
103}
104
105TEST(AhIteratorConstCorrectness, ConstContainerBeginReturnsConstIterator)
106{
107 DynDlist<int> list;
108 list.append(1);
109 list.append(2);
110
111 const DynDlist<int> & clist = list;
112
113 static_assert(std::same_as<decltype(clist.begin()), DynDlist<int>::const_iterator>);
114 static_assert(std::same_as<decltype(begin(clist)), DynDlist<int>::const_iterator>);
115
116 SUCCEED();
117}
118
119TEST(AhIteratorConstCorrectness, MutableIteratorCannotBeCreatedFromConstContainer)
120{
121 using C = const DynDlist<int>;
122
123 static_assert(std::same_as<decltype(std::declval<C &>().begin()), DynDlist<int>::const_iterator>);
124 static_assert(std::same_as<decltype(begin(std::declval<C &>())), DynDlist<int>::const_iterator>);
125
126 SUCCEED();
127}
128
129TEST(AhIteratorRuntime, RangeForWorksForMutableAndConst)
130{
131 DynDlist<int> list;
132 for (int i = 1; i <= 5; ++i)
133 list.append(i);
134
135 std::vector<int> v;
136 for (int & x : list)
137 v.push_back(x);
138
139 ASSERT_EQ(v.size(), 5u);
140 EXPECT_EQ(v[0], 1);
141 EXPECT_EQ(v[4], 5);
142
143 const DynDlist<int> & clist = list;
144 int sum = 0;
145 for (const int & x : clist)
146 sum += x;
147
148 EXPECT_EQ(sum, 15);
149}
150
151TEST(AhIteratorRuntime, MutableIteratorAllowsMutationForSequenceContainers)
152{
153 DynDlist<int> list;
154 list.append(1);
155 list.append(2);
156
157 auto it = list.begin();
158 ASSERT_NE(it, list.end());
159
160 *it = 10;
161 EXPECT_EQ(*list.begin(), 10);
162}
163
164TEST(AhIteratorRuntime, RangesAlgorithmsWork)
165{
166 DynSetTree<int> set;
167 set.insert(5);
168 set.insert(2);
169 set.insert(8);
170 set.insert(1);
171 set.insert(9);
172
173 EXPECT_TRUE(std::ranges::all_of(set, [](int x) { return x > 0; }));
174
175 auto it = std::ranges::find(set, 5);
176 ASSERT_NE(it, set.end());
177 EXPECT_EQ(*it, 5);
178
179 auto min_it = std::ranges::min_element(set);
180 ASSERT_NE(min_it, set.end());
181 EXPECT_EQ(*min_it, 1);
182}
183
184TEST(AhIteratorSemantics, DefaultConstructedIteratorsCompareEqual)
185{
186 using Iter = DynDlist<int>::iterator;
187
188 Iter a;
189 Iter b;
190 EXPECT_TRUE(a == b);
191 EXPECT_FALSE(a != b);
192}
193
194TEST(AhIteratorSemantics, PostIncrementReturnsOldValue)
195{
196 DynDlist<int> list;
197 list.append(1);
198 list.append(2);
199
200 auto it = list.begin();
201 auto old = it++;
202
203 EXPECT_EQ(*old, 1);
204 EXPECT_EQ(*it, 2);
205}
206
207TEST(AhIteratorConcepts, DynArrayIteratorSatisfiesRandomAccess)
208{
209 using Iter = DynArray<int>::iterator;
210
211 static_assert(std::random_access_iterator<Iter>);
212 static_assert(std::same_as<
213 typename std::iterator_traits<Iter>::iterator_category,
214 std::random_access_iterator_tag>);
215
216 SUCCEED();
217}
218
219TEST(AhIteratorConcepts, ArrayIteratorSatisfiesRandomAccess)
220{
221 using Iter = Array<int>::iterator;
222
223 static_assert(std::random_access_iterator<Iter>);
224
225 SUCCEED();
226}
227
228TEST(AhIteratorConcepts, ConstIteratorsArePromotedToRandomAccessToo)
229{
230 static_assert(std::random_access_iterator<DynArray<int>::const_iterator>);
231 static_assert(std::random_access_iterator<Array<int>::const_iterator>);
232
233 // Non-opted-in containers must keep their forward category.
234 static_assert(std::forward_iterator<DynDlist<int>::const_iterator>);
235 static_assert(not std::random_access_iterator<DynDlist<int>::const_iterator>);
236
237 SUCCEED();
238}
239
240TEST(AhIteratorConcepts, DynArrayHeapIteratorStaysNonRandomAccessFailSafe)
241{
242 // The heap has an iterator of its own, on its items, which does not opt in
243 // to the random-access promotion: it must NOT be a random_access_iterator.
244 using Iter = DynArrayHeap<int>::iterator;
245
246 static_assert(std::forward_iterator<Iter>);
247 static_assert(not std::random_access_iterator<Iter>);
248
249 SUCCEED();
250}
251
252TEST(AhIteratorRandomAccess, DynArrayArithmeticIndexingAndOrdering)
253{
255 for (int i = 0; i < 10; ++i)
256 a.append(i);
257
258 auto first = a.begin();
259
260 EXPECT_EQ(a.end() - first, 10);
261 EXPECT_EQ(*(first + 3), 3);
262 EXPECT_EQ(*(3 + first), 3); // symmetric operator+
263 EXPECT_EQ(first[7], 7);
264
265 auto it = first;
266 it += 5;
267 EXPECT_EQ(*it, 5);
268 it -= 2;
269 EXPECT_EQ(*it, 3);
270 EXPECT_EQ(it - first, 3);
271
272 EXPECT_TRUE(first < it);
273 EXPECT_TRUE(it > first);
274 EXPECT_TRUE(first <= first);
275 EXPECT_TRUE(first >= first);
276
277 --it;
278 EXPECT_EQ(*it, 2);
279 auto prev = it--;
280 EXPECT_EQ(*prev, 2);
281 EXPECT_EQ(*it, 1);
282}
283
284TEST(AhIteratorRandomAccess, StdSortAndRangesSortOnDynArray)
285{
287 for (int v : {5, 2, 8, 1, 9, 3, 7, 4, 6, 0})
288 a.append(v);
289
290 std::sort(a.begin(), a.end());
291
292 std::vector<int> v;
293 for (int x : a)
294 v.push_back(x);
295 EXPECT_TRUE(std::is_sorted(v.begin(), v.end()));
296 EXPECT_EQ(v.front(), 0);
297 EXPECT_EQ(v.back(), 9);
298
299 std::ranges::sort(a, std::ranges::greater{});
300 v.clear();
301 for (int x : a)
302 v.push_back(x);
303 EXPECT_EQ(v.front(), 9);
304 EXPECT_EQ(v.back(), 0);
305}
306
307TEST(AhIteratorRandomAccess, StdSortOnArray)
308{
309 Array<int> a;
310 for (int v : {3, 1, 2, 5, 4})
311 a.append(v);
312
313 std::sort(a.begin(), a.end());
314
315 std::vector<int> v;
316 for (int x : a)
317 v.push_back(x);
318 EXPECT_TRUE(std::is_sorted(v.begin(), v.end()));
319 EXPECT_EQ(v.front(), 1);
320 EXPECT_EQ(v.back(), 5);
321}
322
323TEST(AhIteratorSemantics, DefaultConstructedRandomAccessIteratorsCompareEqual)
324{
325 // std::regular (required by std::random_access_iterator) demands that two
326 // value-initialized iterators compare equal. DynArray::Iterator::has_curr()
327 // must therefore be null-safe on singular iterators.
329 EXPECT_TRUE(a == b);
330 EXPECT_FALSE(a != b);
331
333 EXPECT_TRUE(c == d);
334 EXPECT_FALSE(c != d);
335}
336
337TEST(AhIteratorRandomAccess, ConstIteratorArithmeticAndBinarySearch)
338{
340 for (int i = 0; i < 10; ++i)
341 a.append(i);
342
343 const DynArray<int> &ca = a;
344 auto first = ca.begin();
345
346 EXPECT_EQ(ca.end() - first, 10);
347 EXPECT_EQ(*(first + 3), 3);
348 EXPECT_EQ(*(3 + first), 3);
349 EXPECT_EQ(first[7], 7);
350 EXPECT_TRUE(first < ca.end());
351
352 auto it = first;
353 it += 5;
354 EXPECT_EQ(*it, 5);
355 --it;
356 EXPECT_EQ(*it, 4);
357 it -= 2;
358 EXPECT_EQ(*it, 2);
359 EXPECT_EQ(it - first, 2);
360
361 // O(log n) algorithms require random access on the const view.
362 EXPECT_TRUE(std::binary_search(ca.begin(), ca.end(), 5));
363 auto lb = std::lower_bound(ca.begin(), ca.end(), 7);
364 ASSERT_NE(lb, ca.end());
365 EXPECT_EQ(*lb, 7);
366
367 Array<int> arr;
368 for (int v : {1, 2, 3})
369 arr.append(v);
370 const Array<int> &carr = arr;
371 EXPECT_EQ(carr.end() - carr.begin(), 3);
372 EXPECT_EQ(carr.begin()[2], 3);
373}
374
375TEST(AhIteratorRandomAccess, EmptyContainersHaveZeroDistanceAndConsistentOrder)
376{
377 // Regression: Array_Iterator::end() used to leave pos == -1 on empty
378 // containers (reset_last() with num_items == 0), so end() - begin() was -1
379 // and begin() > end() while begin() == end() also held. The end state must
380 // be position num_items (0 when empty).
381 Array<int> a;
382 EXPECT_EQ(a.end() - a.begin(), 0);
383 EXPECT_TRUE(a.begin() == a.end());
384 EXPECT_FALSE(a.begin() < a.end());
385 EXPECT_FALSE(a.begin() > a.end());
386 EXPECT_EQ(std::distance(a.begin(), a.end()), 0);
387 std::sort(a.begin(), a.end()); // must be a harmless no-op
388 EXPECT_EQ(a.size(), 0u);
389
390 const Array<int> &ca = a;
391 EXPECT_EQ(ca.end() - ca.begin(), 0);
392 EXPECT_TRUE(ca.begin() == ca.end());
393
395 EXPECT_EQ(d.end() - d.begin(), 0);
396 EXPECT_TRUE(d.begin() == d.end());
397}
398
399TEST(AhIteratorRandomAccess, ArrayIteratorCircularSetPosMatchesEnd)
400{
401 // Physical buffer of size 5; logical sequence of 4 items starting at physical
402 // index 3 and wrapping around: logical [0..3] -> physical [3, 4, 0, 1].
403 int buf[5] = {20, 30, 999, 0, 10};
404 Array_Iterator<int> it(buf, 5, 4, 3, 1); // ptr, dim, num_items, first, last
405
406 it.set_pos(0);
407 EXPECT_EQ(it.get_curr(), 0); // buf[3]
408 it.set_pos(1);
409 EXPECT_EQ(it.get_curr(), 10); // buf[4]
410 it.set_pos(2);
411 EXPECT_EQ(it.get_curr(), 20); // buf[0]
412 it.set_pos(3);
413 EXPECT_EQ(it.get_curr(), 30); // buf[1]
414
415 // set_pos(num_items) must reproduce the exact end() state.
416 Array_Iterator<int> at_end(buf, 5, 4, 3, 1);
417 at_end.end();
418
419 it.set_pos(4);
420 EXPECT_FALSE(it.has_curr());
421 EXPECT_EQ(it.get_pos(), at_end.get_pos());
422}
423
424#else // !ALEPH_HAS_RANGES
425
426// Dummy test when ranges/concepts are not available
427TEST(AhIterator, ConceptsNotAvailable)
428{
429 GTEST_SKIP() << "C++20 concepts not fully supported on this platform";
430}
431
432#endif // ALEPH_HAS_RANGES
C++20 Ranges support and adaptors for Aleph-w containers.
Iterator wrapper for C++ raw arrays and circular buffers.
Iterator wrapper for C++ raw arrays.
Definition array_it.H:85
Simple dynamic array with automatic resizing and functional operations.
Definition tpl_array.H:141
constexpr size_t size() const noexcept
Return the number of elements stored in the stack.
Definition tpl_array.H:450
T & append(const T &data)
Append a copy of data
Definition tpl_array.H:331
Dynamic heap (priority queue) backed by DynArray.
T & append()
Allocate a new entry to the end of array.
Dynamic doubly linked list with O(1) size and bidirectional access.
T & append(const T &item)
Append a copied item at the end of the list.
Dynamic set backed by balanced binary search trees with automatic memory management.
Key * insert(const Key &key)
Inserts a key into the dynamic set.
iterator end() noexcept
Return an STL-compatible end iterator.
iterator begin() noexcept
Return an STL-compatible iterator to the first element.
#define TEST(name)
Main namespace for Aleph-w library functions.
T sum(const Container &container, const T &init=T{})
Compute sum of all elements.
Dynamic array container with automatic resizing.
Array-based dynamic binary heap.
Lazy and scalable dynamic array implementation.
Dynamic doubly linked list implementation.
Dynamic set implementations based on balanced binary search trees.