38# include <gtest/gtest.h>
44# include <type_traits>
51using namespace testing;
54constexpr size_t N = 17;
62 for (
size_t i = 0; i <
N; ++i, ++
n)
67 cout <<
"q =";
q.
for_each([] (
auto i) { cout <<
" " << i; }); cout << endl;
77 for (
size_t i = 0; i <
N; ++i, ++
n)
78 q.put({ int(i), 1, 2, int(i) });
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);
93 EXPECT_THROW(q.
get(), underflow_error);
94 EXPECT_THROW(q.
getn(0), underflow_error);
95 EXPECT_THROW(q.
getn(1), underflow_error);
103 for (
int i = 0; i <
N; ++i)
105 ASSERT_EQ(q.
put(i), i);
106 ASSERT_EQ(q.
rear(), i);
107 ASSERT_EQ(q.
front(), 0);
109 EXPECT_EQ(q.
size(),
N);
112 for (
int i = 0; i <
N; ++i)
114 ASSERT_EQ(q.
front(i), i);
115 ASSERT_EQ(q.
rear(i),
N - i - 1);
118 for (
int i = 0; i <
N; ++i)
120 ASSERT_EQ(q.
front(), i);
121 ASSERT_EQ(q.
rear(),
N - 1);
122 ASSERT_EQ(q.
get(), i);
125 EXPECT_EQ(q.
size(), 0);
131 EXPECT_LT(q.size(), q.capacity());
134 for (
int i = q.size(); i < q.capacity(); ++i)
135 ASSERT_EQ(q.put(i), i);
137 EXPECT_EQ(q.size(), q.capacity());
139 for (
size_t i = 0; i < q.size(); ++i)
141 ASSERT_EQ(q.front(i), i);
142 ASSERT_EQ(q.rear(i), q.size() - i - 1);
146 for (
size_t i = q.size(), n = 2*q.size(); i < n; ++i)
147 ASSERT_EQ(q.put(i), i);
149 EXPECT_EQ(q.size(), q.capacity());
151 const size_t nn = q.size();
154 for (
size_t i = 0; i < nn/2; ++i)
155 ASSERT_EQ(q.get(), i);
157 EXPECT_EQ(q.size(), nn/2);
160 for (
size_t i = 0; i < nn/2; ++i)
161 ASSERT_EQ(q.front(i), i + nn/2);
164 for (
size_t i = 0; i < nn/2; ++i)
165 ASSERT_EQ(q.get(), i + nn/2);
167 EXPECT_EQ(q.size(), 0);
168 EXPECT_TRUE(q.is_empty());
175 const size_t cap = 16;
176 for (
size_t i = 0; i < cap; ++i)
177 ASSERT_EQ(q.put(i), i);
179 for (
size_t i = 0; i < cap/4; ++i)
180 ASSERT_EQ(q.get(), i);
182 ASSERT_FALSE(q.is_empty());
183 ASSERT_EQ(q.size(), 3*cap/4);
185 for (
size_t i = 0; i < cap/4; ++i)
186 ASSERT_EQ(q.put(i), i);
188 for (
size_t i = 0; i < 3*cap/4; ++i)
189 ASSERT_EQ(q.get(), cap/4 + i);
192 for (
size_t i = 0; i < cap/4; ++i)
193 ASSERT_EQ(q.get(), i);
198 EXPECT_LT(q.size(), q.capacity());
201 for (
int i = q.size(); i < q.capacity(); ++i)
203 auto &
l = q.
put({int(i), 1, 2, int(i)});
206 ASSERT_EQ(
l.
nth(1), 1);
207 ASSERT_EQ(
l.
nth(2), 2);
210 EXPECT_EQ(q.size(), q.capacity());
212 for (
size_t i = 0; i < q.size(); ++i)
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);
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);
228 for (
size_t i = q.size(), n = 2*q.size(); i < n; ++i)
230 auto &
l = q.
put({int(i), 1, 2, int(i)});
233 ASSERT_EQ(
l.
nth(1), 1);
234 ASSERT_EQ(
l.
nth(2), 2);
237 EXPECT_EQ(q.size(), q.capacity());
239 const size_t nn = q.size();
242 for (
size_t i = 0; i < nn/2; ++i)
247 ASSERT_EQ(
l.
nth(1), 1);
248 ASSERT_EQ(
l.
nth(2), 2);
251 EXPECT_EQ(q.size(), nn/2);
254 for (
size_t i = 0; i < nn/2; ++i)
256 auto &
l = q.front(i);
259 ASSERT_EQ(
l.
nth(1), 1);
260 ASSERT_EQ(
l.
nth(2), 2);
264 for (
size_t i = 0; i < nn/2; ++i)
269 ASSERT_EQ(
l.
nth(1), 1);
270 ASSERT_EQ(
l.
nth(2), 2);
273 EXPECT_EQ(q.size(), 0);
274 EXPECT_TRUE(q.is_empty());
281 const size_t cap = 16;
282 for (
size_t i = 0; i < cap; ++i)
284 auto &
l = q.
put({int(i), 1, 2, int(i)});
287 ASSERT_EQ(
l.
nth(1), 1);
288 ASSERT_EQ(
l.
nth(2), 2);
291 for (
size_t i = 0; i < cap/4; ++i)
296 ASSERT_EQ(
l.
nth(1), 1);
297 ASSERT_EQ(
l.
nth(2), 2);
300 ASSERT_FALSE(q.is_empty());
301 ASSERT_EQ(q.size(), 3*cap/4);
303 for (
size_t i = 0; i < cap/4; ++i)
305 auto l = q.
put({int(i), 1, 2, int(i)});
308 ASSERT_EQ(
l.
nth(1), 1);
309 ASSERT_EQ(
l.
nth(2), 2);
312 for (
size_t i = 0; i < 3*cap/4; ++i)
317 ASSERT_EQ(
l.
nth(1), 1);
318 ASSERT_EQ(
l.
nth(2), 2);
322 for (
size_t i = 0; i < cap/4; ++i)
327 ASSERT_EQ(
l.
nth(1), 1);
328 ASSERT_EQ(
l.
nth(2), 2);
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);
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 };
350 for (
size_t i = 0; i <
N; ++i)
351 ASSERT_EQ(q.
put(i), i);
354 for (
auto it = q.
get_it(); it.has_curr(); it.next(), ++
k)
355 ASSERT_EQ(it.get_curr(),
k);
359 for (
size_t i = 0; i <
N/4; ++i)
360 ASSERT_EQ(q.
get(), i);
366 for (
auto it = q.
get_it(); it.has_curr(); it.next(), ++
k)
367 ASSERT_EQ(it.get_curr(),
k);
371 for (
size_t i = 0; i <
N/4; ++i)
372 ASSERT_EQ(q.
put(i), i);
373 ASSERT_EQ(q.
size(),
N);
377 for (
auto it = q.
get_it(); it.has_curr(); it.next(), ++
k)
378 ASSERT_EQ(it.get_curr(), (
k +
N/4) %
N);
384 for (; it.has_curr(); it.prev(), --
k)
385 ASSERT_EQ(it.get_curr(), (
k +
N/4) %
N);
396 for (
size_t i = 0; i <
N; ++i)
397 ASSERT_EQ(q.
put(i), i);
400 auto ret = q.
traverse([&
k] (
int i) {
return i ==
k++; });
405 for (
size_t i = 0; i <
N/4; ++i)
406 ASSERT_EQ(q.
get(), i);
412 ret = q.
traverse([&
k] (
int i) {
return i ==
k++; });
417 for (
size_t i = 0; i <
N/4; ++i)
418 ASSERT_EQ(q.
put(i), i);
419 ASSERT_EQ(q.
size(),
N);
423 ret = q.
traverse([&
k,
N] (
int i) {
return i == (
k++ +
N/4) %
N; });
429 ret = q.
traverse([&
k, n =
N/4] (
int) {
return ++
k < n; });
440 for (
size_t i = 0; i <
N; ++i)
441 ASSERT_EQ(q.
put(i), i);
445 ASSERT_TRUE(
eq(q, qc));
451 ASSERT_EQ(q.
size(), 0);
453 auto ret = qc.
traverse([&
k] (
int i) {
return i ==
k++; });
455 ASSERT_EQ(
k, qc.
size());
458 ASSERT_EQ(qc.
size(), 0);
460 ASSERT_EQ(q.
size(),
N);
466 ASSERT_TRUE(
eq(q, qc));
469 ASSERT_EQ(qc.
size(), 0);
474 ASSERT_EQ(q.
size(), 0);
476 auto ret = qc.
traverse([&
k] (
int i) {
return i ==
k++; });
478 ASSERT_EQ(
k, qc.
size());
490 for (
const char *item : {
"a",
"b",
"c",
"d",
"e",
"f",
"g",
"h"})
492 for (
int i = 0; i < 6; ++i)
504 EXPECT_EQ(q.
front(i++), item);
516 assigned.
put(
"stale");
523 EXPECT_EQ(
copy.get(),
"g");
524 EXPECT_EQ(
copy.rear(),
"k");
531 for (
int i = 1; i <= 5; ++i)
538 EXPECT_EQ(q.
front(), 42);
539 EXPECT_EQ(q.
rear(), 42);
545 EXPECT_EQ(q.
front(), 7);
547 for (
int i = 0; i < 40; ++i)
549 for (
int i = 0; i < 30; ++i)
554 EXPECT_EQ(q.
front(), 9);
555 EXPECT_EQ(q.
rear(), 9);
566 EXPECT_EQ(q.
rear(),
"k");
567 EXPECT_EQ(q.
get(),
"g");
575static_assert(not std::is_convertible_v<ArrayQueue<int> *,
MemArray<int> *>);
576static_assert(std::is_nothrow_move_constructible_v<ArrayQueue<int>>);
582 EXPECT_EQ(q.
first(),
"g");
583 EXPECT_EQ(q.
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);
592 EXPECT_EQ(q.
rear(),
"j");
598 EXPECT_EQ(q.
pop(),
"k");
605 for (
int i = 0; i < 8; ++i)
608 EXPECT_EQ(
copy.get(), 0);
610 for (
int i = 1; i <= 8; ++i)
611 EXPECT_EQ(
copy.get(), i);
620 EXPECT_FALSE(q.
get_it().has_curr());
625 EXPECT_TRUE(empty_moved.
is_empty());
628 EXPECT_EQ(q.
front(),
"again");
629 EXPECT_EQ(q.
get(),
"again");
632 EXPECT_EQ(q.
front(),
"reserved");
634 empty_moved[0] =
"first";
635 empty_moved[1] =
"last";
638 EXPECT_THROW(empty_copy.
putn(0), underflow_error);
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));
651 for (
int i = 6; i < 10; ++i)
652 EXPECT_EQ(*q.
get(), i);
658 for (
int i = 0; i < 40; ++i)
660 for (
int i = 0; i < 30; ++i)
661 EXPECT_EQ(q.
get(), i);
665 EXPECT_EQ(q.
front(), 9);
666 EXPECT_EQ(q.
rear(), 9);
667 EXPECT_EQ(q.
get(), 9);
672 std::mt19937
random(0xa1e9);
673 std::deque<int> reference;
675 for (
int step = 0; step < 2500; ++step)
680 case 0:
case 1:
case 2:
682 reference.push_back(step);
685 if (not reference.empty())
687 EXPECT_EQ(q.
get(), reference.front());
688 reference.pop_front();
701 q = std::move(moved);
714 if (not reference.empty())
717 reference.pop_back();
721 ASSERT_EQ(q.
size(), reference.size());
725 ASSERT_LT(i, reference.size());
726 EXPECT_EQ(
value, reference[i]);
727 EXPECT_EQ(q(i), reference[i]);
730 EXPECT_EQ(i, reference.size());
737 return string(40, c);
766 for (
int i : {10, 20, 30, 40})
771 EXPECT_EQ(q.
put(q.
rear()), 10);
772 ASSERT_EQ(q.
size(), 6u);
773 for (
int i : {10, 20, 30, 40, 10, 10})
774 EXPECT_EQ(q.
get(), i);
780 for (
char c : {
'a',
'b',
'c',
'd'})
787 ASSERT_EQ(q.
size(), 5u);
799 for (
char c : {
'a',
'b',
'c',
'd'})
807 EXPECT_THROW(q.
put(std::move(x)), bad_alloc);
808 EXPECT_THROW(q.
put(q.
front()), bad_alloc);
812 ASSERT_EQ(q.
size(), 4u);
813 for (
size_t i = 0; i < 4; ++i)
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 string long_string(const char c)
A string too long for the small-string buffer.
size_t size_t int32_t value
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.
T & get_last()
Return the last item of the list.
T & put(const T &item)
See insert(const T & item).
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.
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.
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.
iterator end() noexcept
Return an STL-compatible end iterator.
iterator begin() noexcept
Return an STL-compatible iterator to the first element.
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.
ArrayQueue< DynList< int > > q
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.
Circular queue implementations backed by arrays.