Aleph-w 7.10.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
al-domain.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
37# include <gtest/gtest.h>
38
39// al-domain.H first, on its own: keys() sorts with ahSort.H, which the
40// header must include
41# include <al-domain.H>
42
43# include <climits>
44# include <stdexcept>
45# include <vector>
46
47using namespace std;
48using namespace testing;
49using namespace Aleph;
50
51TEST(Domain, basic)
52{
53 AlDomain<int> domain;
54
55 EXPECT_TRUE(domain.is_empty());
56
57 for (size_t i = 0; i < 10; ++i)
58 domain.insert(i);
59
60 EXPECT_FALSE(domain.contains(-1));
61 EXPECT_FALSE(domain.contains(10));
62
63 EXPECT_TRUE(domain.all([] (auto i) { return i >= 0 and i < 10; }));
64}
65
66namespace
67{
68 // The keys of d, as keys() gives them
69 std::vector<int> keys_of(const AlDomain<int> & d)
70 {
71 std::vector<int> ret;
72 d.keys().for_each([&ret] (int k) { ret.push_back(k); });
73 return ret;
74 }
75}
76
77TEST(IntRange, AStepThatIsNotPositiveThrows)
78{
79 // A step of 0 looped forever
80 EXPECT_THROW(IntRange(0, 3, 0), std::domain_error);
81 EXPECT_THROW(IntRange(0, 3, -1), std::domain_error);
82 EXPECT_THROW(IntRange(5, 3, 0), std::domain_error);
83
84 EXPECT_EQ(IntRange(0, 10, 2).size(), 6u);
85 EXPECT_EQ(IntRange(5, 3).size(), 0u);
86}
87
88TEST(IntRange, ARangeUpToTheMaximumOfIntEnds)
89{
90 // Where long has 32 bits, as on Windows, the index of the loop
91 // overflowed after INT_MAX
92 const IntRange r(INT_MAX - 2, INT_MAX);
93 EXPECT_EQ(keys_of(r), (std::vector<int>{INT_MAX - 2, INT_MAX - 1, INT_MAX}));
94
95 const IntRange s(INT_MAX - 3, INT_MAX, 2);
96 EXPECT_EQ(keys_of(s), (std::vector<int>{INT_MAX - 3, INT_MAX - 1}));
97
98 const IntRange t(INT_MIN, INT_MIN + 1);
99 EXPECT_EQ(keys_of(t), (std::vector<int>{INT_MIN, INT_MIN + 1}));
100}
101
102TEST(IntRange, ASizeAboveTheRangeOfIntThrows)
103{
104 // The elements are ints: a negative int converted to size_t, as
105 // IntRange(-1), looped until memory ran out
106 EXPECT_THROW(IntRange(static_cast<size_t>(INT_MAX) + 2), std::domain_error);
107 EXPECT_THROW(IntRange(static_cast<size_t>(-1)), std::domain_error);
108
109 EXPECT_EQ(keys_of(IntRange(size_t(3))), (std::vector<int>{0, 1, 2}));
110}
111
112TEST(AlDomain, KeysFollowTheInsertionsAndTheRemovals)
113{
114 // keys() kept the sorted keys of its first call
116 (void) d.insert(3);
117 (void) d.insert(1);
118 EXPECT_EQ(keys_of(d), (std::vector<int>{1, 3}));
119
120 (void) d.insert(2);
121 EXPECT_EQ(keys_of(d), (std::vector<int>{1, 2, 3}));
122
123 d.remove(1);
124 EXPECT_EQ(keys_of(d), (std::vector<int>{2, 3}));
125
126 (void) d.search_or_insert(0);
127 EXPECT_EQ(keys_of(d), (std::vector<int>{0, 2, 3}));
128
129 (void) d.contains_or_insert(5);
130 EXPECT_EQ(keys_of(d), (std::vector<int>{0, 2, 3, 5}));
131
132 (void) d.append(4);
133 EXPECT_EQ(keys_of(d), (std::vector<int>{0, 2, 3, 4, 5}));
134
135 d.remove_ptr(d.search(3));
136 EXPECT_EQ(keys_of(d), (std::vector<int>{0, 2, 4, 5}));
137
138 d.add(DynList<int>({7, 6}));
139 EXPECT_EQ(keys_of(d), (std::vector<int>{0, 2, 4, 5, 6, 7}));
140
142 (void) e.insert(9);
143 EXPECT_EQ(keys_of(e), (std::vector<int>{9}));
144 d.swap(e);
145 EXPECT_EQ(keys_of(d), (std::vector<int>{9}));
146 EXPECT_EQ(keys_of(e), (std::vector<int>{0, 2, 4, 5, 6, 7}));
147
148 e.empty();
149 EXPECT_TRUE(keys_of(e).empty());
150 (void) e.insert(8);
151 e.clear();
152 EXPECT_TRUE(keys_of(e).empty());
153}
154
155TEST(AlDomain, KeysFollowARemovalThroughAnIterator)
156{
157 // A removal that AlDomain does not see changes the number of keys, which
158 // keys() compares
160 for (int i = 0; i < 5; ++i)
161 (void) d.insert(i);
162 EXPECT_EQ(keys_of(d).size(), 5u);
163
164 auto it = d.get_it();
165 const int removed = it.get_curr();
166 it.del();
167
168 std::vector<int> expected;
169 for (int i = 0; i < 5; ++i)
170 if (i != removed)
171 expected.push_back(i);
172 EXPECT_EQ(keys_of(d), expected);
173}
174
175TEST(AlDomain, ACopyHasTheSameKeys)
176{
178 (void) d.insert(2);
179 (void) d.insert(1);
180 EXPECT_EQ(keys_of(d), (std::vector<int>{1, 2}));
181
182 AlDomain<int> c = d;
183 (void) c.insert(0);
184 EXPECT_EQ(keys_of(c), (std::vector<int>{0, 1, 2}));
185 EXPECT_EQ(keys_of(d), (std::vector<int>{1, 2}));
186}
Integer domain classes for sparse data structures.
Generic domain class based on hash set.
Definition al-domain.H:132
T * insert(const T &key)
Insert key, as HashSet::insert(), and forget the sorted keys.
Definition al-domain.H:162
void swap(AlDomain &other) noexcept
Swap the keys, and the sorted keys, with other.
Definition al-domain.H:252
T * search_or_insert(const T &key)
Search or insert key, as HashSet::search_or_insert(), and forget the sorted keys.
Definition al-domain.H:177
void add(const DynList< T > &l)
Insert the keys of l, as HashSet::add(), and forget the sorted keys.
Definition al-domain.H:220
T * append(const T &key)
Insert key, as HashSet::append(), and forget the sorted keys.
Definition al-domain.H:206
void clear()
Remove every key, as HashSet::clear(), and forget the sorted keys.
Definition al-domain.H:249
const DynList< T > & keys() const
Get sorted list of all elements in the domain.
Definition al-domain.H:151
void remove(const T &key)
Remove key, as HashSet::remove(), and forget the sorted keys.
Definition al-domain.H:227
void remove_ptr(T *key)
Remove the key that key points to, as HashSet::remove_ptr(), and forget the sorted keys.
Definition al-domain.H:235
std::pair< T *, bool > contains_or_insert(const T &key)
Search or insert key, as HashSet::contains_or_insert(), and forget the sorted keys.
Definition al-domain.H:192
void empty()
Remove every key, as HashSet::empty(), and forget the sorted keys.
Definition al-domain.H:242
Doubly-linked list (defined in tpl_dynList.H).
Definition htlist.H:1231
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
#define TEST(name)
Main namespace for Aleph-w library functions.
size_t size(Node *root) noexcept
See compute_cardinality_rec(Node * root).
@ Domain
Preserve the order emitted by for_each_successor().
STL namespace.
Integer range domain [start, end] with optional step.
Definition al-domain.H:311
static int * k
Gsl_Compatible_Rng r