Arene Base
Fundamental Utilities For Safety Critical C++
Loading...
Searching...
No Matches
conditional_assertions.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///
6/// @file conditional_assertions.hpp
7/// @brief Macros to help define assertions that are conditionally static depending on the test's type parameter
8///
9
10#ifndef INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_TESTING_GTEST_CONDITIONAL_ASSERTIONS_HPP_
11#define INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_TESTING_GTEST_CONDITIONAL_ASSERTIONS_HPP_
12
13// IWYU pragma: private
14// IWYU pragma: friend "arene/base/testing/gtest/.*"
15
16#include <gtest/gtest.h>
17
18// parasoft-begin-suppress AUTOSAR-A16_2_2-a-2 "Arene Base aggregate headers permitted by A16-2-2 Permit #1"
22
23#if ARENE_IS_ON(ARENE_GTEST_STATIC_ASSERTIONS)
24#include "arene/base/testing/gtest/detail/conditional_assertions_with_static.hpp" // IWYU pragma: export
25#else
26#include "arene/base/testing/gtest/detail/conditional_assertions_without_static.hpp" // IWYU pragma: export
27#endif
28// parasoft-end-suppress AUTOSAR-A16_2_2-a-2
29
30// NOLINTBEGIN(cppcoreguidelines-macro-usage) This code interacts with Google Test's macros so it needs to be too.
31// parasoft-begin-suppress AUTOSAR-A16_0_1-d "Defining these macros is necessary because Google Test uses macros"
32// parasoft-begin-suppress CERT_C-PRE30-a "Token concatenation is necessary because Google Test does it too"
33// parasoft-begin-suppress AUTOSAR-M16_0_6-a "Some args can not be parenthesized to preserve decltype(auto)"
34
35/// @brief Call a Google Test unary assertion macro at runtime, and statically if @c TypeParam is compatible
36/// @param assertion_type The @c ::arene::base::testing::gtest_operation value specifying the assertion to use
37/// @param ... The condition to assert; should not contain references to non-constexpr variables.
38#define COND_STATIC_ASSERT_UNARY(assertion_type, ...) \
39 struct ARENE_CONCATENATE(condition_impl_, __LINE__) { \
40 constexpr static auto body() -> decltype(auto) { return __VA_ARGS__; } \
41 }; \
42 static_cast<void>((ARENE_CONDITIONAL_ASSERTER((assertion_type))::do_assert()))
43
44/// @brief Call Google Test @c ASSERT_TRUE at runtime, and also statically if @c TypeParam is compatible
45/// @param ... The condition to assert as true; should not contain references to non-constexpr variables.
46#define COND_STATIC_ASSERT_TRUE(...) \
47 COND_STATIC_ASSERT_UNARY(::arene::base::testing::gtest_operation::assert_true, __VA_ARGS__)
48
49/// @brief Call Google Test @c ASSERT_FALSE at runtime, and also statically if @c TypeParam is compatible
50/// @param ... The condition to assert as false; should not contain references to non-constexpr variables.
51#define COND_STATIC_ASSERT_FALSE(...) \
52 COND_STATIC_ASSERT_UNARY(::arene::base::testing::gtest_operation::assert_false, __VA_ARGS__)
53
54/// @brief Call a Google Test binary assertion macro at runtime, and statically if @c TypeParam is compatible
55/// @param left_arg Left half of the assertion; should not contain references to non-constexpr variables
56/// @param right_arg Right half of the assertion; should not contain references to non-constexpr variables
57/// @param assertion_type The @c ::arene::base::testing::gtest_operation value specifying the assertion to use
58#define COND_STATIC_ASSERT_BINARY(left_arg, right_arg, assertion_type) \
59 struct ARENE_CONCATENATE(condition_impl_, __LINE__) { \
60 constexpr static auto left() -> decltype(auto) { return left_arg; } \
61 constexpr static auto right() -> decltype(auto) { return right_arg; } \
62 }; \
63 static_cast<void>((ARENE_CONDITIONAL_ASSERTER((assertion_type))::do_assert()))
64
65/// @brief Call Google Test @c ASSERT_EQ at runtime, and also statically if @c TypeParam is compatible
66/// @param left_arg Left half of the assertion; should not contain references to non-constexpr variables
67/// @param right_arg Right half of the assertion; should not contain references to non-constexpr variables
68#define COND_STATIC_ASSERT_EQ(left_arg, right_arg) \
69 COND_STATIC_ASSERT_BINARY(left_arg, right_arg, ::arene::base::testing::gtest_operation::assert_eq)
70
71/// @brief Call Google Test @c ASSERT_NE at runtime, and also statically if @c TypeParam is compatible
72/// @param left_arg Left half of the assertion; should not contain references to non-constexpr variables
73/// @param right_arg Right half of the assertion; should not contain references to non-constexpr variables
74#define COND_STATIC_ASSERT_NE(left_arg, right_arg) \
75 COND_STATIC_ASSERT_BINARY(left_arg, right_arg, ::arene::base::testing::gtest_operation::assert_ne)
76
77/// @brief Call Google Test @c ASSERT_LT at runtime, and also statically if @c TypeParam is compatible
78/// @param left_arg Left half of the assertion; should not contain references to non-constexpr variables
79/// @param right_arg Right half of the assertion; should not contain references to non-constexpr variables
80#define COND_STATIC_ASSERT_LT(left_arg, right_arg) \
81 COND_STATIC_ASSERT_BINARY(left_arg, right_arg, ::arene::base::testing::gtest_operation::assert_lt)
82
83/// @brief Call Google Test @c ASSERT_LE at runtime, and also statically if @c TypeParam is compatible
84/// @param left_arg Left half of the assertion; should not contain references to non-constexpr variables
85/// @param right_arg Right half of the assertion; should not contain references to non-constexpr variables
86#define COND_STATIC_ASSERT_LE(left_arg, right_arg) \
87 COND_STATIC_ASSERT_BINARY(left_arg, right_arg, ::arene::base::testing::gtest_operation::assert_le)
88
89/// @brief Call Google Test @c ASSERT_GT at runtime, and also statically if @c TypeParam is compatible
90/// @param left_arg Left half of the assertion; should not contain references to non-constexpr variables
91/// @param right_arg Right half of the assertion; should not contain references to non-constexpr variables
92#define COND_STATIC_ASSERT_GT(left_arg, right_arg) \
93 COND_STATIC_ASSERT_BINARY(left_arg, right_arg, ::arene::base::testing::gtest_operation::assert_gt)
94
95/// @brief Call Google Test @c ASSERT_GE at runtime, and also statically if @c TypeParam is compatible
96/// @param left_arg Left half of the assertion; should not contain references to non-constexpr variables
97/// @param right_arg Right half of the assertion; should not contain references to non-constexpr variables
98#define COND_STATIC_ASSERT_GE(left_arg, right_arg) \
99 COND_STATIC_ASSERT_BINARY(left_arg, right_arg, ::arene::base::testing::gtest_operation::assert_ge)
100
101/// @brief Call the Google Test suite instantiation macro with the given arguments
102/// This exists to avoid having more than one @c ## in a single macro, since concatenation is needed for Google Test
103/// The extra comma in the Google macro is part of its undocumented name generator API, which we don't use.
104/// @param Prefix The prefix to use for this instantiation of the tests; passed directly to Google Test
105/// @param SuiteBaseName The name of the suite to instantiate *without* the @c Test , @c DeathTest , etc. at the end
106/// @param SuiteSuffix The suffix which is combined with @c SuiteBaseName to create the Google Test suite name
107/// @param Types The name of a Google Test type list containing the types for which @c SuiteName should be instantiated
108#define ARENE_INSTANTIATE_TYPED_TEST_SUITE_P(Prefix, SuiteBaseName, SuiteSuffix, Types) \
109 INSTANTIATE_TYPED_TEST_SUITE_P(Prefix, SuiteBaseName##SuiteSuffix, Types, )
110
111/// @brief Instantiate the Arene Base parameterized tests for the given suite with the given types.
112/// Always instantiates the general and death tests, and also the constexpr-only tests for compatible types.
113/// @param Prefix The prefix to use for this instantiation of the tests; passed directly to Google Test
114/// @param SuiteBaseName The name of the suite to instantiate *without* the @c Test , @c DeathTest , etc. at the end
115/// @param Types The name of a Google Test type list containing the types for which @c SuiteName should be instantiated
116#define ARENE_INSTANTIATE_TESTS(Prefix, SuiteBaseName, Types) \
117 namespace { \
118 /* NOLINTNEXTLINE(google-build-using-namespace) Google Test puts a lot of names in this namespace */ \
119 using namespace ::arene::base::testing; \
120 ARENE_INSTANTIATE_TYPED_TEST_SUITE_P(Prefix, SuiteBaseName, Test, Types); \
121 ARENE_INSTANTIATE_TYPED_TEST_SUITE_P(Prefix, SuiteBaseName, DeathTest, Types); \
122 } /* namespace */ \
123 static_assert(true, "Dummy static assert to absorb the semicolon after invoking the macro")
124
125// parasoft-end-suppress AUTOSAR-M16_0_6-a
126// parasoft-end-suppress CERT_C-PRE30-a
127// parasoft-end-suppress AUTOSAR-A16_0_1-d
128// NOLINTEND(cppcoreguidelines-macro-usage)
129
130#endif // INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_TESTING_GTEST_CONDITIONAL_ASSERTIONS_HPP_
Compile time configuration for the static assertions macros.