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