Arene Base
Fundamental Utilities For Safety Critical C++
Loading...
Searching...
No Matches
run_property.hpp
Go to the documentation of this file.
1// Copyright 2026, Toyota Motor Corporation
2//
3// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
4
5#ifndef INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_TESTING_RUN_PROPERTY_HPP_
6#define INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_TESTING_RUN_PROPERTY_HPP_
7
9// IWYU pragma: no_include "arene/base/optional/optional.hpp"
10// IWYU pragma: no_include "arene/base/stdlib_choice/remove_cv.hpp"
11#include "arene/base/stdlib_choice/tuple.hpp"
13#include "arene/base/testing/detail/any_prng_ref.hpp"
14#include "arene/base/testing/detail/property_context.hpp"
20#include "arene/base/tuple/detail/tuple_transform.hpp"
26
27namespace arene {
28namespace base {
29namespace testing {
30namespace run_property_detail {
31
32/// @brief function object that generates a value of a requested type
33/// @tparam Prng PRNG type used to generate a value
34template <class Prng>
35struct generate_argument {
36 /// @brief reference to a PRNG
37 Prng& prng;
38
39 /// @brief generate a value of type @c T
40 /// @tparam T type to generate
41 /// @return generated value of type @c T
42 template <class T>
43 constexpr auto operator()(type_identity<T>) const -> T {
44 return default_distribution<T>(prng);
45 }
46};
47
48} // namespace run_property_detail
49
50/// @brief run a property callable against a battery of trials with generated arguments
51/// @tparam Callable function object with a call operator where the first
52/// parameter is a reference to the PRNG and whose remaining parameters are
53/// generated with @c generator<T>
54/// @param callable property to exercise
55/// @param config seed and stopping criteria for a property run
56///
57/// Repeatedly invokes @p callable with generated arguments until one of the
58/// following terminal conditions is reached:
59/// * a trial reports @c property_status::failure -- the run stops
60/// immediately and the failing @c property_error is captured in @c
61/// property_summary::first_failure;
62/// * the number of discarded trials exceeds @c property_config::max_discards --
63/// the run is reported as failed;
64/// * @c property_config::required_successes successful trials have
65/// been observed -- the run is reported as passing.
66///
67/// @return A @c property_summary describing the outcome: number of successes
68/// and discards, whether the property failed, the seed actually used, and (on
69/// failure) the first failed property assertion.
70///
71/// @note @c Callable is synthesized by the @c ARENE_PROPERTY or
72/// @c ARENE_TYPED_PROPERTY macro. It is not typically user provided.
73template <class Callable>
74auto run_property(Callable callable, property_config const config) -> property_summary {
75 auto prng = prng_xoshiro{config.seed};
76 auto summary = property_summary{0U, 0U, false, config.seed, {}};
77
78 using argument_types = type_lists::apply_all_t< //
80 type_lists::drop_t<member_function_arguments_t<decltype(&Callable::operator())>, 1>, //
81 type_identity>, //
83
84 while ((summary.successes < config.required_successes) && (summary.discards <= config.max_discards)) {
85 auto& outcome = detail::property_context();
86 outcome.reset();
87
88 apply(
89 callable,
90 tuple_cat( //
92 tuple_detail::tuple_transform(
93 argument_types{},
94 run_property_detail::generate_argument<decltype(prng)>{prng}
95 )
96 )
97 );
98
99 if (outcome.has_value()) {
100 using error_code = property_error::error_code;
101 switch (outcome->error) {
102 case error_code::failure: {
103 summary.failed = true;
104 summary.first_failure = outcome;
105 return summary;
106 }
107 case error_code::discard: {
108 ++summary.discards;
109 break;
110 }
111 }
112 } else {
113 ++summary.successes;
114 }
115 }
116
117 summary.failed = summary.successes != config.required_successes;
118 return summary;
119}
120
121} // namespace testing
122} // namespace base
123} // namespace arene
124#endif // INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_TESTING_RUN_PROPERTY_HPP_
pseudorandom number generator implementing the xoshiro256++ algorithm
Definition prng_xoshiro.hpp:33
Polyfill of std::type_identity added in C++20.
Definition type_identity.hpp:28
a heterogeneous, fixed-size collection of values
Definition tuple.hpp:695
Definition customization.hpp:31
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:74
constexpr bitwise_uniform_distribution< T > default_distribution
Customization point selecting the default distribution used to generate values of type T.
typename apply_all< L0, Func >::type apply_all_t
Evaluate a template meta-function, passing the elements of the type-list L0 as the arguments.
Definition apply_all.hpp:55
typename apply_each< TypeList, ToApply >::type apply_each_t
Apply the given template individually to every element of the given type list.
Definition apply_each.hpp:35
typename drop< List, Count >::type drop_t
drops types from the front of a type list
Definition drop.hpp:91
Definition accumulate.hpp:37
function_arguments_t< typename member_pointer_traits< F >::pointee_type > member_function_arguments_t
Obtains the argument types of a member function pointer type.
Definition function_traits.hpp:507
Definition accumulate.hpp:36
configuration for property run
Definition property_config.hpp:29
error_code
Definition property_error.hpp:22
result of a property run
Definition property_summary.hpp:24