Aleph-w 3.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 <ahFunctional.H>
42
43using namespace std;
44using namespace testing;
45using namespace Aleph;
46
47constexpr size_t N = 17;
48
49struct SimpleQueue : public Test
50{
51 size_t n = 0;
54 {
55 for (size_t i = 0; i < N; ++i, ++n)
56 q.put(i);
57 }
58};
59
60struct ComplexQueue : public Test
61{
62 size_t n = 0;
65 {
66 for (size_t i = 0; i < N; ++i, ++n)
67 q.put({ int(i), 0, 1, 2, int(i) });
68 }
69};
70
71TEST(DynListQueue, empty_queue)
72{
75 EXPECT_EQ(q.size(), 0);
76 EXPECT_THROW(q.front(), underflow_error);
77 EXPECT_THROW(q.rear(), underflow_error);
78}
79
81{
82 EXPECT_FALSE(q.is_empty());
83 EXPECT_EQ(q.size(), n);
84 EXPECT_EQ(q.front(), 0);
85 EXPECT_EQ(q.rear(), n - 1);
86
87 const size_t m = 100;
88 for (size_t i = 0; i < m; ++i)
89 ASSERT_EQ(q.put(i), i);
90 EXPECT_EQ(q.size(), n + m);
91
92 for (size_t i = 0; i < n; ++i)
93 ASSERT_EQ(q.get(), i);
94
95 EXPECT_EQ(q.rear(), m - 1);
96 EXPECT_EQ(q.front(), 0);
97 EXPECT_EQ(q.size(), m);
98
99 for (size_t i = 0; i < m; ++i)
100 ASSERT_EQ(q.get(), i);
101
102 EXPECT_TRUE(q.is_empty());
103 EXPECT_EQ(q.size(), 0);
104
105 for (size_t i = 0; i < m; ++i)
106 EXPECT_EQ(q.put(i), i);
107 EXPECT_EQ(q.size(), m);
108
109 q.empty();
110 EXPECT_TRUE(q.is_empty());
111 EXPECT_EQ(q.size(), 0);
112}
113
115{
116 EXPECT_FALSE(q.is_empty());
117 EXPECT_EQ(q.size(), n);
118 EXPECT_EQ(q.rear().get_first(), n - 1);
119 EXPECT_EQ(q.front().get_first(), 0);
120
121 const size_t m = 100;
122 for (size_t i = 0; i < m; ++i)
123 {
124 ASSERT_EQ(q.put({int(i), 0, 1, int(i)}).get_first(), i);
125 ASSERT_EQ(q.rear().get_first(), i);
126 ASSERT_EQ(q.rear().get_last(), i);
127 ASSERT_EQ(q.rear().nth(1), 0);
128 ASSERT_EQ(q.rear().nth(2), 1);
129 }
130 EXPECT_EQ(q.size(), n + m);
131
132 for (size_t i = 0; i < n; ++i) // extract n items
133 {
134 auto l = q.get();
135 ASSERT_EQ(l.get_first(), i);
136 ASSERT_EQ(l.get_last(), i);
137 ASSERT_EQ(l.nth(1), 0);
138 ASSERT_EQ(l.nth(2), 1);
139 }
140
141 EXPECT_EQ(q.rear().get_first(), m - 1);
142 EXPECT_EQ(q.rear().get_last(), m - 1);
143 EXPECT_EQ(q.front().get_first(), 0);
144 EXPECT_EQ(q.front().get_last(), 0);
145 EXPECT_EQ(q.size(), m);
146
147 for (size_t i = 0; i < m; ++i)
148 {
149 auto l = q.get();
150 ASSERT_EQ(l.get_first(), i);
151 ASSERT_EQ(l.get_last(), i);
152 ASSERT_EQ(l.nth(1), 0);
153 ASSERT_EQ(l.nth(2), 1);
154 }
155 EXPECT_EQ(q.size(), 0);
156 EXPECT_TRUE(q.is_empty());
157
158 for (size_t i = 0; i < m; ++i)
159 {
160 auto & l = q.put({ int(i), 0, 1, int(i) });
161 EXPECT_EQ(l.get_first(), i);
162 EXPECT_EQ(l.get_last(), i);
163 EXPECT_EQ(l.nth(1), 0);
164 EXPECT_EQ(l.nth(2), 1);
165 }
166 EXPECT_EQ(q.size(), m);
167
168 q.empty();
169 EXPECT_TRUE(q.is_empty());
170 EXPECT_EQ(q.size(), 0);
171}
172
174{
176 auto it = q.get_it();
177 EXPECT_FALSE(it.has_curr());
178 EXPECT_THROW(it.get_curr(), overflow_error);
179 EXPECT_THROW(it.next(), overflow_error);
180}
181
183{
184 auto it = q.get_it();
185 for (size_t i = 0; it.has_curr(); it.next(), ++i)
186 ASSERT_EQ(it.get_curr(), i);
187}
188
190{
191 auto it = q.get_it();
192 for (size_t i = 0; it.has_curr(); it.next(), ++i)
193 {
194 ASSERT_EQ(it.get_curr().get_first(), i);
195 ASSERT_EQ(it.get_curr().get_last(), i);
196 ASSERT_EQ(it.get_curr().nth(1), 0);
197 ASSERT_EQ(it.get_curr().nth(2), 1);
198 }
199}
200
202{
203 { // test copy ctor
206 EXPECT_EQ(qc.size(), n);
207 EXPECT_EQ(q.size(), qc.size());
208 int i = 0;
209 for (; i < n; ++i)
210 ASSERT_EQ(qc.get(), i);
211 EXPECT_EQ(i, n);
212 }
213
214 { // test copy assignment
216 qc = q;
218 EXPECT_EQ(q.size(), qc.size());
219 int i = 0;
220 for (; i < n; ++i)
221 ASSERT_EQ(qc.get(), i);
222 EXPECT_EQ(i, q.size());
223 }
224
225 // test move ctor
228 EXPECT_EQ(qc.size(), n);
229 EXPECT_EQ(q.size(), 0);
230 EXPECT_TRUE(q.is_empty());
231 int i = 0;
232 for (; i < n; ++i)
233 {
234 q.put(qc.get());
235 ASSERT_EQ(q.rear(), i);
236 ASSERT_EQ(q.front(), 0);
237 }
238
239 EXPECT_EQ(i, n);
240 EXPECT_EQ(q.size(), n);
242
243 qc = move(q);
245 EXPECT_EQ(qc.size(), n);
246 EXPECT_TRUE(q.is_empty());
247 i = 0;
248 for (; i < n; ++i)
249 {
250 q.put(qc.get());
251 ASSERT_EQ(q.rear(), i);
252 ASSERT_EQ(q.front(), 0);
253 }
254 EXPECT_EQ(i, n);
255 EXPECT_EQ(q.size(), n);
257}
258
260{
261 { // test copy ctor
264 EXPECT_EQ(q.size(), qc.size());
265 EXPECT_TRUE(eq(q.front(), qc.front()));
266 EXPECT_TRUE(eq(q.rear(), qc.rear()));
267 }
268
269 { // test copy assignment
271 qc = q;
273 EXPECT_EQ(q.size(), qc.size());
274 EXPECT_TRUE(eq(q.rear(), qc.rear()));
275 EXPECT_TRUE(eq(q.front(), qc.front()));
276 }
277
278 // test move ctor
281 EXPECT_EQ(qc.size(), n);
282 EXPECT_EQ(q.size(), 0);
283 EXPECT_TRUE(q.is_empty());
284
285 int k = 0;
286 for (auto it = qc.get_it(); it.has_curr(); it.next(), ++k)
287 {
288 auto & l = it.get_curr();
290 ASSERT_EQ(l.get_last(), k);
291 ASSERT_EQ(l.nth(1), 0);
292 ASSERT_EQ(l.nth(2), 1);
293 }
294
295 EXPECT_EQ(qc.rear().get_first(), n - 1);
296 EXPECT_EQ(qc.rear().get_last(), n - 1);
297 EXPECT_EQ(qc.rear().nth(1), 0);
298 EXPECT_EQ(qc.rear().nth(2), 1);
299
300 q = move(qc);
301 EXPECT_FALSE(q.is_empty());
302 EXPECT_EQ(q.size(), n);
304
305 k = 0;
306 for (auto it = qc.get_it(); it.has_curr(); it.next(), ++k)
307 {
308 auto & l = it.get_curr();
310 ASSERT_EQ(l.get_last(), k);
311 ASSERT_EQ(l.nth(1), 0);
312 ASSERT_EQ(l.nth(2), 1);
313 }
314
315 EXPECT_EQ(q.rear().get_first(), n - 1);
316 EXPECT_EQ(q.rear().get_last(), n - 1);
317 EXPECT_EQ(q.rear().nth(1), 0);
318 EXPECT_EQ(q.rear().nth(2), 1);
319}
320
322{
323 size_t i = 0;
324 bool ret = q.traverse([&i] (int k) { return (k == i++); });
326 EXPECT_EQ(i, n);
327}
328
330{
331 int i = 0;
332 auto ret = q.traverse([&i] (const DynList<int> & l)
333 {
334 return l.get_first() == i and
335 l.get_last() == i++ and
336 l.nth(1) == 0 and l.nth(2) == 1;
337 });
339 EXPECT_EQ(i, n);
340}
341
342
343
Functional programming utilities for Aleph-w containers.
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.
constexpr size_t size() const noexcept
Return the number of elements.
T & front()
Return a modifiable reference to the oldest item in the queue.
T & rear()
Return a modifiable reference to the youngest item in the queue.
bool is_empty() const noexcept
Return true if this is empty.
Dynamic singly linked list with functional programming support.
Definition htlist.H:1423
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 & put(const T &item)
Definition htlist.H:1532
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(SimpleQueue, get_put)
constexpr size_t N
constexpr size_t N
Definition fixedqueue.cc:48
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.
ArrayQueue< DynList< int > > q
Definition arrayqueue.cc:68
DynListQueue< DynList< int > > q
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
ArrayQueue< int > q
Definition arrayqueue.cc:53
DynListQueue< int > q
Dynamic queue implementation based on linked lists.
DynList< int > l