Aleph-w 7.10.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
ca_observer_allocation_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 <cstddef>
47# include <limits>
48# include <new>
49# include <string>
50# include <utility>
51
52# include <ca-observer.H>
53# include <tpl_array.H>
54# include <tpl_ca_lattice.H>
55# include <tpl_ca_storage.H>
56
57using namespace Aleph;
58using namespace Aleph::CA;
59
60namespace
61{
63
64 // n frames that differ from each other: the frame k has k in a cell
65 Array<L> distinct_frames(const int n)
66 {
67 Array<L> frames;
68 for (int k = 0; k < n; ++k)
69 {
70 L frame({4, 4}, 0);
71 frame.set({0, 0}, k);
72 frames.append(std::move(frame));
73 }
74 return frames;
75 }
76}
77
78// A window of SIZE_MAX keeps every frame, so that the buffers grow with the
79// run. They grew inside push(), which was noexcept: a bad_alloc ended the
80// program instead of reaching the caller of the callback
81TEST(StationaryDetectorAllocation, AFailedBufferingReachesTheCaller)
82{
84 GTEST_SKIP() << "ThreadSanitizer replaces the global operator new";
85
86 constexpr int Frames = 40;
87 const Array<L> frames = distinct_frames(Frames);
88 Stationary_Detector det(std::numeric_limits<std::size_t>::max());
89 det.on_step_begin(0, frames(0));
90 ASSERT_EQ(det.buffered_hashes(), 1u);
91
92 size_t refused = 0;
93 for (int k = 1; k < Frames; ++k)
94 {
95 SCOPED_TRACE("step " + std::to_string(k));
96 const size_t before = det.buffered_hashes();
97 bool threw = false;
98 {
99 const Fail_At failing(1);
100 try
101 {
102 det.on_step_end(k, frames(k));
103 }
104 catch (const std::bad_alloc &)
105 {
106 threw = true;
107 }
108 refused += refused_allocations;
109 }
110 if (threw)
111 {
112 EXPECT_EQ(det.buffered_hashes(), before);
113 det.on_step_end(k, frames(k)); // without the failure, it buffers
114 }
115 else // completed: it must not have caught a failed allocation
116 EXPECT_EQ(refused_allocations, 0u) << "no std::bad_alloc reached the caller";
117 EXPECT_EQ(det.buffered_hashes(), before + 1);
118 EXPECT_FALSE(det.cycle_detected());
119 }
120 EXPECT_GT(refused, 0u) << "the buffers did not grow under a failure";
121
122 // The frame 5 again: each hash kept its step, so the cycle starts at 5
123 det.on_step_end(Frames, frames(5));
124 ASSERT_TRUE(det.cycle_detected());
125 EXPECT_EQ(*det.cycle_start(), 5u);
126 EXPECT_EQ(*det.cycle_length(), static_cast<size_t>(Frames - 5));
127}
128
129// A small window keeps few hashes in buffers that start with room for 32:
130// dropping the oldest hash shrank the buffer of the hashes, which
131// allocates, and a failure there left each hash with the step of the next
132// one, so that a cycle was reported from the wrong step
133TEST(StationaryDetectorAllocation, AFullSmallWindowKeepsEachHashWithItsStep)
134{
136 GTEST_SKIP() << "ThreadSanitizer replaces the global operator new";
137
138 constexpr int Frames = 24;
139 const Array<L> frames = distinct_frames(Frames);
140 for (size_t window = 1; window <= 12; ++window)
141 {
142 SCOPED_TRACE("window " + std::to_string(window));
143 Stationary_Detector det(window);
144 det.on_step_begin(0, frames(0));
145 for (int k = 1; k < Frames; ++k)
146 {
147 bool threw = false;
148 {
149 const Fail_At failing(1);
150 try
151 {
152 det.on_step_end(k, frames(k));
153 }
154 catch (const std::bad_alloc &)
155 {
156 threw = true;
157 }
158 }
159 if (threw)
160 det.on_step_end(k, frames(k)); // without the failure, it buffers
161 else // completed: it must not have caught a failed allocation
162 EXPECT_EQ(refused_allocations, 0u) << "no std::bad_alloc reached the caller";
163 }
164 ASSERT_EQ(det.buffered_hashes(), window + 1);
165 EXPECT_FALSE(det.cycle_detected());
166
167 // A frame of the window again: it was seen at its step
168 const size_t seen = Frames - window;
169 det.on_step_end(Frames, frames(seen));
170 ASSERT_TRUE(det.cycle_detected());
171 EXPECT_EQ(*det.cycle_start(), seen);
172 EXPECT_EQ(*det.cycle_length(), window);
173 }
174}
175
176// A window of 32 whose buffers grow past their first 32 entries with the
177// frame of the step 32: if that buffering fails and the caller goes on
178// without the frame, the full window spans one step more than the 33 of a
179// full window, and its oldest hash, which it would have dropped otherwise,
180// matched as a cycle of 34. A match within the 33 steps is still a cycle,
181// with or without the failure
182TEST(StationaryDetectorAllocation, AFrameLostToAFailureMakesNoLongerCycle)
183{
185 GTEST_SKIP() << "ThreadSanitizer replaces the global operator new";
186
187 constexpr size_t Window = 32;
188 const Array<L> frames = distinct_frames(static_cast<int>(Window) + 2);
189 for (const size_t again : {size_t{0}, size_t{1}}) // the frame seen at the step 34
190 {
191 SCOPED_TRACE("the frame " + std::to_string(again) + " at the step 34");
192 Stationary_Detector det(Window);
193 det.on_step_begin(0, frames(0));
194 size_t lost = 0;
195 for (size_t k = 1; k <= Window + 1; ++k)
196 {
197 const Fail_At failing(k == Window ? 1 : 1000000);
198 try
199 {
200 det.on_step_end(k, frames(k));
201 }
202 catch (const std::bad_alloc &)
203 {
204 ++lost; // the caller goes on without the frame
205 }
206 }
207 ASSERT_EQ(lost, 1u) << "the buffering of the step 32 did not allocate";
208 det.on_step_end(Window + 2, frames(again));
209 if (again == 0) // seen 34 steps before
210 EXPECT_FALSE(det.cycle_detected());
211 else
212 {
213 ASSERT_TRUE(det.cycle_detected());
214 EXPECT_EQ(*det.cycle_start(), 1u);
215 EXPECT_EQ(*det.cycle_length(), Window + 1);
216 }
217 }
218
219 // without the failure, the frame 0 at the step 33 is a cycle of 33
220 Stationary_Detector det(Window);
221 det.on_step_begin(0, frames(0));
222 for (size_t k = 1; k <= Window; ++k)
223 det.on_step_end(k, frames(k));
224 det.on_step_end(Window + 1, frames(0));
225 ASSERT_TRUE(det.cycle_detected());
226 EXPECT_EQ(*det.cycle_start(), 0u);
227 EXPECT_EQ(*det.cycle_length(), Window + 1);
228}
A global operator new that a test can make fail.
#define ALEPH_ALLOCATION_FAILURE_UNDER_TSAN
Phase-7 observability layer for cellular automata engines.
Simple dynamic array with automatic resizing and functional operations.
Definition tpl_array.H:141
T & append(const T &data)
Append a copy of data
Definition tpl_array.H:331
Lattice that adds boundary-aware access on top of a storage.
Detects fixed points and short cycles via frame hashing.
std::size_t buffered_hashes() const noexcept
Return the number of buffered hashes.
std::optional< std::size_t > cycle_start() const noexcept
Return the cycle start step.
bool cycle_detected() const noexcept
Return whether a cycle has been detected.
std::optional< std::size_t > cycle_length() const noexcept
Return the detected cycle length.
void on_step_end(std::size_t step, const Lattice &frame)
Check the completed frame against the recent hash window.
void on_step_begin(const std::size_t step, const Lattice &frame)
Seed the detector with the initial frame hash.
#define TEST(name)
Main namespace for Aleph-w library functions.
The lattice wraps around on every axis.
Definition ca-traits.H:124
static int * k
Dynamic array container with automatic resizing.
Cellular automata lattice with pluggable boundary policies.
Dense, contiguous storage for cellular automata cells (1D/2D/3D).