Aleph-w 7.10.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
inlinestack_test.cc
Go to the documentation of this file.
1
2/*
3 Aleph_w
4
5 Data structures & Algorithms
6 version 2.0.0b
7 https://github.com/lrleon/Aleph-w
8
9 This file is part of Aleph-w library
10
11 Copyright (c) 2002-2026 Leandro Rabindranath Leon
12
13 Permission is hereby granted, free of charge, to any person obtaining a copy
14 of this software and associated documentation files (the "Software"), to deal
15 in the Software without restriction, including without limitation the rights
16 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
17 copies of the Software, and to permit persons to whom the Software is
18 furnished to do so, subject to the following conditions:
19
20 The above copyright notice and this permission notice shall be included in all
21 copies or substantial portions of the Software.
22
23 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
24 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
26 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
28 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
29 SOFTWARE.
30*/
31
38#include <gtest/gtest.h>
39
40#include <tpl_arrayStack.H>
41
42#include <cstddef>
43#include <type_traits>
44#include <utility>
45
46using namespace Aleph;
47
48TEST(InlineStack, never_allocates_and_cannot_throw)
49{
50 using S = InlineStack<int *, 8>;
51 static_assert(S::capacity() == 8);
52 static_assert(sizeof(S) >= 8 * sizeof(int *)); // the storage is inside
53 static_assert(std::is_nothrow_default_constructible_v<S>);
54 static_assert(std::is_nothrow_copy_constructible_v<S>);
55 static_assert(std::is_nothrow_copy_assignable_v<S>);
56 static_assert(std::is_nothrow_move_constructible_v<S>);
57 static_assert(std::is_nothrow_move_assignable_v<S>);
58 static_assert(std::is_nothrow_swappable_v<S>);
59}
60
61TEST(InlineStack, push_pop_top_and_size)
62{
63 int a = 1, b = 2, c = 3;
65 EXPECT_TRUE(s.is_empty());
66 EXPECT_EQ(s.size(), 0u);
67
68 EXPECT_EQ(s.push(&a), &a);
69 s.push(&b);
70 s.push(&c);
71 EXPECT_FALSE(s.is_empty());
72 EXPECT_EQ(s.size(), 3u);
73 EXPECT_EQ(s.top(), &c);
74 EXPECT_EQ(s.top(0), &c);
75 EXPECT_EQ(s.top(1), &b);
76 EXPECT_EQ(s.top(2), &a);
77 EXPECT_EQ(s.base(), &a);
78
79 const InlineStack<int *, 4> & cs = s;
80 EXPECT_EQ(cs.top(), &c);
81 EXPECT_EQ(cs.top(1), &b);
82 EXPECT_EQ(cs.base(), &a);
83
84 EXPECT_EQ(s.pop(), &c);
85 EXPECT_EQ(s.top(), &b);
86 s.top() = &c; // the top is a modifiable reference
87 EXPECT_EQ(s.pop(), &c);
88 EXPECT_EQ(s.pop(), &a);
89 EXPECT_TRUE(s.is_empty());
90}
91
92TEST(InlineStack, popn_returns_the_last_popped_item)
93{
95 for (int i = 1; i <= 5; ++i)
96 s.push(i);
97 EXPECT_EQ(s.popn(3), 3); // 5, 4 and 3 are popped
98 EXPECT_EQ(s.size(), 2u);
99 EXPECT_EQ(s.top(), 2);
100 EXPECT_EQ(s.popn(2), 1);
101 EXPECT_TRUE(s.is_empty());
102}
103
104TEST(InlineStack, fills_up_to_its_capacity)
105{
107 s.push(1);
108 s.push(2);
109 s.push(3);
110 EXPECT_EQ(s.size(), s.capacity());
111 EXPECT_EQ(s.top(), 3);
112 s.empty();
113 EXPECT_TRUE(s.is_empty());
114 s.push(4);
115 EXPECT_EQ(s.top(), 4);
116 s.clear();
117 EXPECT_TRUE(s.is_empty());
118}
119
120TEST(InlineStack, copies_only_the_items_in_use)
121{
123 a.push(1);
124 a.push(2);
125
127 EXPECT_EQ(b.size(), 2u);
128 EXPECT_EQ(b.pop(), 2);
129 EXPECT_EQ(a.size(), 2u); // b is independent of a
130 EXPECT_EQ(a.top(), 2);
131
133 c.push(9);
134 c = a;
135 EXPECT_EQ(c.size(), 2u);
136 EXPECT_EQ(c.top(1), 1);
137 c = c; // self-assignment is harmless
138 EXPECT_EQ(c.size(), 2u);
139}
140
141TEST(InlineStack, swap_exchanges_the_items)
142{
144 a.push(1);
145 a.push(2);
146 a.push(3);
147 b.push(9);
148
149 a.swap(b);
150 EXPECT_EQ(a.size(), 1u);
151 EXPECT_EQ(a.top(), 9);
152 EXPECT_EQ(b.size(), 3u);
153 EXPECT_EQ(b.top(), 3);
154 EXPECT_EQ(b.base(), 1);
155
156 b.swap(b); // with itself: unchanged
157 EXPECT_EQ(b.size(), 3u);
158 EXPECT_EQ(b.top(), 3);
159
161 b.swap(empty);
162 EXPECT_TRUE(b.is_empty());
163 EXPECT_EQ(empty.size(), 3u);
164}
Stack of fixed capacity whose storage is inside the object.
size_t size() const noexcept
Return the number of items.
static constexpr size_t capacity() noexcept
Maximum number of items.
T pop() noexcept
Pop the top; the stack must not be empty.
T & push(const T &data) noexcept
Push a copy of data.
T & top() noexcept
Return a modifiable reference to the top; the stack must not be empty.
void clear() noexcept
See empty().
T popn(const size_t n) noexcept
Perform n pops in constant time.
bool is_empty() const noexcept
Return true if the stack is empty.
T & base() noexcept
Return a modifiable reference to the bottom; the stack must not be empty.
void empty() noexcept
Empty the stack.
void swap(InlineStack &s) noexcept
Exchange the items in use of s and this, in time O(size()).
#define TEST(name)
Main namespace for Aleph-w library functions.
Stack implementations backed by dynamic or fixed arrays.