Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
ah_init_guard.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
45#include <gtest/gtest.h>
46#include <stdexcept>
47#include <functional>
48#include <vector>
49
50#include <ah_init_guard.H>
51
52using namespace Aleph;
53
54// ============================================================================
55// Test Suite: Basic Functionality
56// ============================================================================
57
69
82
84{
85 bool dummy = false;
86 Init_Guard guard([&dummy]() { dummy = true; });
87
88 EXPECT_FALSE(guard.is_released());
89 guard.release();
90 EXPECT_TRUE(guard.is_released());
91}
92
94{
95 bool cleanup_called = false;
96
97 {
99 guard.release();
100 guard.release(); // Should be safe to call multiple times
101 guard.release();
102 EXPECT_TRUE(guard.is_released());
103 }
104
106}
107
108// ============================================================================
109// Test Suite: Exception Safety
110// ============================================================================
111
113{
114 bool cleanup_called = false;
115
116 try
117 {
119 throw std::runtime_error("test exception");
120 }
121 catch (const std::runtime_error&)
122 {
123 // Expected
124 }
125
127}
128
130{
131 bool cleanup_called = false;
132
133 try
134 {
136 guard.release();
137 throw std::runtime_error("test exception");
138 }
139 catch (const std::runtime_error&)
140 {
141 // Expected
142 }
143
145}
146
147// ============================================================================
148// Test Suite: Move Semantics
149// ============================================================================
150
152{
153 bool cleanup_called = false;
154
155 {
157
158 // Move to guard2
159 Init_Guard guard2(std::move(guard1));
160
161 // guard1 should be released after move
162 EXPECT_TRUE(guard1.is_released());
163 EXPECT_FALSE(guard2.is_released());
165 }
166
167 // Cleanup should be called when guard2 is destroyed
169}
170
172{
173 bool cleanup_called = false;
174
175 {
177 guard1.release();
178
179 Init_Guard guard2(std::move(guard1));
180
181 // Both should be released
182 EXPECT_TRUE(guard1.is_released());
183 EXPECT_TRUE(guard2.is_released());
184 }
185
187}
188
190{
191 int cleanup_count = 0;
192
193 // Use std::function because lambdas have deleted assignment operator
194 using CleanupType = std::function<void()>;
195
196 {
199
200 // Move assign guard1 to guard2
201 // This should trigger cleanup of guard2's original function
202 guard2 = std::move(guard1);
203
204 EXPECT_EQ(cleanup_count, 1); // guard2's original cleanup was called
205
206 EXPECT_TRUE(guard1.is_released());
207 EXPECT_FALSE(guard2.is_released());
208 }
209
210 // guard2's cleanup (originally from guard1) should be called
212}
213
215{
216 int cleanup_count = 0;
217
218 // Use std::function because lambdas have deleted assignment operator
219 using CleanupType = std::function<void()>;
220
221 {
224
225 guard2.release(); // Release guard2 first
226
227 // Move assign guard1 to guard2
228 // Since guard2 was released, its cleanup should NOT be called
229 guard2 = std::move(guard1);
230
231 EXPECT_EQ(cleanup_count, 0); // No cleanup yet
232
233 EXPECT_TRUE(guard1.is_released());
234 EXPECT_FALSE(guard2.is_released());
235 }
236
237 // Only one cleanup (from guard1 via guard2)
239}
240
242{
243 bool cleanup_called = false;
244
245 // Use std::function because lambdas have deleted assignment operator
246 std::function<void()> cleanup_func = [&cleanup_called]() { cleanup_called = true; };
247
248 {
249 Init_Guard<std::function<void()>> guard(cleanup_func);
250
251 // Self-assignment should be safe and do nothing
252 // Use a reference to avoid self-move warning
253 Init_Guard<std::function<void()>>& ref = guard;
254 guard = std::move(ref);
255
257 EXPECT_FALSE(guard.is_released());
258 }
259
261}
262
263// ============================================================================
264// Test Suite: Factory Function
265// ============================================================================
266
268{
269 bool cleanup_called = false;
270
271 {
272 auto guard = make_init_guard([&cleanup_called]() { cleanup_called = true; });
274 EXPECT_FALSE(guard.is_released());
275 }
276
278}
279
281{
282 bool cleanup_called = false;
283
284 {
285 auto guard = make_init_guard([&cleanup_called]() { cleanup_called = true; });
286 guard.release();
287 }
288
290}
291
292// ============================================================================
293// Test Suite: Complex Cleanup
294// ============================================================================
295
297{
298 int counter = 0;
299
300 {
301 Init_Guard guard([&counter]() { counter = 42; });
302 EXPECT_EQ(counter, 0);
303 }
304
305 EXPECT_EQ(counter, 42);
306}
307
309{
310 bool resource1_cleaned = false;
311 bool resource2_cleaned = false;
312 bool resource3_cleaned = false;
313
314 {
318
319 // Release only guard2
320 guard2.release();
321
325 }
326
328 EXPECT_FALSE(resource2_cleaned); // Was released
330}
331
333{
334 std::vector<int> cleanup_order;
335
336 {
337 Init_Guard guard1([&cleanup_order]() { cleanup_order.push_back(1); });
338 Init_Guard guard2([&cleanup_order]() { cleanup_order.push_back(2); });
339 Init_Guard guard3([&cleanup_order]() { cleanup_order.push_back(3); });
340 }
341
342 // Destructors are called in reverse order of construction (LIFO)
347}
348
349// ============================================================================
350// Test Suite: Callable Types
351// ============================================================================
352
353void free_function_cleanup(bool& flag)
354{
355 flag = true;
356}
357
359{
360 bool called = false;
361 {
362 Init_Guard guard([&called]() { called = true; });
363 }
364 EXPECT_TRUE(called);
365}
366
368{
369 struct Cleanup
370 {
371 bool& flag;
372 explicit Cleanup(bool& f) : flag(f) {}
373 void operator()() { flag = true; }
374 };
375
376 bool called = false;
377 {
378 auto guard = Init_Guard(Cleanup(called));
379 }
380 EXPECT_TRUE(called);
381}
382
384{
385 bool called = false;
386 std::function<void()> cleanup = [&called]() { called = true; };
387
388 {
389 Init_Guard guard(cleanup);
390 }
391 EXPECT_TRUE(called);
392}
393
394// ============================================================================
395// Test Suite: Edge Cases
396// ============================================================================
397
399{
400 // Should compile and work even with empty lambda
401 {
402 Init_Guard guard([]() {});
403 }
404 // Just verify it doesn't crash
405 SUCCEED();
406}
407
409{
410 std::vector<int> log;
411
412 {
413 Init_Guard outer([&log]() { log.push_back(1); });
414 {
415 Init_Guard inner([&log]() { log.push_back(2); });
416 }
417 log.push_back(3); // After inner cleanup
418 }
419 log.push_back(4); // After outer cleanup
420
421 ASSERT_EQ(log.size(), 4);
422 EXPECT_EQ(log[0], 2); // Inner cleanup
423 EXPECT_EQ(log[1], 3); // Between cleanups
424 EXPECT_EQ(log[2], 1); // Outer cleanup
425 EXPECT_EQ(log[3], 4); // After all
426}
427
RAII guard for exception-safe resource cleanup.
void free_function_cleanup(bool &flag)
size_t size() const noexcept
Count the number of elements of the list.
Definition htlist.H:1319
RAII guard for graph algorithm initialization.
#define TEST(name)
__gmp_expr< T, __gmp_unary_expr< __gmp_expr< T, U >, __gmp_log_function > > log(const __gmp_expr< T, U > &expr)
Definition gmpfrxx.h:4063
Main namespace for Aleph-w library functions.
Definition ah-arena.H:89
auto make_init_guard(CleanupFunc f)
Create an Init_Guard with a cleanup function.
DynList< T > maps(const C &c, Op op)
Classic map operation.