Arene Base
Fundamental Utilities For Safety Critical C++
Loading...
Searching...
No Matches
property_macros.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_PROPERTY_MACROS_HPP_
6#define INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_TESTING_PROPERTY_MACROS_HPP_
7
8#include <gtest/gtest.h>
9
11#include "arene/base/testing/detail/any_prng_ref.hpp" // IWYU pragma: keep
12#include "arene/base/testing/detail/property_trampoline.hpp" // IWYU pragma: keep
13#include "arene/base/testing/property_config.hpp" // IWYU pragma: keep
14#include "arene/base/testing/property_summary.hpp" // IWYU pragma: keep
15#include "arene/base/testing/run_property.hpp" // IWYU pragma: keep
16
17// IWYU pragma: no_include "arene/base/optional/optional.hpp"
18// IWYU pragma: no_include "arene/base/testing/property_error.hpp"
19
20/// @brief unique closure type name for a property test
21/// @param TestSuite test suite name
22/// @param TestCase test case name
23/// @return property_closure_<TestSuite>_<TestCase>
24// NOLINTNEXTLINE(cppcoreguidelines-macro-usage)
25#define ARENE_INTERNAL_PROPERTY_CLOSURE_NAME(TestSuite, TestCase) \
26 ARENE_CONCATENATE(ARENE_CONCATENATE(property_closure_, TestSuite), ARENE_CONCATENATE(_, TestCase))
27
28/// @brief three-argument form of @c ARENE_PROPERTY
29/// @param TestSuite test suite name
30/// @param TestCase test case name
31/// @param Params parenthesized parameter list for the property body
32///
33/// Generates the closure type, the GoogleTest/Minitest @c TEST registration,
34/// and the out-of-class definition stub of the property's @c operator(). The user's
35/// braced block that follows the macro invocation becomes the body of that @c
36/// operator().
37// NOLINTNEXTLINE(cppcoreguidelines-macro-usage)
38#define ARENE_INTERNAL_PROPERTY3(TestSuite, TestCase, Params) \
39 struct ARENE_INTERNAL_PROPERTY_CLOSURE_NAME(TestSuite, TestCase) { \
40 ::arene::base::testing::detail::any_prng_ref prng; \
41 auto operator() Params const -> void; \
42 }; \
43 TEST(TestSuite, TestCase) { \
44 auto const summary = ::arene::base::testing::run_property( \
45 ::arene::base::testing::detail::property_trampoline< \
46 ARENE_INTERNAL_PROPERTY_CLOSURE_NAME(TestSuite, TestCase)>{}, \
47 ::arene::base::testing::read_property_config() \
48 ); \
49 if (summary.failed) { \
50 FAIL() << summary; \
51 } \
52 } \
53 auto ARENE_INTERNAL_PROPERTY_CLOSURE_NAME(TestSuite, TestCase)::operator() Params const -> void
54
55/// @brief two-argument form of @c ARENE_PROPERTY
56/// @param TestSuite test suite name
57/// @param TestCase test case name
58///
59/// Forwards to @c ARENE_INTERNAL_PROPERTY3 with an empty parameter list,
60/// producing a property whose body takes no generated arguments.
61// NOLINTNEXTLINE(cppcoreguidelines-macro-usage)
62#define ARENE_INTERNAL_PROPERTY2(TestSuite, TestCase) ARENE_INTERNAL_PROPERTY3(TestSuite, TestCase, ())
63
64/// @brief helper macro to dispatch to the correct internal property macro
65/// @param _1 first positional slot (unused)
66/// @param _2 second positional slot (unused)
67/// @param _3 third positional slot (unused)
68/// @param NAME the selected macro name to return
69/// @param ... any trailing tokens (ignored)
70///
71/// Used as an argument-count dispatcher for @c ARENE_PROPERTY or
72/// @c ARENE_TYPED_PROPERTY. The positional arguments @c _1, @c _2, @c _3, @c NAME
73/// capture tokens from the caller, and the selected @c NAME is returned. The
74/// variadic tail (@c ...) absorbs any additional tokens so that the dispatch works
75/// regardless of overall argument count.
76// NOLINTNEXTLINE(cppcoreguidelines-macro-usage)
77#define ARENE_INTERNAL_PROPERTY_PICK(_1, _2, _3, NAME, ...) NAME
78
79/// @brief define a property-based test
80/// Arguments: test suite name, test case name, and an optional parenthesized parameter list for the property body,
81/// e.g. @c (int a, int b). When omitted, the body takes no generated arguments.
82///
83/// Expands to a @c TEST case whose body is executed many times by @c
84/// ::arene::base::testing::run_property, with pseudorandom arguments drawn
85/// according to @c ::arene::base::testing::read_property_config.
86///
87/// The macro has two forms:
88///
89/// @code{.cpp}
90/// ARENE_PROPERTY(TestSuite, TestCase) { /* body */ }
91/// ARENE_PROPERTY(TestSuite, TestCase, Params) { /* body */ }
92/// @endcode
93///
94/// The first form declares a property with no generated arguments. The
95/// second form accepts a parenthesized parameter list @c Params that
96/// specifies the types (and optional names) of the values the framework
97/// should generate and pass into the body each iteration.
98///
99/// Inside the body, the identifier @c prng refers to a type-erased PRNG, which
100/// may be used to manually produce additional random values if needed.
101///
102/// Example:
103/// @snippet docs/examples/property_examples.cpp arene_property
104// NOLINTNEXTLINE(cppcoreguidelines-macro-usage)
105#define ARENE_PROPERTY(...) \
106 ARENE_INTERNAL_PROPERTY_PICK( \
107 __VA_ARGS__, \
108 ARENE_INTERNAL_PROPERTY3, \
109 ARENE_INTERNAL_PROPERTY2, \
110 unused_token_to_match_ellipses \
111 ) \
112 (__VA_ARGS__)
113
114/// @brief three-argument form of @c ARENE_TYPED_PROPERTY
115/// @param TestSuite typed test suite name
116/// @param TestCase test case name
117/// @param Params parenthesized parameter list for the property body
118///
119/// Generates the closure type template (parameterized by the GoogleTest
120/// @c TypeParam), the @c TYPED_TEST registration, and the out-of-class
121/// definition stub of the property's @c operator(). The user's braced
122/// block that follows the macro invocation becomes the body of that
123/// @c operator(). Inside the body, @c TypeParam refers to the current
124/// type from the type list bound to the typed test suite.
125// NOLINTNEXTLINE(cppcoreguidelines-macro-usage)
126#define ARENE_INTERNAL_TYPED_PROPERTY3(TestSuite, TestCase, Params) \
127 template <class TypeParam> \
128 struct ARENE_INTERNAL_PROPERTY_CLOSURE_NAME(TestSuite, TestCase) { \
129 ::arene::base::testing::detail::any_prng_ref prng; \
130 auto operator() Params const -> void; \
131 }; \
132 TYPED_TEST(TestSuite, TestCase) { \
133 auto const summary = ::arene::base::testing::run_property( \
134 ::arene::base::testing::detail::property_trampoline < \
135 ARENE_INTERNAL_PROPERTY_CLOSURE_NAME(TestSuite, TestCase) < TypeParam >> {}, \
136 ::arene::base::testing::read_property_config() \
137 ); \
138 if (summary.failed) { \
139 FAIL() << summary; \
140 } \
141 } \
142 template <class TypeParam> \
143 auto ARENE_INTERNAL_PROPERTY_CLOSURE_NAME(TestSuite, TestCase)<TypeParam>::operator() Params const -> void
144
145/// @brief two-argument form of @c ARENE_TYPED_PROPERTY
146/// @param TestSuite typed test suite name
147/// @param TestCase test case name
148///
149/// Forwards to @c ARENE_INTERNAL_TYPED_PROPERTY3 with an empty parameter list,
150/// producing a typed property whose body takes no generated arguments.
151// NOLINTNEXTLINE(cppcoreguidelines-macro-usage)
152#define ARENE_INTERNAL_TYPED_PROPERTY2(TestSuite, TestCase) ARENE_INTERNAL_TYPED_PROPERTY3(TestSuite, TestCase, ())
153
154/// @brief define a typed property-based test
155/// Arguments: typed test suite name, test case name, and an optional parenthesized parameter list for the property
156/// body,
157/// e.g. @c (TypeParam a, TypeParam b). When omitted, the body takes no
158/// generated arguments.
159///
160/// Expands to a @c TYPED_TEST case whose body is executed many times by
161/// @c ::arene::base::testing::run_property, with pseudorandom arguments drawn
162/// according to @c ::arene::base::testing::read_property_config. The body is
163/// instantiated for each type in the type list bound to @c TestSuite.
164///
165/// The macro has two forms:
166///
167/// @code{.cpp}
168/// ARENE_TYPED_PROPERTY(TestSuite, TestCase) { /* body */ }
169/// ARENE_TYPED_PROPERTY(TestSuite, TestCase, Params) { /* body */ }
170/// @endcode
171///
172/// The first form declares a typed property with no generated arguments.
173/// The second form accepts a parenthesized parameter list @c Params that
174/// specifies the types (and optional names) of the values the framework
175/// should generate and pass into the body each iteration. The identifier
176/// @c TypeParam is available inside @c Params and the body, referring to
177/// the current type under test.
178///
179/// Inside the body, the identifier @c prng refers to a type-erased PRNG, which
180/// may be used to manually produce additional random values if needed.
181///
182/// Example:
183/// @snippet docs/examples/property_examples.cpp arene_typed_property
184// NOLINTNEXTLINE(cppcoreguidelines-macro-usage)
185#define ARENE_TYPED_PROPERTY(...) \
186 ARENE_INTERNAL_PROPERTY_PICK( \
187 __VA_ARGS__, \
188 ARENE_INTERNAL_TYPED_PROPERTY3, \
189 ARENE_INTERNAL_TYPED_PROPERTY2, \
190 unused_token_to_match_ellipses \
191 ) \
192 (__VA_ARGS__)
193
194/// @brief run an inline property check
195/// @param Description string literal describing the property. Currently
196/// unused by the expansion; reserved for future labeling and reporting.
197/// @param Params parenthesized parameter list for the property body,
198/// e.g. @c (int a, int b). Use @c () for a property that takes no
199/// generated arguments.
200/// @param ... braced block forming the body of the property's @c operator().
201/// The block is passed through as variadic tokens so that it may contain
202/// commas (e.g. inside initializer lists or template arguments).
203/// @return @c proprety_summary of the run
204///
205/// Runs the property body repeatedly with pseudorandom arguments drawn
206/// according to @c ::arene::base::testing::read_property_config, and
207/// yields the resulting @c property_summary.
208///
209/// Unlike @c ARENE_PROPERTY and @c ARENE_TYPED_PROPERTY, this macro does
210/// not register a test case. It is intended for use inside an existing
211/// test body or other context where the caller wants direct access to
212/// the summary, for example to inspect statistics or to assert custom
213/// conditions on the outcome.
214///
215/// Inside the body, the identifier @c prng refers to a type-erased PRNG,
216/// which may be used to manually produce additional random values if
217/// needed.
218///
219/// Example:
220/// @snippet docs/examples/property_examples.cpp arene_prop_check
221// NOLINTNEXTLINE(cppcoreguidelines-macro-usage)
222#define ARENE_PROP_CHECK(Description, Params, ...) \
223 [] { \
224 struct local_proprety_closure { \
225 ::arene::base::testing::detail::any_prng_ref prng; \
226 auto operator() Params const -> void __VA_ARGS__ \
227 }; \
228 return ::arene::base::testing::run_property( \
229 ::arene::base::testing::detail::property_trampoline<local_proprety_closure>{}, \
230 ::arene::base::testing::read_property_config() \
231 ); \
232 }()
233
234#endif // INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_TESTING_PROPERTY_MACROS_HPP_