C/C++ API Reference
Loading...
Searching...
No Matches
constexpr.h
1// Copyright 2024 The Pigweed Authors
2//
3// Licensed under the Apache License, Version 2.0 (the "License"); you may not
4// use this file except in compliance with the License. You may obtain a copy of
5// the License at
6//
7// https://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12// License for the specific language governing permissions and limitations under
13// the License.
14#pragma once
15
16#include <limits>
17
18#include "lib/stdcompat/type_traits.h"
19#include "pw_preprocessor/boolean.h"
20#include "pw_preprocessor/concat.h"
21
23
43#define PW_CONSTEXPR_TEST(test_suite, test_name, ...) \
44 _PW_IF_CONSTEXPR_TEST(constexpr) \
45 void PwConstexprTest_##test_suite##_##test_name() __VA_ARGS__ \
46 \
47 TEST(test_suite, test_name) { \
48 PwConstexprTest_##test_suite##_##test_name(); \
49 } \
50 \
51 static_assert([] { \
52 _PW_IF_CONSTEXPR_TEST(PwConstexprTest_##test_suite##_##test_name();) \
53 return true; \
54 }())
55
62#ifdef __clang__
63#define PW_CONSTEXPR_TEST_IF_CLANG PW_CONSTEXPR_TEST
64#else
65#define PW_CONSTEXPR_TEST_IF_CLANG(test_suite, test_case, ...) \
66 TEST(test_suite, test_case) \
67 __VA_ARGS__ \
68 static_assert(true)
69#endif // __clang__
70
76#if !defined(__clang__) && defined(__GNUC__)
77#define PW_CONSTEXPR_TEST_IF_GCC PW_CONSTEXPR_TEST
78#else
79#define PW_CONSTEXPR_TEST_IF_GCC(test_suite, test_case, ...) \
80 TEST(test_suite, test_case) \
81 __VA_ARGS__ \
82 static_assert(true)
83#endif // !defined(__clang__) && defined(__GNUC__)
84
85// GoogleTest-style test macros for PW_CONSTEXPR_TEST.
86
87#define PW_TEST_EXPECT_TRUE(expr) _PW_CEXPECT(TRUE, expr)
88#define PW_TEST_EXPECT_FALSE(expr) _PW_CEXPECT(FALSE, expr)
89
90#define PW_TEST_EXPECT_EQ(lhs, rhs) _PW_CEXPECT(EQ, lhs, rhs)
91#define PW_TEST_EXPECT_NE(lhs, rhs) _PW_CEXPECT(NE, lhs, rhs)
92#define PW_TEST_EXPECT_GT(lhs, rhs) _PW_CEXPECT(GT, lhs, rhs)
93#define PW_TEST_EXPECT_GE(lhs, rhs) _PW_CEXPECT(GE, lhs, rhs)
94#define PW_TEST_EXPECT_LT(lhs, rhs) _PW_CEXPECT(LT, lhs, rhs)
95#define PW_TEST_EXPECT_LE(lhs, rhs) _PW_CEXPECT(LE, lhs, rhs)
96
97#define PW_TEST_EXPECT_NEAR(lhs, rhs, error) _PW_CEXPECT(NEAR, lhs, rhs, error)
98#define PW_TEST_EXPECT_FLOAT_EQ(lhs, rhs) _PW_CEXPECT(FLOAT_EQ, lhs, rhs)
99#define PW_TEST_EXPECT_DOUBLE_EQ(lhs, rhs) _PW_CEXPECT(DOUBLE_EQ, lhs, rhs)
100
101#define PW_TEST_EXPECT_STREQ(lhs, rhs) _PW_CEXPECT(STREQ, lhs, rhs)
102#define PW_TEST_EXPECT_STRNE(lhs, rhs) _PW_CEXPECT(STRNE, lhs, rhs)
103
104#define PW_TEST_ASSERT_TRUE(expr) _PW_CASSERT(TRUE, expr)
105#define PW_TEST_ASSERT_FALSE(expr) _PW_CASSERT(FALSE, expr)
106
107#define PW_TEST_ASSERT_EQ(lhs, rhs) _PW_CASSERT(EQ, lhs, rhs)
108#define PW_TEST_ASSERT_NE(lhs, rhs) _PW_CASSERT(NE, lhs, rhs)
109#define PW_TEST_ASSERT_GT(lhs, rhs) _PW_CASSERT(GT, lhs, rhs)
110#define PW_TEST_ASSERT_GE(lhs, rhs) _PW_CASSERT(GE, lhs, rhs)
111#define PW_TEST_ASSERT_LT(lhs, rhs) _PW_CASSERT(LT, lhs, rhs)
112#define PW_TEST_ASSERT_LE(lhs, rhs) _PW_CASSERT(LE, lhs, rhs)
113
114#define PW_TEST_ASSERT_NEAR(lhs, rhs, error) _PW_CASSERT(NEAR, lhs, rhs, error)
115#define PW_TEST_ASSERT_FLOAT_EQ(lhs, rhs) _PW_CASSERT(FLOAT_EQ, lhs, rhs)
116#define PW_TEST_ASSERT_DOUBLE_EQ(lhs, rhs) _PW_CASSERT(DOUBLE_EQ, lhs, rhs)
117
118#define PW_TEST_ASSERT_STREQ(lhs, rhs) _PW_CASSERT(STREQ, lhs, rhs)
119#define PW_TEST_ASSERT_STRNE(lhs, rhs) _PW_CASSERT(STRNE, lhs, rhs)
120
122
123// Implementation details
124
125#define _PW_CEXPECT(macro, ...) \
126 if (cpp20::is_constant_evaluated()) { \
127 ::pw::unit_test::internal::Constexpr_EXPECT_##macro(__VA_ARGS__); \
128 } else \
129 EXPECT_##macro(__VA_ARGS__)
130
131#define _PW_CASSERT(macro, ...) \
132 if (cpp20::is_constant_evaluated()) { \
133 if (!::pw::unit_test::internal::Constexpr_EXPECT_##macro(__VA_ARGS__)) { \
134 return; \
135 } \
136 } else \
137 ASSERT_##macro(__VA_ARGS__)
138
139// Expands to the provided expression if constexpr tests are enabled.
140// block-submission: disable
141#define _PW_IF_CONSTEXPR_TEST(a) \
142 PW_CONCAT( \
143 _PW_IF_CONSTEXPR_TEST_, \
144 PW_AND(LIB_STDCOMPAT_CONSTEVAL_SUPPORT, \
145 _PW_CONSTEXPR_TEST_ENABLED(SKIP_CONSTEXPR_TESTS_DONT_SUBMIT)))(a)
146
147#define _PW_IF_CONSTEXPR_TEST_0(a)
148#define _PW_IF_CONSTEXPR_TEST_1(a) a
149
150#define _PW_CONSTEXPR_TEST_ENABLED(a) _PW_CONSTEXPR_TEST_ENABLED2(a)
151#define _PW_CONSTEXPR_TEST_ENABLED2(a) _PW_CONSTEXPR_TEST_##a
152
153// Check if the SKIP_CONSTEXPR_TESTS_DONT_SUBMIT macro is defined, which can
154// be used to temporarily disable the constexpr part of tests.
155#define _PW_CONSTEXPR_TEST_SKIP_CONSTEXPR_TESTS_DONT_SUBMIT 1
156#define _PW_CONSTEXPR_TEST_ 0
157#define _PW_CONSTEXPR_TEST_1 0
158#define _PW_CONSTEXPR_TEST_0 \
159 _Do_not_define_SKIP_CONSTEXPR_TESTS_DONT_SUBMIT_to_0_remove_it_instead
160// block-submission: enable
161
162namespace pw::unit_test::internal {
163
164// These undefined, non-constexpr functions are called when a constexpr test
165// assertion fails. They appear in the error message to inform the user.
166bool EXPECT_TRUE_FAILED();
167bool EXPECT_FALSE_FAILED();
168bool EXPECT_EQ_FAILED();
169bool EXPECT_NE_FAILED();
170bool EXPECT_GT_FAILED();
171bool EXPECT_GE_FAILED();
172bool EXPECT_LT_FAILED();
173bool EXPECT_LE_FAILED();
174bool EXPECT_NEAR_FAILED();
175bool EXPECT_STREQ_FAILED();
176bool EXPECT_STRNE_FAILED();
177
178template <typename T>
179constexpr bool Constexpr_EXPECT_TRUE(const T& expr) {
180 return (expr) ? true : EXPECT_TRUE_FAILED();
181}
182
183template <typename T>
184constexpr bool Constexpr_EXPECT_FALSE(const T& expr) {
185 return !(expr) ? true : EXPECT_FALSE_FAILED();
186}
187
188template <typename Lhs, typename Rhs>
189constexpr bool Constexpr_EXPECT_EQ(const Lhs& lhs, const Rhs& rhs) {
190 return (lhs == rhs) ? true : EXPECT_EQ_FAILED();
191}
192
193template <typename Lhs, typename Rhs>
194constexpr bool Constexpr_EXPECT_NE(const Lhs& lhs, const Rhs& rhs) {
195 return (lhs != rhs) ? true : EXPECT_NE_FAILED();
196}
197
198template <typename Lhs, typename Rhs>
199constexpr bool Constexpr_EXPECT_GT(const Lhs& lhs, const Rhs& rhs) {
200 return (lhs > rhs) ? true : EXPECT_GT_FAILED();
201}
202
203template <typename Lhs, typename Rhs>
204constexpr bool Constexpr_EXPECT_GE(const Lhs& lhs, const Rhs& rhs) {
205 return (lhs >= rhs) ? true : EXPECT_GE_FAILED();
206}
207
208template <typename Lhs, typename Rhs>
209constexpr bool Constexpr_EXPECT_LT(const Lhs& lhs, const Rhs& rhs) {
210 return (lhs < rhs) ? true : EXPECT_LT_FAILED();
211}
212
213template <typename Lhs, typename Rhs>
214constexpr bool Constexpr_EXPECT_LE(const Lhs& lhs, const Rhs& rhs) {
215 return (lhs <= rhs) ? true : EXPECT_LE_FAILED();
216}
217
218template <typename T>
219constexpr bool Constexpr_EXPECT_NEAR(T lhs, T rhs, T error) {
220 T diff = lhs - rhs;
221 if (diff < 0) {
222 diff *= -1;
223 }
224 return (diff <= error) ? true : EXPECT_NEAR_FAILED();
225}
226
227constexpr bool Constexpr_EXPECT_FLOAT_EQ(float lhs, float rhs) {
228 // Compare within 4 ULPs, for consistency with GoogleTest's EXPECT_FLOAT_EQ.
229 return Constexpr_EXPECT_NEAR(
230 lhs, rhs, 4 * std::numeric_limits<float>::epsilon());
231}
232
233constexpr bool Constexpr_EXPECT_DOUBLE_EQ(double lhs, double rhs) {
234 // Compare within 4 ULPs, for consistency with GoogleTest's EXPECT_DOUBLE_EQ.
235 return Constexpr_EXPECT_NEAR(
236 lhs, rhs, 4 * std::numeric_limits<double>::epsilon());
237}
238
239[[nodiscard]] constexpr bool StringsAreEqual(const char* lhs, const char* rhs) {
240 if (lhs == nullptr || rhs == nullptr) {
241 return lhs == rhs;
242 }
243
244 while (*lhs == *rhs && *lhs != '\0') {
245 lhs += 1;
246 rhs += 1;
247 }
248
249 return *lhs == '\0' && *rhs == '\0';
250}
251
252constexpr bool Constexpr_EXPECT_STREQ(const char* lhs, const char* rhs) {
253 return StringsAreEqual(lhs, rhs) ? true : EXPECT_STREQ_FAILED();
254}
255
256constexpr bool Constexpr_EXPECT_STRNE(const char* lhs, const char* rhs) {
257 return !StringsAreEqual(lhs, rhs) ? true : EXPECT_STRNE_FAILED();
258}
259
260} // namespace pw::unit_test::internal