38# include <gtest/gtest.h>
48using namespace testing;
51constexpr size_t N = 17;
59 for (
size_t i = 0; i <
N; ++i, ++
n)
70 for (
size_t i = 0; i <
N; ++i, ++
n)
71 s.push({ int(i), 0, 1, 2, int(i) });
79 EXPECT_EQ(s.
size(), 0);
80 EXPECT_THROW(s.
top(), underflow_error);
81 EXPECT_THROW(s.
top(10), out_of_range);
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);
93 for (
size_t i = 0; i <
m; ++i)
94 EXPECT_EQ(s.push(i), i);
95 EXPECT_EQ(s.size(), n +
m);
97 for (
size_t i = 0; i <
m; ++i)
98 EXPECT_EQ(s.pop(),
m - i - 1);
100 EXPECT_EQ(s.size(), n);
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);
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);
114 EXPECT_EQ(s.size(), n);
115 for (
size_t i = 0; i < n; ++i)
118 for (
size_t i = 0; i < n; ++i)
119 EXPECT_EQ(s.top(i), i);
121 EXPECT_EQ(s.popn(n), n - 1);
122 EXPECT_TRUE(s.is_empty());
123 EXPECT_EQ(s.size(), 0);
125 for (
size_t i = 0; i <
m; ++i)
126 EXPECT_EQ(s.push(i), i);
127 EXPECT_EQ(s.size(),
m);
130 EXPECT_TRUE(s.is_empty());
131 EXPECT_EQ(s.size(), 0);
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)
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);
146 const size_t m = 100;
147 for (
size_t i = 0; i <
m; ++i)
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);
155 EXPECT_EQ(s.size(), n +
m);
157 for (
size_t i = 0; i <
m; ++i)
162 EXPECT_EQ(
l.
nth(1), 0);
163 EXPECT_EQ(
l.
nth(2), 1);
166 EXPECT_EQ(s.size(), n);
168 for (
size_t i = 0; i <
m; ++i)
170 auto &
l = s.
push({ int(i), 0, 1, int(i) });
173 EXPECT_EQ(
l.
nth(1), 0);
174 EXPECT_EQ(
l.
nth(2), 1);
176 EXPECT_EQ(s.size(), n +
m);
177 EXPECT_EQ(s.popn(
m).get_first(), 0);
178 EXPECT_EQ(s.size(), n);
180 for (
size_t i = 0; i < n; ++i)
185 EXPECT_EQ(
l.
nth(1), 0);
186 EXPECT_EQ(
l.
nth(2), 1);
188 EXPECT_TRUE(s.is_empty());
189 EXPECT_EQ(s.size(), 0);
192 auto & ll = s.pushn(n);
193 EXPECT_TRUE(ll.is_empty());
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) };
199 for (
size_t i = 0; i < n; ++i)
204 EXPECT_EQ(
l.
nth(1), 0);
205 EXPECT_EQ(
l.
nth(2), 1);
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);
215 EXPECT_TRUE(s.is_empty());
216 EXPECT_EQ(s.size(), 0);
218 for (
size_t i = 0; i <
m; ++i)
220 auto &
l = s.
push({ int(i), 0, 1, int(i) });
223 EXPECT_EQ(
l.
nth(1), 0);
224 EXPECT_EQ(
l.
nth(2), 1);
226 EXPECT_EQ(s.size(),
m);
229 EXPECT_TRUE(s.is_empty());
230 EXPECT_EQ(s.size(), 0);
237 EXPECT_FALSE(it.has_curr());
238 EXPECT_THROW(it.get_curr(), overflow_error);
239 EXPECT_THROW(it.next(), overflow_error);
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);
249 for (
size_t i = 0; it.has_curr(); it.prev(), ++i)
250 EXPECT_EQ(it.get_curr(), n - i -1);
255 auto it = s.get_it();
256 for (
size_t i = 0; it.has_curr(); it.next(), ++i)
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);
265 for (
size_t i = 0; it.has_curr(); it.prev(), ++i)
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);
279 EXPECT_EQ(s.size(), sc.
size());
281 for (; i < sc.
size(); ++i)
282 EXPECT_EQ(s.top(i), sc.
top(i));
283 EXPECT_EQ(i, s.size());
290 EXPECT_EQ(s.size(), sc.
size());
292 for (; i < sc.
size(); ++i)
293 EXPECT_EQ(s.top(i), sc.
top(i));
294 EXPECT_EQ(i, s.size());
300 EXPECT_EQ(sc.
size(), n);
301 EXPECT_EQ(s.size(), 0);
302 EXPECT_TRUE(s.is_empty());
304 for (; i < sc.
size(); ++i)
305 EXPECT_EQ(sc.
top(i), n - i - 1);
306 EXPECT_EQ(i, sc.
size());
309 EXPECT_FALSE(s.is_empty());
310 EXPECT_EQ(s.size(), n);
313 for (; i < s.size(); ++i)
314 EXPECT_EQ(s.top(i), n - i - 1);
315 EXPECT_EQ(i, s.size());
323 EXPECT_EQ(s.size(), sc.
size());
325 for (; i < sc.
size(); ++i)
326 EXPECT_TRUE(
eq(s.top(i), sc.
top(i)));
327 EXPECT_EQ(i, s.size());
334 EXPECT_EQ(s.size(), sc.
size());
336 for (; i < sc.
size(); ++i)
337 EXPECT_TRUE(
eq(s.top(i), sc.
top(i)));
338 EXPECT_EQ(i, s.size());
344 EXPECT_EQ(sc.
size(), n);
345 EXPECT_EQ(s.size(), 0);
346 EXPECT_TRUE(s.is_empty());
348 for (; i < sc.
size(); ++i)
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);
355 EXPECT_EQ(i, sc.
size());
358 EXPECT_FALSE(s.is_empty());
359 EXPECT_EQ(s.size(), n);
362 for (; i < s.size(); ++i)
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);
369 EXPECT_EQ(i, s.size());
375 auto ret = s.traverse([&i] (
auto k) { EXPECT_EQ(
k, i);
return k == i++; });
387 EXPECT_EQ(
l.
nth(1), 0);
388 EXPECT_EQ(
l.
nth(2), 1);
401 for (
int i : {10, 20, 30, 40})
405 EXPECT_EQ(s.
push(s.
top()), 40);
406 EXPECT_EQ(s.
push(s.
top(4)), 10);
407 ASSERT_EQ(s.
size(), 6u);
408 for (
int i : {10, 40, 40, 30, 20, 10})
409 EXPECT_EQ(s.
pop(), i);
415 for (
char c : {
'a',
'b',
'c',
'd'})
416 s.
push(
string(40, c));
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'));
427static_assert(not
noexcept(std::declval<ArrayStack<int> &>().base()));
428static_assert(not
noexcept(std::declval<const ArrayStack<int> &>().base()));
434 EXPECT_THROW((
void) s.
base(), std::underflow_error);
435 EXPECT_THROW((
void) cs.
base(), std::underflow_error);
439 EXPECT_EQ(cs.
base(), 10);
441 EXPECT_EQ(s.
top(1), 1);
Functional programming utilities for Aleph-w containers.
TEST_F(SimpleStack, push_pop)
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).
T & get_first()
Return the first item of the list.
T & get_last()
Return the last item of the list.
T & top()
See get_first().
T & push(const T &item)
See insert(const T & item).
Number Theoretic Transform over Z / MOD Z.
Type & nth(const size_t n)
Return the n-th item of the container.
auto get_it()
Return a properly initialized iterator positioned at the first item on the container.
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.
Fixture with a stack of complex objects (DynList<int>)
ArrayStack< DynList< int > > s
Fixture with a stack of integers.
FooMap m(5, fst_unit_pair_hash, snd_unit_pair_hash)
Stack implementations backed by dynamic or fixed arrays.