42#include <gtest/gtest.h>
56#ifdef ALEPH_DFS_STACK_POISON
57# include <sanitizer/asan_interface.h>
68 static inline long alive = 0;
69 static inline long throw_at = -1;
76 throw std::runtime_error(
"built from throw_at");
83 if (value == throw_at)
84 throw std::runtime_error(
"copied from throw_at");
94 static inline long alive = 0;
95 static inline long throw_at = -1;
99 explicit Move_Only(
long v) :
value(v) { ++alive; }
101 Move_Only(Move_Only && other) :
value(other.
value)
103 if (value == throw_at)
104 throw std::runtime_error(
"moved from throw_at");
109 Move_Only(
const Move_Only &) =
delete;
111 ~Move_Only() { --alive; }
115 struct CountedFrames :
public ::testing::Test
117 void SetUp()
override
120 Counted::throw_at = -1;
123 void TearDown()
override { Counted::throw_at = -1; }
127TEST(DfsStack, PopsInTheReverseOrderOfThePushes)
131 EXPECT_EQ(stack.
size(), 0u);
132 for (
long i = 0; i < 10000; ++i)
134 long & frame = stack.
push(i);
136 EXPECT_EQ(&frame, &stack.
top());
138 EXPECT_EQ(stack.
size(), 10000u);
139 for (
long i = 9999; i >= 0; --i)
141 ASSERT_EQ(stack.
top(), i);
147TEST(DfsStack, GrowingKeepsTheFrames)
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)
155 ASSERT_EQ(stack.
top(), std::to_string(i) +
" is a frame long enough not to fit inline");
160TEST_F(CountedFrames, ClearAndTheDestructorDestroyTheFrames)
164 for (
long i = 0; i < 300; ++i)
166 EXPECT_EQ(Counted::alive, 300);
168 EXPECT_EQ(Counted::alive, 0);
170 for (
long i = 0; i < 100; ++i)
172 EXPECT_EQ(Counted::alive, 100);
174 EXPECT_EQ(Counted::alive, 0);
177TEST_F(CountedFrames, AFrameThatThrowsLeavesTheStackAsItWas)
180 for (
long i = 0; i < 10; ++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);
191TEST_F(CountedFrames, ACopyThatThrowsWhileGrowingLeavesTheStackAsItWas)
194 for (
long i = 0; i < 10; ++i)
197 Counted::throw_at = 5;
200 while (not thrown
and next < 10000000)
206 catch (
const std::runtime_error &)
210 Counted::throw_at = -1;
213 ASSERT_EQ(stack.
size(),
size_t(
next));
214 EXPECT_EQ(Counted::alive,
next);
215 for (
long i =
next - 1; i >= 0; --i)
217 ASSERT_EQ(stack.
top().value, i);
220 EXPECT_EQ(Counted::alive, 0);
225TEST(DfsStack, AMoveThatThrowsWhileGrowingFreesTheNewRoom)
227 Move_Only::alive = 0;
230 for (
long i = 0; i < 10; ++i)
233 Move_Only::throw_at = 5;
236 while (not thrown
and next < 10000000)
242 catch (
const std::runtime_error &)
246 Move_Only::throw_at = -1;
249 EXPECT_EQ(stack.
size(),
size_t(
next));
250 EXPECT_EQ(stack.
top().value,
next - 1);
251 EXPECT_EQ(Move_Only::alive,
next);
253 EXPECT_EQ(Move_Only::alive, 0);
256TEST(DfsStack, TheCapacityIsThatOfTheRoom)
260 for (
long i = 0; i < 5000; ++i)
265 const size_t capacity = stack.
capacity();
267 EXPECT_EQ(stack.
capacity(), capacity);
270TEST_F(CountedFrames, ReleaseDestroysTheFramesAndGivesTheRoomBack)
273 for (
long i = 0; i < 300; ++i)
276 EXPECT_EQ(Counted::alive, 0);
282 for (
long i = 0; i < 10; ++i)
284 EXPECT_EQ(stack.
size(), 10u);
285 EXPECT_EQ(stack.
top().value, 9);
288TEST(DfsStack, MovesTakeTheFrames)
291 for (
long i = 0; i < 1000; ++i)
296 ASSERT_EQ(b.
size(), 1000u);
297 EXPECT_EQ(b.
top(), 999);
303 ASSERT_EQ(c.
size(), 1000u);
304 EXPECT_EQ(c.
top(), 999);
309 EXPECT_EQ(a.
top(), 5);
310 EXPECT_EQ(b.
top(), 6);
316 struct DfsStackRoom :
public ::testing::Test
318 void SetUp()
override
321 GTEST_SKIP() <<
"ThreadSanitizer replaces the global operator new";
326 void keep_room_for(
size_t n)
329 for (
size_t i = 0; i < n; ++i)
335TEST_F(DfsStackRoom, AStackOfTheSameThreadReusesTheRoom)
337 keep_room_for(20000);
341 for (
long i = 0; i < 20000; ++i)
342 ASSERT_NO_THROW(stack.
push(i));
344 EXPECT_EQ(refused_allocations, 0u);
347TEST_F(DfsStackRoom, AStackOfAnotherThreadHasNoRoom)
349 keep_room_for(20000);
351 std::thread other([&thrown]
359 catch (
const std::bad_alloc &)
373 keep_room_for(20000);
380 ASSERT_GE(kept, 20000u);
382 keep_room_for(1024 * 1024);
385 for (
size_t i = 0; i < kept; ++i)
386 ASSERT_NO_THROW(stack.
push(
long(i)));
388 EXPECT_THROW(stack.
push(-1), std::bad_alloc);
389 EXPECT_EQ(refused_allocations, 1u);
394TEST_F(DfsStackRoom, TheRoomTakenFromTheThreadGoesBackWhenTheStackGrows)
396 size_t kept = 0, next_capacity = 0;
398 std::thread other([&]
412 next_capacity =
next.capacity();
414 catch (
const std::bad_alloc &)
420 EXPECT_FALSE(thrown);
421 EXPECT_EQ(next_capacity, kept);
425TEST_F(DfsStackRoom, ARoomReleasedIsReused)
428 for (
long i = 0; i < 20000; ++i)
430 const size_t capacity = stack.
capacity();
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);
443TEST(DfsStack, TheThreadOfMainIsToldByItsId)
445 bool main_is_told =
false;
446#if defined(__GLIBC__)
447# if __GLIBC_PREREQ(2, 30)
452 bool other_is_main =
true;
455 EXPECT_FALSE(other_is_main);
461TEST(DfsStack, TheRoomWithoutFramesIsPoisoned)
463#ifdef ALEPH_DFS_STACK_POISON
464 long * first =
nullptr;
467 for (
long i = 0; i < 1000; ++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));
474 EXPECT_TRUE(__asan_address_is_poisoned(top));
475 EXPECT_FALSE(__asan_address_is_poisoned(top - 1));
477 EXPECT_TRUE(__asan_address_is_poisoned(first));
479 EXPECT_FALSE(__asan_address_is_poisoned(first));
482 EXPECT_TRUE(__asan_address_is_poisoned(first));
485 EXPECT_EQ(&
next.top(), first);
486 EXPECT_FALSE(__asan_address_is_poisoned(first));
487 EXPECT_TRUE(__asan_address_is_poisoned(first + 1));
489 GTEST_SKIP() <<
"only under AddressSanitizer with ALEPH_ANNOTATE_CONTAINERS";
499 static inline long alive = 0;
503 explicit Unassigned(
long v) :
value(v)
506 throw std::runtime_error(
"built from a negative value");
510 Unassigned(
const Unassigned & other) :
value(other.
value) { ++alive; }
512 ~Unassigned() { --alive; }
515 static_assert(not std::is_move_assignable_v<Unassigned>);
520TEST(DfsHere, ReplacesAFrameThatCannotBeAssigned)
522 static_assert(std::is_same_v<Dfs_Here<Unassigned>, std::optional<Unassigned>>);
523 Unassigned::alive = 0;
525 auto here = dfs_here<Unassigned>(1);
527 EXPECT_EQ(Unassigned::alive, 1);
530 EXPECT_EQ(Unassigned::alive, 1);
534 for (
long i = 0; i < 1000; ++i)
539 EXPECT_EQ(Unassigned::alive, 1001);
540 EXPECT_EQ(stack.
top().value, 1001);
543 EXPECT_THROW(
dfs_replace(here, -1), std::runtime_error);
544 EXPECT_FALSE(here.has_value());
545 EXPECT_EQ(Unassigned::alive, 1000);
549 EXPECT_EQ(Unassigned::alive, 0);
555TEST(DfsHere, AssignsTheFramesThatCanBe)
560 explicit Assigned(
long v) :
value(v)
563 throw std::runtime_error(
"built from a negative value");
566 static_assert(std::is_same_v<Dfs_Here<Assigned>, Assigned>);
568 auto here = dfs_here<Assigned>(1);
571 EXPECT_THROW(
dfs_replace(here, -1), std::runtime_error);
A global operator new that a test can make fail.
#define ALEPH_ALLOCATION_FAILURE_UNDER_TSAN
size_t size_t int32_t value
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)
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).
Stack of the frames of a depth-first search that does not recurse.