Aleph-w 7.10.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
58TEST(InitGuard, CleanupCalledOnDestruction)
59{
60 bool cleanup_called = false;
61
62 {
63 Init_Guard guard([&cleanup_called]() { cleanup_called = true; });
64 EXPECT_FALSE(cleanup_called);
65 }
66
67 EXPECT_TRUE(cleanup_called);
68}
69
70TEST(InitGuard, CleanupNotCalledIfReleased)
71{
72 bool cleanup_called = false;
73
74 {
75 Init_Guard guard([&cleanup_called]() { cleanup_called = true; });
76 guard.release();
77 EXPECT_FALSE(cleanup_called);
78 }
79
80 EXPECT_FALSE(cleanup_called);
81}
82
83TEST(InitGuard, IsReleasedGetter)
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
93TEST(InitGuard, MultipleReleaseCalls)
94{
95 bool cleanup_called = false;
96
97 {
98 Init_Guard guard([&cleanup_called]() { cleanup_called = true; });
99 guard.release();
100 guard.release(); // Should be safe to call multiple times
101 guard.release();
102 EXPECT_TRUE(guard.is_released());
103 }
104
105 EXPECT_FALSE(cleanup_called);
106}
107
108// ============================================================================
109// Test Suite: Exception Safety
110// ============================================================================
111
112TEST(InitGuard, CleanupCalledOnException)
113{
114 bool cleanup_called = false;
115
116 try
117 {
118 Init_Guard guard([&cleanup_called]() { cleanup_called = true; });
119 throw std::runtime_error("test exception");
120 }
121 catch (const std::runtime_error&)
122 {
123 // Expected
124 }
125
126 EXPECT_TRUE(cleanup_called);
127}
128
129TEST(InitGuard, CleanupNotCalledIfReleasedBeforeException)
130{
131 bool cleanup_called = false;
132
133 try
134 {
135 Init_Guard guard([&cleanup_called]() { cleanup_called = true; });
136 guard.release();
137 throw std::runtime_error("test exception");
138 }
139 catch (const std::runtime_error&)
140 {
141 // Expected
142 }
143
144 EXPECT_FALSE(cleanup_called);
145}
146
147// ============================================================================
148// Test Suite: Move Semantics
149// ============================================================================
150
151TEST(InitGuard, MoveConstructor)
152{
153 bool cleanup_called = false;
154
155 {
156 Init_Guard guard1([&cleanup_called]() { cleanup_called = true; });
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());
164 EXPECT_FALSE(cleanup_called);
165 }
166
167 // Cleanup should be called when guard2 is destroyed
168 EXPECT_TRUE(cleanup_called);
169}
170
171TEST(InitGuard, MoveConstructorFromReleased)
172{
173 bool cleanup_called = false;
174
175 {
176 Init_Guard guard1([&cleanup_called]() { cleanup_called = true; });
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
186 EXPECT_FALSE(cleanup_called);
187}
188
189TEST(InitGuard, MoveAssignment)
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 {
197 Init_Guard<CleanupType> guard1([&cleanup_count]() { cleanup_count++; });
198 Init_Guard<CleanupType> guard2([&cleanup_count]() { cleanup_count++; });
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
211 EXPECT_EQ(cleanup_count, 2);
212}
213
214TEST(InitGuard, MoveAssignmentToReleased)
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 {
222 Init_Guard<CleanupType> guard1([&cleanup_count]() { cleanup_count++; });
223 Init_Guard<CleanupType> guard2([&cleanup_count]() { cleanup_count++; });
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)
238 EXPECT_EQ(cleanup_count, 1);
239}
240
241TEST(InitGuard, SelfMoveAssignment)
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
256 EXPECT_FALSE(cleanup_called);
257 EXPECT_FALSE(guard.is_released());
258 }
259
260 EXPECT_TRUE(cleanup_called);
261}
262
263// ============================================================================
264// Test Suite: Factory Function
265// ============================================================================
266
267TEST(InitGuard, MakeInitGuard)
268{
269 bool cleanup_called = false;
270
271 {
272 auto guard = make_init_guard([&cleanup_called]() { cleanup_called = true; });
273 EXPECT_FALSE(cleanup_called);
274 EXPECT_FALSE(guard.is_released());
275 }
276
277 EXPECT_TRUE(cleanup_called);
278}
279
280TEST(InitGuard, MakeInitGuardWithRelease)
281{
282 bool cleanup_called = false;
283
284 {
285 auto guard = make_init_guard([&cleanup_called]() { cleanup_called = true; });
286 guard.release();
287 }
288
289 EXPECT_FALSE(cleanup_called);
290}
291
292// ============================================================================
293// Test Suite: Complex Cleanup
294// ============================================================================
295
296TEST(InitGuard, ModifiesExternalState)
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
308TEST(InitGuard, MultipleResourceCleanup)
309{
310 bool resource1_cleaned = false;
311 bool resource2_cleaned = false;
312 bool resource3_cleaned = false;
313
314 {
315 Init_Guard guard1([&resource1_cleaned]() { resource1_cleaned = true; });
316 Init_Guard guard2([&resource2_cleaned]() { resource2_cleaned = true; });
317 Init_Guard guard3([&resource3_cleaned]() { resource3_cleaned = true; });
318
319 // Release only guard2
320 guard2.release();
321
322 EXPECT_FALSE(resource1_cleaned);
323 EXPECT_FALSE(resource2_cleaned);
324 EXPECT_FALSE(resource3_cleaned);
325 }
326
327 EXPECT_TRUE(resource1_cleaned);
328 EXPECT_FALSE(resource2_cleaned); // Was released
329 EXPECT_TRUE(resource3_cleaned);
330}
331
332TEST(InitGuard, CleanupOrder)
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)
343 ASSERT_EQ(cleanup_order.size(), 3);
344 EXPECT_EQ(cleanup_order[0], 3);
345 EXPECT_EQ(cleanup_order[1], 2);
346 EXPECT_EQ(cleanup_order[2], 1);
347}
348
349// ============================================================================
350// Test Suite: Callable Types
351// ============================================================================
352
353void free_function_cleanup(bool& flag)
354{
355 flag = true;
356}
357
358TEST(InitGuard, WithLambda)
359{
360 bool called = false;
361 {
362 Init_Guard guard([&called]() { called = true; });
363 }
364 EXPECT_TRUE(called);
365}
366
367TEST(InitGuard, WithFunctionObject)
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
383TEST(InitGuard, WithStdFunction)
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
398TEST(InitGuard, EmptyLambda)
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
408TEST(InitGuard, NestedGuards)
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)
RAII guard for graph algorithm initialization.
void release() noexcept
Release the guard - cleanup will not be called.
bool is_released() const noexcept
Check if the guard has been released.
#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:4141
Main namespace for Aleph-w library functions.
auto make_init_guard(CleanupFunc f)
Create an Init_Guard with a cleanup function.
static long counter
Definition test-splice.C:40