Aleph-w 3.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
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(DynListStack, empty_stack)
72{
75 EXPECT_EQ(s.size(), 0);
76 EXPECT_THROW(s.top(), underflow_error);
77}
78
80{
81 EXPECT_FALSE(s.is_empty());
82 EXPECT_EQ(s.size(), n);
83 EXPECT_EQ(s.top(), n - 1);
84
85 const size_t m = 100;
86 for (size_t i = 0; i < m; ++i)
87 ASSERT_EQ(s.push(i), i);
88 EXPECT_EQ(s.size(), n + m);
89
90 for (size_t i = 0; i < m; ++i)
91 ASSERT_EQ(s.pop(), m - i - 1);
92
93 EXPECT_EQ(s.size(), n);
94
95 for (size_t i = 0; i < m; ++i)
96 ASSERT_EQ(s.push(i), i);
97 EXPECT_EQ(s.size(), n + m);
98
99 for (size_t i = 0; i < m; ++i)
100 ASSERT_EQ(s.pop(), m - i - 1);
101 EXPECT_FALSE(s.is_empty());
102 EXPECT_EQ(s.size(), n);
103
104 s.empty();
105 EXPECT_TRUE(s.is_empty());
106 EXPECT_EQ(s.size(), 0);
107}
108
110{
111 EXPECT_FALSE(s.is_empty());
112 EXPECT_EQ(s.size(), n);
113 EXPECT_EQ(s.top().get_first(), n - 1);
114
115 const size_t m = 100;
116 for (size_t i = 0; i < m; ++i)
117 {
118 ASSERT_EQ(s.push({int(i), 0, 1, int(i)}).get_first(), i);
119 ASSERT_EQ(s.top().get_first(), i);
120 ASSERT_EQ(s.top().get_last(), i);
121 ASSERT_EQ(s.top().nth(1), 0);
122 ASSERT_EQ(s.top().nth(2), 1);
123 }
124 EXPECT_EQ(s.size(), n + m);
125
126 for (size_t i = 0; i < m; ++i)
127 {
128 auto l = s.pop();
129 ASSERT_EQ(l.get_first(), m - i - 1);
130 ASSERT_EQ(l.get_last(), m - i - 1);
131 ASSERT_EQ(l.nth(1), 0);
132 ASSERT_EQ(l.nth(2), 1);
133 }
134
135 EXPECT_EQ(s.size(), n);
136
137 for (size_t i = 0; i < m; ++i)
138 {
139 auto & l = s.push({ int(i), 0, 1, int(i) });
140 ASSERT_EQ(l.get_first(), i);
141 ASSERT_EQ(l.get_last(), i);
142 ASSERT_EQ(l.nth(1), 0);
143 ASSERT_EQ(l.nth(2), 1);
144 }
145 EXPECT_EQ(s.size(), n + m);
146
147 for (size_t i = 0; i < m; ++i)
148 {
149 auto l = s.pop();
150 ASSERT_EQ(l.get_first(), m - i - 1);
151 ASSERT_EQ(l.get_last(), m - i - 1);
152 ASSERT_EQ(l.nth(1), 0);
153 ASSERT_EQ(l.nth(2), 1);
154 }
155 EXPECT_EQ(s.size(), n);
156
157 for (size_t i = 0; i < n; ++i)
158 {
159 auto l = s.pop();
160 ASSERT_EQ(l.get_first(), n - i - 1);
161 ASSERT_EQ(l.get_last(), n - i - 1);
162 ASSERT_EQ(l.nth(1), 0);
163 ASSERT_EQ(l.nth(2), 1);
164 }
165 EXPECT_TRUE(s.is_empty());
166 EXPECT_EQ(s.size(), 0);
167
168 EXPECT_TRUE(s.is_empty());
169 EXPECT_EQ(s.size(), 0);
170
171 for (size_t i = 0; i < m; ++i)
172 {
173 auto & l = s.push({ int(i), 0, 1, int(i) });
174 ASSERT_EQ(l.get_first(), i);
175 ASSERT_EQ(l.get_last(), i);
176 ASSERT_EQ(l.nth(1), 0);
177 ASSERT_EQ(l.nth(2), 1);
178 }
179 EXPECT_EQ(s.size(), m);
180
181 s.empty();
182 EXPECT_TRUE(s.is_empty());
183 EXPECT_EQ(s.size(), 0);
184}
185
187{
189 auto it = s.get_it();
190 EXPECT_FALSE(it.has_curr());
191 EXPECT_THROW(it.get_curr(), overflow_error);
192 EXPECT_THROW(it.next(), overflow_error);
193}
194
196{
197 auto it = s.get_it();
198 for (size_t i = 0; it.has_curr(); it.next(), ++i)
199 ASSERT_EQ(it.get_curr(), n - i - 1);
200}
201
203{
204 auto it = s.get_it();
205 for (size_t i = 0; it.has_curr(); it.next(), ++i)
206 {
207 ASSERT_EQ(it.get_curr().get_first(), n - i -1);
208 ASSERT_EQ(it.get_curr().get_last(), n - i - 1);
209 ASSERT_EQ(it.get_curr().nth(1), 0);
210 ASSERT_EQ(it.get_curr().nth(2), 1);
211 }
212}
213
215{
216 { // test copy ctor
219 EXPECT_EQ(sc.size(), n);
220 EXPECT_EQ(s.size(), sc.size());
221 int i = 0;
222 for (; i < n; ++i)
223 ASSERT_EQ(sc.pop(), n - i - 1);
224 EXPECT_EQ(i, n);
225 }
226
227 { // test copy assignment
229 sc = s;
231 EXPECT_EQ(s.size(), sc.size());
232 int i = 0;
233 for (; i < n; ++i)
234 ASSERT_EQ(sc.pop(), n - i - 1);
235 EXPECT_EQ(i, s.size());
236 }
237
238 // test move ctor
241 EXPECT_EQ(sc.size(), n);
242 EXPECT_EQ(s.size(), 0);
243 EXPECT_TRUE(s.is_empty());
244 int i = 0;
245 for (; i < n; ++i)
246 {
247 s.push(sc.pop());
248 ASSERT_EQ(s.top(), n - i - 1);
249 }
250 EXPECT_EQ(i, n);
251 EXPECT_EQ(s.size(), n);
253
254 sc = move(s);
256 EXPECT_EQ(sc.size(), n);
257 EXPECT_TRUE(s.is_empty());
258 i = 0;
259 for (; i < n; ++i)
260 {
261 s.push(sc.pop());
262 ASSERT_EQ(s.top(), i);
263 }
264 EXPECT_EQ(i, n);
265 EXPECT_EQ(s.size(), n);
267}
268
270{
271 { // test copy ctor
274 ASSERT_EQ(s.size(), sc.size());
275 ASSERT_TRUE(eq(s.top(), sc.top()));
276 }
277
278 { // test copy assignment
280 sc = s;
282 ASSERT_EQ(s.size(), sc.size());
283 ASSERT_TRUE(eq(s.top(), sc.top()));
284 }
285
286 // test move ctor
289 EXPECT_EQ(sc.size(), n);
290 EXPECT_EQ(s.size(), 0);
291 EXPECT_TRUE(s.is_empty());
292
293 EXPECT_EQ(sc.top().get_first(), n - 1);
294 EXPECT_EQ(sc.top().get_last(), n - 1);
295 EXPECT_EQ(sc.top().nth(1), 0);
296 EXPECT_EQ(sc.top().nth(2), 1);
297
298 s = move(sc);
299 EXPECT_FALSE(s.is_empty());
300 EXPECT_EQ(s.size(), n);
302
303 EXPECT_EQ(s.top().get_first(), n - 1);
304 EXPECT_EQ(s.top().get_last(), n - 1);
305 EXPECT_EQ(s.top().nth(1), 0);
306 EXPECT_EQ(s.top().nth(2), 1);
307}
308
310{
311 size_t i = 0;
312 bool ret = s.traverse([&i, N = n] (int k) { return (k == N - i++ - 1); });
314 EXPECT_EQ(i, n);
315}
316
318{
319 int i = 0;
320 auto ret = s.traverse([&i, N = n] (const DynList<int> & l)
321 {
322 return l.get_first() == N - i - 1 and
323 l.get_last() == N - i++ - 1 and
324 l.nth(1) == 0 and l.nth(2) == 1;
325 });
327 EXPECT_EQ(i, n);
328}
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.
constexpr size_t size() const noexcept
Return the number of elements in the stack.
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)
TEST_F(SimpleStack, push_pop)
constexpr size_t N
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
DynListStack< DynList< int > > s
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
DynListStack< int > s
ArrayStack< int > s
Definition arraystack.cc:52
Dynamic stack implementation based on linked lists.
DynList< int > l