1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
|
// Copyright (c) 2026 Progmasoft.
// SPDX-License-Identifier: MPL-2.0 WITH AdditionRef-Progmasoft-Exception-1.1
#pragma once
#include <Progmasoft/Catch3/Generator.hpp>
#include <catch2/catch_tostring.hpp>
#include <concepts>
#include <cstddef>
#include <cstdint>
#include <exception>
#include <functional>
#include <optional>
#include <sstream>
#include <string>
#include <tuple>
#include <type_traits>
#include <utility>
#include <vector>
namespace Progmasoft::Catch3
{
/// Controls a property run without changing the compatibility runner's CLI.
struct PropertyOptions final
{
std::size_t Trials = 100;
std::uint64_t Seed = 0xC47C4A53ULL;
std::size_t MaxShrinkSteps = 1000;
/// When set, runs exactly one trial with this seed instead of
/// deriving Trials seeds from Seed. Use the "replay seed" of a
/// reported failure to reproduce that single trial, including its
/// shrinking, without rerunning the trials before it.
std::optional<std::uint64_t> ReplayTrialSeed = std::nullopt;
};
enum class PropertyStatus : std::uint8_t
{
kPassed,
kFailed,
kErrored,
kInvalidConfiguration,
};
struct PropertyFailure final
{
std::uint64_t RootSeed = 0;
std::uint64_t TrialSeed = 0;
std::size_t Trial = 0;
std::size_t ShrinkSteps = 0;
std::string Counterexample;
std::string Reason;
};
/// Stable, machine-readable outcome with a deterministic human-readable
/// report.
struct PropertyResult final
{
PropertyStatus Status = PropertyStatus::kPassed;
std::size_t TrialsRun = 0;
std::optional<PropertyFailure> Failure;
std::string Error;
[[nodiscard]] bool
Succeeded() const noexcept
{
return Status == PropertyStatus::kPassed;
}
[[nodiscard]] std::string
Describe() const
{
std::ostringstream output;
switch (Status)
{
case PropertyStatus::kPassed:
output << "property passed: " << TrialsRun << " trials";
return output.str();
case PropertyStatus::kInvalidConfiguration:
output << "invalid property configuration: " << Error;
return output.str();
case PropertyStatus::kErrored:
output << "property execution errored after " << TrialsRun
<< " trials: " << Error;
break;
case PropertyStatus::kFailed:
output << "property failed after " << TrialsRun
<< " trials";
break;
}
if (Failure.has_value())
{
output << "; failing trial " << Failure->Trial << ", root seed "
<< Failure->RootSeed << ", replay seed "
<< Failure->TrialSeed << ", shrink checks "
<< Failure->ShrinkSteps
<< "\ncounterexample: " << Failure->Counterexample
<< "\nreason: " << Failure->Reason
<< "\nreplay: set PropertyOptions::ReplayTrialSeed to "
<< Failure->TrialSeed;
}
return output.str();
}
};
namespace Detail
{
template<typename ValueType>
[[nodiscard]] std::string
StringifyPropertyValue(const ValueType &value);
/// Renders one counterexample. Scalars use Catch2's StringMaker, so
/// user specializations apply. Tuples, pairs and vectors are
/// rendered here, element by element, because Catch2 only prints
/// tuples when a configuration macro is defined before its headers
/// and a counterexample must not degrade to "{?}" depending on
/// include order.
template<typename ValueType>
struct PropertyValuePrinter final
{
[[nodiscard]] static std::string
Print(const ValueType &value)
{
return Catch::StringMaker<ValueType>::convert(value);
}
};
template<typename... ValueTypes>
struct PropertyValuePrinter<std::tuple<ValueTypes...>> final
{
[[nodiscard]] static std::string
Print(const std::tuple<ValueTypes...> &value)
{
std::string text = "{";
bool first = true;
std::apply(
[&text, &first](const ValueTypes &...component) {
const auto append = [&text, &first](std::string item) {
text += first ? " " : ", ";
text += item;
first = false;
};
(append(StringifyPropertyValue(component)), ...);
},
value);
text += first ? "}" : " }";
return text;
}
};
template<typename FirstType, typename SecondType>
struct PropertyValuePrinter<std::pair<FirstType, SecondType>> final
{
[[nodiscard]] static std::string
Print(const std::pair<FirstType, SecondType> &value)
{
return "{ " + StringifyPropertyValue(value.first) + ", "
+ StringifyPropertyValue(value.second) + " }";
}
};
template<typename ElementType, typename AllocatorType>
struct PropertyValuePrinter<std::vector<ElementType, AllocatorType>>
final
{
[[nodiscard]] static std::string
Print(const std::vector<ElementType, AllocatorType> &value)
{
std::string text = "{";
bool first = true;
for (const auto &element : value)
{
text += first ? " " : ", ";
// The cast turns std::vector<bool>'s proxy into a value.
text += StringifyPropertyValue(
static_cast<ElementType>(element));
first = false;
}
text += first ? "}" : " }";
return text;
}
};
template<typename ValueType>
[[nodiscard]] std::string
StringifyPropertyValue(const ValueType &value)
{
return PropertyValuePrinter<std::remove_cvref_t<ValueType>>::Print(
value);
}
template<typename Predicate, typename ValueType>
[[nodiscard]] std::optional<std::string>
EvaluateProperty(Predicate &predicate, const ValueType &value)
{
#if defined(__cpp_exceptions)
try
{
#endif
if (static_cast<bool>(std::invoke(predicate, value)))
{
return std::nullopt;
}
return "predicate returned false";
#if defined(__cpp_exceptions)
}
catch (const std::exception &exception)
{
return std::string("predicate threw std::exception: ")
+ exception.what();
}
catch (...)
{
return "predicate threw a non-standard exception";
}
#endif
}
template<typename ValueType>
[[nodiscard]] std::string
DescribeValue(const ValueType &value) noexcept
{
#if defined(__cpp_exceptions)
try
{
#endif
return StringifyPropertyValue(value);
#if defined(__cpp_exceptions)
}
catch (const std::exception &exception)
{
return std::string("<stringification failed: ")
+ exception.what() + ">";
}
catch (...)
{
return "<stringification failed with a non-standard exception>";
}
#endif
}
} // namespace Detail
/// Runs independently seeded trials and greedily accepts failing shrink
/// candidates.
template<typename ValueType, typename Predicate>
requires std::invocable<Predicate &, const ValueType &>
&& std::convertible_to<
std::invoke_result_t<Predicate &, const ValueType &>,
bool>
[[nodiscard]] PropertyResult
CheckProperty(const Generator<ValueType> &generator,
Predicate predicate,
PropertyOptions options = {})
{
if (options.Trials == 0)
{
return { PropertyStatus::kInvalidConfiguration,
0,
std::nullopt,
"Trials must be greater than zero" };
}
PropertyResult result;
const std::size_t trials
= options.ReplayTrialSeed.has_value() ? 1 : options.Trials;
for (std::size_t trial = 0; trial < trials; ++trial)
{
result.TrialsRun = trial + 1;
const std::uint64_t trialSeed
= options.ReplayTrialSeed.has_value()
? *options.ReplayTrialSeed
: Random::DeriveSeed(options.Seed, trial);
Random random(trialSeed);
std::optional<ValueType> sample;
#if defined(__cpp_exceptions)
try
{
#endif
sample.emplace(generator.Generate(random));
#if defined(__cpp_exceptions)
}
catch (const std::exception &exception)
{
result.Status = PropertyStatus::kErrored;
result.Error = std::string("generator threw std::exception: ")
+ exception.what();
result.Failure
= PropertyFailure{ options.Seed, trialSeed,
trial + 1, 0,
"<not generated>", result.Error };
return result;
}
catch (...)
{
result.Status = PropertyStatus::kErrored;
result.Error = "generator threw a non-standard exception";
result.Failure
= PropertyFailure{ options.Seed, trialSeed,
trial + 1, 0,
"<not generated>", result.Error };
return result;
}
#endif
auto reason = Detail::EvaluateProperty(predicate, *sample);
if (!reason.has_value())
{
continue;
}
std::size_t shrinkChecks = 0;
#if defined(__cpp_exceptions)
try
{
#endif
bool improved = true;
while (improved && shrinkChecks < options.MaxShrinkSteps)
{
improved = false;
const std::vector<ValueType> candidates
= generator.Shrink(*sample);
for (std::size_t candidateIndex = 0;
candidateIndex < candidates.size();
++candidateIndex)
{
if (shrinkChecks >= options.MaxShrinkSteps)
{
break;
}
++shrinkChecks;
const ValueType candidate = static_cast<ValueType>(
candidates[candidateIndex]);
auto candidateReason
= Detail::EvaluateProperty(predicate, candidate);
if (candidateReason.has_value())
{
sample.emplace(candidate);
reason = std::move(candidateReason);
improved = true;
break;
}
}
}
#if defined(__cpp_exceptions)
}
catch (const std::exception &exception)
{
result.Status = PropertyStatus::kErrored;
result.Error = std::string("shrinker threw std::exception: ")
+ exception.what();
result.Failure
= PropertyFailure{ options.Seed,
trialSeed,
trial + 1,
shrinkChecks,
Detail::DescribeValue(*sample),
*reason };
return result;
}
catch (...)
{
result.Status = PropertyStatus::kErrored;
result.Error = "shrinker threw a non-standard exception";
result.Failure
= PropertyFailure{ options.Seed,
trialSeed,
trial + 1,
shrinkChecks,
Detail::DescribeValue(*sample),
*reason };
return result;
}
#endif
result.Status = PropertyStatus::kFailed;
result.Failure = PropertyFailure{ options.Seed,
trialSeed,
trial + 1,
shrinkChecks,
Detail::DescribeValue(*sample),
*reason };
return result;
}
result.Status = PropertyStatus::kPassed;
return result;
}
} // namespace Progmasoft::Catch3
|