Aleph-w 7.10.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 <stdexcept>
41# include <string>
42# include <utility>
43
44# include <tpl_arrayStack.H>
45# include <ahFunctional.H>
46
47using namespace std;
48using namespace testing;
49using namespace Aleph;
50
51constexpr size_t N = 17;
52
53struct SimpleStack : public Test
54{
55 size_t n = 0;
58 {
59 for (size_t i = 0; i < N; ++i, ++n)
60 s.push(i);
61 }
62};
63
64struct ComplexStack : public Test
65{
66 size_t n = 0;
69 {
70 for (size_t i = 0; i < N; ++i, ++n)
71 s.push({ int(i), 0, 1, 2, int(i) });
72 }
73};
74
75TEST(ArrayStack, empty_stack)
76{
78 EXPECT_TRUE(s.is_empty());
79 EXPECT_EQ(s.size(), 0);
80 EXPECT_THROW(s.top(), underflow_error);
81 EXPECT_THROW(s.top(10), out_of_range);
82}
83
85{
86 EXPECT_FALSE(s.is_empty());
87 EXPECT_EQ(s.size(), n);
88 EXPECT_EQ(s.top(), n - 1);
89 for (size_t i = 0; i < n; ++i)
90 EXPECT_EQ(s.top(i), n - i - 1);
91
92 const size_t m = 100;
93 for (size_t i = 0; i < m; ++i)
94 EXPECT_EQ(s.push(i), i);
95 EXPECT_EQ(s.size(), n + m);
96
97 for (size_t i = 0; i < m; ++i)
98 EXPECT_EQ(s.pop(), m - i - 1);
99
100 EXPECT_EQ(s.size(), n);
101
102 for (size_t i = 0; i < m; ++i)
103 EXPECT_EQ(s.push(i), i);
104 EXPECT_EQ(s.size(), n + m);
105 EXPECT_EQ(s.popn(m), 0);
106 EXPECT_EQ(s.size(), n);
107
108 for (size_t i = 0; i < n; ++i)
109 EXPECT_EQ(s.pop(), n - i - 1);
110 EXPECT_TRUE(s.is_empty());
111 EXPECT_EQ(s.size(), 0);
112
113 s.pushn(n);
114 EXPECT_EQ(s.size(), n);
115 for (size_t i = 0; i < n; ++i)
116 s.top(i) = i;
117
118 for (size_t i = 0; i < n; ++i)
119 EXPECT_EQ(s.top(i), i);
120
121 EXPECT_EQ(s.popn(n), n - 1);
122 EXPECT_TRUE(s.is_empty());
123 EXPECT_EQ(s.size(), 0);
124
125 for (size_t i = 0; i < m; ++i)
126 EXPECT_EQ(s.push(i), i);
127 EXPECT_EQ(s.size(), m);
128
129 s.empty();
130 EXPECT_TRUE(s.is_empty());
131 EXPECT_EQ(s.size(), 0);
132}
133
135{
136 EXPECT_FALSE(s.is_empty());
137 EXPECT_EQ(s.size(), n);
138 EXPECT_EQ(s.top().get_first(), n - 1);
139 for (size_t i = 0; i < n; ++i)
140 {
141 EXPECT_EQ(s.top(i).get_first(), n - i - 1);
142 EXPECT_EQ(s.top(i).get_last(), n - i - 1);
143 EXPECT_EQ(s.top(i).nth(1), 0);
144 }
145
146 const size_t m = 100;
147 for (size_t i = 0; i < m; ++i)
148 {
149 EXPECT_EQ(s.push({int(i), 0, 1, int(i)}).get_first(), i);
150 EXPECT_EQ(s.top().get_first(), i);
151 EXPECT_EQ(s.top().get_last(), i);
152 EXPECT_EQ(s.top().nth(1), 0);
153 EXPECT_EQ(s.top().nth(2), 1);
154 }
155 EXPECT_EQ(s.size(), n + m);
156
157 for (size_t i = 0; i < m; ++i)
158 {
159 auto l = s.pop();
160 EXPECT_EQ(l.get_first(), m - i - 1);
161 EXPECT_EQ(l.get_last(), m - i - 1);
162 EXPECT_EQ(l.nth(1), 0);
163 EXPECT_EQ(l.nth(2), 1);
164 }
165
166 EXPECT_EQ(s.size(), n);
167
168 for (size_t i = 0; i < m; ++i)
169 {
170 auto & l = s.push({ int(i), 0, 1, int(i) });
171 EXPECT_EQ(l.get_first(), i);
172 EXPECT_EQ(l.get_last(), i);
173 EXPECT_EQ(l.nth(1), 0);
174 EXPECT_EQ(l.nth(2), 1);
175 }
176 EXPECT_EQ(s.size(), n + m);
177 EXPECT_EQ(s.popn(m).get_first(), 0);
178 EXPECT_EQ(s.size(), n);
179
180 for (size_t i = 0; i < n; ++i)
181 {
182 auto l = s.pop();
183 EXPECT_EQ(l.get_first(), n - i - 1);
184 EXPECT_EQ(l.get_last(), n - i - 1);
185 EXPECT_EQ(l.nth(1), 0);
186 EXPECT_EQ(l.nth(2), 1);
187 }
188 EXPECT_TRUE(s.is_empty());
189 EXPECT_EQ(s.size(), 0);
190
191 {
192 auto & ll = s.pushn(n);
193 EXPECT_TRUE(ll.is_empty());
194 }
195 EXPECT_EQ(s.size(), n);
196 for (size_t i = 0; i < n; ++i)
197 s.top(i) = { int(i), 0, 1, int(i) };
198
199 for (size_t i = 0; i < n; ++i)
200 {
201 auto & l = s.top(i);
202 EXPECT_EQ(l.get_first(), i);
203 EXPECT_EQ(l.get_last(), i);
204 EXPECT_EQ(l.nth(1), 0);
205 EXPECT_EQ(l.nth(2), 1);
206 }
207
208 {
209 auto ll = s.popn(n);
210 EXPECT_EQ(ll.get_first(), n - 1);
211 EXPECT_EQ(ll.get_last(), n - 1);
212 EXPECT_EQ(ll.nth(1), 0);
213 EXPECT_EQ(ll.nth(2), 1);
214 }
215 EXPECT_TRUE(s.is_empty());
216 EXPECT_EQ(s.size(), 0);
217
218 for (size_t i = 0; i < m; ++i)
219 {
220 auto & l = s.push({ int(i), 0, 1, int(i) });
221 EXPECT_EQ(l.get_first(), i);
222 EXPECT_EQ(l.get_last(), i);
223 EXPECT_EQ(l.nth(1), 0);
224 EXPECT_EQ(l.nth(2), 1);
225 }
226 EXPECT_EQ(s.size(), m);
227
228 s.empty();
229 EXPECT_TRUE(s.is_empty());
230 EXPECT_EQ(s.size(), 0);
231}
232
233TEST(ArrayStack, Iterator_on_empty_stack)
234{
236 auto it = s.get_it();
237 EXPECT_FALSE(it.has_curr());
238 EXPECT_THROW(it.get_curr(), overflow_error);
239 EXPECT_THROW(it.next(), overflow_error);
240}
241
243{
244 auto it = s.get_it();
245 for (size_t i = 0; it.has_curr(); it.next(), ++i)
246 EXPECT_EQ(it.get_curr(), i);
247
248 it.reset_last();
249 for (size_t i = 0; it.has_curr(); it.prev(), ++i)
250 EXPECT_EQ(it.get_curr(), n - i -1);
251}
252
254{
255 auto it = s.get_it();
256 for (size_t i = 0; it.has_curr(); it.next(), ++i)
257 {
258 EXPECT_EQ(it.get_curr().get_first(), i);
259 EXPECT_EQ(it.get_curr().get_last(), i);
260 EXPECT_EQ(it.get_curr().nth(1), 0);
261 EXPECT_EQ(it.get_curr().nth(2), 1);
262 }
263
264 it.reset_last();
265 for (size_t i = 0; it.has_curr(); it.prev(), ++i)
266 {
267 EXPECT_EQ(it.get_curr().get_first(), n - i -1);
268 EXPECT_EQ(it.get_curr().get_last(), n - i -1);
269 EXPECT_EQ(it.get_curr().nth(1), 0);
270 EXPECT_EQ(it.get_curr().nth(2), 1);
271 }
272}
273
274TEST_F(SimpleStack, copy_operations)
275{
276 { // test copy ctor
277 ArrayStack<int> sc = s;
278 EXPECT_FALSE(sc.is_empty());
279 EXPECT_EQ(s.size(), sc.size());
280 int i = 0;
281 for (; i < sc.size(); ++i)
282 EXPECT_EQ(s.top(i), sc.top(i));
283 EXPECT_EQ(i, s.size());
284 }
285
286 { // test copy assignment
288 sc = s;
289 EXPECT_FALSE(sc.is_empty());
290 EXPECT_EQ(s.size(), sc.size());
291 int i = 0;
292 for (; i < sc.size(); ++i)
293 EXPECT_EQ(s.top(i), sc.top(i));
294 EXPECT_EQ(i, s.size());
295 }
296
297 // test move ctor
298 ArrayStack<int> sc(move(s));
299 EXPECT_FALSE(sc.is_empty());
300 EXPECT_EQ(sc.size(), n);
301 EXPECT_EQ(s.size(), 0);
302 EXPECT_TRUE(s.is_empty());
303 int i = 0;
304 for (; i < sc.size(); ++i)
305 EXPECT_EQ(sc.top(i), n - i - 1);
306 EXPECT_EQ(i, sc.size());
307
308 s = move(sc);
309 EXPECT_FALSE(s.is_empty());
310 EXPECT_EQ(s.size(), n);
311 EXPECT_TRUE(sc.is_empty());
312 i = 0;
313 for (; i < s.size(); ++i)
314 EXPECT_EQ(s.top(i), n - i - 1);
315 EXPECT_EQ(i, s.size());
316}
317
318TEST_F(ComplexStack, copy_operations)
319{
320 { // test copy ctor
322 EXPECT_FALSE(sc.is_empty());
323 EXPECT_EQ(s.size(), sc.size());
324 int i = 0;
325 for (; i < sc.size(); ++i)
326 EXPECT_TRUE(eq(s.top(i), sc.top(i)));
327 EXPECT_EQ(i, s.size());
328 }
329
330 { // test copy assignment
332 sc = s;
333 EXPECT_FALSE(sc.is_empty());
334 EXPECT_EQ(s.size(), sc.size());
335 int i = 0;
336 for (; i < sc.size(); ++i)
337 EXPECT_TRUE(eq(s.top(i), sc.top(i)));
338 EXPECT_EQ(i, s.size());
339 }
340
341 // test move ctor
342 ArrayStack<DynList<int>> sc(move(s));
343 EXPECT_FALSE(sc.is_empty());
344 EXPECT_EQ(sc.size(), n);
345 EXPECT_EQ(s.size(), 0);
346 EXPECT_TRUE(s.is_empty());
347 int i = 0;
348 for (; i < sc.size(); ++i)
349 {
350 EXPECT_EQ(sc.top(i).get_first(), n - i - 1);
351 EXPECT_EQ(sc.top(i).get_last(), n - i - 1);
352 EXPECT_EQ(sc.top(i).nth(1), 0);
353 EXPECT_EQ(sc.top(i).nth(2), 1);
354 }
355 EXPECT_EQ(i, sc.size());
356
357 s = move(sc);
358 EXPECT_FALSE(s.is_empty());
359 EXPECT_EQ(s.size(), n);
360 EXPECT_TRUE(sc.is_empty());
361 i = 0;
362 for (; i < s.size(); ++i)
363 {
364 EXPECT_EQ(s.top(i).get_first(), n - i - 1);
365 EXPECT_EQ(s.top(i).get_last(), n - i - 1);
366 EXPECT_EQ(s.top(i).nth(1), 0);
367 EXPECT_EQ(s.top(i).nth(2), 1);
368 }
369 EXPECT_EQ(i, s.size());
370}
371
373{
374 size_t i = 0;
375 auto ret = s.traverse([&i] (auto k) { EXPECT_EQ(k, i); return k == i++; });
376 EXPECT_TRUE(ret);
377 EXPECT_EQ(i, n);
378}
379
381{
382 int i = 0;
383 auto ret = s.traverse([&i] (const DynList<int> & l)
384 {
385 EXPECT_EQ(l.get_first(), i);
386 EXPECT_EQ(l.get_last(), i);
387 EXPECT_EQ(l.nth(1), 0);
388 EXPECT_EQ(l.nth(2), 1);
389 return l.get_first() == i++;
390 });
391 EXPECT_TRUE(ret);
392 EXPECT_EQ(i, n);
393}
394
395// The argument of push() may be an item of the stack itself, which growth
396// used to free before reading it.
397
398TEST(ArrayStack, push_of_own_item_copies_it_before_growing)
399{
400 ArrayStack<int> s(4);
401 for (int i : {10, 20, 30, 40})
402 s.push(i);
403 ASSERT_EQ(s.size(), s.capacity());
404
405 EXPECT_EQ(s.push(s.top()), 40);
406 EXPECT_EQ(s.push(s.top(4)), 10); // with free capacity
407 ASSERT_EQ(s.size(), 6u);
408 for (int i : {10, 40, 40, 30, 20, 10})
409 EXPECT_EQ(s.pop(), i);
410}
411
412TEST(ArrayStack, push_of_own_moved_item_inserts_its_value)
413{
415 for (char c : {'a', 'b', 'c', 'd'})
416 s.push(string(40, c));
417 ASSERT_EQ(s.size(), s.capacity());
418
419 EXPECT_EQ(s.push(std::move(s.top(3))), string(40, 'a'));
420 ASSERT_EQ(s.size(), 5u);
421 EXPECT_EQ(s.top(1), string(40, 'd'));
422 EXPECT_EQ(s.top(3), string(40, 'b'));
423}
424
425// base() was noexcept, so the underflow_error thrown on an empty stack
426// called std::terminate.
427static_assert(not noexcept(std::declval<ArrayStack<int> &>().base()));
428static_assert(not noexcept(std::declval<const ArrayStack<int> &>().base()));
429
430TEST(ArrayStack, base_of_empty_stack_throws)
431{
433 const ArrayStack<int> &cs = s;
434 EXPECT_THROW((void) s.base(), std::underflow_error);
435 EXPECT_THROW((void) cs.base(), std::underflow_error);
436
437 s.push(10);
438 s.push(20);
439 EXPECT_EQ(cs.base(), 10);
440 s.base() = 1;
441 EXPECT_EQ(s.top(1), 1);
442}
Functional programming utilities for Aleph-w containers.
TEST_F(SimpleStack, push_pop)
Definition arraystack.cc:84
Stack implemented with simple dynamic array and with bounds verification.
T & push(const T &data)
Push into stack a copy of data
T & top()
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 pop()
Extract the last more recently inserted element.
size_t capacity() const noexcept
Return the internal capacity.
T & base()
Return a modifiable reference to the oldest element (the base).
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 pop()
See remove().
Definition htlist.H:1479
T & get_last()
Return the last item of the list.
Definition htlist.H:1529
T & top()
See get_first().
Definition htlist.H:1563
T & push(const T &item)
See insert(const T & item).
Definition htlist.H:1329
Number Theoretic Transform over Z / MOD Z.
Definition ntt.H:115
Type & nth(const size_t n)
Return the n-th item of the container.
Definition ah-dry.H:774
auto get_it()
Return a properly initialized iterator positioned at the first item on the container.
Definition ah-dry.H:663
#define TEST(name)
constexpr size_t N
Definition fixedstack.cc:47
bool traverse(Node *root, Op op)
See infix_traverse().
Main namespace for Aleph-w library functions.
bool eq(const C1 &c1, const C2 &c2, Eq e=Eq())
Check equality of two containers using a predicate.
STL namespace.
Fixture with a stack of complex objects (DynList<int>)
Definition arraystack.cc:65
ArrayStack< DynList< int > > s
Definition arraystack.cc:67
Fixture with a stack of integers.
Definition arraystack.cc:54
ArrayStack< int > s
Definition arraystack.cc:56
FooMap m(5, fst_unit_pair_hash, snd_unit_pair_hash)
Dnode< int > Test
Definition testDnode.C:42
static int * k
Stack implementations backed by dynamic or fixed arrays.
DynList< int > l