42# include <gtest/gtest.h>
53using namespace testing;
66 static constexpr size_t N = 100;
70 for (
size_t i = 0; i <
N; ++i)
94 while (!stack_with_items.is_empty())
105 for (
int i = 0; i < 10; ++i)
128 std::vector<int>
vec = {10, 20, 30, 40, 50};
153 s = stack_with_items;
174 target = std::move(source);
199 std::string value =
"hello";
201 std::string&
ref = s.
push(std::move(value));
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));
327 stack_with_items.empty();
420 for (
auto it = s.
get_it(); it.has_curr(); it.next())
421 visited.push_back(it.get_curr());
434 for (
const auto& item : s)
459 for (
const auto& item : s)
480 bool result = stack_with_items.traverse([&
sum](
int& item) {
492 bool result = stack_with_items.traverse([&
count](
int&) {
503 bool result = empty_stack.traverse([&called](
int&) {
532 stack_with_items.for_each([&
sum](
const int& item) {
542 auto doubled = s.
maps([](
int i) {
return i * 2; });
554 auto evens = s.
filter([](
int i) {
return i % 2 == 0; });
627 auto ptr = stack_with_items.find_ptr([](
int i) {
return i == 50; });
635 auto ptr = stack_with_items.find_ptr([](
int i) {
return i == 9999; });
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; });
660 EXPECT_EQ(stack_with_items.nth(0),
static_cast<int>(
N - 1));
666 EXPECT_THROW(stack_with_items.nth(
N + 100), std::out_of_range);
671 auto it = stack_with_items.get_it();
673 EXPECT_EQ(it.get_curr(),
static_cast<int>(
N - 1));
683 auto keys = s.
keys();
691 auto items = s.
items();
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");
733 s.
push(std::make_unique<int>(1));
734 s.
push(std::make_unique<int>(2));
735 s.
push(std::make_unique<int>(3));
784 throw std::runtime_error(
"Too many constructions");
790 throw std::runtime_error(
"Too many constructions");
805 for (
int i = 0; i < 50; ++i)
817 constexpr size_t LARGE_N = 100000;
820 for (
size_t i = 0; i <
LARGE_N; ++i)
821 s.
push(
static_cast<int>(i));
827 for (
size_t i =
LARGE_N; i > 0; --i)
842 for (
int i = 0; i < 3; ++i)
861 for (
int i = 0; i < 100; ++i)
894 for (
int i = 0; i < 100; ++i)
921 for (
int i = -100; i <= 100; ++i)
925 for (
int i = 100; i >= -100; --i)
949 static_assert(
noexcept(
s1.
swap(
s2)),
"swap should be noexcept");
955 static_assert(
noexcept(s.
size()),
"size should be noexcept");
961 static_assert(
noexcept(s.
is_empty()),
"is_empty should be noexcept");
967 static_assert(
noexcept(s.
empty()),
"empty should be noexcept");
973 static_assert(
noexcept(s.
clear()),
"clear should be noexcept");
978 static_assert(std::is_nothrow_move_constructible_v<DynListStack<int>>,
979 "Move constructor should be noexcept");
984 static_assert(std::is_nothrow_move_assignable_v<DynListStack<int>>,
985 "Move assignment should be noexcept");
1039 for (
int i = 0; i < 1000; ++i)
1050 for (
int i = 0; i < 1000; ++i)
1058 for (
int i = 0; i < 100; ++i)
1072 const int&
ref = s.
top();
1159 bool neq = (s != s);
1226 const int* ptr = s.
search(3);
1249 for (
int i = 1; i <= 5; ++i)
1264 for (
int i = 1; i <= 10; ++i)
1351 ::testing::InitGoogleTest(&
argc,
argv);
Functional programming utilities for Aleph-w containers.
Dynamic stack of elements of generic type T based on a singly linked list.
void clear() noexcept
Alias for empty() - removes all elements.
T & top()
Return a modifiable reference to the top item of the stack.
bool is_empty() const noexcept
Check if the stack is empty.
constexpr size_t size() const noexcept
Return the number of elements in the stack.
T & emplace(Args &&... args)
Construct an item in place at the top of 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 * search(const T &key) noexcept
Search for an item in the stack using equality comparison.
T get()
Alias for pop() - removes and returns the top item.
T Item_Type
The type of elements stored in the stack.
T & put(const T &data)
Alias for push() - for compatibility with queue-like interfaces.
T & insert(const T &data)
Alias for push() - for STL-like insert semantics.
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.
Dynamic singly linked list with functional programming support.
DynList & swap(DynList &l) noexcept
Swap this with l.
void next()
Move the iterator one item forward.
bool has_curr() const noexcept
Return true if iterator has current item.
constexpr bool is_empty() const noexcept
Return true if list is empty.
size_t size() const noexcept
Count the number of elements of the list.
DynListStack< int > stack_with_items
DynListStack< int > empty_stack
static constexpr size_t N
__T foldl(const __T &init, Op &op) const
Fold the elements of the container to a specific result.
Aleph::DynList< T > filter(Operation &operation) const
Filter the elements of a container according to a matching criteria.
std::pair< Aleph::DynList< T >, Aleph::DynList< T > > partition(Operation &op) const
Exclusive partition of container according to a filter criteria.
Aleph::DynList< T > take(const size_t n) const
Return a list with the first n elements seen in the container during its traversal.
bool exists(Operation &op) const
Test for existence in the container of an element satisfying a criteria.
bool all(Operation &operation) const
Check if all the elements of container satisfy a condition.
Aleph::DynList< T > rev() const
Return a list with the elements of container in reverse order respect to its traversal order.
Aleph::DynList< __T > maps(Operation &op) const
Map the elements of the container.
Aleph::DynList< T > drop(const size_t n) const
Drop the first n elements seen in the container during its traversal.
auto get_it() const
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.
DynList< T > maps(const C &c, Op op)
Classic map operation.
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 > keys() const
Aleph::DynList< T > items() const
Return a list of all the elements of a container sorted by traversal order.
bool traverse(Operation &operation) noexcept(traverse_is_noexcept< Operation >())
Traverse the container via its iterator and performs a conditioned operation on each item.
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)