Aleph-w 7.10.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
dynliststack.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
34
39# include <gtest/gtest.h>
40
41# include <tpl_dynListStack.H>
42# include <stdexcept>
43
44using namespace std;
45using namespace testing;
46using namespace Aleph;
47
48constexpr size_t N = 17;
49
50struct SimpleStack : public Test
51{
52 size_t n = 0;
55 {
56 for (size_t i = 0; i < N; ++i, ++n)
57 s.push(i);
58 }
59};
60
61struct ComplexStack : public Test
62{
63 size_t n = 0;
66 {
67 for (size_t i = 0; i < N; ++i, ++n)
68 s.push({ int(i), 0, 1, 2, int(i) });
69 }
70};
71
72TEST(DynListStack, empty_stack)
73{
75 EXPECT_TRUE(s.is_empty());
76 EXPECT_EQ(s.size(), 0);
77 EXPECT_THROW(s.top(), underflow_error);
78}
79
81{
82 EXPECT_FALSE(s.is_empty());
83 EXPECT_EQ(s.size(), n);
84 EXPECT_EQ(s.top(), n - 1);
85
86 const size_t m = 100;
87 for (size_t i = 0; i < m; ++i)
88 ASSERT_EQ(s.push(i), i);
89 EXPECT_EQ(s.size(), n + m);
90
91 for (size_t i = 0; i < m; ++i)
92 ASSERT_EQ(s.pop(), m - i - 1);
93
94 EXPECT_EQ(s.size(), n);
95
96 for (size_t i = 0; i < m; ++i)
97 ASSERT_EQ(s.push(i), i);
98 EXPECT_EQ(s.size(), n + m);
99
100 for (size_t i = 0; i < m; ++i)
101 ASSERT_EQ(s.pop(), m - i - 1);
102 EXPECT_FALSE(s.is_empty());
103 EXPECT_EQ(s.size(), n);
104
105 s.empty();
106 EXPECT_TRUE(s.is_empty());
107 EXPECT_EQ(s.size(), 0);
108}
109
111{
112 EXPECT_FALSE(s.is_empty());
113 EXPECT_EQ(s.size(), n);
114 EXPECT_EQ(s.top().get_first(), n - 1);
115
116 const size_t m = 100;
117 for (size_t i = 0; i < m; ++i)
118 {
119 ASSERT_EQ(s.push({int(i), 0, 1, int(i)}).get_first(), i);
120 ASSERT_EQ(s.top().get_first(), i);
121 ASSERT_EQ(s.top().get_last(), i);
122 ASSERT_EQ(s.top().nth(1), 0);
123 ASSERT_EQ(s.top().nth(2), 1);
124 }
125 EXPECT_EQ(s.size(), n + m);
126
127 for (size_t i = 0; i < m; ++i)
128 {
129 auto l = s.pop();
130 ASSERT_EQ(l.get_first(), m - i - 1);
131 ASSERT_EQ(l.get_last(), m - i - 1);
132 ASSERT_EQ(l.nth(1), 0);
133 ASSERT_EQ(l.nth(2), 1);
134 }
135
136 EXPECT_EQ(s.size(), n);
137
138 for (size_t i = 0; i < m; ++i)
139 {
140 auto & l = s.push({ int(i), 0, 1, int(i) });
141 ASSERT_EQ(l.get_first(), i);
142 ASSERT_EQ(l.get_last(), i);
143 ASSERT_EQ(l.nth(1), 0);
144 ASSERT_EQ(l.nth(2), 1);
145 }
146 EXPECT_EQ(s.size(), n + m);
147
148 for (size_t i = 0; i < m; ++i)
149 {
150 auto l = s.pop();
151 ASSERT_EQ(l.get_first(), m - i - 1);
152 ASSERT_EQ(l.get_last(), m - i - 1);
153 ASSERT_EQ(l.nth(1), 0);
154 ASSERT_EQ(l.nth(2), 1);
155 }
156 EXPECT_EQ(s.size(), n);
157
158 for (size_t i = 0; i < n; ++i)
159 {
160 auto l = s.pop();
161 ASSERT_EQ(l.get_first(), n - i - 1);
162 ASSERT_EQ(l.get_last(), n - i - 1);
163 ASSERT_EQ(l.nth(1), 0);
164 ASSERT_EQ(l.nth(2), 1);
165 }
166 EXPECT_TRUE(s.is_empty());
167 EXPECT_EQ(s.size(), 0);
168
169 EXPECT_TRUE(s.is_empty());
170 EXPECT_EQ(s.size(), 0);
171
172 for (size_t i = 0; i < m; ++i)
173 {
174 auto & l = s.push({ int(i), 0, 1, int(i) });
175 ASSERT_EQ(l.get_first(), i);
176 ASSERT_EQ(l.get_last(), i);
177 ASSERT_EQ(l.nth(1), 0);
178 ASSERT_EQ(l.nth(2), 1);
179 }
180 EXPECT_EQ(s.size(), m);
181
182 s.empty();
183 EXPECT_TRUE(s.is_empty());
184 EXPECT_EQ(s.size(), 0);
185}
186
187TEST(DynListStack, Iterator_on_empty_stack)
188{
190 auto it = s.get_it();
191 EXPECT_FALSE(it.has_curr());
192 EXPECT_THROW(it.get_curr(), overflow_error);
193 EXPECT_THROW(it.next(), overflow_error);
194}
195
197{
198 auto it = s.get_it();
199 for (size_t i = 0; it.has_curr(); it.next(), ++i)
200 ASSERT_EQ(it.get_curr(), n - i - 1);
201}
202
204{
205 auto it = s.get_it();
206 for (size_t i = 0; it.has_curr(); it.next(), ++i)
207 {
208 ASSERT_EQ(it.get_curr().get_first(), n - i -1);
209 ASSERT_EQ(it.get_curr().get_last(), n - i - 1);
210 ASSERT_EQ(it.get_curr().nth(1), 0);
211 ASSERT_EQ(it.get_curr().nth(2), 1);
212 }
213}
214
215TEST_F(SimpleStack, copy_operations)
216{
217 { // test copy ctor
218 DynListStack<int> sc = s;
219 EXPECT_FALSE(sc.is_empty());
220 EXPECT_EQ(sc.size(), n);
221 EXPECT_EQ(s.size(), sc.size());
222 int i = 0;
223 for (; i < n; ++i)
224 ASSERT_EQ(sc.pop(), n - i - 1);
225 EXPECT_EQ(i, n);
226 }
227
228 { // test copy assignment
230 sc = s;
231 EXPECT_FALSE(sc.is_empty());
232 EXPECT_EQ(s.size(), sc.size());
233 int i = 0;
234 for (; i < n; ++i)
235 ASSERT_EQ(sc.pop(), n - i - 1);
236 EXPECT_EQ(i, s.size());
237 }
238
239 // test move ctor
240 DynListStack<int> sc(move(s));
241 EXPECT_FALSE(sc.is_empty());
242 EXPECT_EQ(sc.size(), n);
243 EXPECT_EQ(s.size(), 0);
244 EXPECT_TRUE(s.is_empty());
245 int i = 0;
246 for (; i < n; ++i)
247 {
248 s.push(sc.pop());
249 ASSERT_EQ(s.top(), n - i - 1);
250 }
251 EXPECT_EQ(i, n);
252 EXPECT_EQ(s.size(), n);
253 EXPECT_TRUE(sc.is_empty());
254
255 sc = move(s);
256 EXPECT_FALSE(sc.is_empty());
257 EXPECT_EQ(sc.size(), n);
258 EXPECT_TRUE(s.is_empty());
259 i = 0;
260 for (; i < n; ++i)
261 {
262 s.push(sc.pop());
263 ASSERT_EQ(s.top(), i);
264 }
265 EXPECT_EQ(i, n);
266 EXPECT_EQ(s.size(), n);
267 EXPECT_TRUE(sc.is_empty());
268}
269
270TEST_F(ComplexStack, copy_operations)
271{
272 { // test copy ctor
274 ASSERT_FALSE(sc.is_empty());
275 ASSERT_EQ(s.size(), sc.size());
276 ASSERT_TRUE(eq(s.top(), sc.top()));
277 }
278
279 { // test copy assignment
281 sc = s;
282 ASSERT_FALSE(sc.is_empty());
283 ASSERT_EQ(s.size(), sc.size());
284 ASSERT_TRUE(eq(s.top(), sc.top()));
285 }
286
287 // test move ctor
288 DynListStack<DynList<int>> sc(move(s));
289 EXPECT_FALSE(sc.is_empty());
290 EXPECT_EQ(sc.size(), n);
291 EXPECT_EQ(s.size(), 0);
292 EXPECT_TRUE(s.is_empty());
293
294 EXPECT_EQ(sc.top().get_first(), n - 1);
295 EXPECT_EQ(sc.top().get_last(), n - 1);
296 EXPECT_EQ(sc.top().nth(1), 0);
297 EXPECT_EQ(sc.top().nth(2), 1);
298
299 s = move(sc);
300 EXPECT_FALSE(s.is_empty());
301 EXPECT_EQ(s.size(), n);
302 EXPECT_TRUE(sc.is_empty());
303
304 EXPECT_EQ(s.top().get_first(), n - 1);
305 EXPECT_EQ(s.top().get_last(), n - 1);
306 EXPECT_EQ(s.top().nth(1), 0);
307 EXPECT_EQ(s.top().nth(2), 1);
308}
309
311{
312 size_t i = 0;
313 bool ret = s.traverse([&i, N = n] (int k) { return (k == N - i++ - 1); });
314 EXPECT_TRUE(ret);
315 EXPECT_EQ(i, n);
316}
317
319{
320 int i = 0;
321 auto ret = s.traverse([&i, N = n] (const DynList<int> & l)
322 {
323 return l.get_first() == N - i - 1 and
324 l.get_last() == N - i++ - 1 and
325 l.nth(1) == 0 and l.nth(2) == 1;
326 });
327 EXPECT_TRUE(ret);
328 EXPECT_EQ(i, n);
329}
330
331
332namespace
333{
334 // Its move throws while fail is true; moves counts them
335 struct Fragile_Move
336 {
337 static inline bool fail = false;
338 static inline int moves = 0;
339 int value = 0;
340
341 Fragile_Move(int v = 0) noexcept : value(v) {}
342 Fragile_Move(const Fragile_Move &) = default;
343 Fragile_Move(Fragile_Move && c) : value(c.value)
344 {
345 ++moves;
346 if (fail)
347 throw std::runtime_error("move failed");
348 }
349 Fragile_Move & operator = (const Fragile_Move &) = default;
350 Fragile_Move & operator = (Fragile_Move &&) = default;
351 };
352} // namespace
353
354// pop() took the item from a list that had dropped its node already, so that
355// a move that threw lost the item and left the size one more than the items
356TEST(DynListStack, AMoveThatThrowsLeavesTheItemInTheStack)
357{
359 for (int i = 1; i <= 3; ++i)
360 stack.push(Fragile_Move(i));
361
362 Fragile_Move::fail = true;
363 EXPECT_THROW((void) stack.pop(), std::runtime_error);
364 EXPECT_THROW((void) stack.get(), std::runtime_error);
365 Fragile_Move::fail = false;
366
367 ASSERT_EQ(stack.size(), 3u);
368 EXPECT_EQ(stack.top().value, 3);
369 Fragile_Move::moves = 0;
370 EXPECT_EQ(stack.pop().value, 3); // one move, straight into the result
371 EXPECT_EQ(Fragile_Move::moves, 1);
372 EXPECT_EQ(stack.size(), 2u);
373
375 EXPECT_THROW((void) empty.pop(), std::underflow_error);
376 EXPECT_EQ(empty.size(), 0u);
377}
378
379// The stack had a count of its own, which a delete through its iterator, that
380// removes from the list, left one more than the items. Now its size is that
381// of its list
382TEST(DynListStack, ADeleteThroughTheIteratorLowersTheSize)
383{
384 DynListStack<int> stack;
385 for (int i = 1; i <= 3; ++i)
386 stack.push(i);
387
389 it.next();
390 EXPECT_EQ(it.del(), 2);
391 EXPECT_EQ(stack.size(), 2u);
392 EXPECT_EQ(stack.pop(), 3);
393 EXPECT_EQ(stack.pop(), 1);
394 EXPECT_EQ(stack.size(), 0u);
395 EXPECT_TRUE(stack.is_empty());
396}
size_t size_t int32_t value
Definition ca-c-api.h:116
T & push(const T &data)
Push into stack a copy of data
Dynamic stack of elements of generic type T based on a singly linked list.
T & top()
Return a modifiable reference to the top item of the stack.
bool is_empty() const noexcept
Check if the stack is empty.
size_t size() const noexcept
Return the number of elements in the stack.
T get()
Alias for pop() - removes and returns the top item.
T pop()
Remove and return the top item of the stack.
T & push(const T &data)
Push an item by copy onto the top of the stack.
T del()
Remove the current item of the iterator.
Definition htlist.H:1695
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 & 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)
TEST_F(SimpleStack, push_pop)
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.
and
Check uniqueness with explicit hash + equality functors.
STL namespace.
Iterator for traversing elements of the stack.
Fixture with a stack of complex objects (DynList<int>)
Definition arraystack.cc:65
ArrayStack< DynList< int > > s
Definition arraystack.cc:67
DynListStack< DynList< int > > s
Fixture with a stack of integers.
Definition arraystack.cc:54
DynListStack< int > s
ArrayStack< int > s
Definition arraystack.cc:56
FooMap m(5, fst_unit_pair_hash, snd_unit_pair_hash)
static int * k
Dynamic stack implementation based on linked lists.
DynList< int > l