Aleph-w
3.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
ah_stdc_utils_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
44
#include <gtest/gtest.h>
45
46
#include <
ah_stdc++_utils.H
>
47
48
#include <stdexcept>
49
50
using namespace
Aleph
;
51
52
// ============================================================================
53
// Mock Container and Iterator for testing
54
// ============================================================================
55
56
namespace
57
{
58
59
// Simple mock container
60
class
MockContainer
61
{
62
int
id_;
63
64
public
:
65
explicit
MockContainer
(
int
id
) : id_(id) {}
66
int
id()
const
{
return
id_; }
67
};
68
69
// Mock iterator that tracks which container it belongs to
70
class
MockIterator
71
{
72
const
MockContainer
* container_;
73
74
public
:
75
explicit
MockIterator(
const
MockContainer
* c =
nullptr
) : container_(c) {}
76
77
// Verify this iterator belongs to the given container
78
bool
verify
(
const
MockContainer
& c)
const
79
{
80
return
container_ == &c;
81
}
82
83
// Verify this iterator and another belong to the same container
84
bool
verify
(
const
MockIterator&
other
)
const
85
{
86
return
container_ ==
other
.container_;
87
}
88
89
const
MockContainer
* container()
const
{
return
container_; }
90
};
91
92
}
// namespace
93
94
// ============================================================================
95
// verify_container_and_iterator Tests
96
// ============================================================================
97
98
TEST
(
VerifyContainerAndIterator
,
ValidIteratorDoesNotThrow
)
99
{
100
MockContainer
container(1);
101
MockIterator
iter
(&container);
102
103
EXPECT_NO_THROW
(
verify_container_and_iterator
(container,
iter
));
104
}
105
106
TEST
(
VerifyContainerAndIterator
,
InvalidIteratorThrows
)
107
{
108
MockContainer
container1
(1);
109
MockContainer
container2
(2);
110
MockIterator
iter
(&
container1
);
111
112
EXPECT_THROW
(
verify_container_and_iterator
(
container2
,
iter
), std::domain_error);
113
}
114
115
TEST
(
VerifyContainerAndIterator
,
NullIteratorThrows
)
116
{
117
MockContainer
container(1);
118
MockIterator
iter
(
nullptr
);
119
120
EXPECT_THROW
(
verify_container_and_iterator
(container,
iter
), std::domain_error);
121
}
122
123
TEST
(
VerifyContainerAndIterator
,
ExceptionMessageIsDescriptive
)
124
{
125
MockContainer
container1
(1);
126
MockContainer
container2
(2);
127
MockIterator
iter
(&
container1
);
128
129
try
130
{
131
verify_container_and_iterator
(
container2
,
iter
);
132
FAIL
() <<
"Expected exception was not thrown"
;
133
}
134
catch
(
const
std::domain_error& e)
135
{
136
std::string msg = e.what();
137
EXPECT_TRUE
(msg.find(
"Iterator"
) != std::string::npos ||
138
msg.find(
"iterator"
) != std::string::npos);
139
}
140
}
141
142
// ============================================================================
143
// verify_iterators Tests
144
// ============================================================================
145
146
TEST
(
VerifyIterators
,
SameContainerDoesNotThrow
)
147
{
148
MockContainer
container(1);
149
MockIterator
iter1
(&container);
150
MockIterator
iter2
(&container);
151
152
EXPECT_NO_THROW
(
verify_iterators
(
iter1
,
iter2
));
153
}
154
155
TEST
(
VerifyIterators
,
DifferentContainersThrows
)
156
{
157
MockContainer
container1
(1);
158
MockContainer
container2
(2);
159
MockIterator
iter1
(&
container1
);
160
MockIterator
iter2
(&
container2
);
161
162
EXPECT_THROW
(
verify_iterators
(
iter1
,
iter2
), std::domain_error);
163
}
164
165
TEST
(
VerifyIterators
,
SameIteratorDoesNotThrow
)
166
{
167
MockContainer
container(1);
168
MockIterator
iter
(&container);
169
170
// Same iterator compared to itself should pass
171
EXPECT_NO_THROW
(
verify_iterators
(
iter
,
iter
));
172
}
173
174
TEST
(
VerifyIterators
,
BothNullIteratorsDoNotThrow
)
175
{
176
MockIterator
iter1
(
nullptr
);
177
MockIterator
iter2
(
nullptr
);
178
179
// Both null - same "container" (none)
180
EXPECT_NO_THROW
(
verify_iterators
(
iter1
,
iter2
));
181
}
182
183
TEST
(
VerifyIterators
,
OneNullIteratorThrows
)
184
{
185
MockContainer
container(1);
186
MockIterator
iter1
(&container);
187
MockIterator
iter2
(
nullptr
);
188
189
EXPECT_THROW
(
verify_iterators
(
iter1
,
iter2
), std::domain_error);
190
}
191
192
// ============================================================================
193
// verify_container_and_iterators (2-iterator overload) Tests
194
// ============================================================================
195
196
TEST
(
VerifyContainerAndIterators2
,
BothIteratorsValidDoesNotThrow
)
197
{
198
MockContainer
container(1);
199
MockIterator
iter1
(&container);
200
MockIterator
iter2
(&container);
201
202
EXPECT_NO_THROW
(
verify_container_and_iterators
(container,
iter1
,
iter2
));
203
}
204
205
TEST
(
VerifyContainerAndIterators2
,
FirstIteratorInvalidThrows
)
206
{
207
MockContainer
container1
(1);
208
MockContainer
container2
(2);
209
MockIterator
iter1
(&
container2
);
// Wrong container
210
MockIterator
iter2
(&
container1
);
211
212
EXPECT_THROW
(
verify_container_and_iterators
(
container1
,
iter1
,
iter2
),
213
std::domain_error);
214
}
215
216
TEST
(
VerifyContainerAndIterators2
,
SecondIteratorInvalidThrows
)
217
{
218
MockContainer
container1
(1);
219
MockContainer
container2
(2);
220
MockIterator
iter1
(&
container1
);
221
MockIterator
iter2
(&
container2
);
// Wrong container
222
223
EXPECT_THROW
(
verify_container_and_iterators
(
container1
,
iter1
,
iter2
),
224
std::domain_error);
225
}
226
227
TEST
(
VerifyContainerAndIterators2
,
BothIteratorsInvalidThrows
)
228
{
229
MockContainer
container1
(1);
230
MockContainer
container2
(2);
231
MockIterator
iter1
(&
container2
);
232
MockIterator
iter2
(&
container2
);
233
234
EXPECT_THROW
(
verify_container_and_iterators
(
container1
,
iter1
,
iter2
),
235
std::domain_error);
236
}
237
238
// ============================================================================
239
// verify_container_and_iterators (3-iterator overload) Tests
240
// ============================================================================
241
242
TEST
(
VerifyContainerAndIterators3
,
ValidDistinctIteratorsDoNotThrow
)
243
{
244
MockContainer
dest_container
(1);
245
MockContainer
src_container
(2);
246
247
MockIterator
dest_iter
(&
dest_container
);
248
MockIterator
src_iter1
(&
src_container
);
249
MockIterator
src_iter2
(&
src_container
);
250
251
EXPECT_NO_THROW
(
verify_container_and_iterators
(
252
dest_container
,
dest_iter
,
src_iter1
,
src_iter2
));
253
}
254
255
TEST
(
VerifyContainerAndIterators3
,
DestIteratorInvalidThrows
)
256
{
257
MockContainer
dest_container
(1);
258
MockContainer
src_container
(2);
259
MockContainer
other_container
(3);
260
261
MockIterator
dest_iter
(&
other_container
);
// Wrong!
262
MockIterator
src_iter1
(&
src_container
);
263
MockIterator
src_iter2
(&
src_container
);
264
265
EXPECT_THROW
(
verify_container_and_iterators
(
266
dest_container
,
dest_iter
,
src_iter1
,
src_iter2
), std::domain_error);
267
}
268
269
TEST
(
VerifyContainerAndIterators3
,
SourceIteratorsFromDifferentContainersThrows
)
270
{
271
MockContainer
dest_container
(1);
272
MockContainer
src_container1
(2);
273
MockContainer
src_container2
(3);
274
275
MockIterator
dest_iter
(&
dest_container
);
276
MockIterator
src_iter1
(&
src_container1
);
277
MockIterator
src_iter2
(&
src_container2
);
// Different from src_iter1!
278
279
EXPECT_THROW
(
verify_container_and_iterators
(
280
dest_container
,
dest_iter
,
src_iter1
,
src_iter2
), std::domain_error);
281
}
282
283
TEST
(
VerifyContainerAndIterators3
,
DestAndSourceSameContainerThrows
)
284
{
285
MockContainer
container(1);
286
287
MockIterator
dest_iter
(&container);
288
MockIterator
src_iter1
(&container);
// Same as dest!
289
MockIterator
src_iter2
(&container);
290
291
// This should throw because dest and source are the same container
292
EXPECT_THROW
(
verify_container_and_iterators
(
293
container,
dest_iter
,
src_iter1
,
src_iter2
), std::domain_error);
294
}
295
296
// ============================================================================
297
// Integration with real Aleph containers (if available)
298
// ============================================================================
299
300
// Note: Additional tests with real DynList/DynArray iterators could be added
301
// here to verify integration with actual Aleph container types.
302
303
int
main
(
int
argc
,
char
**
argv
)
304
{
305
::testing::InitGoogleTest(&
argc
,
argv
);
306
return
RUN_ALL_TESTS
();
307
}
main
int main()
Definition
ah_parallel_example.cc:690
ah_stdc++_utils.H
Iterator and container validation utilities.
FAIL
#define FAIL(msg)
Definition
disjoint_sparse_table_test.cc:107
TEST
#define TEST(name)
Definition
disjoint_sparse_table_test.cc:95
verify
void verify()
Definition
hash_resize.C:201
Aleph
Main namespace for Aleph-w library functions.
Definition
ah-arena.H:89
Aleph::verify_container_and_iterators
void verify_container_and_iterators(const Container &container, const Iterator &itor_container, const Iterator &itor1, const Iterator &itor2)
Verifies container-iterator relationships with distinctness check.
Definition
ah_stdc++_utils.H:163
Aleph::verify_iterators
void verify_iterators(const Iterator &itor1, const Iterator &itor2)
Verifies that two iterators belong to the same container.
Definition
ah_stdc++_utils.H:137
Aleph::verify_container_and_iterator
void verify_container_and_iterator(const Container &container, const Iterator &itor)
Verifies that an iterator belongs to a specific container.
Definition
ah_stdc++_utils.H:105
Aleph::maps
DynList< T > maps(const C &c, Op op)
Classic map operation.
Definition
ahFunctional.H:693
MockContainer
Definition
ah-utils.cc:182
Tests
ah_stdc_utils_test.cc
Generated by
1.9.8