Aleph-w 7.10.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
special_ctors_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
32
42#include <gtest/gtest.h>
43
44#include <cstddef>
45#include <iterator>
46#include <type_traits>
47#include <vector>
48
49#include <ahDry.H>
50#include <htlist.H>
51#include <polygon.H>
52#include <tpl_array.H>
53#include <tpl_arrayHeap.H>
54#include <tpl_arrayQueue.H>
55#include <tpl_arrayStack.H>
56#include <tpl_dynArray.H>
57#include <tpl_dynArrayHeap.H>
58#include <tpl_dynBinHeap.H>
59#include <tpl_dynDlist.H>
60#include <tpl_dynListQueue.H>
61#include <tpl_dynListStack.H>
62#include <tpl_dynSetHash.H>
63#include <tpl_dynSetTree.H>
64#include <tpl_dynSkipList.H>
65#include <tpl_hash.H>
66#include <tpl_interval_tree.H>
67#include <tpl_odhash.H>
68#include <tpl_olhash.H>
69
70using namespace Aleph;
71
72namespace
73{
75 template <typename T>
76 class Count_Value_Probe
77 {
78 Array<T> items;
79
80 public:
81 Count_Value_Probe() = default;
82
83 Count_Value_Probe(const size_t n, const T & value) : items(n, value) {}
84
85 T & append(const T & item) { return items.append(item); }
86
87 template <class Operation>
88 void for_each(Operation op) const { items.for_each(op); }
89
90 [[nodiscard]] const Array<T> & get_items() const noexcept { return items; }
91
92 Special_Ctors(Count_Value_Probe, T);
93 };
94
96 class Bare_Iterator
97 {
98 const int * pos;
99
100 public:
101 explicit Bare_Iterator(const int * p) noexcept : pos(p) {}
102
103 const int & operator * () const noexcept { return *pos; }
104
105 Bare_Iterator & operator ++ () noexcept
106 {
107 ++pos;
108 return *this;
109 }
110
111 bool operator != (const Bare_Iterator & other) const noexcept
112 {
113 return pos != other.pos;
114 }
115 };
116
117 static_assert(not std::input_iterator<Bare_Iterator>,
118 "the probe must be weaker than std::input_iterator");
119
121 template <class C, typename I = int>
122 constexpr bool accepts_integer_pair =
123 std::is_constructible_v<C, I, I> or std::is_constructible_v<C, I &, I &>;
124} // namespace
125
126TEST(SpecialCtors, IntegerPairsNeverSelectTheIteratorConstructor)
127{
128 // No other constructor takes two integers, so the pair is rejected with a
129 // "no matching constructor" error instead of failing inside the macro.
130 static_assert(not accepts_integer_pair<ArrayQueue<int>>);
131 static_assert(not accepts_integer_pair<FixedQueue<int>>);
132 static_assert(not accepts_integer_pair<ArrayStack<int>>);
133 static_assert(not accepts_integer_pair<FixedStack<int>>);
134 static_assert(not accepts_integer_pair<ArrayHeap<int>>);
135 static_assert(not accepts_integer_pair<DynDlist<int>>);
136 // DynList inherits the constructors of SpecialCtors (ah-dry.H), whose
137 // iterator constructor had no constraint: DynList<int>(3, 7) chose it.
138 static_assert(not accepts_integer_pair<DynList<int>>);
139 static_assert(not accepts_integer_pair<DynListQueue<int>>);
140 static_assert(not accepts_integer_pair<DynListStack<int>>);
141 static_assert(not accepts_integer_pair<DynSetTree<int>>);
142 static_assert(not accepts_integer_pair<DynSetHash<int>>);
143 static_assert(not accepts_integer_pair<HashSet<int>>);
144 static_assert(not accepts_integer_pair<ODhashTable<int>>);
145 static_assert(not accepts_integer_pair<OLhashTable<int>>);
146 static_assert(not accepts_integer_pair<DynIntervalTree<int>>);
147 static_assert(not accepts_integer_pair<Polygon>);
148
149 static_assert(not accepts_integer_pair<ArrayQueue<size_t>, size_t>);
150 static_assert(not accepts_integer_pair<DynDlist<size_t>, size_t>);
151 static_assert(not accepts_integer_pair<DynList<size_t>, size_t>);
152 static_assert(not accepts_integer_pair<DynSetTree<size_t>, size_t>);
153 static_assert(not accepts_integer_pair<OLhashTable<size_t>, size_t>);
154
155 // DynArray has a count/value constructor: the pair reaches it and builds
156 // n copies of the value, as std::vector does.
157 static_assert(accepts_integer_pair<DynArray<int>>);
158 static_assert(accepts_integer_pair<DynArray<size_t>, size_t>);
159 const DynArray<int> repeated(3, 7);
160 ASSERT_EQ(repeated.size(), 3u);
161 EXPECT_EQ(repeated.access(0), 7);
162 EXPECT_EQ(repeated.access(2), 7);
163
164 // DynSkipList has a (seed, probability) constructor: the pair selects it,
165 // not a range of items, and 7 is not a probability in (0, 1).
166 EXPECT_THROW(DynSkipList<int>(3, 7), std::domain_error);
167 DynSkipList<int> skip(3, 0.25);
168 EXPECT_TRUE(skip.is_empty());
169
170 // The heaps keep their variadic item constructor, which binds lvalues only:
171 // two int variables are two items, while two literals are rejected.
172 static_assert(not std::is_constructible_v<DynBinHeap<int>, int, int>);
173 static_assert(not std::is_constructible_v<DynArrayHeap<int>, int, int>);
174 int a = 3, b = 7;
175 DynBinHeap<int> bin_heap(a, b);
176 DynArrayHeap<int> array_heap(a, b);
177 EXPECT_EQ(bin_heap.size(), 2u);
178 EXPECT_EQ(array_heap.size(), 2u);
179}
180
181TEST(SpecialCtors, CountValueConstructorWinsOverTheIteratorTemplate)
182{
183 const Count_Value_Probe<int> literals(3, 7);
184 EXPECT_EQ(literals.get_items(), (Array<int>{7, 7, 7}));
185
186 int n = 2, value = 9;
187 const Count_Value_Probe<int> lvalues(n, value);
188 EXPECT_EQ(lvalues.get_items(), (Array<int>{9, 9}));
189
190 const size_t m = 2, v = 5;
191 const Count_Value_Probe<size_t> same_type(m, v);
192 EXPECT_EQ(same_type.get_items(), (Array<size_t>{5, 5}));
193}
194
195TEST(SpecialCtors, AcceptsIteratorsWithoutIteratorTraits)
196{
197 const int raw[] = {4, 5, 6};
198
199 const Count_Value_Probe<int> probe(Bare_Iterator(raw), Bare_Iterator(raw + 3));
200 EXPECT_EQ(probe.get_items(), (Array<int>{4, 5, 6}));
201
202 DynDlist<int> list(Bare_Iterator(raw), Bare_Iterator(raw + 3));
203 ASSERT_EQ(list.size(), 3u);
204 EXPECT_EQ(list.get_first(), 4);
205 EXPECT_EQ(list.get_last(), 6);
206}
207
208TEST(SpecialCtors, BuildsFromVectorPointerAndAlephRanges)
209{
210 const std::vector<int> vec = {3, 1, 2};
211 const int raw[] = {3, 1, 2};
212 const DynList<int> aleph = {3, 1, 2};
213
214 const DynDlist<int> from_vector(vec.begin(), vec.end());
215 const DynDlist<int> from_pointers(raw, raw + 3);
216 const DynDlist<int> from_aleph(aleph.begin(), aleph.end());
217 for (const DynDlist<int> * list : {&from_vector, &from_pointers, &from_aleph})
218 {
219 ASSERT_EQ(list->size(), 3u);
220 EXPECT_EQ(list->get_first(), 3);
221 EXPECT_EQ(list->get_last(), 2);
222 }
223
224 const DynArray<int> array(raw, raw + 3);
225 ASSERT_EQ(array.size(), 3u);
226 EXPECT_EQ(array(0), 3);
227 EXPECT_EQ(array(2), 2);
228
229 ArrayQueue<int> queue(aleph.begin(), aleph.end());
230 ASSERT_EQ(queue.size(), 3u);
231 EXPECT_EQ(queue.front(), 3);
232 EXPECT_EQ(queue.rear(), 2);
233
234 const DynSetTree<int> set(vec.begin(), vec.end());
235 ASSERT_EQ(set.size(), 3u);
236 EXPECT_EQ(set.min(), 1);
237 EXPECT_EQ(set.max(), 3);
238}
DRY (Don't Repeat Yourself) utilities and macros.
#define Special_Ctors(Set_Type, Type)
Generates special constructors for containers.
Definition ahDry.H:123
size_t size_t int32_t value
Definition ca-c-api.h:116
Queue implemented with a single dynamic array.
T & front(const size_t i=0)
Return the i-th oldest item of the queue.
T & rear(const size_t i=0)
Return the i-th youngest item of the queue.
Simple dynamic array with automatic resizing and functional operations.
Definition tpl_array.H:141
T & append(const T &data)
Append a copy of data
Definition tpl_array.H:331
Dynamic heap (priority queue) backed by DynArray.
constexpr size_t size() const noexcept
Return the number of elements.
size_t size() const noexcept
Return the current dimension of array.
Dynamic heap of elements of type T ordered by a comparison functor.
Dynamic doubly linked list with O(1) size and bidirectional access.
const size_t & size() const noexcept
Return the number of elements (constant time)
T & get_first()
Return a modifiable reference to first item in the list.
T & get_last()
Return a modifiable reference to last item in 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
Dynamic set backed by balanced binary search trees with automatic memory management.
const Key & min() const
Returns the smallest key contained in the set according to the criterion comparison given.
const size_t & size() const
Returns the cardinality of the set.
const Key & max() const
Returns the largest key contained in the set according to the criteria comparison given.
Dynamic ordered set implemented with a Skip List.
bool is_empty() const noexcept
Return true if the set is empty.
const size_t & size() const noexcept
size_t size() const noexcept
Return the number of elements.
void for_each(Operation &operation) const
Traverse all the container and performs an operation on each element.
Definition ah-dry.H:1475
iterator end() noexcept
Return an STL-compatible end iterator.
iterator begin() noexcept
Return an STL-compatible iterator to the first element.
#define TEST(name)
bool operator!=(const __gmp_expr< T, U > &expr1, const __gmp_expr< V, W > &expr2)
Definition gmpfrxx.h:4120
Singly linked list implementations with head-tail access.
Main namespace for Aleph-w library functions.
DynList< T > repeated(const Container< T > &c)
Return elements that appear more than once in the container.
std::decay_t< typename HeadC::Item_Type > T
Definition ah-zip.H:152
Matrix< Trow, Tcol, NumType > operator*(const NumType &scalar, const Matrix< Trow, Tcol, NumType > &m)
Scalar-matrix multiplication (scalar * matrix).
Definition al-matrix.H:1228
Operation for_each(Itor beg, const Itor &end, Operation op)
Apply an operation to each element in a range.
Definition ahAlgo.H:76
2D polygon representation and geometric operations.
Special constructors common to Aleph-w ( ) containers.
Definition ah-dry.H:1100
FooMap m(5, fst_unit_pair_hash, snd_unit_pair_hash)
Fixed-capacity binary heap and heapsort algorithms.
Circular queue implementations backed by arrays.
Stack implementations backed by dynamic or fixed arrays.
Dynamic array container with automatic resizing.
Array-based dynamic binary heap.
Lazy and scalable dynamic array implementation.
Dynamic binary heap with node-based storage.
Dynamic doubly linked list implementation.
Dynamic queue implementation based on linked lists.
Dynamic stack implementation based on linked lists.
Dynamic set implementations based on hash tables.
Dynamic set implementations based on balanced binary search trees.
Dynamic ordered set implemented with a Skip List.
Unified hash table interface.
Interval tree: augmented BST for overlap/stabbing queries.
Open addressing hash table with double hashing.
Open addressing hash table with linear probing.