Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
fixedstack.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(FixedStack, empty_stack)
72{
75 EXPECT_EQ(s.size(), 0);
76}
77
79{
80 EXPECT_FALSE(s.is_empty());
81 EXPECT_EQ(s.size(), n);
82 EXPECT_EQ(s.top(), n - 1);
83 for (size_t i = 0; i < n; ++i)
84 EXPECT_EQ(s.top(i), n - i - 1);
85
86 for (size_t i = 0; i < n; ++i)
87 EXPECT_EQ(s.pop(), n - i - 1);
88 EXPECT_TRUE(s.is_empty());
89 EXPECT_EQ(s.size(), 0);
90
91 s.pushn(n);
92 EXPECT_EQ(s.size(), n);
93 for (size_t i = 0; i < n; ++i)
94 s.top(i) = i;
95
96 for (size_t i = 0; i < n; ++i)
97 EXPECT_EQ(s.top(i), i);
98
99 EXPECT_EQ(s.popn(n), n - 1);
100 EXPECT_TRUE(s.is_empty());
101 EXPECT_EQ(s.size(), 0);
102
103 s.empty();
104 EXPECT_TRUE(s.is_empty());
105 EXPECT_EQ(s.size(), 0);
106}
107
109{
110 EXPECT_FALSE(s.is_empty());
111 EXPECT_EQ(s.size(), n);
112 EXPECT_EQ(s.top().get_first(), n - 1);
113 for (size_t i = 0; i < n; ++i)
114 {
115 EXPECT_EQ(s.top(i).get_first(), n - i - 1);
116 EXPECT_EQ(s.top(i).get_last(), n - i - 1);
117 EXPECT_EQ(s.top(i).nth(1), 0);
118 }
119
120 for (size_t i = 0; i < n; ++i)
121 {
122 auto l = s.pop();
123 EXPECT_EQ(l.get_first(), n - i - 1);
124 EXPECT_EQ(l.get_last(), n - i - 1);
125 EXPECT_EQ(l.nth(1), 0);
126 EXPECT_EQ(l.nth(2), 1);
127 }
128 EXPECT_TRUE(s.is_empty());
129 EXPECT_EQ(s.size(), 0);
130
131 {
132 auto & ll = s.pushn(n);
134 }
135 EXPECT_EQ(s.size(), n);
136 for (size_t i = 0; i < n; ++i)
137 s.top(i) = { int(i), 0, 1, int(i) };
138
139 for (size_t i = 0; i < n; ++i)
140 {
141 auto & l = s.top(i);
142 EXPECT_EQ(l.get_first(), i);
143 EXPECT_EQ(l.get_last(), i);
144 EXPECT_EQ(l.nth(1), 0);
145 EXPECT_EQ(l.nth(2), 1);
146 }
147
148 {
149 auto ll = s.popn(n);
150 EXPECT_EQ(ll.get_first(), n - 1);
151 EXPECT_EQ(ll.get_last(), n - 1);
152 EXPECT_EQ(ll.nth(1), 0);
153 EXPECT_EQ(ll.nth(2), 1);
154 }
155 EXPECT_TRUE(s.is_empty());
156 EXPECT_EQ(s.size(), 0);
157}
158
160{
162 auto it = s.get_it();
163 ASSERT_FALSE(it.has_curr());
164 ASSERT_THROW(it.get_curr(), overflow_error);
165 ASSERT_THROW(it.next(), overflow_error);
166}
167
169{
170 auto it = s.get_it();
171 for (size_t i = 0; it.has_curr(); it.next(), ++i)
172 ASSERT_EQ(it.get_curr(), i);
173
174 it.reset_last();
175 for (size_t i = 0; it.has_curr(); it.prev(), ++i)
176 ASSERT_EQ(it.get_curr(), n - i -1);
177}
178
180{
181 auto it = s.get_it();
182 for (size_t i = 0; it.has_curr(); it.next(), ++i)
183 {
184 ASSERT_EQ(it.get_curr().get_first(), i);
185 ASSERT_EQ(it.get_curr().get_last(), i);
186 ASSERT_EQ(it.get_curr().nth(1), 0);
187 ASSERT_EQ(it.get_curr().nth(2), 1);
188 }
189
190 it.reset_last();
191 for (size_t i = 0; it.has_curr(); it.prev(), ++i)
192 {
193 ASSERT_EQ(it.get_curr().get_first(), n - i -1);
194 ASSERT_EQ(it.get_curr().get_last(), n - i -1);
195 ASSERT_EQ(it.get_curr().nth(1), 0);
196 ASSERT_EQ(it.get_curr().nth(2), 1);
197 }
198}
199
201{
202 { // test copy ctor
205 ASSERT_EQ(s.size(), sc.size());
206 int i = 0;
207 for (; i < sc.size(); ++i)
208 ASSERT_EQ(s.top(i), sc.top(i));
209 ASSERT_EQ(i, s.size());
210 }
211
212 { // test copy assignment
214 sc = s;
216 ASSERT_EQ(s.size(), sc.size());
217 int i = 0;
218 for (; i < sc.size(); ++i)
219 ASSERT_EQ(s.top(i), sc.top(i));
220 ASSERT_EQ(i, s.size());
221 }
222
223 // test move ctor
226 ASSERT_EQ(sc.size(), n);
227 ASSERT_EQ(s.size(), 0);
228 ASSERT_TRUE(s.is_empty());
229 int i = 0;
230 for (; i < sc.size(); ++i)
231 ASSERT_EQ(sc.top(i), n - i - 1);
232 ASSERT_EQ(i, sc.size());
233
234 s = move(sc);
235 ASSERT_FALSE(s.is_empty());
236 ASSERT_EQ(s.size(), n);
238 i = 0;
239 for (; i < s.size(); ++i)
240 ASSERT_EQ(s.top(i), n - i - 1);
241 ASSERT_EQ(i, s.size());
242}
243
245{
246 { // test copy ctor
249 ASSERT_EQ(s.size(), sc.size());
250 int i = 0;
251 for (; i < sc.size(); ++i)
252 ASSERT_TRUE(eq(s.top(i), sc.top(i)));
253 ASSERT_EQ(i, s.size());
254 }
255
256 { // test copy assignment
258 sc = s;
260 ASSERT_EQ(s.size(), sc.size());
261 int i = 0;
262 for (; i < sc.size(); ++i)
263 ASSERT_TRUE(eq(s.top(i), sc.top(i)));
264 ASSERT_EQ(i, s.size());
265 }
266
267 // test move ctor
270 ASSERT_EQ(sc.size(), n);
271 ASSERT_EQ(s.size(), 0);
272 ASSERT_TRUE(s.is_empty());
273 int i = 0;
274 for (; i < sc.size(); ++i)
275 {
276 ASSERT_EQ(sc.top(i).get_first(), n - i - 1);
277 ASSERT_EQ(sc.top(i).get_last(), n - i - 1);
278 ASSERT_EQ(sc.top(i).nth(1), 0);
279 ASSERT_EQ(sc.top(i).nth(2), 1);
280 }
281 ASSERT_EQ(i, sc.size());
282
283 s = move(sc);
284 ASSERT_FALSE(s.is_empty());
285 ASSERT_EQ(s.size(), n);
287 i = 0;
288 for (; i < s.size(); ++i)
289 {
290 ASSERT_EQ(s.top(i).get_first(), n - i - 1);
291 ASSERT_EQ(s.top(i).get_last(), n - i - 1);
292 ASSERT_EQ(s.top(i).nth(1), 0);
293 ASSERT_EQ(s.top(i).nth(2), 1);
294 }
295 ASSERT_EQ(i, s.size());
296}
297
299{
300 size_t i = 0;
301 s.traverse([&i] (auto k) { EXPECT_EQ(k, i); return k == i++; });
302 ASSERT_EQ(i, n);
303}
304
306{
307 int i = 0;
308 s.traverse([&i] (const DynList<int> & l)
309 {
310 EXPECT_EQ(l.get_first(), i);
311 EXPECT_EQ(l.get_last(), i);
312 EXPECT_EQ(l.nth(1), 0);
313 EXPECT_EQ(l.nth(2), 1);
314 return l.get_first() == i++;
315 });
316 ASSERT_EQ(i, n);
317}
Functional programming utilities for Aleph-w containers.
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
Fixed length stack.
size_t size() const noexcept
Return the number of elements stored in the stack.
bool is_empty() const noexcept
Return true if stack is empty.
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)
Definition fixedstack.cc:78
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
FixedStack< DynList< int > > s
Definition fixedstack.cc:63
Fixture with a stack of integers.
Definition arraystack.cc:50
ArrayStack< int > s
Definition arraystack.cc:52
FixedStack< int > s
Definition fixedstack.cc:52
Stack implementations backed by dynamic or fixed arrays.
DynList< int > l