Aleph-w 3.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 <type_traits>
41#include <utility>
42#include <vector>
43
44#include <ah-ranges.H>
45#include <tpl_dynDlist.H>
46#include <tpl_dynSetTree.H>
47
48using namespace Aleph;
49
50#if ALEPH_HAS_RANGES
51
52#include <concepts>
53#include <iterator>
54
55namespace
56{
57
58template <class C>
59concept HasBeginEnd = requires(C & c) {
60 c.begin();
61 c.end();
62};
63
64} // namespace
65
67{
70
71 static_assert(std::forward_iterator<Iter>);
72 static_assert(std::forward_iterator<CIter>);
73
74 static_assert(std::same_as<std::iter_reference_t<Iter>, std::iter_reference_t<const Iter>>);
75 static_assert(std::same_as<std::iter_reference_t<CIter>, std::iter_reference_t<const CIter>>);
76
77 SUCCEED();
78}
79
81{
82 using Set = DynSetTree<int>;
83 using Iter = Set::iterator;
84 using CIter = Set::const_iterator;
85
86 static_assert(std::forward_iterator<Iter>);
87 static_assert(std::forward_iterator<CIter>);
88
89 static_assert(std::same_as<std::iter_reference_t<Iter>, std::iter_reference_t<const Iter>>);
90 static_assert(std::same_as<std::iter_reference_t<CIter>, std::iter_reference_t<const CIter>>);
91
92 using Ref = std::iter_reference_t<Iter>;
93 using Ptr = std::add_pointer_t<std::remove_reference_t<Ref>>;
94 static_assert(std::same_as<decltype(std::declval<const Iter &>().operator->()), Ptr>,
95 "operator-> must return a pointer consistent with operator*");
96
97 SUCCEED();
98}
99
101{
102 DynDlist<int> list;
103 list.append(1);
104 list.append(2);
105
106 const DynDlist<int> & clist = list;
107
108 static_assert(std::same_as<decltype(clist.begin()), DynDlist<int>::const_iterator>);
109 static_assert(std::same_as<decltype(begin(clist)), DynDlist<int>::const_iterator>);
110
111 SUCCEED();
112}
113
115{
116 using C = const DynDlist<int>;
117
118 static_assert(std::same_as<decltype(std::declval<C &>().begin()), DynDlist<int>::const_iterator>);
119 static_assert(std::same_as<decltype(begin(std::declval<C &>())), DynDlist<int>::const_iterator>);
120
121 SUCCEED();
122}
123
125{
126 DynDlist<int> list;
127 for (int i = 1; i <= 5; ++i)
128 list.append(i);
129
130 std::vector<int> v;
131 for (int & x : list)
132 v.push_back(x);
133
134 ASSERT_EQ(v.size(), 5u);
135 EXPECT_EQ(v[0], 1);
136 EXPECT_EQ(v[4], 5);
137
138 const DynDlist<int> & clist = list;
139 int sum = 0;
140 for (const int & x : clist)
141 sum += x;
142
143 EXPECT_EQ(sum, 15);
144}
145
147{
148 DynDlist<int> list;
149 list.append(1);
150 list.append(2);
151
152 auto it = list.begin();
153 ASSERT_NE(it, list.end());
154
155 *it = 10;
156 EXPECT_EQ(*list.begin(), 10);
157}
158
160{
161 DynSetTree<int> set;
162 set.insert(5);
163 set.insert(2);
164 set.insert(8);
165 set.insert(1);
166 set.insert(9);
167
168 EXPECT_TRUE(std::ranges::all_of(set, [](int x) { return x > 0; }));
169
170 auto it = std::ranges::find(set, 5);
171 ASSERT_NE(it, set.end());
172 EXPECT_EQ(*it, 5);
173
174 auto min_it = std::ranges::min_element(set);
175 ASSERT_NE(min_it, set.end());
176 EXPECT_EQ(*min_it, 1);
177}
178
180{
182
183 Iter a;
184 Iter b;
185 EXPECT_TRUE(a == b);
186 EXPECT_FALSE(a != b);
187}
188
190{
191 DynDlist<int> list;
192 list.append(1);
193 list.append(2);
194
195 auto it = list.begin();
196 auto old = it++;
197
198 EXPECT_EQ(*old, 1);
199 EXPECT_EQ(*it, 2);
200}
201
202#else // !ALEPH_HAS_RANGES
203
204// Dummy test when ranges/concepts are not available
206{
207 GTEST_SKIP() << "C++20 concepts not fully supported on this platform";
208}
209
210#endif // ALEPH_HAS_RANGES
C++20 Ranges support and adaptors for Aleph-w containers.
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.
Definition ah-arena.H:89
DynList< T > maps(const C &c, Op op)
Classic map operation.
T sum(const Container &container, const T &init=T{})
Compute sum of all elements.
static StlIterator end(SetType &s)
Create an end iterator for the container.
Dynamic doubly linked list implementation.
Dynamic set implementations based on balanced binary search trees.