Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
ah_concepts_test.cc
Go to the documentation of this file.
1/*
2 Aleph_w
3
4 Data structures & Algorithms
5 version 2.0.0b
6 https://github.com/lrleon/Aleph-w
7
8 This file is part of Aleph-w library
9
10 Copyright (c) 2002-2026 Leandro Rabindranath Leon
11
12 Permission is hereby granted, free of charge, to any person obtaining a copy
13 of this software and associated documentation files (the "Software"), to deal
14 in the Software without restriction, including without limitation the rights
15 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
16 copies of the Software, and to permit persons to whom the Software is
17 furnished to do so, subject to the following conditions:
18
19 The above copyright notice and this permission notice shall be included in all
20 copies or substantial portions of the Software.
21
22 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
25 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28 SOFTWARE.
29*/
30
31
38# include <gtest/gtest.h>
39
40# include <ah-concepts.H>
41# include <ahFunction.H>
42# include <ahFunctional.H>
43# include <tpl_avl.H>
44# include <tpl_splay_tree.H>
45# include <tpl_odhash.H>
46# include <htlist.H>
47# include <tpl_dynDlist.H>
48# include <tpl_array.H>
49# include <tpl_dynArray.H>
50# include <functional>
51
52using namespace Aleph;
53
54// ====================================================================
55// static_assert positive: functors that MUST satisfy the concepts
56// ====================================================================
57
58// Aleph functors
62
65
67
68// std functors
69static_assert(Aleph::StrictWeakOrder<std::less<int>, int>);
72
73// Lambda (via decltype)
74constexpr auto my_less = [](const int & a, const int & b) { return a < b; };
75static_assert(Aleph::StrictWeakOrder<decltype(my_less), int>);
76
77// Function pointer
78using BoolBinFn = bool (*)(const int &, const int &);
80
81// ====================================================================
82// static_assert negative: types that must NOT satisfy
83// ====================================================================
84
85struct VoidBinary {
86 void operator()(int, int) const {}
87};
89
90struct Unary {
91 bool operator()(int) const { return true; }
92};
94
95struct NotCallable {};
97
98// ====================================================================
99// BSTPolicy: positive and negative
100// ====================================================================
101
102static_assert(Aleph::BSTPolicy<Avl_Tree<int>, int>);
103static_assert(Aleph::BSTPolicy<Splay_Tree<int>, int>);
105
106// ====================================================================
107// Compilation tests: constrained classes instantiate correctly
108// ====================================================================
109
111{
112 Avl_Tree<int> tree;
113 auto * node = new Avl_Tree<int>::Node(42);
114 tree.insert(node);
115 EXPECT_NE(tree.search(42), nullptr);
116 delete tree.remove(42);
117}
118
120{
121 ODhashTable<int> table;
122 EXPECT_EQ(table.search(42), nullptr); // empty table search
123 table.insert(42);
124 EXPECT_NE(table.search(42), nullptr);
125 EXPECT_NO_THROW(table.remove(42));
126 EXPECT_EQ(table.search(42), nullptr);
127}
128
129// ====================================================================
130// AlephSequentialContainer: static_assert positive cases
131// ====================================================================
132
137
140
141// Negative: plain types and non-Aleph types are not sequential containers
145
146// ====================================================================
147// fill(): set all existing elements to a constant
148// ====================================================================
149
151{
152 DynList<int> lst = {1, 2, 3, 4, 5};
153 fill(lst, 0);
154
155 lst.for_each([](int v) { EXPECT_EQ(v, 0); });
156 EXPECT_EQ(lst.size(), 5u);
157}
158
160{
161 Array<int> arr;
162 for (int i = 0; i < 5; ++i) arr.append(i);
163 fill(arr, 7);
164
165 for (size_t i = 0; i < arr.size(); ++i)
166 EXPECT_EQ(arr[i], 7);
167 EXPECT_EQ(arr.size(), 5u);
168}
169
171{
173 for (int i = 0; i < 4; ++i) lst.append(static_cast<double>(i));
174 fill(lst, 3.14);
175
176 lst.for_each([](double v) { EXPECT_DOUBLE_EQ(v, 3.14); });
177}
178
180{
181 DynArray<int> arr;
182 for (int i = 0; i < 6; ++i) arr.append(i);
183 fill(arr, -1);
184
185 arr.for_each([](int v) { EXPECT_EQ(v, -1); });
186 EXPECT_EQ(arr.size(), 6u);
187}
188
195
197{
198 auto lst = rep(8, 0);
199 fill(lst, 99);
200 EXPECT_EQ(lst.size(), 8u);
201}
202
203// ====================================================================
204// iota(): fill with unit-step sequential values
205// ====================================================================
206
208{
209 auto lst = rep(5, 0);
210 iota(lst, 1);
211
212 int expected = 1;
213 lst.for_each([&expected](int v) { EXPECT_EQ(v, expected++); });
214}
215
217{
218 Array<int> arr;
219 for (int i = 0; i < 5; ++i) arr.append(0);
220 iota(arr, 10);
221
222 for (size_t i = 0; i < arr.size(); ++i)
223 EXPECT_EQ(arr[i], static_cast<int>(10 + i));
224}
225
227{
228 DynArray<int> arr;
229 for (int i = 0; i < 4; ++i) arr.append(0);
230 iota(arr, 0);
231
232 int expected = 0;
233 arr.for_each([&expected](int v) { EXPECT_EQ(v, expected++); });
234}
235
237{
239 for (int i = 0; i < 3; ++i) lst.append(0);
240 iota(lst, 5);
241
242 int expected = 5;
243 lst.for_each([&expected](int v) { EXPECT_EQ(v, expected++); });
244}
245
252
253// ====================================================================
254// iota(container, start, step): custom step
255// ====================================================================
256
258{
259 auto lst = rep(5, 0);
260 iota(lst, 0, 2); // {0, 2, 4, 6, 8}
261
262 int expected = 0;
263 lst.for_each([&expected](int v) {
265 expected += 2;
266 });
267}
268
270{
271 Array<double> arr;
272 for (int i = 0; i < 4; ++i) arr.append(0.0);
273 iota(arr, 1.0, 0.5); // {1.0, 1.5, 2.0, 2.5}
274
275 double expected = 1.0;
276 for (size_t i = 0; i < arr.size(); ++i)
277 {
278 EXPECT_DOUBLE_EQ(arr[i], expected);
279 expected += 0.5;
280 }
281}
282
284{
285 auto lst = rep(4, 0);
286 iota(lst, 10, -3); // {10, 7, 4, 1}
287
288 int expected = 10;
289 lst.for_each([&expected](int v) {
291 expected -= 3;
292 });
293}
294
296{
297 auto lst = rep(1, 0);
298 iota(lst, 42, 100);
299 EXPECT_EQ(lst.get_first(), 42);
300}
301
302// ====================================================================
303// Interaction: fill then iota
304// ====================================================================
305
307{
308 auto lst = rep(5, 99);
309 fill(lst, 0);
310 iota(lst, 1);
311
312 int expected = 1;
313 lst.for_each([&expected](int v) { EXPECT_EQ(v, expected++); });
314}
C++20 concepts for constraining comparison functors.
Standard functor implementations and comparison objects.
Functional programming utilities for Aleph-w containers.
constexpr auto my_less
bool(*)(const int &, const int &) BoolBinFn
Simple dynamic array with automatic resizing and functional operations.
Definition tpl_array.H:139
constexpr size_t size() const noexcept
Return the number of elements stored in the stack.
Definition tpl_array.H:351
T & append(const T &data)
Append a copy of data
Definition tpl_array.H:245
size_t size() const noexcept
Return the current dimension of array.
T & append()
Allocate a new entry to the end of array.
Dynamic doubly linked list with O(1) size and bidirectional access.
T & append(const T &item)
Append a copied item at the end of the list.
Dynamic singly linked list with functional programming support.
Definition htlist.H:1155
Node * search(const Key &key) const noexcept
Search a node containing key; if found, then a pointer to the node containing it is returned; otherwi...
Definition tpl_avl.H:533
Node * insert(Node *p) noexcept
Insert the node pointed by p in the tree.
Definition tpl_avl.H:561
Node * remove(const Key &key) noexcept
Remove from an AVL tree the node containing key key.
Definition tpl_avl.H:640
Open addressing hash table with double hashing collision resolution.
Definition tpl_odhash.H:182
Key * search(const Key &key) const noexcept
searches the table for the key.
Definition tpl_odhash.H:543
void remove(const Key &key)
Remove a key from the hash table.
Definition tpl_odhash.H:903
void for_each(Operation &operation)
Traverse all the container and performs an operation on each element.
Definition ah-dry.H:779
Key * insert(const Key &key)
Inserts a key into the hash table (copy version).
Definition hashDry.H:203
Concept for Aleph-w mutable sequential containers.
Concept for BST tree policies used by DynSetTree.
A callable that takes two const T& and returns bool.
Definition ah-concepts.H:67
Equivalence relation constraint for equality comparators.
Definition ah-concepts.H:97
Strict weak ordering constraint for BST comparators.
Definition ah-concepts.H:84
#define TEST(name)
Singly linked list implementations with head-tail access.
Main namespace for Aleph-w library functions.
Definition ah-arena.H:89
void fill(Itor beg, const Itor &end, const T &value)
Fill a range with a value.
Definition ahAlgo.H:707
Divide_Conquer_DP_Result< Cost > divide_and_conquer_partition_dp(const size_t groups, const size_t n, Transition_Cost_Fn transition_cost, const Cost inf=dp_optimization_detail::default_inf< Cost >())
Optimize partition DP using divide-and-conquer optimization.
void iota(C &container, typename C::Item_Type start)
Fill all elements of a container with unit-step sequential values.
DynList< T > rep(const size_t n, const T &item)
Create a sequence of repeated items.
AVL binary search tree with nodes without virtual destructor.
Definition tpl_avl.H:737
bool operator()(int) const
void operator()(int, int) const
Dynamic array container with automatic resizing.
AVL tree implementation (height-balanced BST).
Lazy and scalable dynamic array implementation.
Dynamic doubly linked list implementation.
Open addressing hash table with double hashing.
Top-down splay tree implementation (without rank support).