Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
tpl_dynarray_set_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
33
38#include <gtest/gtest.h>
39
40#include <stdexcept>
41#include <vector>
42
43#include <tpl_dynarray_set.H>
44
45using namespace Aleph;
46
47namespace
48{
49 struct AbsEqual
50 {
51 bool operator()(int a, int b) const noexcept
52 {
53 auto aa = a < 0 ? -a : a;
54 auto bb = b < 0 ? -b : b;
55 return aa == bb;
56 }
57 };
58
59 struct ModEqual
60 {
61 int mod = 1;
62
63 bool operator()(int a, int b) const noexcept
64 {
65 auto aa = a % mod;
66 auto bb = b % mod;
67 if (aa < 0)
68 aa += mod;
69 if (bb < 0)
70 bb += mod;
71 return aa == bb;
72 }
73 };
74}
75
77{
79 EXPECT_EQ(s.size(), 0u);
81 EXPECT_EQ(s.search(1), nullptr);
83 EXPECT_EQ(s.count(1), 0u);
84}
85
87{
89
90 auto *p1 = s.insert(10);
91 ASSERT_NE(p1, nullptr);
92 EXPECT_EQ(*p1, 10);
93 EXPECT_EQ(s.size(), 1u);
94
95 auto *p2 = s.insert(20);
96 ASSERT_NE(p2, nullptr);
97 EXPECT_EQ(*p2, 20);
98 EXPECT_EQ(s.size(), 2u);
99
100 auto *f10 = s.search(10);
101 ASSERT_NE(f10, nullptr);
102 EXPECT_EQ(*f10, 10);
103 EXPECT_TRUE(s.contains(20));
104 EXPECT_FALSE(s.contains(30));
105}
106
108{
110 s.insert(7);
111 s.insert(7);
112 s.insert(7);
113
114 EXPECT_EQ(s.size(), 3u);
115 EXPECT_EQ(s.count(7), 3u);
116 EXPECT_NE(s.search(7), nullptr);
117}
118
120{
122 s.insert(1);
123
124 EXPECT_NO_THROW((void)s.find(1));
125 EXPECT_THROW((void)s.find(2), std::domain_error);
126}
127
129{
131 s.insert(1);
132 s.insert(2);
133 s.insert(1);
134
135 EXPECT_EQ(s.count(1), 2u);
137 EXPECT_EQ(s.count(1), 1u);
138 EXPECT_EQ(s.size(), 2u);
139
141 EXPECT_EQ(s.count(1), 0u);
142 EXPECT_EQ(s.size(), 1u);
143
145 EXPECT_EQ(s.size(), 1u);
146}
147
149{
151 s.insert(5);
152 s.insert(6);
153 s.insert(5);
154 s.insert(7);
155 s.insert(5);
156
157 EXPECT_EQ(s.count(5), 3u);
158 auto removed = s.remove_all(5);
159 EXPECT_EQ(removed, 3u);
160 EXPECT_EQ(s.count(5), 0u);
161 EXPECT_EQ(s.size(), 2u);
162 EXPECT_TRUE(s.contains(6));
163 EXPECT_TRUE(s.contains(7));
164}
165
167{
168 DynArray_Set<int, AbsEqual> s(AbsEqual{});
169
170 s.insert(10);
171 s.insert(-10);
172 s.insert(20);
173
174 EXPECT_TRUE(s.contains(10));
175 EXPECT_TRUE(s.contains(-10));
176
177 // Under AbsEqual, both 10 and -10 match the key 10.
178 EXPECT_EQ(s.count(10), 2u);
179
180 // search should return a pointer to some matching element.
181 auto *p = s.search(10);
182 ASSERT_NE(p, nullptr);
183 EXPECT_TRUE(*p == 10 || *p == -10);
184}
185
187{
188 DynArray_Set<int, ModEqual> s(ModEqual{10});
189
190 s.insert(10);
191 s.insert(11);
192
193 // With mod=10, 10 and 20 are equivalent.
194 EXPECT_TRUE(s.contains(20));
195 EXPECT_EQ(s.count(20), 1u);
196
197 s.set_equal(ModEqual{11});
198
199 // With mod=11, 10 and 20 are no longer equivalent.
200 EXPECT_FALSE(s.contains(20));
201}
202
204{
205 DynArray_Set<int> s(12, 10, 4);
206 s.append(1);
207 s.append(2);
208 s.append(3);
209
210 EXPECT_EQ(s.size(), 3u);
211 EXPECT_EQ(s.get_first(), 1);
212 EXPECT_EQ(s.get_last(), 3);
213
214 // removal-by-reference (swap-with-last) is inherited.
215 auto &ref = s.access(0);
216 s.remove(ref);
217 EXPECT_EQ(s.size(), 2u);
218}
Set-like container backed by a dynamic array.
size_t count(const T &key) const noexcept
Count how many occurrences of key exist.
T & find(const T &key)
Find and return a reference to the first matching element.
T * insert(const T &item)
Insert item (duplicates are allowed).
bool remove_one(const T &key)
Remove a single occurrence of key.
size_t remove_all(const T &key)
Remove all occurrences of key.
T * search(const T &key) noexcept
Search for the first element equal to key.
bool contains(const T &key) const noexcept
Check whether key is present.
void remove(T &item)
Given a valid reference to an item in the array, it removes it and decrease the dimension.
T & get_last() const
Return a modifiable reference to the last item of array (as if this was a queue)
T & get_first() const
Return a modifiable reference to the first item of array (as if this was a queue)
size_t size() const noexcept
Return the current dimension of array.
T & access(const size_t i) const noexcept
Fast access without checking allocation and bound_min_clock checking.
T & append()
Allocate a new entry to the end of array.
bool is_empty() const noexcept
Return true if the array is empty.
#define TEST(name)
Main namespace for Aleph-w library functions.
Definition ah-arena.H:89
DynList< T > maps(const C &c, Op op)
Classic map operation.
double mod(double a, double b)
Array-based dynamic set.