48# include <gtest/gtest.h>
60using namespace testing;
67constexpr size_t N = 17;
78 for (
size_t i = 0; i <
N; ++i, ++
n)
91 for (
size_t i = 0; i <
N; ++i, ++
n)
92 s.push({ int(i), 0, 1, 2, int(i) });
120 other.moved_from =
true;
125 other.moved_from =
true;
144 constructions = destructions = copies = moves = 0;
189 EXPECT_EQ(s.
size(), 0);
210 EXPECT_EQ(s.
size(), 5);
215 EXPECT_EQ(s.
pop(), 5);
216 EXPECT_EQ(s.
pop(), 4);
217 EXPECT_EQ(s.
pop(), 3);
218 EXPECT_EQ(s.
pop(), 2);
219 EXPECT_EQ(s.
pop(), 1);
227 EXPECT_EQ(s.
size(), 0);
234 EXPECT_EQ(s.
size(), 3);
235 EXPECT_EQ(s.
top(),
"test");
246 EXPECT_EQ(
copy.size(), s.size());
247 EXPECT_EQ(
copy.top(), s.top());
251 EXPECT_NE(
copy.size(), s.size());
252 EXPECT_EQ(
copy.size(), n);
259 for (
size_t i = 0; i < n; ++i)
261 ASSERT_EQ(
copy.top(), s.top());
272 EXPECT_TRUE(
copy.is_empty());
273 EXPECT_EQ(
copy.size(), 0);
284 EXPECT_FALSE(
copy.top().exists([](
int x) { return x == 999; }));
293 size_t original_size = s.size();
294 int original_top = s.top();
298 EXPECT_EQ(moved.
size(), original_size);
299 EXPECT_EQ(moved.
top(), original_top);
300 EXPECT_TRUE(s.is_empty());
315 s.
push(make_unique<int>(42));
316 s.
push(make_unique<int>(43));
320 EXPECT_EQ(moved.
size(), 2);
321 EXPECT_EQ(*moved.
top(), 43);
337 EXPECT_EQ(other.
size(), n);
338 EXPECT_EQ(other.
top(), s.top());
345 EXPECT_EQ(s.size(), n);
346 EXPECT_EQ(s.top(),
int(n - 1));
356 EXPECT_EQ(dest.
size(), 3);
357 EXPECT_EQ(dest.
top(), 3);
376 size_t original_size = s.size();
382 EXPECT_EQ(dest.
size(), original_size);
385 EXPECT_NO_THROW(s.push(123));
386 EXPECT_FALSE(s.is_empty());
391 size_t original_size = s.size();
396 EXPECT_EQ(s.size(), original_size);
407 size_t s_size = s.
size();
408 size_t other_size = other.
size();
410 int other_top = other.
top();
414 EXPECT_EQ(s.size(), other_size);
415 EXPECT_EQ(other.
size(), s_size);
416 EXPECT_EQ(s.top(), other_top);
417 EXPECT_EQ(other.
top(), s_top);
428 EXPECT_EQ(empty.
size(), 3);
434 EXPECT_TRUE(
noexcept(s1.
swap(s2)));
444 string value =
"test";
448 EXPECT_EQ(s.
size(), 1);
449 EXPECT_EQ(s.
top(),
"test");
450 EXPECT_EQ(
value,
"test");
456 string value =
"test";
460 EXPECT_EQ(s.
size(), 1);
461 EXPECT_EQ(s.
top(),
"test");
462 EXPECT_TRUE(
value.empty());
469 int& ref = s.
push(42);
472 EXPECT_EQ(&ref, &s.
top());
475 EXPECT_EQ(s.
top(), 100);
482 for (
int i = 0; i < 10; ++i)
485 for (
int i = 9; i >= 0; --i)
486 ASSERT_EQ(s.
pop(), i);
499 EXPECT_EQ(s.
size(), 1);
500 EXPECT_EQ(s.
top().first, 42);
501 EXPECT_EQ(s.
top().second,
"hello");
511 ThreeArgs(
int a_,
double b_,
string c_) : a(a_), b(b_), c(c_) {}
517 EXPECT_EQ(s.
top().a, 1);
518 EXPECT_DOUBLE_EQ(s.
top().b, 2.5);
519 EXPECT_EQ(s.
top().c,
"test");
530 EXPECT_EQ(s.
top(), 100);
539 int top_value = s.top();
541 int popped = s.pop();
543 EXPECT_EQ(popped, top_value);
544 EXPECT_EQ(s.size(), n - 1);
551 EXPECT_THROW(s.
pop(), underflow_error);
558 EXPECT_EQ(s.
pop(), 3);
559 EXPECT_EQ(s.
pop(), 2);
560 EXPECT_EQ(s.
pop(), 1);
562 EXPECT_THROW(s.
pop(), underflow_error);
568 s.
push(make_unique<int>(42));
570 unique_ptr<int> ptr = s.
pop();
582 EXPECT_EQ(s.top(),
int(n - 1));
585 EXPECT_EQ(s.top(), 999);
592 EXPECT_EQ(cs.
top(),
int(n - 1));
599 EXPECT_THROW(s.
top(), underflow_error);
606 EXPECT_THROW(s.
top(), underflow_error);
611 EXPECT_EQ(s.peek(), s.top());
612 EXPECT_EQ(&s.peek(), &s.top());
618 EXPECT_EQ(cs.
peek(), cs.
top());
627 int pop_result = s.pop();
628 int get_result = s.get();
630 EXPECT_EQ(s.size(), n - 2);
631 EXPECT_NE(pop_result, get_result);
637 EXPECT_THROW(s.
get(), underflow_error);
647 EXPECT_EQ(s.
size(), 0);
652 EXPECT_EQ(s.size(), n);
655 EXPECT_EQ(s.size(), n + 1);
659 EXPECT_EQ(s.size(), n - 1);
670 EXPECT_FALSE(s.is_empty());
690 EXPECT_FALSE(s.is_empty());
694 EXPECT_TRUE(s.is_empty());
695 EXPECT_EQ(s.size(), 0);
711 EXPECT_TRUE(s.is_empty());
712 EXPECT_EQ(s.size(), 0);
718 EXPECT_TRUE(
noexcept(s.
empty()));
724 EXPECT_TRUE(
noexcept(s.
clear()));
739 EXPECT_EQ(s.
size(), 3);
740 EXPECT_EQ(s.
pop(), 3);
741 EXPECT_EQ(s.
pop(), 2);
742 EXPECT_EQ(s.
pop(), 1);
753 EXPECT_EQ(s.
size(), 3);
754 EXPECT_EQ(s.
pop(), 3);
765 EXPECT_EQ(s.
size(), 3);
766 EXPECT_EQ(s.
pop(), 3);
775 int* ptr = s.search(5);
777 ASSERT_NE(ptr,
nullptr);
783 int* ptr = s.search(999);
785 EXPECT_EQ(ptr,
nullptr);
792 EXPECT_EQ(s.
search(1),
nullptr);
799 const int* ptr = cs.
search(5);
801 ASSERT_NE(ptr,
nullptr);
807 string* ptr = s.search(
"second");
809 ASSERT_NE(ptr,
nullptr);
810 EXPECT_EQ(*ptr,
"second");
812 EXPECT_EQ(s.search(
"missing"),
nullptr);
821 EXPECT_TRUE(s.contains(5));
822 EXPECT_TRUE(s.contains(0));
823 EXPECT_TRUE(s.contains(
int(n - 1)));
828 EXPECT_FALSE(s.contains(-1));
829 EXPECT_FALSE(s.contains(
int(n)));
830 EXPECT_FALSE(s.contains(999));
841 EXPECT_EQ(s.has(5), s.contains(5));
842 EXPECT_EQ(s.has(999), s.contains(999));
854 EXPECT_FALSE(it.has_curr());
855 EXPECT_THROW(it.get_curr(), overflow_error);
856 EXPECT_THROW(it.next(), overflow_error);
861 auto it = s.get_it();
863 for (
size_t i = 0; it.has_curr(); it.next(), ++i)
864 ASSERT_EQ(it.get_curr(),
int(n - i - 1));
870 for (
auto it = s.get_it(); it.has_curr(); it.next())
878 vector<int> elements;
880 for (
const auto& item : s)
881 elements.push_back(item);
883 ASSERT_EQ(elements.size(), n);
884 for (
size_t i = 0; i < n; ++i)
885 EXPECT_EQ(elements[i],
int(n - i - 1));
890 auto it = s.get_it();
892 for (
size_t i = 0; it.has_curr(); it.next(), ++i)
894 const auto& list = it.get_curr();
895 ASSERT_EQ(list.get_first(),
int(n - i - 1));
896 ASSERT_EQ(list.get_last(),
int(n - i - 1));
907 bool result = s.traverse([&
count](
int) { ++
count;
return true; });
916 bool result = s.traverse([&
count](
int)
922 EXPECT_FALSE(result);
929 s.traverse([&i,
this](
int value)
931 EXPECT_EQ(
value,
int(n - i - 1));
942 bool result = s.
traverse([&called](
int) { called =
true;
return true; });
945 EXPECT_FALSE(called);
964 auto doubled = s.maps([](
int x) {
return x * 2; });
966 EXPECT_EQ(doubled.size(), n);
968 auto it = doubled.get_it();
969 for (
size_t i = 0; it.has_curr(); it.next(), ++i)
970 EXPECT_EQ(it.get_curr(),
int((n - i - 1) * 2));
975 auto strings = s.template maps<string>([](
int x)
980 EXPECT_EQ(strings.size(), n);
981 EXPECT_EQ(strings.get_first(),
to_string(
int(n - 1)));
986 auto evens = s.filter([](
int x) {
return x % 2 == 0; });
988 for (
auto it = evens.get_it(); it.has_curr(); it.next())
989 EXPECT_EQ(it.get_curr() % 2, 0);
994 auto result = s.filter([](
int x) {
return x > 1000; });
996 EXPECT_TRUE(result.is_empty());
1001 int sum = s.foldl(0, [](
int acc,
int x) {
return acc + x; });
1009 string result = s.template foldl<string>(
"", [](
const string& acc,
int x)
1014 EXPECT_FALSE(result.empty());
1019 bool result = s.all([](
int x) {
return x >= 0; });
1020 EXPECT_TRUE(result);
1025 bool result = s.all([](
int x) {
return x < 10; });
1026 EXPECT_FALSE(result);
1031 bool result = s.exists([](
int x) {
return x == 5; });
1032 EXPECT_TRUE(result);
1037 bool result = s.exists([](
int x) {
return x == 999; });
1038 EXPECT_FALSE(result);
1044 s.for_each([&
sum](
int x) {
sum += x; });
1058 static_assert(is_same_v<Stack::Item_Type, int>);
1059 static_assert(is_same_v<Stack::Key_Type, int>);
1060 static_assert(is_same_v<Stack::Set_Type, Stack>);
1073 for (
int i = 0; i < 100; ++i)
1078 EXPECT_EQ(Counted::constructions, Counted::destructions);
1086 for (
int i = 0; i < 50; ++i)
1089 int constructions_before = Counted::constructions;
1092 EXPECT_EQ(Counted::destructions, constructions_before);
1103 s.
push(make_unique<int>(42));
1104 s.
push(make_unique<int>(43));
1106 EXPECT_EQ(s.
size(), 2);
1107 EXPECT_EQ(*s.
top(), 43);
1110 EXPECT_EQ(*ptr, 43);
1120 s.
push(std::move(c));
1123 EXPECT_GE(Counted::moves, 1);
1135 EXPECT_EQ(s.
size(), 1);
1136 EXPECT_EQ(s.
top(), 42);
1139 EXPECT_EQ(s.
pop(), 42);
1147 for (
int cycle = 0; cycle < 10; ++cycle)
1149 for (
int i = 0; i < 100; ++i)
1152 EXPECT_EQ(s.
size(), 100);
1154 for (
int i = 99; i >= 0; --i)
1155 ASSERT_EQ(s.
pop(), i);
1167 EXPECT_EQ(s.
pop(), 2);
1170 EXPECT_EQ(s.
pop(), 4);
1171 EXPECT_EQ(s.
pop(), 3);
1173 EXPECT_EQ(s.
pop(), 5);
1174 EXPECT_EQ(s.
pop(), 1);
1186 for (
size_t i = 0; i <
LARGE_N; ++i)
1191 for (
size_t i = 0; i <
LARGE_N; ++i)
1201 for (
size_t i = 0; i < 1000; ++i)
1204 EXPECT_EQ(s.
size(), 1000);
1206 for (
size_t i = 999; i != size_t(-1); --i)
1214 for (
size_t i = 0; i <
LARGE_N; ++i)
1223 auto it1 = original.
get_it();
1224 auto it2 =
copy.get_it();
1225 while (it1.has_curr() && it2.has_curr())
1227 ASSERT_EQ(it1.get_curr(), it2.get_curr());
1246 throw runtime_error(
"Copy failed");
1253bool ThrowOnCopy::should_throw =
false;
1261 ThrowOnCopy::should_throw =
true;
1264 EXPECT_THROW(s.
push(item), runtime_error);
1266 ThrowOnCopy::should_throw =
false;
1269 EXPECT_EQ(s.
size(), 2);
1270 EXPECT_EQ(s.
top().value, 2);
1282 int max_val = s.
foldl(numeric_limits<int>::min(),
1283 [](
int acc,
int x) {
return std::max(acc, x); });
1285 EXPECT_EQ(max_val, 9);
1288 int min_val = s.
foldl(numeric_limits<int>::max(),
1289 [](
int acc,
int x) {
return std::min(acc, x); });
1291 EXPECT_EQ(min_val, 1);
1303 EXPECT_EQ(s2.
size(), 3);
1304 EXPECT_EQ(s1.
size(), 0);
1313 size_t count_before = 0;
1314 for (
auto it = s.
get_it(); it.has_curr(); it.next())
1321 size_t count_after = 0;
1322 for (
auto it = s.
get_it(); it.has_curr(); it.next())
1325 EXPECT_EQ(count_before, count_after);
1334 ::testing::InitGoogleTest(&argc, argv);
1335 return RUN_ALL_TESTS();
Functional programming utilities for Aleph-w containers.
size_t size_t int32_t value
T & push(const T &data)
Push into stack a copy of data
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.
bool contains(const T &key) const noexcept(noexcept(static_cast< bool >(std::declval< const T & >()==std::declval< const T & >())))
Check if the stack contains a specific value.
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 & append(const T &data)
Alias for push() - required by Special_Ctors macro.
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.
Number Theoretic Transform over Z / MOD Z.
Minimal std::expected-style result type for C++20.
Acc foldl(const Acc &init, Op &op) const
Fold the elements of the container to a specific result.
auto get_it()
Return a properly initialized iterator positioned at the first item on the container.
TEST_F(SimpleStack, copy_constructor_creates_independent_copy)
Main namespace for Aleph-w library functions.
Itor2 copy(Itor1 sourceBeg, const Itor1 &sourceEnd, Itor2 destBeg)
Copy elements from one range to another.
std::string to_string(const time_t t, const std::string &format)
Format a time_t value into a string using format.
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.
Fixture with a stack of complex objects (DynList<int>)
ArrayStack< DynList< int > > s
Type that counts constructions/destructions.
Counted & operator=(Counted &&other) noexcept
Counted & operator=(const Counted &other)
Counted(Counted &&other) noexcept
bool operator==(const Counted &other) const
Counted(const Counted &other)
Move-only type for testing move semantics.
bool operator==(const MoveOnly &other) const
MoveOnly(MoveOnly &&other) noexcept
MoveOnly & operator=(const MoveOnly &)=delete
MoveOnly & operator=(MoveOnly &&other) noexcept
MoveOnly(const MoveOnly &)=delete
Fixture with a stack of integers.
Fixture with a stack of strings.
ThrowOnCopy(const ThrowOnCopy &other)
ThrowOnCopy & operator=(ThrowOnCopy &&)=default
ThrowOnCopy(ThrowOnCopy &&)=default
ThrowOnCopy & operator=(const ThrowOnCopy &)=default
Dynamic stack implementation based on linked lists.