42# include <gtest/gtest.h>
54using namespace testing;
67 static constexpr size_t N = 100;
71 for (
size_t i = 0; i <
N; ++i)
84 EXPECT_EQ(q.
size(), 0u);
91 EXPECT_EQ(
copy.size(), queue_with_items.size());
92 EXPECT_EQ(
copy.size(),
N);
95 while (!queue_with_items.is_empty())
97 EXPECT_EQ(queue_with_items.get(),
copy.get());
99 EXPECT_TRUE(queue_with_items.is_empty());
100 EXPECT_TRUE(
copy.is_empty());
106 for (
int i = 0; i < 10; ++i)
109 size_t original_size = source.size();
112 EXPECT_EQ(moved.
size(), original_size);
113 EXPECT_TRUE(source.is_empty());
116 for (
int i = 0; i < 10; ++i)
117 EXPECT_EQ(moved.
get(), i);
124 EXPECT_EQ(q.
size(), 5u);
125 EXPECT_EQ(q.
front(), 1);
126 EXPECT_EQ(q.
rear(), 5);
131 std::vector<int> vec = {10, 20, 30, 40, 50};
134 EXPECT_EQ(q.
size(), vec.size());
136 EXPECT_EQ(q.
get(), val);
147 while (!q.
is_empty() && it.has_curr())
149 EXPECT_EQ(q.
get(), it.get_curr());
163 q = queue_with_items;
165 EXPECT_EQ(q.
size(),
N);
166 EXPECT_EQ(q.
front(), 0);
167 EXPECT_EQ(q.
rear(),
static_cast<int>(
N - 1));
175 EXPECT_EQ(q.
size(), 3u);
176 EXPECT_EQ(q.
front(), 1);
185 target = std::move(source);
187 EXPECT_EQ(target.
size(), 3u);
188 EXPECT_EQ(target.
front(), 1);
203 EXPECT_EQ(q.
size(), 1u);
205 EXPECT_EQ(q.
front(), 42);
206 EXPECT_EQ(q.
rear(), 42);
212 std::string
value =
"hello";
214 std::string& ref = q.
put(std::move(
value));
216 EXPECT_EQ(q.
size(), 1u);
217 EXPECT_EQ(ref,
"hello");
229 EXPECT_EQ(q.
size(), 3u);
231 EXPECT_EQ(q.
get(), 1);
232 EXPECT_EQ(q.
get(), 2);
233 EXPECT_EQ(q.
get(), 3);
239 for (
int i = 0; i < 10; ++i)
242 for (
int i = 0; i < 10; ++i)
244 EXPECT_EQ(q.
front(), i);
245 EXPECT_EQ(q.
get(), i);
254 EXPECT_THROW(q.
get(), std::underflow_error);
259 EXPECT_EQ(queue_with_items.front(), 0);
262 EXPECT_EQ(queue_with_items.front(), 0);
263 EXPECT_EQ(queue_with_items.front(), 0);
266 EXPECT_EQ(queue_with_items.size(),
N);
272 EXPECT_THROW(q.
front(), std::underflow_error);
277 EXPECT_EQ(queue_with_items.rear(),
static_cast<int>(
N - 1));
280 EXPECT_EQ(queue_with_items.rear(),
static_cast<int>(
N - 1));
283 EXPECT_EQ(queue_with_items.size(),
N);
289 EXPECT_THROW(q.
rear(), std::underflow_error);
297 EXPECT_EQ(q.
get(), 100);
298 EXPECT_EQ(q.
get(), 2);
308 EXPECT_EQ(q.
get(), 300);
319 EXPECT_EQ(q.
size(), 0u);
322 EXPECT_EQ(q.
size(), 1u);
325 EXPECT_EQ(q.
size(), 2u);
328 EXPECT_EQ(q.
size(), 1u);
331 EXPECT_EQ(q.
size(), 0u);
349 EXPECT_EQ(queue_with_items.size(),
N);
351 queue_with_items.empty();
353 EXPECT_TRUE(queue_with_items.is_empty());
354 EXPECT_EQ(queue_with_items.size(), 0u);
361 EXPECT_TRUE(empty_queue.is_empty());
362 EXPECT_EQ(empty_queue.size(), 0u);
376 EXPECT_EQ(q1.
size(), 2u);
377 EXPECT_EQ(q2.
size(), 3u);
379 EXPECT_EQ(q1.
front(), 10);
380 EXPECT_EQ(q2.
front(), 1);
391 EXPECT_EQ(q2.
size(), 3u);
392 EXPECT_EQ(q2.
front(), 1);
401 EXPECT_EQ(q.
size(), 3u);
402 EXPECT_EQ(q.
front(), 1);
432 std::vector<int> visited;
433 for (
auto it = q.
get_it(); it.has_curr(); it.next())
434 visited.push_back(it.get_curr());
436 ASSERT_EQ(visited.size(), 3u);
437 EXPECT_EQ(visited[0], 1);
438 EXPECT_EQ(visited[1], 2);
439 EXPECT_EQ(visited[2], 3);
447 for (
const auto& item : q)
464 EXPECT_EQ(it, q.
end());
472 for (
const auto& item : q)
493 bool result = queue_with_items.traverse([&
sum](
int& item) {
499 EXPECT_EQ(
sum,
static_cast<int>(
N * (
N - 1) / 2));
505 bool result = queue_with_items.traverse([&
count](
int&) {
509 EXPECT_FALSE(result);
516 bool result = empty_queue.traverse([&called](
int&) {
522 EXPECT_FALSE(called);
535 EXPECT_EQ(
sum,
static_cast<int>(
N * (
N - 1) / 2));
545 queue_with_items.for_each([&
sum](
const int& item) {
549 EXPECT_EQ(
sum,
static_cast<int>(
N * (
N - 1) / 2));
555 auto doubled = q.
maps([](
int i) {
return i * 2; });
557 EXPECT_EQ(doubled.size(), 5u);
566 auto evens = q.
filter([](
int i) {
return i % 2 == 0; });
575 int product = q.
foldl(1, [](
int acc,
int item) {
return acc * item; });
584 EXPECT_TRUE(q.
all([](
int i) { return i % 2 == 0; }));
585 EXPECT_FALSE(q.
all([](
int i) { return i > 5; }));
592 EXPECT_TRUE(q.
exists([](
int i) { return i == 3; }));
593 EXPECT_FALSE(q.
exists([](
int i) { return i == 10; }));
599 auto [evens, odds] = q.
partition([](
int i) {
return i % 2 == 0; });
601 EXPECT_EQ(evens.size(), 3u);
602 EXPECT_EQ(odds.size(), 3u);
607 auto first_five = queue_with_items.take(5);
609 EXPECT_EQ(first_five.size(), 5u);
610 EXPECT_EQ(first_five.get_first(), 0);
611 EXPECT_EQ(first_five.get_last(), 4);
616 size_t drop_count =
N - 5;
617 auto last_five = queue_with_items.drop(drop_count);
619 EXPECT_EQ(last_five.size(), 5u);
620 EXPECT_EQ(last_five.get_first(),
static_cast<int>(
N - 5));
621 EXPECT_EQ(last_five.get_last(),
static_cast<int>(
N - 1));
627 auto reversed = q.
rev();
635 EXPECT_EQ(queue_with_items.length(),
N);
636 EXPECT_EQ(empty_queue.length(), 0u);
645 auto ptr = queue_with_items.find_ptr([](
int i) {
return i == 50; });
647 ASSERT_NE(ptr,
nullptr);
653 auto ptr = queue_with_items.find_ptr([](
int i) {
return i == 9999; });
655 EXPECT_EQ(ptr,
nullptr);
660 auto idx = queue_with_items.find_index([](
int i) {
return i == 50; });
667 auto [found,
value] = queue_with_items.find_item([](
int i) {
return i == 50; });
670 EXPECT_EQ(
value, 50);
675 EXPECT_EQ(queue_with_items.nth(0), 0);
676 EXPECT_EQ(queue_with_items.nth(50), 50);
677 EXPECT_EQ(queue_with_items.nth(
N - 1),
static_cast<int>(
N - 1));
682 EXPECT_THROW(queue_with_items.nth(
N), std::out_of_range);
683 EXPECT_THROW(queue_with_items.nth(
N + 100), std::out_of_range);
688 auto it = queue_with_items.get_it();
689 EXPECT_TRUE(it.has_curr());
690 EXPECT_EQ(it.get_curr(), 0);
695 auto it = queue_with_items.get_it(50);
696 EXPECT_TRUE(it.has_curr());
697 EXPECT_EQ(it.get_curr(), 50);
709 EXPECT_EQ(
keys.size(), 3u);
710 EXPECT_EQ(
keys.get_first(), 1);
711 EXPECT_EQ(
keys.get_last(), 3);
717 auto items = q.
items();
719 EXPECT_EQ(items.size(), 3u);
731 static_assert(std::is_same_v<SetType, DynListQueue<int>>,
732 "Set_Type should be DynListQueue<int>");
733 static_assert(std::is_same_v<ItemType, int>,
734 "Item_Type should be int");
741TEST(DynListQueueComplexTypes, StringQueue)
749 EXPECT_EQ(q.
size(), 3u);
750 EXPECT_EQ(q.
get(),
"hello");
751 EXPECT_EQ(q.
get(),
"world");
752 EXPECT_EQ(q.
get(),
"!");
755TEST(DynListQueueComplexTypes, UniquePtr)
759 q.
put(std::make_unique<int>(1));
760 q.
put(std::make_unique<int>(2));
761 q.
put(std::make_unique<int>(3));
763 EXPECT_EQ(q.
size(), 3u);
789TEST(DynListQueueComplexTypes, MoveOnlyType)
796 EXPECT_EQ(q.
size(), 2u);
799 EXPECT_EQ(item.value, 1);
810 throw std::runtime_error(
"Too many constructions");
816 throw std::runtime_error(
"Too many constructions");
826TEST(DynListQueueComplexTypes, ExceptionSafety)
832 for (
int i = 0; i < 50; ++i)
835 EXPECT_EQ(q.
size(), 50u);
842TEST(DynListQueueStress, LargeQueue)
844 constexpr size_t LARGE_N = 100000;
847 for (
size_t i = 0; i <
LARGE_N; ++i)
848 q.
put(
static_cast<int>(i));
851 EXPECT_EQ(q.
front(), 0);
852 EXPECT_EQ(q.
rear(),
static_cast<int>(
LARGE_N - 1));
854 for (
size_t i = 0; i <
LARGE_N; ++i)
855 EXPECT_EQ(q.
get(),
static_cast<int>(i));
860TEST(DynListQueueStress, InterleavedOperations)
868 for (
int round = 0; round < 1000; ++round)
871 for (
int i = 0; i < 3; ++i)
875 for (
int i = 0; i < 2; ++i)
877 EXPECT_EQ(q.
get(), get_count);
883 EXPECT_EQ(q.
size(), 1000u);
888 EXPECT_EQ(q.
get(), get_count);
893TEST(DynListQueueStress, RepeatedEmptyFill)
897 for (
int round = 0; round < 100; ++round)
900 for (
int i = 0; i < 100; ++i)
903 EXPECT_EQ(q.
size(), 100u);
909 EXPECT_EQ(q.
size(), 0u);
917TEST(DynListQueueEdgeCases, SingleElement)
923 EXPECT_EQ(q.
size(), 1u);
924 EXPECT_EQ(q.
front(), 42);
925 EXPECT_EQ(q.
rear(), 42);
926 EXPECT_EQ(q.
get(), 42);
930TEST(DynListQueueEdgeCases, AlternatingEmptyNonEmpty)
934 for (
int i = 0; i < 100; ++i)
940 EXPECT_EQ(q.
front(), i);
941 EXPECT_EQ(q.
rear(), i);
949TEST(DynListQueueEdgeCases, ZeroValue)
954 EXPECT_EQ(q.
front(), 0);
955 EXPECT_EQ(q.
get(), 0);
958TEST(DynListQueueEdgeCases, NegativeValues)
962 for (
int i = -100; i <= 100; ++i)
965 for (
int i = -100; i <= 100; ++i)
966 EXPECT_EQ(q.
get(), i);
969TEST(DynListQueueEdgeCases, EmptyString)
977 EXPECT_EQ(q.
get(),
"");
978 EXPECT_EQ(q.
get(),
"non-empty");
979 EXPECT_EQ(q.
get(),
"");
986TEST(DynListQueueNoexcept, SwapIsNoexcept)
989 static_assert(
noexcept(q1.
swap(q2)),
"swap should be noexcept");
992TEST(DynListQueueNoexcept, SizeIsNoexcept)
995 static_assert(
noexcept(q.
size()),
"size should be noexcept");
998TEST(DynListQueueNoexcept, IsEmptyIsNoexcept)
1001 static_assert(
noexcept(q.
is_empty()),
"is_empty should be noexcept");
1004TEST(DynListQueueNoexcept, EmptyIsNoexcept)
1007 static_assert(
noexcept(q.
empty()),
"empty should be noexcept");
1010TEST(DynListQueueNoexcept, MoveConstructorIsNoexcept)
1012 static_assert(std::is_nothrow_move_constructible_v<DynListQueue<int>>,
1013 "Move constructor should be noexcept");
1016TEST(DynListQueueNoexcept, MoveAssignmentIsNoexcept)
1018 static_assert(std::is_nothrow_move_assignable_v<DynListQueue<int>>,
1019 "Move assignment should be noexcept");
1026TEST(DynListQueueEmplace, EmplaceBasic)
1034 EXPECT_EQ(q.
size(), 3u);
1037 EXPECT_EQ(p1.first, 1);
1038 EXPECT_EQ(p1.second,
"one");
1041TEST(DynListQueueEmplace, EmplaceReturnsReference)
1045 auto& ref = q.
emplace(10, 20);
1046 EXPECT_EQ(ref.first, 10);
1047 EXPECT_EQ(ref.second, 20);
1051 EXPECT_EQ(q.
front().first, 100);
1054TEST(DynListQueueEmplace, EmplaceWithString)
1062 EXPECT_EQ(q.
get(),
"hello");
1063 EXPECT_EQ(q.
get(),
"xxxxx");
1074 EXPECT_EQ(q.
pop(), 1);
1075 EXPECT_EQ(q.
pop(), 2);
1076 EXPECT_EQ(q.
pop(), 3);
1080TEST(DynListQueueAliases, PopFromEmpty)
1083 EXPECT_THROW(q.
pop(), std::underflow_error);
1086TEST(DynListQueueAliases, ClearAlias)
1090 EXPECT_EQ(q.
size(), 5u);
1093 EXPECT_EQ(q.
size(), 0u);
1096TEST(DynListQueueAliases, ClearOnEmpty)
1104TEST(DynListQueueAliases, ClearIsNoexcept)
1107 static_assert(
noexcept(q.
clear()),
"clear should be noexcept");
1114TEST(DynListQueueMemory, DestructorFreesMemory)
1121 for (
int i = 0; i < 1000; ++i)
1130TEST(DynListQueueMemory, EmptyFreesMemory)
1134 for (
int i = 0; i < 1000; ++i)
1142 for (
int i = 0; i < 100; ++i)
1145 EXPECT_EQ(q.
size(), 100u);
1152TEST(DynListQueueConstCorrectness, ConstFrontReturnsConstReference)
1156 const int& ref = q.
front();
1163TEST(DynListQueueConstCorrectness, NonConstFrontReturnsModifiableReference)
1167 int& ref = q.
front();
1170 EXPECT_EQ(q.
front(), 100);
1173TEST(DynListQueueConstCorrectness, ConstRearReturnsConstReference)
1177 const int& ref = q.
rear();
1181TEST(DynListQueueConstCorrectness, NonConstRearReturnsModifiableReference)
1185 int& ref = q.
rear();
1190 EXPECT_EQ(q.
get(), 300);
1197TEST(DynListQueueEquality, EqualQueuesAreEqual)
1203 bool eq = (q1 == q2);
1204 bool neq = (q1 != q2);
1209TEST(DynListQueueEquality, DifferentSizesAreNotEqual)
1214 bool eq = (q1 == q2);
1215 bool neq = (q1 != q2);
1220TEST(DynListQueueEquality, DifferentElementsAreNotEqual)
1225 bool eq = (q1 == q2);
1226 bool neq = (q1 != q2);
1231TEST(DynListQueueEquality, EmptyQueuesAreEqual)
1236 bool eq = (q1 == q2);
1237 bool neq = (q1 != q2);
1242TEST(DynListQueueEquality, SelfEquality)
1248 bool neq = (q != q);
1253TEST(DynListQueueEquality, EmptyVsNonEmpty)
1258 bool eq = (empty == non_empty);
1259 bool neq = (empty != non_empty);
1268TEST(DynListQueueSearch, SearchExistingElement)
1273 ASSERT_NE(ptr,
nullptr);
1277TEST(DynListQueueSearch, SearchNonExistingElement)
1282 EXPECT_EQ(ptr,
nullptr);
1285TEST(DynListQueueSearch, SearchInEmptyQueue)
1290 EXPECT_EQ(ptr,
nullptr);
1293TEST(DynListQueueSearch, SearchFirstElement)
1298 ASSERT_NE(ptr,
nullptr);
1302TEST(DynListQueueSearch, SearchLastElement)
1307 ASSERT_NE(ptr,
nullptr);
1311TEST(DynListQueueSearch, ConstSearch)
1315 const int* ptr = q.
search(3);
1316 ASSERT_NE(ptr,
nullptr);
1320TEST(DynListQueueSearch, SearchDuplicates)
1325 ASSERT_NE(ptr,
nullptr);
1336 ::testing::InitGoogleTest(&argc, argv);
1337 return RUN_ALL_TESTS();
Zip iterators and functional operations for multiple containers.
Functional programming utilities for Aleph-w containers.
size_t size_t int32_t value
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.
void swap(DynListQueue &__q) noexcept
Swap this with __q in constant time.
void empty() noexcept
Empty the queue.
T * search(const T &key) noexcept(noexcept(static_cast< bool >(std::declval< const T & >()==std::declval< const T & >())))
Search for an item in the queue using equality comparison.
T & front()
Return a modifiable reference to the oldest item in the queue.
void clear() noexcept
Empties the container.
T pop()
Alias for get() - removes and returns the front item.
T & insert(const T &data)
T & append(const T &data)
T & emplace(Args &&... args)
Construct an item in place at the rear of the queue.
bool traverse(Operation &operation) noexcept(noexcept(q.traverse(operation)))
Traverse all the elements from the oldest (front) to the youngest (rear) and execute operation on eac...
size_t size() const noexcept
Return the number of elements.
T Item_Type
The type of set.
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 & get_curr() const
Return the current item.
Doubly-linked list (defined in tpl_dynList.H).
size_t size() const noexcept
Return the number of items of the list, in constant time.
bool has_curr() const noexcept
Number Theoretic Transform over Z / MOD Z.
Minimal std::expected-style result type for C++20.
DynListQueue< int > queue_with_items
DynListQueue< int > empty_queue
static constexpr size_t N
std::pair< Aleph::DynList< T >, Aleph::DynList< T > > partition(Operation &op) const
Exclusive partition of container according to a filter criterion.
Aleph::DynList< Aleph::dry_detail::map_result_t< R, Operation &, T > > maps(Operation &op) const
bool exists(Operation &op) const
Test for existence in the container of an element satisfying a criterion.
Aleph::DynList< T > filter(Operation &operation) const
Filter the elements of a container according to a matching criterion.
bool all(Operation &operation) const
Check if all the elements of the container satisfy a condition.
Acc foldl(const Acc &init, Op &op) const
Fold the elements of the container to a specific result.
Aleph::DynList< T > rev() const
Return a list with the elements of container in reverse order respect to its traversal order.
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.
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.
T product(const Container &container, const T &init=T{1})
Compute product of all elements.
Itor::difference_type count(const Itor &beg, const Itor &end, const T &value)
Count elements equal to a value.
T sum(const Container &container, const T &init=T{})
Compute sum of all elements.
Iterator on elements of a queue.
Aleph::DynList< T > items() const
Return a list of all the elements of a container sorted by traversal order.
Aleph::DynList< T > keys() const
See items().
NonCopyable & operator=(const NonCopyable &)=delete
NonCopyable(const NonCopyable &)=delete
NonCopyable(NonCopyable &&other) noexcept
NonCopyable & operator=(NonCopyable &&other) noexcept
ThrowingType(const ThrowingType &other)
static int construction_count
ThrowingType(ThrowingType &&other) noexcept
Dynamic queue implementation based on linked lists.
TEST_F(DynListQueueTest, DefaultConstruction)