Aleph-w 7.10.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 <string>
42# include <utility>
43# include <ahFunction.H>
44# include <ahFunctional.H>
45# include <tpl_avl.H>
46# include <tpl_splay_tree.H>
47# include <tpl_odhash.H>
48# include <htlist.H>
49# include <tpl_dynDlist.H>
50# include <tpl_array.H>
51# include <tpl_dynArray.H>
52# include <functional>
53
54using namespace Aleph;
55
56// ====================================================================
57// static_assert positive: functors that MUST satisfy the concepts
58// ====================================================================
59
60// Aleph functors
64
67
69
70// std functors
71static_assert(Aleph::StrictWeakOrder<std::less<int>, int>);
74
75// Lambda (via decltype)
76constexpr auto my_less = [](const int & a, const int & b) { return a < b; };
77static_assert(Aleph::StrictWeakOrder<decltype(my_less), int>);
78
79// Function pointer
80using BoolBinFn = bool (*)(const int &, const int &);
82
83// ====================================================================
84// static_assert negative: types that must NOT satisfy
85// ====================================================================
86
87struct VoidBinary {
88 void operator()(int, int) const {}
89};
91
92struct Unary {
93 bool operator()(int) const { return true; }
94};
96
97struct NotCallable {};
99
100// ====================================================================
101// BSTPolicy: positive and negative
102// ====================================================================
103
104// Its operators are not noexcept, and only unevaluated operands use them:
105// a named namespace, as GCC 11 and Clang 14 warn of an internal function
106// declared and not defined
129
131
132namespace
133{
134 // Bare has no operators
135 struct Bare
136 {
137 };
138
139 // Whether the binary functor F, on T, is noexcept
140 template <template <class> class F, class T>
141 constexpr bool nothrow2 = noexcept(F<T>()(std::declval<const T &>(),
142 std::declval<const T &>()));
143
144 template <template <class> class F, class T>
145 constexpr bool nothrow1 = noexcept(F<T>()(std::declval<const T &>()));
146}
147
148// The functors were noexcept whatever the operators of their type: what
149// such an operator threw, as the + of std::string, ended the program
150static_assert(nothrow2<Aleph::plus, int> and nothrow2<Aleph::minus, int>
151 and nothrow2<Aleph::multiplies, int> and nothrow2<Aleph::divides, int>
152 and nothrow2<Aleph::modulus, int> and nothrow1<Aleph::negate, int>
153 and nothrow2<Aleph::equal_to, int> and nothrow2<Aleph::not_equal_to, int>
154 and nothrow2<Aleph::greater, int> and nothrow2<Aleph::less, int>
155 and nothrow2<Aleph::greater_equal, int> and nothrow2<Aleph::less_equal, int>
156 and nothrow2<Aleph::logical_and, int> and nothrow2<Aleph::logical_or, int>
157 and nothrow1<Aleph::logical_not, int>);
158static_assert(not nothrow2<Aleph::plus, std::string>);
159static_assert(not nothrow2<Aleph::plus, Throwing_Ops> and not nothrow2<Aleph::minus, Throwing_Ops>
160 and not nothrow2<Aleph::multiplies, Throwing_Ops>
161 and not nothrow2<Aleph::divides, Throwing_Ops>
162 and not nothrow2<Aleph::modulus, Throwing_Ops>
163 and not nothrow1<Aleph::negate, Throwing_Ops>
164 and not nothrow2<Aleph::equal_to, Throwing_Ops>
165 and not nothrow2<Aleph::not_equal_to, Throwing_Ops>
166 and not nothrow2<Aleph::greater, Throwing_Ops>
167 and not nothrow2<Aleph::less, Throwing_Ops>
168 and not nothrow2<Aleph::greater_equal, Throwing_Ops>
169 and not nothrow2<Aleph::less_equal, Throwing_Ops>
170 and not nothrow2<Aleph::logical_and, Throwing_Ops>
171 and not nothrow2<Aleph::logical_or, Throwing_Ops>
172 and not nothrow1<Aleph::logical_not, Throwing_Ops>);
173// A type without the operator gives false, not an error, and the functor
174// keeps its operator(), as before
175static_assert(not nothrow2<Aleph::greater, Bare> and not nothrow2<Aleph::plus, Bare>);
177
178static_assert(Aleph::BSTPolicy<Avl_Tree<int>, int>);
179static_assert(Aleph::BSTPolicy<Splay_Tree<int>, int>);
181
182// ====================================================================
183// Compilation tests: constrained classes instantiate correctly
184// ====================================================================
185
186TEST(AhConceptsTest, AvlTreeInstantiates)
187{
188 Avl_Tree<int> tree;
189 auto * node = new Avl_Tree<int>::Node(42);
190 tree.insert(node);
191 EXPECT_NE(tree.search(42), nullptr);
192 delete tree.remove(42);
193}
194
195TEST(AhConceptsTest, ODhashTableInstantiates)
196{
197 ODhashTable<int> table;
198 EXPECT_EQ(table.search(42), nullptr); // empty table search
199 table.insert(42);
200 EXPECT_NE(table.search(42), nullptr);
201 EXPECT_NO_THROW(table.remove(42));
202 EXPECT_EQ(table.search(42), nullptr);
203}
204
205// ====================================================================
206// AlephSequentialContainer: static_assert positive cases
207// ====================================================================
208
213
216
217// Negative: plain types and non-Aleph types are not sequential containers
218static_assert(not Aleph::AlephSequentialContainer<int>);
221
222// ====================================================================
223// fill(): set all existing elements to a constant
224// ====================================================================
225
226TEST(AlephFillTest, FillDynList)
227{
228 DynList<int> lst = {1, 2, 3, 4, 5};
229 fill(lst, 0);
230
231 lst.for_each([](int v) { EXPECT_EQ(v, 0); });
232 EXPECT_EQ(lst.size(), 5u);
233}
234
235TEST(AlephFillTest, FillArray)
236{
237 Array<int> arr;
238 for (int i = 0; i < 5; ++i) arr.append(i);
239 fill(arr, 7);
240
241 for (size_t i = 0; i < arr.size(); ++i)
242 EXPECT_EQ(arr[i], 7);
243 EXPECT_EQ(arr.size(), 5u);
244}
245
246TEST(AlephFillTest, FillDynDlist)
247{
249 for (int i = 0; i < 4; ++i) lst.append(static_cast<double>(i));
250 fill(lst, 3.14);
251
252 lst.for_each([](double v) { EXPECT_DOUBLE_EQ(v, 3.14); });
253}
254
255TEST(AlephFillTest, FillDynArray)
256{
257 DynArray<int> arr;
258 for (int i = 0; i < 6; ++i) arr.append(i);
259 fill(arr, -1);
260
261 arr.for_each([](int v) { EXPECT_EQ(v, -1); });
262 EXPECT_EQ(arr.size(), 6u);
263}
264
265TEST(AlephFillTest, FillEmptyContainerIsNoOp)
266{
267 DynList<int> lst;
268 EXPECT_NO_THROW(fill(lst, 42));
269 EXPECT_TRUE(lst.is_empty());
270}
271
272TEST(AlephFillTest, FillDoesNotChangeSize)
273{
274 auto lst = rep(8, 0);
275 fill(lst, 99);
276 EXPECT_EQ(lst.size(), 8u);
277}
278
279// ====================================================================
280// iota(): fill with unit-step sequential values
281// ====================================================================
282
283TEST(AlephIotaTest, IotaDynList)
284{
285 auto lst = rep(5, 0);
286 iota(lst, 1);
287
288 int expected = 1;
289 lst.for_each([&expected](int v) { EXPECT_EQ(v, expected++); });
290}
291
292TEST(AlephIotaTest, IotaArray)
293{
294 Array<int> arr;
295 for (int i = 0; i < 5; ++i) arr.append(0);
296 iota(arr, 10);
297
298 for (size_t i = 0; i < arr.size(); ++i)
299 EXPECT_EQ(arr[i], static_cast<int>(10 + i));
300}
301
302TEST(AlephIotaTest, IotaDynArray)
303{
304 DynArray<int> arr;
305 for (int i = 0; i < 4; ++i) arr.append(0);
306 iota(arr, 0);
307
308 int expected = 0;
309 arr.for_each([&expected](int v) { EXPECT_EQ(v, expected++); });
310}
311
312TEST(AlephIotaTest, IotaDynDlist)
313{
314 DynDlist<int> lst;
315 for (int i = 0; i < 3; ++i) lst.append(0);
316 iota(lst, 5);
317
318 int expected = 5;
319 lst.for_each([&expected](int v) { EXPECT_EQ(v, expected++); });
320}
321
322TEST(AlephIotaTest, IotaEmptyIsNoOp)
323{
324 DynList<int> lst;
325 EXPECT_NO_THROW(iota(lst, 0));
326 EXPECT_TRUE(lst.is_empty());
327}
328
329// ====================================================================
330// iota(container, start, step): custom step
331// ====================================================================
332
333TEST(AlephIotaStepTest, EvenNumbers)
334{
335 auto lst = rep(5, 0);
336 iota(lst, 0, 2); // {0, 2, 4, 6, 8}
337
338 int expected = 0;
339 lst.for_each([&expected](int v) {
340 EXPECT_EQ(v, expected);
341 expected += 2;
342 });
343}
344
345TEST(AlephIotaStepTest, DoubleStep)
346{
347 Array<double> arr;
348 for (int i = 0; i < 4; ++i) arr.append(0.0);
349 iota(arr, 1.0, 0.5); // {1.0, 1.5, 2.0, 2.5}
350
351 double expected = 1.0;
352 for (size_t i = 0; i < arr.size(); ++i)
353 {
354 EXPECT_DOUBLE_EQ(arr[i], expected);
355 expected += 0.5;
356 }
357}
358
359TEST(AlephIotaStepTest, NegativeStep)
360{
361 auto lst = rep(4, 0);
362 iota(lst, 10, -3); // {10, 7, 4, 1}
363
364 int expected = 10;
365 lst.for_each([&expected](int v) {
366 EXPECT_EQ(v, expected);
367 expected -= 3;
368 });
369}
370
371TEST(AlephIotaStepTest, SingleElement)
372{
373 auto lst = rep(1, 0);
374 iota(lst, 42, 100);
375 EXPECT_EQ(lst.get_first(), 42);
376}
377
378// ====================================================================
379// Interaction: fill then iota
380// ====================================================================
381
382TEST(AlephFillIotaTest, FillThenIota)
383{
384 auto lst = rep(5, 99);
385 fill(lst, 0);
386 iota(lst, 1);
387
388 int expected = 1;
389 lst.for_each([&expected](int v) { EXPECT_EQ(v, expected++); });
390}
C++20 concepts hub: comparison, BST policy, and Aleph container concepts.
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:141
constexpr size_t size() const noexcept
Return the number of elements stored in the stack.
Definition tpl_array.H:450
T & append(const T &data)
Append a copy of data
Definition tpl_array.H:331
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.
Doubly-linked list (defined in tpl_dynList.H).
Definition htlist.H:1231
size_t size() const noexcept
Return the number of items of the list, in constant time.
Definition htlist.H:1264
Node * insert(Node *p) noexcept(nothrow_compare_v< Compare, Key >)
Insert the node pointed by p in the tree.
Definition tpl_avl.H:640
Node * remove(const Key &key) noexcept(nothrow_compare_v< Compare, Key >)
Remove from an AVL tree the node containing key key.
Definition tpl_avl.H:738
Node * search(const Key &key) const noexcept(nothrow_compare_v< Compare, Key >)
Search a node containing key.
Definition tpl_avl.H:607
constexpr bool is_empty() const noexcept
Definition htlist.H:434
Open addressing hash table with double hashing collision resolution.
Definition tpl_odhash.H:193
void remove(const Key &key)
Remove a key from the hash table.
Key * search(const Key &key) noexcept
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition tpl_odhash.H:657
Minimal std::expected-style result type for C++20.
void for_each(Operation &operation) const
Traverse all the container and performs an operation on each element.
Definition ah-dry.H:1475
Key * insert(const Key &key)
Inserts a key into the hash table (copy version).
Definition hashDry.H:507
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.
Equivalence relation constraint for equality comparators.
Strict weak ordering constraint for BST comparators.
#define TEST(name)
Singly linked list implementations with head-tail access.
FFT< double > F
Main namespace for Aleph-w library functions.
void fill(Itor beg, const Itor &end, const T &value)
Fill a range with a value.
Definition ahAlgo.H:707
std::decay_t< typename HeadC::Item_Type > T
Definition ah-zip.H:152
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.
and
Check uniqueness with explicit hash + equality functors.
Throwing_Ops operator-(const Throwing_Ops &, const Throwing_Ops &)
bool operator<=(const Throwing_Ops &, const Throwing_Ops &)
bool operator&&(const Throwing_Ops &, const Throwing_Ops &)
bool operator!(const Throwing_Ops &)
Throwing_Ops operator%(const Throwing_Ops &, const Throwing_Ops &)
bool operator||(const Throwing_Ops &, const Throwing_Ops &)
bool operator<(const Throwing_Ops &, const Throwing_Ops &)
bool operator==(const Throwing_Ops &, const Throwing_Ops &)
Throwing_Ops operator*(const Throwing_Ops &, const Throwing_Ops &)
bool operator!=(const Throwing_Ops &, const Throwing_Ops &)
Throwing_Ops operator+(const Throwing_Ops &, const Throwing_Ops &)
bool operator>(const Throwing_Ops &, const Throwing_Ops &)
Throwing_Ops operator/(const Throwing_Ops &, const Throwing_Ops &)
bool operator>=(const Throwing_Ops &, const Throwing_Ops &)
AVL binary search tree with nodes without a virtual destructor.
Definition tpl_avl.H:831
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).