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