Aleph-w 7.10.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
dfs_stack_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
42#include <gtest/gtest.h>
43
44#include "allocation_failure.H"
45
46#include <new>
47#include <optional>
48#include <stdexcept>
49#include <string>
50#include <thread>
51#include <type_traits>
52#include <utility>
53
54#include <tpl_dfs_stack.H>
55
56#ifdef ALEPH_DFS_STACK_POISON
57# include <sanitizer/asan_interface.h>
58#endif
59
60using namespace Aleph;
61
62namespace
63{
64 // Counts the frames that live, and throws when it is built from, or
65 // copied from, the value throw_at
66 struct Counted
67 {
68 static inline long alive = 0;
69 static inline long throw_at = -1;
70
71 long value;
72
73 explicit Counted(long v) : value(v)
74 {
75 if (v == throw_at)
76 throw std::runtime_error("built from throw_at");
77 ++alive;
78 }
79
80 // Not noexcept, so that the stack copies the frames when it grows
81 Counted(const Counted & other) : value(other.value)
82 {
83 if (value == throw_at)
84 throw std::runtime_error("copied from throw_at");
85 ++alive;
86 }
87
88 ~Counted() { --alive; }
89 };
90
91 // A frame that cannot be copied, and whose move can throw
92 struct Move_Only
93 {
94 static inline long alive = 0;
95 static inline long throw_at = -1;
96
97 long value;
98
99 explicit Move_Only(long v) : value(v) { ++alive; }
100
101 Move_Only(Move_Only && other) : value(other.value)
102 {
103 if (value == throw_at)
104 throw std::runtime_error("moved from throw_at");
105 other.value = -1;
106 ++alive;
107 }
108
109 Move_Only(const Move_Only &) = delete;
110
111 ~Move_Only() { --alive; }
112 };
113
114 // Resets the counters of Counted around a test
115 struct CountedFrames : public ::testing::Test
116 {
117 void SetUp() override
118 {
119 Counted::alive = 0;
120 Counted::throw_at = -1;
121 }
122
123 void TearDown() override { Counted::throw_at = -1; }
124 };
125}
126
127TEST(DfsStack, PopsInTheReverseOrderOfThePushes)
128{
129 Dfs_Stack<long> stack;
130 EXPECT_TRUE(stack.is_empty());
131 EXPECT_EQ(stack.size(), 0u);
132 for (long i = 0; i < 10000; ++i)
133 {
134 long & frame = stack.push(i);
135 EXPECT_EQ(frame, i);
136 EXPECT_EQ(&frame, &stack.top());
137 }
138 EXPECT_EQ(stack.size(), 10000u);
139 for (long i = 9999; i >= 0; --i)
140 {
141 ASSERT_EQ(stack.top(), i);
142 stack.pop();
143 }
144 EXPECT_TRUE(stack.is_empty());
145}
146
147TEST(DfsStack, GrowingKeepsTheFrames)
148{
150 for (int i = 0; i < 5000; ++i)
151 stack.push(std::to_string(i) + " is a frame long enough not to fit inline");
152 ASSERT_EQ(stack.size(), 5000u);
153 for (int i = 4999; i >= 0; --i)
154 {
155 ASSERT_EQ(stack.top(), std::to_string(i) + " is a frame long enough not to fit inline");
156 stack.pop();
157 }
158}
159
160TEST_F(CountedFrames, ClearAndTheDestructorDestroyTheFrames)
161{
162 {
163 Dfs_Stack<Counted> stack;
164 for (long i = 0; i < 300; ++i)
165 stack.push(i);
166 EXPECT_EQ(Counted::alive, 300);
167 stack.clear();
168 EXPECT_EQ(Counted::alive, 0);
169 EXPECT_TRUE(stack.is_empty());
170 for (long i = 0; i < 100; ++i)
171 stack.push(i);
172 EXPECT_EQ(Counted::alive, 100);
173 }
174 EXPECT_EQ(Counted::alive, 0);
175}
176
177TEST_F(CountedFrames, AFrameThatThrowsLeavesTheStackAsItWas)
178{
179 Dfs_Stack<Counted> stack;
180 for (long i = 0; i < 10; ++i)
181 stack.push(i);
182 Counted::throw_at = 77;
183 EXPECT_THROW(stack.push(77), std::runtime_error);
184 EXPECT_EQ(stack.size(), 10u);
185 EXPECT_EQ(stack.top().value, 9);
186 EXPECT_EQ(Counted::alive, 10);
187}
188
189// Without a move that cannot throw, the stack copies its frames when it
190// grows; if a copy throws, the frames are those it had
191TEST_F(CountedFrames, ACopyThatThrowsWhileGrowingLeavesTheStackAsItWas)
192{
193 Dfs_Stack<Counted> stack;
194 for (long i = 0; i < 10; ++i)
195 stack.push(i);
196
197 Counted::throw_at = 5; // copying the frame 5 throws, so growing does
198 long next = 10;
199 bool thrown = false;
200 while (not thrown and next < 10000000)
201 try
202 {
203 stack.push(next);
204 ++next;
205 }
206 catch (const std::runtime_error &)
207 {
208 thrown = true;
209 }
210 Counted::throw_at = -1;
211
212 ASSERT_TRUE(thrown);
213 ASSERT_EQ(stack.size(), size_t(next));
214 EXPECT_EQ(Counted::alive, next);
215 for (long i = next - 1; i >= 0; --i)
216 {
217 ASSERT_EQ(stack.top().value, i);
218 stack.pop();
219 }
220 EXPECT_EQ(Counted::alive, 0);
221}
222
223// A frame that cannot be copied, and whose move throws while the stack
224// grows: the new room went to no one, which LeakSanitizer reports
225TEST(DfsStack, AMoveThatThrowsWhileGrowingFreesTheNewRoom)
226{
227 Move_Only::alive = 0;
228 {
230 for (long i = 0; i < 10; ++i)
231 stack.push(i);
232
233 Move_Only::throw_at = 5; // moving the frame 5 throws, so growing does
234 long next = 10;
235 bool thrown = false;
236 while (not thrown and next < 10000000)
237 try
238 {
239 stack.push(next);
240 ++next;
241 }
242 catch (const std::runtime_error &)
243 {
244 thrown = true;
245 }
246 Move_Only::throw_at = -1;
247
248 ASSERT_TRUE(thrown);
249 EXPECT_EQ(stack.size(), size_t(next)); // the frame 5 and the next ones
250 EXPECT_EQ(stack.top().value, next - 1);
251 EXPECT_EQ(Move_Only::alive, next);
252 }
253 EXPECT_EQ(Move_Only::alive, 0);
254}
255
256TEST(DfsStack, TheCapacityIsThatOfTheRoom)
257{
258 Dfs_Stack<long> stack;
259 EXPECT_EQ(stack.capacity(), 0u); // no room until a frame is pushed
260 for (long i = 0; i < 5000; ++i)
261 {
262 stack.push(i);
263 ASSERT_GE(stack.capacity(), stack.size());
264 }
265 const size_t capacity = stack.capacity();
266 stack.clear();
267 EXPECT_EQ(stack.capacity(), capacity); // clear() keeps the room
268}
269
270TEST_F(CountedFrames, ReleaseDestroysTheFramesAndGivesTheRoomBack)
271{
272 Dfs_Stack<Counted> stack;
273 for (long i = 0; i < 300; ++i)
274 stack.push(i);
275 stack.release();
276 EXPECT_EQ(Counted::alive, 0);
277 EXPECT_TRUE(stack.is_empty());
278 EXPECT_EQ(stack.capacity(), 0u);
279
280 stack.release(); // without room, it does nothing
281 EXPECT_EQ(stack.capacity(), 0u);
282 for (long i = 0; i < 10; ++i) // and the stack takes room again
283 stack.push(i);
284 EXPECT_EQ(stack.size(), 10u);
285 EXPECT_EQ(stack.top().value, 9);
286}
287
288TEST(DfsStack, MovesTakeTheFrames)
289{
291 for (long i = 0; i < 1000; ++i)
292 a.push(i);
293
294 Dfs_Stack<long> b(std::move(a));
295 EXPECT_TRUE(a.is_empty());
296 ASSERT_EQ(b.size(), 1000u);
297 EXPECT_EQ(b.top(), 999);
298
300 c.push(-1);
301 c = std::move(b);
302 EXPECT_TRUE(b.is_empty());
303 ASSERT_EQ(c.size(), 1000u);
304 EXPECT_EQ(c.top(), 999);
305
306 // the moved from stacks still work
307 a.push(5);
308 b.push(6);
309 EXPECT_EQ(a.top(), 5);
310 EXPECT_EQ(b.top(), 6);
311}
312
313namespace
314{
315 // The tests need the operator new of allocation_failure.H
316 struct DfsStackRoom : public ::testing::Test
317 {
318 void SetUp() override
319 {
321 GTEST_SKIP() << "ThreadSanitizer replaces the global operator new";
322 }
323 };
324
325 // Leaves in the room of the thread one for at least n frames
326 void keep_room_for(size_t n)
327 {
328 Dfs_Stack<long> stack;
329 for (size_t i = 0; i < n; ++i)
330 stack.push(long(i));
331 }
332}
333
334// A search that follows another in the same thread allocates nothing
335TEST_F(DfsStackRoom, AStackOfTheSameThreadReusesTheRoom)
336{
337 keep_room_for(20000);
338 {
339 Dfs_Stack<long> stack;
340 No_Memory no_memory;
341 for (long i = 0; i < 20000; ++i)
342 ASSERT_NO_THROW(stack.push(i));
343 }
344 EXPECT_EQ(refused_allocations, 0u);
345}
346
347TEST_F(DfsStackRoom, AStackOfAnotherThreadHasNoRoom)
348{
349 keep_room_for(20000);
350 bool thrown = false;
351 std::thread other([&thrown]
352 {
353 Dfs_Stack<long> stack;
354 No_Memory no_memory;
355 try
356 {
357 stack.push(1);
358 }
359 catch (const std::bad_alloc &)
360 {
361 thrown = true;
362 }
363 });
364 other.join();
365 EXPECT_TRUE(thrown);
366}
367
368// A room larger than 1 MiB is freed, so that it does not stay taken; the
369// room that the stack took from the thread goes back to the thread when
370// the stack outgrows it, so that the next search allocates nothing
371TEST_F(DfsStackRoom, ALargeRoomIsNotKept)
372{
373 keep_room_for(20000);
374 size_t kept = 0; // the frames of the room that the thread keeps
375 {
376 Dfs_Stack<long> stack;
377 stack.push(0);
378 kept = stack.capacity();
379 }
380 ASSERT_GE(kept, 20000u);
381
382 keep_room_for(1024 * 1024); // 8 MiB of frames, from the room kept
383 Dfs_Stack<long> stack;
384 No_Memory no_memory;
385 for (size_t i = 0; i < kept; ++i) // in the room kept, which came back
386 ASSERT_NO_THROW(stack.push(long(i)));
387 EXPECT_EQ(stack.capacity(), kept);
388 EXPECT_THROW(stack.push(-1), std::bad_alloc); // the large one was freed
389 EXPECT_EQ(refused_allocations, 1u);
390}
391
392// The room that a stack took from its thread goes back to the thread when
393// the stack outgrows it, here while the stack lives
394TEST_F(DfsStackRoom, TheRoomTakenFromTheThreadGoesBackWhenTheStackGrows)
395{
396 size_t kept = 0, next_capacity = 0;
397 bool thrown = false;
398 std::thread other([&] // a thread that keeps no room yet
399 {
400 keep_room_for(10); // the first room of the thread, which it keeps
401 Dfs_Stack<long> stack;
402 stack.push(0);
403 kept = stack.capacity(); // that room
404 while (stack.capacity() == kept) // until the stack outgrows it
405 stack.push(1);
406
408 No_Memory no_memory;
409 try
410 {
411 next.push(2);
412 next_capacity = next.capacity();
413 }
414 catch (const std::bad_alloc &)
415 {
416 thrown = true;
417 }
418 });
419 other.join();
420 EXPECT_FALSE(thrown);
421 EXPECT_EQ(next_capacity, kept);
422}
423
424// release() gives the room to the thread, as the destructor does
425TEST_F(DfsStackRoom, ARoomReleasedIsReused)
426{
427 Dfs_Stack<long> stack;
428 for (long i = 0; i < 20000; ++i)
429 stack.push(i);
430 const size_t capacity = stack.capacity();
431 stack.release();
432 No_Memory no_memory;
433 for (long i = 0; i < long(capacity); ++i)
434 ASSERT_NO_THROW(stack.push(i));
435 EXPECT_EQ(stack.capacity(), capacity);
436 EXPECT_EQ(refused_allocations, 0u);
437}
438
439// The thread that runs main() is told by its id, and not by a mark taken
440// when the program starts, which a library loaded with dlopen() from
441// another thread took in that one: that thread then did not free its room
442// when it ended (#27 of the 7.3.2 review)
443TEST(DfsStack, TheThreadOfMainIsToldByItsId)
444{
445 bool main_is_told = false; // with glibc 2.30 or later; else none is
446#if defined(__GLIBC__)
447# if __GLIBC_PREREQ(2, 30)
448 main_is_told = true;
449# endif
450#endif
451 EXPECT_EQ(dfs_stack_detail::in_main_thread(), main_is_told);
452 bool other_is_main = true;
453 std::thread other([&other_is_main] { other_is_main = dfs_stack_detail::in_main_thread(); });
454 other.join();
455 EXPECT_FALSE(other_is_main);
456}
457
458// Under AddressSanitizer, with ALEPH_ANNOTATE_CONTAINERS, the room where no
459// frame is cannot be reached: that of the frames popped or cleared, and that
460// which the thread keeps
461TEST(DfsStack, TheRoomWithoutFramesIsPoisoned)
462{
463#ifdef ALEPH_DFS_STACK_POISON
464 long * first = nullptr;
465 {
466 Dfs_Stack<long> stack;
467 for (long i = 0; i < 1000; ++i)
468 stack.push(i);
469 first = &stack.top() - 999;
470 long * top = &stack.top();
471 EXPECT_FALSE(__asan_address_is_poisoned(top));
472 EXPECT_TRUE(__asan_address_is_poisoned(top + 1)); // beyond the top
473 stack.pop();
474 EXPECT_TRUE(__asan_address_is_poisoned(top));
475 EXPECT_FALSE(__asan_address_is_poisoned(top - 1));
476 stack.clear();
477 EXPECT_TRUE(__asan_address_is_poisoned(first));
478 stack.push(7);
479 EXPECT_FALSE(__asan_address_is_poisoned(first));
480 }
481 // the stack is gone, and the thread keeps its room
482 EXPECT_TRUE(__asan_address_is_poisoned(first));
483 Dfs_Stack<long> next; // which the next stack takes
484 next.push(8);
485 EXPECT_EQ(&next.top(), first);
486 EXPECT_FALSE(__asan_address_is_poisoned(first));
487 EXPECT_TRUE(__asan_address_is_poisoned(first + 1));
488#else
489 GTEST_SKIP() << "only under AddressSanitizer with ALEPH_ANNOTATE_CONTAINERS";
490#endif
491}
492
493namespace
494{
495 // A frame that cannot be assigned, as one whose iterator keeps a lambda
496 // that captures, or a filter with a const member
497 struct Unassigned
498 {
499 static inline long alive = 0;
500
501 const long value;
502
503 explicit Unassigned(long v) : value(v)
504 {
505 if (v < 0)
506 throw std::runtime_error("built from a negative value");
507 ++alive;
508 }
509
510 Unassigned(const Unassigned & other) : value(other.value) { ++alive; }
511
512 ~Unassigned() { --alive; }
513 };
514
515 static_assert(not std::is_move_assignable_v<Unassigned>);
516}
517
518// With frames that cannot be assigned, Dfs_Here is an optional frame, which
519// dfs_replace() builds again in its place
520TEST(DfsHere, ReplacesAFrameThatCannotBeAssigned)
521{
522 static_assert(std::is_same_v<Dfs_Here<Unassigned>, std::optional<Unassigned>>);
523 Unassigned::alive = 0;
524 {
525 auto here = dfs_here<Unassigned>(1);
526 EXPECT_EQ(dfs_frame(here).value, 1);
527 EXPECT_EQ(Unassigned::alive, 1);
528 dfs_replace(here, 2);
529 EXPECT_EQ(dfs_frame(here).value, 2);
530 EXPECT_EQ(Unassigned::alive, 1);
531
532 // a stack of them, which copies them when it grows
534 for (long i = 0; i < 1000; ++i)
535 {
536 stack.push(dfs_frame(here));
537 dfs_replace(here, i + 3);
538 }
539 EXPECT_EQ(Unassigned::alive, 1001);
540 EXPECT_EQ(stack.top().value, 1001);
541 EXPECT_EQ(dfs_frame(here).value, 1002);
542
543 EXPECT_THROW(dfs_replace(here, -1), std::runtime_error);
544 EXPECT_FALSE(here.has_value()); // the frame that was there is gone
545 EXPECT_EQ(Unassigned::alive, 1000);
546 dfs_replace(here, 5);
547 EXPECT_EQ(dfs_frame(here).value, 5);
548 }
549 EXPECT_EQ(Unassigned::alive, 0);
550}
551
552// With frames that can be assigned, Dfs_Here is the frame, which
553// dfs_replace() assigns, as the searches did: the frame that was there
554// stays if building the next one throws
555TEST(DfsHere, AssignsTheFramesThatCanBe)
556{
557 struct Assigned
558 {
559 long value;
560 explicit Assigned(long v) : value(v)
561 {
562 if (v < 0)
563 throw std::runtime_error("built from a negative value");
564 }
565 };
566 static_assert(std::is_same_v<Dfs_Here<Assigned>, Assigned>);
567
568 auto here = dfs_here<Assigned>(1);
569 dfs_replace(here, 2);
570 EXPECT_EQ(dfs_frame(here).value, 2);
571 EXPECT_THROW(dfs_replace(here, -1), std::runtime_error);
572 EXPECT_EQ(dfs_frame(here).value, 2);
573}
A global operator new that a test can make fail.
#define ALEPH_ALLOCATION_FAILURE_UNDER_TSAN
size_t size_t int32_t value
Definition ca-c-api.h:116
Stack of the frames of a depth-first search that does not recurse.
void clear() noexcept
Destroys all the frames, and keeps the room.
size_t capacity() const noexcept
The number of frames that fit in the room of the stack, 0 while it has none.
T & push(Args &&...args)
Builds a frame on top of the stack.
bool is_empty() const noexcept
Whether the stack has no frames.
T & top() noexcept
The top frame; the stack must not be empty.
void release() noexcept
Destroys all the frames, and gives the room back.
size_t size() const noexcept
The number of frames.
void pop() noexcept
Destroys the top frame; the stack must not be empty.
TEST_F(CountedFrames, ClearAndTheDestructorDestroyTheFrames)
#define TEST(name)
void dfs_replace(T &here, Args &&...args)
Replaces the frame that a Dfs_Here holds with one built with args, by assignment.
T & dfs_frame(T &here) noexcept
The frame that a Dfs_Here holds.
bool in_main_thread() noexcept
Main namespace for Aleph-w library functions.
and
Check uniqueness with explicit hash + equality functors.
void next()
Advance all underlying iterators (bounds-checked).
Definition ah-zip.H:236
Counted(int v=0)
Stack of the frames of a depth-first search that does not recurse.