Arene Base
Fundamental Utilities For Safety Critical C++
Loading...
Searching...
No Matches
run_property.hpp
Go to the documentation of this file.
1#ifndef INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_TESTING_RUN_PROPERTY_HPP_
2#define INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_TESTING_RUN_PROPERTY_HPP_
3
4#include "arene/base/functional/function_traits.hpp"
5// IWYU pragma: no_include "arene/base/optional/optional.hpp"
6// IWYU pragma: no_include "arene/base/stdlib_choice/remove_cv.hpp"
7#include "arene/base/stdlib_choice/tuple.hpp"
8#include "arene/base/testing/detail/property_context.hpp"
9#include "arene/base/testing/prng_xoshiro.hpp"
10#include "arene/base/testing/property_config.hpp"
11#include "arene/base/testing/property_error.hpp"
12#include "arene/base/testing/property_summary.hpp"
13#include "arene/base/tuple/apply.hpp"
14#include "arene/base/tuple/detail/tuple_transform.hpp"
15#include "arene/base/tuple/tuple_cat.hpp"
16#include "arene/base/type_list/apply_all.hpp"
17#include "arene/base/type_list/apply_each.hpp"
18#include "arene/base/type_list/drop.hpp"
19#include "arene/base/type_traits/type_identity.hpp"
20
21namespace arene {
22namespace base {
23namespace testing {
24namespace run_property_detail {
25
26/// @brief function object that generates a value of a requested type
27/// @tparam Prng PRNG type used to generate a value
28template <class Prng>
29struct generate_argument {
30 /// @brief reference to a PRNG
31 Prng& prng;
32
33 /// @brief generate a value of type @c T
34 /// @tparam T type to generate
35 /// @return generated value of type @c T
36 template <class T>
37 constexpr auto operator()(type_identity<T>) const -> T {
38 // TODO: use prng to generate the argument
39 return {};
40 }
41};
42
43} // namespace run_property_detail
44
45/// @brief run a property callable against a battery of trials with generated arguments
46/// @tparam Callable function object with a call operator where the first
47/// parameter is a reference to the PRNG and whose remaining parameters are
48/// generated with @c generator<T>
49/// @param callable property to exercise
50/// @param config seed and stopping criteria for a property run
51///
52/// Repeatedly invokes @p callable with generated arguments until one of the
53/// following terminal conditions is reached:
54/// * a trial reports @c property_status::failure -- the run stops
55/// immediately and the failing @c property_error is captured in @c
56/// property_summary::first_failure;
57/// * the number of discarded trials exceeds @c property_config::max_discards --
58/// the run is reported as failed;
59/// * @c property_config::required_successes successful trials have
60/// been observed -- the run is reported as passing.
61///
62/// @return A @c property_summary describing the outcome: number of successes
63/// and discards, whether the property failed, the seed actually used, and (on
64/// failure) the first failed property assertion.
65///
66/// @note @c Callable is synthesized by the @c ARENE_PROPERTY or
67/// @c ARENE_TYPED_PROPERTY macro. It is not typically user provided.
68template <class Callable>
69auto run_property(Callable callable, property_config const config) -> property_summary {
70 auto prng = prng_xoshiro{config.seed};
71 auto summary = property_summary{0U, 0U, false, config.seed, {}};
72
73 using argument_types = type_lists::apply_all_t< //
74 type_lists::apply_each_t< //
75 type_lists::drop_t<member_function_arguments_t<decltype(&Callable::operator())>, 1>, //
76 type_identity>, //
77 std::tuple>;
78
79 while ((summary.successes < config.required_successes) && (summary.discards <= config.max_discards)) {
80 auto& outcome = detail::property_context();
81 outcome.reset();
82
83 apply(
84 callable,
85 tuple_cat( //
86 std::tuple<decltype(prng)&>{prng},
87 tuple_detail::tuple_transform(
88 argument_types{},
89 run_property_detail::generate_argument<decltype(prng)>{prng}
90 )
91 )
92 );
93
94 if (outcome.has_value()) {
95 using error_code = property_error::error_code;
96 switch (outcome->error) {
97 case error_code::failure: {
98 summary.failed = true;
99 summary.first_failure = outcome;
100 return summary;
101 }
102 case error_code::discard: {
103 ++summary.discards;
104 break;
105 }
106 }
107 } else {
108 ++summary.successes;
109 }
110 }
111
112 summary.failed = summary.successes != config.required_successes;
113 return summary;
114}
115
116} // namespace testing
117} // namespace base
118} // namespace arene
119#endif // INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_TESTING_RUN_PROPERTY_HPP_
Definition customization.hpp:36
auto run_property(Callable callable, property_config const config) -> property_summary
run a property callable against a battery of trials with generated arguments
Definition run_property.hpp:69
Definition array_exceptions_disabled.cpp:11
Copyright 2026, Toyota Motor Corporation.
Definition array_exceptions_disabled.cpp:10