Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
array-it.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 <array_it.H>
41
42using namespace std;
43using namespace testing;
44using namespace Aleph;
45
47{
48 Array_Container<int> a(nullptr, 0);
50 EXPECT_EQ(a.size(), 0);
51 EXPECT_THROW(a.get_first(), std::underflow_error);
52 EXPECT_THROW(a.get_last(), std::underflow_error);
53}
54
64
66{
67 int ptr[4] = {0, 1, 2, 3};
68 auto c = make_array_container(ptr, 4);
69
70 EXPECT_FALSE(c.is_empty());
71 EXPECT_EQ(c.size(), 4);
72 EXPECT_EQ(c.get_first(), 0);
73 EXPECT_EQ(c.get_last(), 3);
74
75 auto it = c.get_it();
76 for (int i = 0; it.has_curr(); it.next(), ++i)
77 EXPECT_EQ(it.get_curr(), i);
78}
79
81{
82 int ptr[20];
83 Array_Iterator<int> it(ptr, 10, 0);
85 EXPECT_THROW(it.get_curr(), std::overflow_error);
86 EXPECT_THROW(it.next(), std::overflow_error);
87 EXPECT_THROW(it.prev(), std::underflow_error);
88
89 it.reset();
91 EXPECT_THROW(it.get_curr(), std::overflow_error);
92 EXPECT_THROW(it.next(), std::overflow_error);
93 EXPECT_THROW(it.prev(), std::underflow_error);
94
95 it.reset_last();
97 EXPECT_THROW(it.get_curr(), std::underflow_error);
98 EXPECT_THROW(it.next(), std::overflow_error);
99 EXPECT_THROW(it.prev(), std::underflow_error);
100}
101
103{
104 int ptr[10];
105
106 EXPECT_THROW(Array_Iterator<int>(nullptr, 5, 1), std::invalid_argument);
107 EXPECT_THROW(Array_Iterator<int>(ptr, 5, 6), std::domain_error);
108 EXPECT_THROW(Array_Iterator<int>(ptr, 0, 1), std::domain_error);
109 EXPECT_THROW(Array_Iterator<int>(ptr, 5, 3, 4, 5), std::domain_error);
110
111 EXPECT_NO_THROW(Array_Iterator<int>(ptr, 5, 3, 1, 2));
112}
113
114constexpr size_t N = 29;
115
116struct Array_of_n_items : public testing::Test
117{
118 size_t n = 0;
119 int * a = nullptr;
121 {
122 for (size_t i = 0; i < N; ++i, ++n)
123 a[i] = i;
124 }
125 ~Array_of_n_items() { delete [] a; }
126};
127
129{
130 Array_Iterator<int> it = { a, n, n };
131
132 EXPECT_TRUE(it.has_curr());
134 for (size_t i = 0; it.has_curr(); it.next(), ++i)
135 ASSERT_EQ(it.get_curr(), i);
136 EXPECT_THROW(it.get_curr(), std::overflow_error);
137 EXPECT_THROW(it.next(), std::overflow_error);
138
139 it.reset();
140 EXPECT_TRUE(it.has_curr());
142 for (size_t i = 0; it.has_curr(); it.next(), ++i)
143 ASSERT_EQ(it.get_curr(), i);
144 EXPECT_THROW(it.get_curr(), std::overflow_error);
145 EXPECT_THROW(it.next(), std::overflow_error);
146 EXPECT_NO_THROW(it.prev());
147 EXPECT_EQ(it.get_curr(), n - 1);
148
149 it.reset_last();
150 EXPECT_TRUE(it.has_curr());
152 for (size_t i = n - 1; it.has_curr(); it.prev(), --i)
153 ASSERT_EQ(it.get_curr(), i);
154 EXPECT_THROW(it.get_curr(), std::underflow_error);
155 EXPECT_THROW(it.prev(), std::underflow_error);
156 EXPECT_NO_THROW(it.next());
157 EXPECT_EQ(it.get_curr(), 0);
158}
159
161{
162 Array_Container<int> c(a, n);
163 auto it = c.get_it();
164
165 EXPECT_TRUE(it.has_curr());
166 EXPECT_NO_THROW(it.get_curr());
167 for (size_t i = 0; it.has_curr(); it.next(), ++i)
168 ASSERT_EQ(it.get_curr(), i);
169 EXPECT_THROW(it.get_curr(), std::overflow_error);
170 EXPECT_THROW(it.next(), std::overflow_error);
171
172 it.reset();
173 EXPECT_TRUE(it.has_curr());
174 EXPECT_NO_THROW(it.get_curr());
175 for (size_t i = 0; it.has_curr(); it.next(), ++i)
176 ASSERT_EQ(it.get_curr(), i);
177 EXPECT_THROW(it.get_curr(), std::overflow_error);
178 EXPECT_THROW(it.next(), std::overflow_error);
179 EXPECT_NO_THROW(it.prev());
180 EXPECT_EQ(it.get_curr(), n - 1);
181
182 it.reset_last();
183 EXPECT_TRUE(it.has_curr());
184 EXPECT_NO_THROW(it.get_curr());
185 for (size_t i = n - 1; it.has_curr(); it.prev(), --i)
186 ASSERT_EQ(it.get_curr(), i);
187 EXPECT_THROW(it.get_curr(), std::underflow_error);
188 EXPECT_THROW(it.prev(), std::underflow_error);
189 EXPECT_NO_THROW(it.next());
190 EXPECT_EQ(it.get_curr(), 0);
191
192 it.end();
193 EXPECT_FALSE(it.has_curr());
194 EXPECT_THROW(it.get_curr(), std::overflow_error);
195 EXPECT_THROW(it.next(), std::overflow_error);
196 EXPECT_NO_THROW(it.prev());
197 EXPECT_NO_THROW(it.get_curr());
198 EXPECT_EQ(it.get_curr(), n - 1);
199}
200
201struct Array100 : public testing::Test
202{
203 const size_t dim = 100;
204 int * ptr = new int [dim];
206 {
207 for (size_t i = 0; i < dim; ++i)
208 ptr[i] = i;
209 }
210 ~Array100() { delete [] ptr; }
211};
212
214{
215 Array_Iterator<int> it(ptr + 23, 0, 0);
217 ASSERT_THROW(it.get_curr(), std::overflow_error);
218 ASSERT_THROW(it.next(), std::overflow_error);
219 ASSERT_THROW(it.prev(), std::underflow_error);
220}
221
223{
224 // Iterate on [23, 47]
225 Array_Iterator<int> it(ptr + 23, dim - 23 + 1, 47 - 23 + 1);
226
227 ASSERT_TRUE(it.has_curr());
228
229 int i = 23;
230 for (; it.has_curr(); it.next(), ++i)
231 ASSERT_EQ(it.get_curr(), i);
232 ASSERT_EQ(i, 48);
233
234 it.reset_first();
235 i = 23;
236 for (; it.has_curr(); it.next(), ++i)
237 ASSERT_EQ(it.get_curr(), i);
238 ASSERT_EQ(i, 48);
239
240 it.reset_last();
241 ASSERT_TRUE(it.has_curr());
242 for (i = 47; it.has_curr(); --i, it.prev())
243 ASSERT_EQ(it.get_curr(), i);
244 ASSERT_EQ(i, 22);
245}
246
248{
249 // iterate on [47, 7]
250 Array_Iterator<int> it(ptr, dim, dim - 47 + 1 + 7, 47, 7);
251
252 ASSERT_TRUE(it.has_curr());
253 int i = 47;
254 for (; it.has_curr(); it.next(), i = (i + 1) % dim)
255 ASSERT_EQ(it.get_curr(), i);
256 ASSERT_EQ(i, 8);
257
258 it.reset_last();
259 i = 7;
260 for (; it.has_curr(); it.prev(), --i)
261 {
262 ASSERT_EQ(it.get_curr(), i);
263 if (i == 0)
264 i = dim;
265 }
266}
267
269{
270 Array_Iterator<int> it(ptr, dim, dim, 47, 47);
271 ASSERT_TRUE(it.has_curr());
272
273 int i = 47, k = 0;
274 for (; it.has_curr(); it.next(), i = (i + 1) % dim, ++k)
275 ASSERT_EQ(it.get_curr(), i);
276 ASSERT_EQ(k, dim);
277
278 it.reset_last();
279 i = 47, k = 0;
280 for (; it.has_curr(); it.prev(), --i, ++k)
281 {
282 ASSERT_EQ(it.get_curr(), i);
283 if (i == 0)
284 i = dim;
285 }
286 ASSERT_EQ(k, dim);
287}
TEST_F(Array_of_n_items, Iterator_with_simple_bounds)
Definition array-it.cc:128
constexpr size_t N
Definition array-it.cc:114
Iterator wrapper for C++ raw arrays and circular buffers.
Lightweight wrapper that provides Aleph-w container interface for raw arrays.
Definition array_it.H:373
T & get_last() const
Get the last element.
Definition array_it.H:427
T & get_first() const
Get the first element.
Definition array_it.H:416
Iterator get_it() const
Get an iterator to the beginning.
Definition array_it.H:444
constexpr size_t size() const noexcept
Get the number of elements.
Definition array_it.H:405
constexpr bool is_empty() const noexcept
Check if the container is empty.
Definition array_it.H:400
Iterator wrapper for C++ raw arrays.
Definition array_it.H:77
T & get_curr() const
Get the current item with bounds checking.
Definition array_it.H:245
void reset_last() noexcept
Reset the iterator to the last item.
Definition array_it.H:307
void reset() noexcept
Reset the iterator to the first item.
Definition array_it.H:297
void prev()
Move to the previous item with bounds checking.
Definition array_it.H:289
void next()
Advance to the next item with bounds checking.
Definition array_it.H:269
void reset_first() noexcept
Reset the iterator to the first item (alias for reset()).
Definition array_it.H:304
bool has_curr() const noexcept
Check if there is a current valid item.
Definition array_it.H:219
#define TEST(name)
__gmp_expr< typename __gmp_resolve_expr< T, V >::value_type, __gmp_binary_expr< __gmp_expr< T, U >, __gmp_expr< V, W >, __gmp_dim_function > > dim(const __gmp_expr< T, U > &expr1, const __gmp_expr< V, W > &expr2)
Definition gmpfrxx.h:4052
Main namespace for Aleph-w library functions.
Definition ah-arena.H:89
Array_Container< T > make_array_container(T *array, size_t n)
Create an Array_Container from a raw array.
Definition array_it.H:345
DynList< T > maps(const C &c, Op op)
Classic map operation.
STL namespace.
int * ptr
Definition array-it.cc:204
const size_t dim
Definition array-it.cc:203