Aleph-w 7.10.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
arrayqueue.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# include <memory>
40# include <deque>
41# include <new>
42# include <random>
43# include <string>
44# include <type_traits>
45
46# include <tpl_arrayQueue.H>
47
48# include <ahFunctional.H>
49
50using namespace std;
51using namespace testing;
52using namespace Aleph;
53
54constexpr size_t N = 17;
55
56struct SimpleQueue : public Test
57{
58 size_t n = 0;
61 {
62 for (size_t i = 0; i < N; ++i, ++n)
63 q.put(i);
64 }
65 void print() const
66 {
67 cout << "q ="; q.for_each([] (auto i) { cout << " " << i; }); cout << endl;
68 }
69};
70
71struct ComplexQueue : public Test
72{
73 size_t n = 0;
76 {
77 for (size_t i = 0; i < N; ++i, ++n)
78 q.put({ int(i), 1, 2, int(i) });
79 }
80};
81
82TEST(ArrayQueue, empty_queue)
83{
85 EXPECT_TRUE(q.is_empty());
86 EXPECT_EQ(q.size(), 0);
87 EXPECT_THROW(q.rear(), range_error);
88 EXPECT_THROW(q.front(), range_error);
89 EXPECT_THROW(q.rear(2), range_error);
90 EXPECT_THROW(q.front(2), range_error);
91 EXPECT_THROW(q.rear(q.capacity()), range_error);
92 EXPECT_THROW(q.front(q.capacity()), range_error);
93 EXPECT_THROW(q.get(), underflow_error);
94 EXPECT_THROW(q.getn(0), underflow_error);
95 EXPECT_THROW(q.getn(1), underflow_error);
96 EXPECT_THROW(q.getn(q.capacity()), underflow_error);
97}
98
99TEST(ArrayQueue, fill_and_empty_queue)
100{
102 const size_t N = q.capacity();
103 for (int i = 0; i < N; ++i)
104 {
105 ASSERT_EQ(q.put(i), i);
106 ASSERT_EQ(q.rear(), i);
107 ASSERT_EQ(q.front(), 0);
108 }
109 EXPECT_EQ(q.size(), N);
110 EXPECT_FALSE(q.is_empty());
111
112 for (int i = 0; i < N; ++i)
113 {
114 ASSERT_EQ(q.front(i), i);
115 ASSERT_EQ(q.rear(i), N - i - 1);
116 }
117
118 for (int i = 0; i < N; ++i)
119 {
120 ASSERT_EQ(q.front(), i);
121 ASSERT_EQ(q.rear(), N - 1);
122 ASSERT_EQ(q.get(), i);
123 }
124 EXPECT_TRUE(q.is_empty());
125 EXPECT_EQ(q.size(), 0);
126 EXPECT_EQ(q.capacity(), N);
127}
128
129TEST_F(SimpleQueue, put_and_get_stress)
130{
131 EXPECT_LT(q.size(), q.capacity());
132
133 // fill until complete initial_cap
134 for (int i = q.size(); i < q.capacity(); ++i)
135 ASSERT_EQ(q.put(i), i);
136
137 EXPECT_EQ(q.size(), q.capacity());
138
139 for (size_t i = 0; i < q.size(); ++i)
140 {
141 ASSERT_EQ(q.front(i), i);
142 ASSERT_EQ(q.rear(i), q.size() - i - 1);
143 }
144
145 // now put more entries
146 for (size_t i = q.size(), n = 2*q.size(); i < n; ++i)
147 ASSERT_EQ(q.put(i), i);
148
149 EXPECT_EQ(q.size(), q.capacity());
150
151 const size_t nn = q.size();
152
153 // get out the half
154 for (size_t i = 0; i < nn/2; ++i)
155 ASSERT_EQ(q.get(), i);
156
157 EXPECT_EQ(q.size(), nn/2);
158
159 // test consistency of remaining items
160 for (size_t i = 0; i < nn/2; ++i)
161 ASSERT_EQ(q.front(i), i + nn/2);
162
163 // now extract them all
164 for (size_t i = 0; i < nn/2; ++i)
165 ASSERT_EQ(q.get(), i + nn/2);
166
167 EXPECT_EQ(q.size(), 0);
168 EXPECT_TRUE(q.is_empty());
169
170 // now we put the queue thus
171 //
172 // xxx------xxxxxxx
173 //
174 // where x is an item
175 const size_t cap = 16;
176 for (size_t i = 0; i < cap; ++i)
177 ASSERT_EQ(q.put(i), i);
178
179 for (size_t i = 0; i < cap/4; ++i) // extract a fourth
180 ASSERT_EQ(q.get(), i);
181
182 ASSERT_FALSE(q.is_empty());
183 ASSERT_EQ(q.size(), 3*cap/4);
184
185 for (size_t i = 0; i < cap/4; ++i) // put them again
186 ASSERT_EQ(q.put(i), i);
187
188 for (size_t i = 0; i < 3*cap/4; ++i) // extract and verify the 3/4 oldest
189 ASSERT_EQ(q.get(), cap/4 + i);
190
191 // now extract and verify the 1/4 remaining
192 for (size_t i = 0; i < cap/4; ++i)
193 ASSERT_EQ(q.get(), i);
194}
195
196TEST_F(ComplexQueue, put_and_stress_capacity)
197{
198 EXPECT_LT(q.size(), q.capacity());
199
200 // fill until complete initial_cap
201 for (int i = q.size(); i < q.capacity(); ++i)
202 {
203 auto & l = q.put({int(i), 1, 2, int(i)});
204 ASSERT_EQ(l.get_first(), i);
205 ASSERT_EQ(l.get_last(), i);
206 ASSERT_EQ(l.nth(1), 1);
207 ASSERT_EQ(l.nth(2), 2);
208 }
209
210 EXPECT_EQ(q.size(), q.capacity());
211
212 for (size_t i = 0; i < q.size(); ++i)
213 {
214 auto & lf = q.front(i);
215 ASSERT_EQ(lf.get_first(), i);
216 ASSERT_EQ(lf.get_last(), i);
217 ASSERT_EQ(lf.nth(1), 1);
218 ASSERT_EQ(lf.nth(2), 2);
219
220 auto & lr = q.rear(i);
221 ASSERT_EQ(lr.get_first(), q.size() - i - 1);
222 ASSERT_EQ(lr.get_last(), q.size() - i - 1);
223 ASSERT_EQ(lr.nth(1), 1);
224 ASSERT_EQ(lr.nth(2), 2);
225 }
226
227 // now put more entries
228 for (size_t i = q.size(), n = 2*q.size(); i < n; ++i)
229 {
230 auto & l = q.put({int(i), 1, 2, int(i)});
231 ASSERT_EQ(l.get_first(), i);
232 ASSERT_EQ(l.get_last(), i);
233 ASSERT_EQ(l.nth(1), 1);
234 ASSERT_EQ(l.nth(2), 2);
235 }
236
237 EXPECT_EQ(q.size(), q.capacity());
238
239 const size_t nn = q.size();
240
241 // get out the half
242 for (size_t i = 0; i < nn/2; ++i)
243 {
244 auto l = q.get();
245 ASSERT_EQ(l.get_first(), i);
246 ASSERT_EQ(l.get_last(), i);
247 ASSERT_EQ(l.nth(1), 1);
248 ASSERT_EQ(l.nth(2), 2);
249 }
250
251 EXPECT_EQ(q.size(), nn/2);
252
253 // test consistency of remaining items
254 for (size_t i = 0; i < nn/2; ++i)
255 {
256 auto & l = q.front(i);
257 ASSERT_EQ(l.get_first(), i + nn/2);
258 ASSERT_EQ(l.get_last(), i + nn/2);
259 ASSERT_EQ(l.nth(1), 1);
260 ASSERT_EQ(l.nth(2), 2);
261 }
262
263 // now extract them all
264 for (size_t i = 0; i < nn/2; ++i)
265 {
266 auto l = q.get();
267 ASSERT_EQ(l.get_first(), i + nn/2);
268 ASSERT_EQ(l.get_last(), i + nn/2);
269 ASSERT_EQ(l.nth(1), 1);
270 ASSERT_EQ(l.nth(2), 2);
271 }
272
273 EXPECT_EQ(q.size(), 0);
274 EXPECT_TRUE(q.is_empty());
275
276 // now we put the queue thus
277 //
278 // xxx------xxxxxxx
279 //
280 // where x is an item
281 const size_t cap = 16;
282 for (size_t i = 0; i < cap; ++i)
283 {
284 auto & l = q.put({int(i), 1, 2, int(i)});
285 ASSERT_EQ(l.get_first(), i);
286 ASSERT_EQ(l.get_last(), i);
287 ASSERT_EQ(l.nth(1), 1);
288 ASSERT_EQ(l.nth(2), 2);
289 }
290
291 for (size_t i = 0; i < cap/4; ++i) // extract a fourth
292 {
293 auto l = q.get();
294 ASSERT_EQ(l.get_first(), i);
295 ASSERT_EQ(l.get_last(), i);
296 ASSERT_EQ(l.nth(1), 1);
297 ASSERT_EQ(l.nth(2), 2);
298 }
299
300 ASSERT_FALSE(q.is_empty());
301 ASSERT_EQ(q.size(), 3*cap/4);
302
303 for (size_t i = 0; i < cap/4; ++i) // put them again
304 {
305 auto l = q.put({int(i), 1, 2, int(i)});
306 ASSERT_EQ(l.get_first(), i);
307 ASSERT_EQ(l.get_last(), i);
308 ASSERT_EQ(l.nth(1), 1);
309 ASSERT_EQ(l.nth(2), 2);
310 }
311
312 for (size_t i = 0; i < 3*cap/4; ++i) // extract and verify the 3/4 oldest
313 {
314 auto l = q.get();
315 ASSERT_EQ(l.get_first(), cap/4 + i);
316 ASSERT_EQ(l.get_last(), cap/4 + i);
317 ASSERT_EQ(l.nth(1), 1);
318 ASSERT_EQ(l.nth(2), 2);
319 }
320
321 // now extract and verify the 1/4 remaining
322 for (size_t i = 0; i < cap/4; ++i)
323 {
324 auto l = q.get();
325 ASSERT_EQ(l.get_first(), i);
326 ASSERT_EQ(l.get_last(), i);
327 ASSERT_EQ(l.nth(1), 1);
328 ASSERT_EQ(l.nth(2), 2);
329 }
330}
331
332TEST(ArrayQueue, Iterator_on_empty_queue)
333{
335 auto it = q.get_it();
336 ASSERT_FALSE(it.has_curr());
337 ASSERT_THROW(it.get_curr(), overflow_error);
338 ASSERT_THROW(it.next(), overflow_error);
339 ASSERT_THROW(it.prev(), underflow_error);
340}
341
342static size_t primes[] = {13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59,
343 61, 67, 71, 73, 79, 83, 89, 97, 197 };
344TEST(ArrayQueue, Iterator)
345{
346 for (size_t i = 0, N = primes[i]; N < 100; ++i, N = primes[i])
347 {
349
350 for (size_t i = 0; i < N; ++i)
351 ASSERT_EQ(q.put(i), i);
352
353 int k = 0;
354 for (auto it = q.get_it(); it.has_curr(); it.next(), ++k)
355 ASSERT_EQ(it.get_curr(), k);
356 ASSERT_EQ(k, N); // test that queue has been traversed
357
358 // extract 1/4 of items
359 for (size_t i = 0; i < N/4; ++i)
360 ASSERT_EQ(q.get(), i);
361
362 ASSERT_FALSE(q.is_empty());
363
364 // Test iterator again
365 k = N/4;
366 for (auto it = q.get_it(); it.has_curr(); it.next(), ++k)
367 ASSERT_EQ(it.get_curr(), k);
368 ASSERT_EQ(k, N); // test that queue has been traversed
369
370 // now put again N/4 for testing iterator on queue of form xxx----xxxxxxx
371 for (size_t i = 0; i < N/4; ++i)
372 ASSERT_EQ(q.put(i), i);
373 ASSERT_EQ(q.size(), N);
374
375 // now test if iterator still works
376 k = 0;
377 for (auto it = q.get_it(); it.has_curr(); it.next(), ++k)
378 ASSERT_EQ(it.get_curr(), (k + N/4) % N);
379 ASSERT_EQ(k, N); // test that queue has been traversed
380
381 k = N - 1;
382 auto it = q.get_it();
383 it.reset_last();
384 for (; it.has_curr(); it.prev(), --k)
385 ASSERT_EQ(it.get_curr(), (k + N/4) % N);
386 ASSERT_EQ(k, -1); // test that queue has been traversed
387 }
388}
389
391{
392 for (size_t i = 0, N = primes[i]; N < 100; ++i, N = primes[i])
393 {
395
396 for (size_t i = 0; i < N; ++i)
397 ASSERT_EQ(q.put(i), i);
398
399 int k = 0;
400 auto ret = q.traverse([&k] (int i) { return i == k++; });
401 ASSERT_TRUE(ret);
402 ASSERT_EQ(k, N);
403
404 // extract 1/4 of items
405 for (size_t i = 0; i < N/4; ++i)
406 ASSERT_EQ(q.get(), i);
407
408 ASSERT_FALSE(q.is_empty());
409
410 // Test traverse
411 k = N/4;
412 ret = q.traverse([&k] (int i) { return i == k++; });
413 ASSERT_TRUE(ret);
414 ASSERT_EQ(k, N);
415
416 // now put again N/4 for testing iterator on queue of form xxx----xxxxxxx
417 for (size_t i = 0; i < N/4; ++i)
418 ASSERT_EQ(q.put(i), i);
419 ASSERT_EQ(q.size(), N);
420
421 // now test if iterator still works
422 k = 0;
423 ret = q.traverse([&k, N] (int i) { return i == (k++ + N/4) % N; });
424 ASSERT_TRUE(ret);
425 ASSERT_EQ(k, N);
426
427 // finally test partial traverse
428 k = 0;
429 ret = q.traverse([&k, n = N/4] (int) { return ++k < n; });
430 ASSERT_FALSE(ret);
431 ASSERT_EQ(k, N/4);
432 }
433}
434
435TEST(ArrayQueue, copy_operations)
436{
437 size_t N = 31;
438 {
440 for (size_t i = 0; i < N; ++i)
441 ASSERT_EQ(q.put(i), i);
442
443 {
444 ArrayQueue<int> qc = q;
445 ASSERT_TRUE(eq(q, qc));
446 }
447
448 {
449 ArrayQueue<int> qc = move(q);
450 ASSERT_TRUE(q.is_empty());
451 ASSERT_EQ(q.size(), 0);
452 int k = 0;
453 auto ret = qc.traverse([&k] (int i) { return i == k++; });
454 ASSERT_TRUE(ret);
455 ASSERT_EQ(k, qc.size());
456
457 q.swap(qc);
458 ASSERT_EQ(qc.size(), 0);
459 ASSERT_TRUE(qc.is_empty());
460 ASSERT_EQ(q.size(), N);
461 ASSERT_FALSE(q.is_empty());
462 }
463
465 qc = q;
466 ASSERT_TRUE(eq(q, qc));
467
468 qc.empty();
469 ASSERT_EQ(qc.size(), 0);
470 ASSERT_TRUE(qc.is_empty());
471
472 qc = move(q);
473 ASSERT_TRUE(q.is_empty());
474 ASSERT_EQ(q.size(), 0);
475 int k = 0;
476 auto ret = qc.traverse([&k] (int i) { return i == k++; });
477 ASSERT_TRUE(ret);
478 ASSERT_EQ(k, qc.size());
479 }
480}
481
482// Regression tests for queues whose front advanced past slot 0. Copy
483// construction and copy assignment used to copy the raw buffer prefix, so the
484// copy kept the indices of the source but not its items; empty() and
485// reserve() also ignored the circular layout.
486
489{
490 for (const char *item : {"a", "b", "c", "d", "e", "f", "g", "h"})
491 q.put(item);
492 for (int i = 0; i < 6; ++i)
493 (void) q.get();
494 q.put("i");
495 q.put("j");
496}
497
499static void expect_order(const ArrayQueue<string> &q, initializer_list<const char *> expected)
500{
501 ASSERT_EQ(q.size(), expected.size());
502 size_t i = 0;
503 for (const char *item : expected)
504 EXPECT_EQ(q.front(i++), item);
505}
506
507TEST(ArrayQueue, copy_keeps_the_items_of_a_wrapped_queue)
508{
510 fill_wrapped(q);
511
513 expect_order(copy, {"g", "h", "i", "j"});
514
515 ArrayQueue<string> assigned;
516 assigned.put("stale");
517 assigned = q;
518 expect_order(assigned, {"g", "h", "i", "j"});
519 expect_order(q, {"g", "h", "i", "j"});
520
521 // The copy keeps working as a queue.
522 copy.put("k");
523 EXPECT_EQ(copy.get(), "g");
524 EXPECT_EQ(copy.rear(), "k");
525 expect_order(copy, {"h", "i", "j", "k"});
526}
527
528TEST(ArrayQueue, empty_restarts_both_ends)
529{
530 ArrayQueue<int> q(8);
531 for (int i = 1; i <= 5; ++i)
532 q.put(i);
533 (void) q.get();
534 (void) q.get();
535 q.empty();
536 EXPECT_TRUE(q.is_empty());
537 q.put(42);
538 EXPECT_EQ(q.front(), 42);
539 EXPECT_EQ(q.rear(), 42);
540
541 q.put(43);
542 (void) q.get();
543 q.clear();
544 q.put(7);
545 EXPECT_EQ(q.front(), 7);
546
547 for (int i = 0; i < 40; ++i)
548 q.put(i);
549 for (int i = 0; i < 30; ++i)
550 (void) q.get();
552 EXPECT_TRUE(q.is_empty());
553 q.put(9);
554 EXPECT_EQ(q.front(), 9);
555 EXPECT_EQ(q.rear(), 9);
556}
557
558TEST(ArrayQueue, reserve_keeps_the_order_of_a_wrapped_queue)
559{
561 fill_wrapped(q);
562 q.reserve(64);
563 EXPECT_GE(q.capacity(), 64U);
564 expect_order(q, {"g", "h", "i", "j"});
565 q.put("k");
566 EXPECT_EQ(q.rear(), "k");
567 EXPECT_EQ(q.get(), "g");
568 expect_order(q, {"h", "i", "j", "k"});
569
570 // A capacity that is already available leaves the queue untouched.
571 q.reserve(4);
572 expect_order(q, {"h", "i", "j", "k"});
573}
574
575static_assert(not std::is_convertible_v<ArrayQueue<int> *, MemArray<int> *>);
576static_assert(std::is_nothrow_move_constructible_v<ArrayQueue<int>>);
577
578TEST(ArrayQueue, public_accessors_and_removals_use_fifo_order)
579{
581 fill_wrapped(q);
582 EXPECT_EQ(q.first(), "g");
583 EXPECT_EQ(q.last(), "j");
584 EXPECT_EQ(q.get_first(), "g");
585 EXPECT_EQ(q.get_last(), "j");
586 EXPECT_EQ(q.top(), "g");
587 EXPECT_EQ(q[0], "g");
588 EXPECT_EQ(q(1), "h");
589 EXPECT_THROW(q[q.size()], out_of_range);
590 EXPECT_EQ(q.remove_first(), "g");
591 expect_order(q, {"h", "i", "j"});
592 EXPECT_EQ(q.rear(), "j");
593 EXPECT_EQ(q.remove_last(), "j");
594 q.push("k");
595 expect_order(q, {"h", "i", "k"});
596 q.reverse();
597 expect_order(q, {"k", "i", "h"});
598 EXPECT_EQ(q.pop(), "k");
599 expect_order(q, {"i", "h"});
600}
601
602TEST(ArrayQueue, copied_full_queue_wraps_the_next_insertion)
603{
604 ArrayQueue<int> q(8);
605 for (int i = 0; i < 8; ++i)
606 q.put(i);
608 EXPECT_EQ(copy.get(), 0);
609 copy.put(8);
610 for (int i = 1; i <= 8; ++i)
611 EXPECT_EQ(copy.get(), i);
612}
613
614TEST(ArrayQueue, moved_from_queue_can_be_iterated_copied_and_reused)
615{
617 fill_wrapped(q);
618 ArrayQueue<string> moved(std::move(q));
619 EXPECT_TRUE(q.is_empty());
620 EXPECT_FALSE(q.get_it().has_curr());
621 EXPECT_EQ(q.begin(), q.end());
622 ArrayQueue<string> empty_copy(q);
623 EXPECT_TRUE(empty_copy.is_empty());
624 ArrayQueue<string> empty_moved(std::move(q));
625 EXPECT_TRUE(empty_moved.is_empty());
626
627 q.put("again");
628 EXPECT_EQ(q.front(), "again");
629 EXPECT_EQ(q.get(), "again");
630 q.reserve(64);
631 q.put("reserved");
632 EXPECT_EQ(q.front(), "reserved");
633 empty_moved.putn(2);
634 empty_moved[0] = "first";
635 empty_moved[1] = "last";
636 expect_order(empty_moved, {"first", "last"});
637 expect_order(moved, {"g", "h", "i", "j"});
638 EXPECT_THROW(empty_copy.putn(0), underflow_error);
639}
640
641TEST(ArrayQueue, reserve_preserves_wrapped_move_only_elements)
642{
644 for (int i = 0; i < 8; ++i)
645 q.put(std::make_unique<int>(i));
646 for (int i = 0; i < 6; ++i)
647 EXPECT_EQ(*q.get(), i);
648 q.put(std::make_unique<int>(8));
649 q.put(std::make_unique<int>(9));
650 q.reserve(64);
651 for (int i = 6; i < 10; ++i)
652 EXPECT_EQ(*q.get(), i);
653}
654
655TEST(ArrayQueue, release_then_insert_reproduces_the_reported_capacity_64_case)
656{
657 ArrayQueue<int> q(64);
658 for (int i = 0; i < 40; ++i)
659 q.put(i);
660 for (int i = 0; i < 30; ++i)
661 EXPECT_EQ(q.get(), i);
663 EXPECT_EQ(q.capacity(), 4u);
664 q.put(9);
665 EXPECT_EQ(q.front(), 9);
666 EXPECT_EQ(q.rear(), 9);
667 EXPECT_EQ(q.get(), 9);
668}
669
670TEST(ArrayQueue, state_transitions_match_a_fifo_reference)
671{
672 std::mt19937 random(0xa1e9);
673 std::deque<int> reference;
674 ArrayQueue<int> q(8);
675 for (int step = 0; step < 2500; ++step)
676 {
677 SCOPED_TRACE(step);
678 switch (random() % 10)
679 {
680 case 0: case 1: case 2:
681 q.put(step);
682 reference.push_back(step);
683 break;
684 case 3:
685 if (not reference.empty())
686 {
687 EXPECT_EQ(q.get(), reference.front());
688 reference.pop_front();
689 }
690 break;
691 case 4:
692 {
694 q = copy;
695 break;
696 }
697 case 5:
698 {
699 ArrayQueue<int> moved(std::move(q));
700 q.put(-1); // exercise the moved-from source before assignment
701 q = std::move(moved);
702 break;
703 }
704 case 6: q.reserve(16 + random() % 64); break;
705 case 7:
706 q.clear();
707 reference.clear();
708 break;
709 case 8:
711 reference.clear();
712 break;
713 case 9:
714 if (not reference.empty())
715 {
716 EXPECT_EQ(q.remove_last(), reference.back());
717 reference.pop_back();
718 }
719 break;
720 }
721 ASSERT_EQ(q.size(), reference.size());
722 size_t i = 0;
723 for (int value : q)
724 {
725 ASSERT_LT(i, reference.size());
726 EXPECT_EQ(value, reference[i]);
727 EXPECT_EQ(q(i), reference[i]);
728 ++i;
729 }
730 EXPECT_EQ(i, reference.size());
731 }
732}
733
735static string long_string(const char c)
736{
737 return string(40, c);
738}
739
743{
744 static inline int constructions_left = -1;
745 string value;
746
749 {
750 if (constructions_left == 0)
751 throw bad_alloc();
752 if (constructions_left > 0)
754 }
755
757 explicit Failing_Slot(string v) : value(std::move(v)) {}
758};
759
760// The argument of put() may be an item of the queue itself, which growth
761// used to free before reading it.
762
763TEST(ArrayQueue, put_of_own_item_copies_it_before_growing)
764{
765 ArrayQueue<int> q(4);
766 for (int i : {10, 20, 30, 40})
767 q.put(i);
768 ASSERT_EQ(q.size(), q.capacity());
769
770 EXPECT_EQ(q.put(q.front()), 10);
771 EXPECT_EQ(q.put(q.rear()), 10); // with free capacity
772 ASSERT_EQ(q.size(), 6u);
773 for (int i : {10, 20, 30, 40, 10, 10})
774 EXPECT_EQ(q.get(), i);
775}
776
777TEST(ArrayQueue, put_of_own_item_of_a_full_wrapped_queue)
778{
780 for (char c : {'a', 'b', 'c', 'd'})
781 q.put(long_string(c));
782 EXPECT_EQ(q.get(), long_string('a'));
783 q.put(long_string('e')); // the rear wraps around to slot 0
784 ASSERT_EQ(q.size(), q.capacity());
785
786 EXPECT_EQ(q.put(std::move(q.front(1))), long_string('c'));
787 ASSERT_EQ(q.size(), 5u);
788 EXPECT_EQ(q.front(), long_string('b'));
789 EXPECT_EQ(q.front(3), long_string('e'));
790 EXPECT_EQ(q.rear(), long_string('c'));
791
792 EXPECT_EQ(q.put(q.front(3)), long_string('e')); // with free capacity
793 EXPECT_EQ(q.rear(1), long_string('c'));
794}
795
796TEST(ArrayQueue, failed_growth_leaves_the_queue_and_the_argument_untouched)
797{
799 for (char c : {'a', 'b', 'c', 'd'})
801 (void) q.get();
802 q.put(Failing_Slot(long_string('e'))); // full and wrapped
803 ASSERT_EQ(q.size(), q.capacity());
804
806 Failing_Slot::constructions_left = 0; // the new buffer cannot be built
807 EXPECT_THROW(q.put(std::move(x)), bad_alloc);
808 EXPECT_THROW(q.put(q.front()), bad_alloc);
810
811 EXPECT_EQ(x.value, long_string('x'));
812 ASSERT_EQ(q.size(), 4u);
813 for (size_t i = 0; i < 4; ++i)
814 EXPECT_EQ(q.front(i).value, long_string(static_cast<char>('b' + i)));
815
816 EXPECT_EQ(q.put(std::move(x)).value, long_string('x'));
817 EXPECT_EQ(q.front().value, long_string('b'));
818}
Functional programming utilities for Aleph-w containers.
TEST_F(SimpleQueue, put_and_get_stress)
static void fill_wrapped(ArrayQueue< string > &q)
Place g/h at slots 6/7 and wrap i/j around to slots 0/1.
static void expect_order(const ArrayQueue< string > &q, initializer_list< const char * > expected)
Check the logical FIFO sequence against the expected values.
static size_t primes[]
constexpr size_t N
Definition arrayqueue.cc:54
static string long_string(const char c)
A string too long for the small-string buffer.
size_t size_t int32_t value
Definition ca-c-api.h:116
Queue implemented with a single dynamic array.
T & front(const size_t i=0)
Return the i-th oldest item of the queue.
void empty_and_release()
Empty the queue and reduce storage to Min_Dim.
T & top()
Alias for first(); throws std::underflow_error if empty.
T pop()
Alias for get(); remove the oldest item in FIFO order.
T & rear(const size_t i=0)
Return the i-th youngest item of the queue.
T & put(const T &item)
Copy and put an item in the queue.
T & last()
Return the youngest item, in O(1).
bool traverse(Operation &operation) noexcept(Aleph::NothrowPredicateWith< Operation &, T & >)
Traverse all the elements from the oldest to the youngest and execute operation on each on them.
void clear() noexcept
See empty().
T & first()
Return the oldest item, in O(1).
void empty() noexcept
Empty the queue keeping its capacity; both ends restart at slot 0.
T & get_last()
Alias for last(); throws std::underflow_error if empty.
T remove_last()
Remove the youngest item.
ArrayQueue & reverse()
Reverse the logical queue in place.
T & getn(const size_t i)
Remove the i oldest items of the queue.
T remove_first()
Remove the oldest item; same complexity and exceptions as get().
T & get_first()
Alias for first(); throws std::underflow_error if empty.
T & putn(const size_t sz)
Add sz logical slots at the rear without initializing scalars.
T & push(const T &item)
Alias for put(); enqueue a copy at the rear.
T get()
Remove the oldest item of the queue and return a copy.
void reserve(const size_t cap)
Reserve capacity for at least cap items, keeping the queue order.
void swap(ArrayQueue &q) noexcept
Swap this with q
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
Simple, scalable and fast dynamic array.
size_t size() const noexcept
Return the number of elements.
constexpr size_t capacity() const noexcept
The type of element of array.
bool is_empty() const noexcept
Return true is the array is empty.
Number Theoretic Transform over Z / MOD Z.
Definition ntt.H:115
Minimal std::expected-style result type for C++20.
void for_each(Operation &operation) const
Traverse all the container and performs an operation on each element.
Definition ah-dry.H:1475
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
iterator end() noexcept
Return an STL-compatible end iterator.
iterator begin() noexcept
Return an STL-compatible iterator to the first element.
#define TEST(name)
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.
Itor2 copy(Itor1 sourceBeg, const Itor1 &sourceEnd, Itor2 destBeg)
Copy elements from one range to another.
Definition ahAlgo.H:584
STL namespace.
ArrayQueue< DynList< int > > q
Definition arrayqueue.cc:74
Element whose default construction, that of every slot of a new buffer, can fail as an allocation doe...
static int constructions_left
Failing_Slot(string v)
Build an element that holds v.
Failing_Slot()
Fail at the configured default construction.
ArrayQueue< int > q
Definition arrayqueue.cc:59
void print() const
Definition arrayqueue.cc:65
Dnode< int > Test
Definition testDnode.C:42
static int * k
int random(int l, int r)
Circular queue implementations backed by arrays.
DynList< int > l