42# include <gtest/gtest.h>
53using namespace testing;
66 static constexpr size_t N = 100;
70 for (
size_t i = 0; i <
N; ++i)
83 EXPECT_EQ(s.
size(), 0u);
90 EXPECT_EQ(
copy.size(), stack_with_items.size());
91 EXPECT_EQ(
copy.size(),
N);
94 while (!stack_with_items.is_empty())
96 EXPECT_EQ(stack_with_items.pop(),
copy.pop());
98 EXPECT_TRUE(stack_with_items.is_empty());
99 EXPECT_TRUE(
copy.is_empty());
105 for (
int i = 0; i < 10; ++i)
108 size_t original_size = source.size();
109 int top_value = source.top();
112 EXPECT_EQ(moved.
size(), original_size);
113 EXPECT_TRUE(source.is_empty());
114 EXPECT_EQ(moved.
top(), top_value);
121 EXPECT_EQ(s.
size(), 5u);
123 EXPECT_EQ(s.
top(), 5);
128 std::vector<int> vec = {10, 20, 30, 40, 50};
131 EXPECT_EQ(s.
size(), vec.size());
133 EXPECT_EQ(s.
top(), 50);
153 s = stack_with_items;
155 EXPECT_EQ(s.
size(),
N);
156 EXPECT_EQ(s.
top(),
static_cast<int>(
N - 1));
164 EXPECT_EQ(s.
size(), 3u);
165 EXPECT_EQ(s.
top(), 3);
174 target = std::move(source);
176 EXPECT_EQ(target.
size(), 3u);
177 EXPECT_EQ(target.
top(), 3);
191 EXPECT_EQ(s.
size(), 1u);
193 EXPECT_EQ(s.
top(), 42);
199 std::string
value =
"hello";
201 std::string& ref = s.
push(std::move(
value));
203 EXPECT_EQ(s.
size(), 1u);
204 EXPECT_EQ(ref,
"hello");
212 EXPECT_EQ(s.
top(), 1);
215 EXPECT_EQ(s.
top(), 2);
218 EXPECT_EQ(s.
top(), 3);
220 EXPECT_EQ(s.
size(), 3u);
230 EXPECT_EQ(s.
pop(), 3);
231 EXPECT_EQ(s.
pop(), 2);
232 EXPECT_EQ(s.
pop(), 1);
239 EXPECT_THROW(s.
pop(), std::underflow_error);
246 EXPECT_EQ(s.
get(), 3);
247 EXPECT_EQ(s.
get(), 2);
248 EXPECT_EQ(s.
get(), 1);
254 EXPECT_EQ(stack_with_items.top(),
static_cast<int>(
N - 1));
257 EXPECT_EQ(stack_with_items.top(),
static_cast<int>(
N - 1));
258 EXPECT_EQ(stack_with_items.top(),
static_cast<int>(
N - 1));
261 EXPECT_EQ(stack_with_items.size(),
N);
267 EXPECT_THROW(s.
top(), std::underflow_error);
274 EXPECT_EQ(s.
peek(), 3);
275 EXPECT_EQ(s.
size(), 3u);
283 EXPECT_EQ(s.
pop(), 100);
284 EXPECT_EQ(s.
pop(), 2);
295 EXPECT_EQ(s.
size(), 0u);
298 EXPECT_EQ(s.
size(), 1u);
301 EXPECT_EQ(s.
size(), 2u);
304 EXPECT_EQ(s.
size(), 1u);
307 EXPECT_EQ(s.
size(), 0u);
325 EXPECT_EQ(stack_with_items.size(),
N);
327 stack_with_items.empty();
329 EXPECT_TRUE(stack_with_items.is_empty());
330 EXPECT_EQ(stack_with_items.size(), 0u);
337 EXPECT_TRUE(empty_stack.is_empty());
338 EXPECT_EQ(empty_stack.size(), 0u);
348 EXPECT_EQ(s.
size(), 0u);
362 EXPECT_EQ(s1.
size(), 2u);
363 EXPECT_EQ(s2.
size(), 3u);
365 EXPECT_EQ(s1.
top(), 20);
366 EXPECT_EQ(s2.
top(), 3);
377 EXPECT_EQ(s2.
size(), 3u);
378 EXPECT_EQ(s2.
top(), 3);
387 EXPECT_EQ(s.
size(), 3u);
388 EXPECT_EQ(s.
top(), 3);
419 std::vector<int> visited;
420 for (
auto it = s.
get_it(); it.has_curr(); it.next())
421 visited.push_back(it.get_curr());
423 ASSERT_EQ(visited.size(), 3u);
424 EXPECT_EQ(visited[0], 3);
425 EXPECT_EQ(visited[1], 2);
426 EXPECT_EQ(visited[2], 1);
434 for (
const auto& item : s)
451 EXPECT_EQ(it, s.
end());
459 for (
const auto& item : s)
480 bool result = stack_with_items.traverse([&
sum](
int& item) {
486 EXPECT_EQ(
sum,
static_cast<int>(
N * (
N - 1) / 2));
492 bool result = stack_with_items.traverse([&
count](
int&) {
496 EXPECT_FALSE(result);
503 bool result = empty_stack.traverse([&called](
int&) {
509 EXPECT_FALSE(called);
522 EXPECT_EQ(
sum,
static_cast<int>(
N * (
N - 1) / 2));
532 stack_with_items.for_each([&
sum](
const int& item) {
536 EXPECT_EQ(
sum,
static_cast<int>(
N * (
N - 1) / 2));
542 auto doubled = s.
maps([](
int i) {
return i * 2; });
544 EXPECT_EQ(doubled.size(), 5u);
554 auto evens = s.
filter([](
int i) {
return i % 2 == 0; });
556 EXPECT_EQ(evens.size(), 5u);
562 int product = s.
foldl(1, [](
int acc,
int item) {
return acc * item; });
571 EXPECT_TRUE(s.
all([](
int i) { return i % 2 == 0; }));
572 EXPECT_FALSE(s.
all([](
int i) { return i > 5; }));
579 EXPECT_TRUE(s.
exists([](
int i) { return i == 3; }));
580 EXPECT_FALSE(s.
exists([](
int i) { return i == 10; }));
586 auto [evens, odds] = s.
partition([](
int i) {
return i % 2 == 0; });
588 EXPECT_EQ(evens.size(), 3u);
589 EXPECT_EQ(odds.size(), 3u);
594 auto first_five = stack_with_items.take(5);
596 EXPECT_EQ(first_five.size(), 5u);
601 size_t drop_count =
N - 5;
602 auto last_five = stack_with_items.drop(drop_count);
604 EXPECT_EQ(last_five.size(), 5u);
610 auto reversed = s.
rev();
612 EXPECT_EQ(reversed.size(), 5u);
617 EXPECT_EQ(stack_with_items.length(),
N);
618 EXPECT_EQ(empty_stack.length(), 0u);
627 auto ptr = stack_with_items.find_ptr([](
int i) {
return i == 50; });
629 ASSERT_NE(ptr,
nullptr);
635 auto ptr = stack_with_items.find_ptr([](
int i) {
return i == 9999; });
637 EXPECT_EQ(ptr,
nullptr);
644 auto idx = stack_with_items.find_index([](
int i) {
return i ==
static_cast<int>(
N - 1); });
651 auto [found,
value] = stack_with_items.find_item([](
int i) {
return i == 50; });
654 EXPECT_EQ(
value, 50);
660 EXPECT_EQ(stack_with_items.nth(0),
static_cast<int>(
N - 1));
665 EXPECT_THROW(stack_with_items.nth(
N), std::out_of_range);
666 EXPECT_THROW(stack_with_items.nth(
N + 100), std::out_of_range);
671 auto it = stack_with_items.get_it();
672 EXPECT_TRUE(it.has_curr());
673 EXPECT_EQ(it.get_curr(),
static_cast<int>(
N - 1));
685 EXPECT_EQ(
keys.size(), 3u);
691 auto items = s.
items();
693 EXPECT_EQ(items.size(), 3u);
705 static_assert(std::is_same_v<SetType, DynListStack<int>>,
706 "Set_Type should be DynListStack<int>");
707 static_assert(std::is_same_v<ItemType, int>,
708 "Item_Type should be int");
723 EXPECT_EQ(s.
size(), 3u);
724 EXPECT_EQ(s.
pop(),
"!");
725 EXPECT_EQ(s.
pop(),
"world");
726 EXPECT_EQ(s.
pop(),
"hello");
729TEST(DynListStackComplexTypes, UniquePtr)
733 s.
push(std::make_unique<int>(1));
734 s.
push(std::make_unique<int>(2));
735 s.
push(std::make_unique<int>(3));
737 EXPECT_EQ(s.
size(), 3u);
763TEST(DynListStackComplexTypes, MoveOnlyType)
770 EXPECT_EQ(s.
size(), 2u);
773 EXPECT_EQ(item.value, 2);
784 throw std::runtime_error(
"Too many constructions");
790 throw std::runtime_error(
"Too many constructions");
800TEST(DynListStackComplexTypes, ExceptionSafety)
805 for (
int i = 0; i < 50; ++i)
808 EXPECT_EQ(s.
size(), 50u);
815TEST(DynListStackStress, LargeStack)
817 constexpr size_t LARGE_N = 100000;
820 for (
size_t i = 0; i <
LARGE_N; ++i)
821 s.
push(
static_cast<int>(i));
824 EXPECT_EQ(s.
top(),
static_cast<int>(
LARGE_N - 1));
827 for (
size_t i =
LARGE_N; i > 0; --i)
828 EXPECT_EQ(s.
pop(),
static_cast<int>(i - 1));
833TEST(DynListStackStress, InterleavedOperations)
839 for (
int round = 0; round < 1000; ++round)
842 for (
int i = 0; i < 3; ++i)
843 s.
push(push_count++);
851 EXPECT_EQ(s.
size(), 1000u);
854TEST(DynListStackStress, RepeatedEmptyFill)
858 for (
int round = 0; round < 100; ++round)
861 for (
int i = 0; i < 100; ++i)
864 EXPECT_EQ(s.
size(), 100u);
870 EXPECT_EQ(s.
size(), 0u);
878TEST(DynListStackEdgeCases, SingleElement)
884 EXPECT_EQ(s.
size(), 1u);
885 EXPECT_EQ(s.
top(), 42);
886 EXPECT_EQ(s.
pop(), 42);
890TEST(DynListStackEdgeCases, AlternatingEmptyNonEmpty)
894 for (
int i = 0; i < 100; ++i)
900 EXPECT_EQ(s.
top(), i);
908TEST(DynListStackEdgeCases, ZeroValue)
913 EXPECT_EQ(s.
top(), 0);
914 EXPECT_EQ(s.
pop(), 0);
917TEST(DynListStackEdgeCases, NegativeValues)
921 for (
int i = -100; i <= 100; ++i)
925 for (
int i = 100; i >= -100; --i)
926 EXPECT_EQ(s.
pop(), i);
929TEST(DynListStackEdgeCases, EmptyString)
937 EXPECT_EQ(s.
pop(),
"");
938 EXPECT_EQ(s.
pop(),
"non-empty");
939 EXPECT_EQ(s.
pop(),
"");
946TEST(DynListStackNoexcept, SwapIsNoexcept)
949 static_assert(
noexcept(s1.
swap(s2)),
"swap should be noexcept");
952TEST(DynListStackNoexcept, SizeIsNoexcept)
955 static_assert(
noexcept(s.
size()),
"size should be noexcept");
958TEST(DynListStackNoexcept, IsEmptyIsNoexcept)
961 static_assert(
noexcept(s.
is_empty()),
"is_empty should be noexcept");
964TEST(DynListStackNoexcept, EmptyIsNoexcept)
967 static_assert(
noexcept(s.
empty()),
"empty should be noexcept");
970TEST(DynListStackNoexcept, ClearIsNoexcept)
973 static_assert(
noexcept(s.
clear()),
"clear should be noexcept");
976TEST(DynListStackNoexcept, MoveConstructorIsNoexcept)
978 static_assert(std::is_nothrow_move_constructible_v<DynListStack<int>>,
979 "Move constructor should be noexcept");
982TEST(DynListStackNoexcept, MoveAssignmentIsNoexcept)
984 static_assert(std::is_nothrow_move_assignable_v<DynListStack<int>>,
985 "Move assignment should be noexcept");
992TEST(DynListStackEmplace, EmplaceBasic)
1000 EXPECT_EQ(s.
size(), 3u);
1003 EXPECT_EQ(p3.first, 3);
1004 EXPECT_EQ(p3.second,
"three");
1007TEST(DynListStackEmplace, EmplaceReturnsReference)
1011 auto& ref = s.
emplace(10, 20);
1012 EXPECT_EQ(ref.first, 10);
1013 EXPECT_EQ(ref.second, 20);
1017 EXPECT_EQ(s.
top().first, 100);
1020TEST(DynListStackEmplace, EmplaceWithString)
1027 EXPECT_EQ(s.
pop(),
"xxxxx");
1028 EXPECT_EQ(s.
pop(),
"hello");
1035TEST(DynListStackMemory, DestructorFreesMemory)
1039 for (
int i = 0; i < 1000; ++i)
1046TEST(DynListStackMemory, EmptyFreesMemory)
1050 for (
int i = 0; i < 1000; ++i)
1058 for (
int i = 0; i < 100; ++i)
1061 EXPECT_EQ(s.
size(), 100u);
1068TEST(DynListStackConstCorrectness, ConstTopReturnsConstReference)
1072 const int& ref = s.
top();
1076TEST(DynListStackConstCorrectness, NonConstTopReturnsModifiableReference)
1083 EXPECT_EQ(s.
top(), 100);
1086TEST(DynListStackConstCorrectness, ConstPeekReturnsConstReference)
1090 const int& ref = s.
peek();
1094TEST(DynListStackConstCorrectness, NonConstPeekReturnsModifiableReference)
1098 int& ref = s.
peek();
1101 EXPECT_EQ(s.
pop(), 300);
1108TEST(DynListStackEquality, EqualStacksAreEqual)
1114 bool eq = (s1 == s2);
1115 bool neq = (s1 != s2);
1120TEST(DynListStackEquality, DifferentSizesAreNotEqual)
1125 bool eq = (s1 == s2);
1126 bool neq = (s1 != s2);
1131TEST(DynListStackEquality, DifferentElementsAreNotEqual)
1136 bool eq = (s1 == s2);
1137 bool neq = (s1 != s2);
1142TEST(DynListStackEquality, EmptyStacksAreEqual)
1147 bool eq = (s1 == s2);
1148 bool neq = (s1 != s2);
1153TEST(DynListStackEquality, SelfEquality)
1159 bool neq = (s != s);
1164TEST(DynListStackEquality, EmptyVsNonEmpty)
1169 bool eq = (empty == non_empty);
1170 bool neq = (empty != non_empty);
1179TEST(DynListStackSearch, SearchExistingElement)
1184 ASSERT_NE(ptr,
nullptr);
1188TEST(DynListStackSearch, SearchNonExistingElement)
1193 EXPECT_EQ(ptr,
nullptr);
1196TEST(DynListStackSearch, SearchInEmptyStack)
1201 EXPECT_EQ(ptr,
nullptr);
1204TEST(DynListStackSearch, SearchTopElement)
1209 ASSERT_NE(ptr,
nullptr);
1213TEST(DynListStackSearch, SearchBottomElement)
1218 ASSERT_NE(ptr,
nullptr);
1222TEST(DynListStackSearch, ConstSearch)
1226 const int* ptr = s.
search(3);
1227 ASSERT_NE(ptr,
nullptr);
1231TEST(DynListStackSearch, SearchDuplicates)
1236 ASSERT_NE(ptr,
nullptr);
1244TEST(DynListStackLIFO, VerifyLIFOBehavior)
1249 for (
int i = 1; i <= 5; ++i)
1253 EXPECT_EQ(s.
pop(), 5);
1254 EXPECT_EQ(s.
pop(), 4);
1255 EXPECT_EQ(s.
pop(), 3);
1256 EXPECT_EQ(s.
pop(), 2);
1257 EXPECT_EQ(s.
pop(), 1);
1260TEST(DynListStackLIFO, TopAlwaysReturnsLastPushed)
1264 for (
int i = 1; i <= 10; ++i)
1267 EXPECT_EQ(s.
top(), i);
1275TEST(DynListStackCompatibility, PutAlias)
1283 EXPECT_EQ(s.
size(), 3u);
1284 EXPECT_EQ(s.
top(), 3);
1287TEST(DynListStackCompatibility, GetAlias)
1295 EXPECT_EQ(s.
get(), 3);
1296 EXPECT_EQ(s.
get(), 2);
1297 EXPECT_EQ(s.
get(), 1);
1300TEST(DynListStackCompatibility, InsertAlias)
1308 EXPECT_EQ(s.
size(), 3u);
1309 EXPECT_EQ(s.
top(), 30);
1312TEST(DynListStackCompatibility, PutAndGetSymmetry)
1322 EXPECT_EQ(s.
get(), 3);
1323 EXPECT_EQ(s.
get(), 2);
1324 EXPECT_EQ(s.
get(), 1);
1328TEST(DynListStackCompatibility, MixedOperations)
1337 EXPECT_EQ(s.
size(), 3u);
1340 EXPECT_EQ(s.
pop(), 3);
1341 EXPECT_EQ(s.
get(), 2);
1342 EXPECT_EQ(s.
pop(), 1);
1351 ::testing::InitGoogleTest(&argc, argv);
1352 return RUN_ALL_TESTS();
Functional programming utilities for Aleph-w containers.
size_t size_t int32_t value
Dynamic stack of elements of generic type T based on a singly linked list.
T * search(const T &key) noexcept(noexcept(static_cast< bool >(std::declval< const T & >()==std::declval< const T & >())))
Search for an item in the stack using equality comparison.
T & put(const T &data)
Alias for push() - for compatibility with queue-like interfaces.
void clear() noexcept
Empties the container.
T & top()
Return a modifiable reference to the top item of the stack.
bool is_empty() const noexcept
Check if the stack is empty.
T & insert(const T &data)
Alias for push() - for STL-like insert semantics.
T & emplace(Args &&... args)
Construct an item in place at the top of the stack.
size_t size() const noexcept
Return the number of elements in the stack.
T & peek()
Alias for top() - returns reference to top item.
void swap(DynListStack &other) noexcept
Swap the contents of this stack with another.
T get()
Alias for pop() - removes and returns the top item.
T Item_Type
The type of elements stored in the stack.
bool traverse(Operation &operation) noexcept(noexcept(s.traverse(operation)))
Traverse all elements from top to bottom.
T pop()
Remove and return the top item of the stack.
T & push(const T &data)
Push an item by copy onto the top of the stack.
void empty() noexcept
Remove all elements from the stack.
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.
DynListStack< int > stack_with_items
DynListStack< int > empty_stack
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 for traversing elements of the stack.
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
Fixture with a stack of strings.
ThrowingType(const ThrowingType &other)
static int construction_count
ThrowingType(ThrowingType &&other) noexcept
Dynamic stack implementation based on linked lists.
TEST_F(DynListStackTest, DefaultConstruction)