Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
arraystack.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 <tpl_arrayStack.H>
41# include <ahFunctional.H>
42
43using namespace std;
44using namespace testing;
45using namespace Aleph;
46
47constexpr size_t N = 17;
48
49struct SimpleStack : public Test
50{
51 size_t n = 0;
54 {
55 for (size_t i = 0; i < N; ++i, ++n)
56 s.push(i);
57 }
58};
59
60struct ComplexStack : public Test
61{
62 size_t n = 0;
65 {
66 for (size_t i = 0; i < N; ++i, ++n)
67 s.push({ int(i), 0, 1, 2, int(i) });
68 }
69};
70
71TEST(ArrayStack, empty_stack)
72{
75 EXPECT_EQ(s.size(), 0);
76 EXPECT_THROW(s.top(), underflow_error);
77 EXPECT_THROW(s.top(10), out_of_range);
78}
79
81{
82 EXPECT_FALSE(s.is_empty());
83 EXPECT_EQ(s.size(), n);
84 EXPECT_EQ(s.top(), n - 1);
85 for (size_t i = 0; i < n; ++i)
86 EXPECT_EQ(s.top(i), n - i - 1);
87
88 const size_t m = 100;
89 for (size_t i = 0; i < m; ++i)
90 EXPECT_EQ(s.push(i), i);
91 EXPECT_EQ(s.size(), n + m);
92
93 for (size_t i = 0; i < m; ++i)
94 EXPECT_EQ(s.pop(), m - i - 1);
95
96 EXPECT_EQ(s.size(), n);
97
98 for (size_t i = 0; i < m; ++i)
99 EXPECT_EQ(s.push(i), i);
100 EXPECT_EQ(s.size(), n + m);
101 EXPECT_EQ(s.popn(m), 0);
102 EXPECT_EQ(s.size(), n);
103
104 for (size_t i = 0; i < n; ++i)
105 EXPECT_EQ(s.pop(), n - i - 1);
106 EXPECT_TRUE(s.is_empty());
107 EXPECT_EQ(s.size(), 0);
108
109 s.pushn(n);
110 EXPECT_EQ(s.size(), n);
111 for (size_t i = 0; i < n; ++i)
112 s.top(i) = i;
113
114 for (size_t i = 0; i < n; ++i)
115 EXPECT_EQ(s.top(i), i);
116
117 EXPECT_EQ(s.popn(n), n - 1);
118 EXPECT_TRUE(s.is_empty());
119 EXPECT_EQ(s.size(), 0);
120
121 for (size_t i = 0; i < m; ++i)
122 EXPECT_EQ(s.push(i), i);
123 EXPECT_EQ(s.size(), m);
124
125 s.empty();
126 EXPECT_TRUE(s.is_empty());
127 EXPECT_EQ(s.size(), 0);
128}
129
131{
132 EXPECT_FALSE(s.is_empty());
133 EXPECT_EQ(s.size(), n);
134 EXPECT_EQ(s.top().get_first(), n - 1);
135 for (size_t i = 0; i < n; ++i)
136 {
137 EXPECT_EQ(s.top(i).get_first(), n - i - 1);
138 EXPECT_EQ(s.top(i).get_last(), n - i - 1);
139 EXPECT_EQ(s.top(i).nth(1), 0);
140 }
141
142 const size_t m = 100;
143 for (size_t i = 0; i < m; ++i)
144 {
145 EXPECT_EQ(s.push({int(i), 0, 1, int(i)}).get_first(), i);
146 EXPECT_EQ(s.top().get_first(), i);
147 EXPECT_EQ(s.top().get_last(), i);
148 EXPECT_EQ(s.top().nth(1), 0);
149 EXPECT_EQ(s.top().nth(2), 1);
150 }
151 EXPECT_EQ(s.size(), n + m);
152
153 for (size_t i = 0; i < m; ++i)
154 {
155 auto l = s.pop();
156 EXPECT_EQ(l.get_first(), m - i - 1);
157 EXPECT_EQ(l.get_last(), m - i - 1);
158 EXPECT_EQ(l.nth(1), 0);
159 EXPECT_EQ(l.nth(2), 1);
160 }
161
162 EXPECT_EQ(s.size(), n);
163
164 for (size_t i = 0; i < m; ++i)
165 {
166 auto & l = s.push({ int(i), 0, 1, int(i) });
167 EXPECT_EQ(l.get_first(), i);
168 EXPECT_EQ(l.get_last(), i);
169 EXPECT_EQ(l.nth(1), 0);
170 EXPECT_EQ(l.nth(2), 1);
171 }
172 EXPECT_EQ(s.size(), n + m);
173 EXPECT_EQ(s.popn(m).get_first(), 0);
174 EXPECT_EQ(s.size(), n);
175
176 for (size_t i = 0; i < n; ++i)
177 {
178 auto l = s.pop();
179 EXPECT_EQ(l.get_first(), n - i - 1);
180 EXPECT_EQ(l.get_last(), n - i - 1);
181 EXPECT_EQ(l.nth(1), 0);
182 EXPECT_EQ(l.nth(2), 1);
183 }
184 EXPECT_TRUE(s.is_empty());
185 EXPECT_EQ(s.size(), 0);
186
187 {
188 auto & ll = s.pushn(n);
190 }
191 EXPECT_EQ(s.size(), n);
192 for (size_t i = 0; i < n; ++i)
193 s.top(i) = { int(i), 0, 1, int(i) };
194
195 for (size_t i = 0; i < n; ++i)
196 {
197 auto & l = s.top(i);
198 EXPECT_EQ(l.get_first(), i);
199 EXPECT_EQ(l.get_last(), i);
200 EXPECT_EQ(l.nth(1), 0);
201 EXPECT_EQ(l.nth(2), 1);
202 }
203
204 {
205 auto ll = s.popn(n);
206 EXPECT_EQ(ll.get_first(), n - 1);
207 EXPECT_EQ(ll.get_last(), n - 1);
208 EXPECT_EQ(ll.nth(1), 0);
209 EXPECT_EQ(ll.nth(2), 1);
210 }
211 EXPECT_TRUE(s.is_empty());
212 EXPECT_EQ(s.size(), 0);
213
214 for (size_t i = 0; i < m; ++i)
215 {
216 auto & l = s.push({ int(i), 0, 1, int(i) });
217 EXPECT_EQ(l.get_first(), i);
218 EXPECT_EQ(l.get_last(), i);
219 EXPECT_EQ(l.nth(1), 0);
220 EXPECT_EQ(l.nth(2), 1);
221 }
222 EXPECT_EQ(s.size(), m);
223
224 s.empty();
225 EXPECT_TRUE(s.is_empty());
226 EXPECT_EQ(s.size(), 0);
227}
228
230{
232 auto it = s.get_it();
233 EXPECT_FALSE(it.has_curr());
234 EXPECT_THROW(it.get_curr(), overflow_error);
235 EXPECT_THROW(it.next(), overflow_error);
236}
237
239{
240 auto it = s.get_it();
241 for (size_t i = 0; it.has_curr(); it.next(), ++i)
242 EXPECT_EQ(it.get_curr(), i);
243
244 it.reset_last();
245 for (size_t i = 0; it.has_curr(); it.prev(), ++i)
246 EXPECT_EQ(it.get_curr(), n - i -1);
247}
248
250{
251 auto it = s.get_it();
252 for (size_t i = 0; it.has_curr(); it.next(), ++i)
253 {
254 EXPECT_EQ(it.get_curr().get_first(), i);
255 EXPECT_EQ(it.get_curr().get_last(), i);
256 EXPECT_EQ(it.get_curr().nth(1), 0);
257 EXPECT_EQ(it.get_curr().nth(2), 1);
258 }
259
260 it.reset_last();
261 for (size_t i = 0; it.has_curr(); it.prev(), ++i)
262 {
263 EXPECT_EQ(it.get_curr().get_first(), n - i -1);
264 EXPECT_EQ(it.get_curr().get_last(), n - i -1);
265 EXPECT_EQ(it.get_curr().nth(1), 0);
266 EXPECT_EQ(it.get_curr().nth(2), 1);
267 }
268}
269
271{
272 { // test copy ctor
275 EXPECT_EQ(s.size(), sc.size());
276 int i = 0;
277 for (; i < sc.size(); ++i)
278 EXPECT_EQ(s.top(i), sc.top(i));
279 EXPECT_EQ(i, s.size());
280 }
281
282 { // test copy assignment
284 sc = s;
286 EXPECT_EQ(s.size(), sc.size());
287 int i = 0;
288 for (; i < sc.size(); ++i)
289 EXPECT_EQ(s.top(i), sc.top(i));
290 EXPECT_EQ(i, s.size());
291 }
292
293 // test move ctor
296 EXPECT_EQ(sc.size(), n);
297 EXPECT_EQ(s.size(), 0);
298 EXPECT_TRUE(s.is_empty());
299 int i = 0;
300 for (; i < sc.size(); ++i)
301 EXPECT_EQ(sc.top(i), n - i - 1);
302 EXPECT_EQ(i, sc.size());
303
304 s = move(sc);
305 EXPECT_FALSE(s.is_empty());
306 EXPECT_EQ(s.size(), n);
308 i = 0;
309 for (; i < s.size(); ++i)
310 EXPECT_EQ(s.top(i), n - i - 1);
311 EXPECT_EQ(i, s.size());
312}
313
315{
316 { // test copy ctor
319 EXPECT_EQ(s.size(), sc.size());
320 int i = 0;
321 for (; i < sc.size(); ++i)
322 EXPECT_TRUE(eq(s.top(i), sc.top(i)));
323 EXPECT_EQ(i, s.size());
324 }
325
326 { // test copy assignment
328 sc = s;
330 EXPECT_EQ(s.size(), sc.size());
331 int i = 0;
332 for (; i < sc.size(); ++i)
333 EXPECT_TRUE(eq(s.top(i), sc.top(i)));
334 EXPECT_EQ(i, s.size());
335 }
336
337 // test move ctor
340 EXPECT_EQ(sc.size(), n);
341 EXPECT_EQ(s.size(), 0);
342 EXPECT_TRUE(s.is_empty());
343 int i = 0;
344 for (; i < sc.size(); ++i)
345 {
346 EXPECT_EQ(sc.top(i).get_first(), n - i - 1);
347 EXPECT_EQ(sc.top(i).get_last(), n - i - 1);
348 EXPECT_EQ(sc.top(i).nth(1), 0);
349 EXPECT_EQ(sc.top(i).nth(2), 1);
350 }
351 EXPECT_EQ(i, sc.size());
352
353 s = move(sc);
354 EXPECT_FALSE(s.is_empty());
355 EXPECT_EQ(s.size(), n);
357 i = 0;
358 for (; i < s.size(); ++i)
359 {
360 EXPECT_EQ(s.top(i).get_first(), n - i - 1);
361 EXPECT_EQ(s.top(i).get_last(), n - i - 1);
362 EXPECT_EQ(s.top(i).nth(1), 0);
363 EXPECT_EQ(s.top(i).nth(2), 1);
364 }
365 EXPECT_EQ(i, s.size());
366}
367
369{
370 size_t i = 0;
371 auto ret = s.traverse([&i] (auto k) { EXPECT_EQ(k, i); return k == i++; });
373 EXPECT_EQ(i, n);
374}
375
377{
378 int i = 0;
379 auto ret = s.traverse([&i] (const DynList<int> & l)
380 {
381 EXPECT_EQ(l.get_first(), i);
382 EXPECT_EQ(l.get_last(), i);
383 EXPECT_EQ(l.nth(1), 0);
384 EXPECT_EQ(l.nth(2), 1);
385 return l.get_first() == i++;
386 });
388 EXPECT_EQ(i, n);
389}
Functional programming utilities for Aleph-w containers.
TEST_F(SimpleStack, push_pop)
Definition arraystack.cc:80
constexpr size_t N
Definition arraystack.cc:47
Stack implemented with simple dynamic array and with bounds verification.
T & top() const
Return a modifiable reference to youngest element of stack (called the top)
bool is_empty() const noexcept
Return true if stack is empty.
size_t size() const noexcept
Return the number of elements stored in the stack.
T & push(const T &data)
Push into stack a copy of data
Dynamic singly linked list with functional programming support.
Definition htlist.H:1423
T & top() const
Definition htlist.H:1683
T & get_last() const
Return the last item of the list.
Definition htlist.H:1663
T & get_first() const
Return the first item of the list.
Definition htlist.H:1675
T & push(const T &item)
Definition htlist.H:1523
constexpr bool is_empty() const noexcept
Return true if list is empty.
Definition htlist.H:523
size_t size() const noexcept
Count the number of elements of the list.
Definition htlist.H:1319
Type & nth(const size_t n)
Return the n-th item of container.
Definition ah-dry.H:267
auto get_it() const
Return a properly initialized iterator positioned at the first item on the container.
Definition ah-dry.H:190
#define TEST(name)
constexpr size_t N
Definition fixedstack.cc:47
Main namespace for Aleph-w library functions.
Definition ah-arena.H:89
bool eq(const C1 &c1, const C2 &c2, Eq e=Eq())
Check equality of two containers using a predicate.
bool traverse(Node *root, Op op)
DynList< T > maps(const C &c, Op op)
Classic map operation.
STL namespace.
Fixture with a stack of complex objects (DynList<int>)
Definition arraystack.cc:61
ArrayStack< DynList< int > > s
Definition arraystack.cc:63
bool traverse(Operation &operation) noexcept(traverse_is_noexcept< Operation >())
Traverse the container via its iterator and performs a conditioned operation on each item.
Definition ah-dry.H:95
Fixture with a stack of integers.
Definition arraystack.cc:50
ArrayStack< int > s
Definition arraystack.cc:52
Stack implementations backed by dynamic or fixed arrays.
DynList< int > l