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
15 #include <limits>
16
17 #include "pw_compilation_testing/negative_compilation.h"
18
19 // Start with an example test for the docs, which includes the #includes.
20 // DOCSTAG[pw_unit_test-constexpr]
21 #include "pw_unit_test/constexpr.h"
22 #include "pw_unit_test/framework.h"
23
24 namespace {
25
ComputeSum(int lhs,int rhs)26 constexpr int ComputeSum(int lhs, int rhs) { return lhs + rhs; }
27
28 PW_CONSTEXPR_TEST(PwConstexprTestExample, AddNumbersOverflow, {
29 // Use PW_TEST_EXPECT/ASSERT macros like regular GoogleTest macros.
30 PW_TEST_EXPECT_EQ(ComputeSum(1, -2), -1);
31 PW_TEST_EXPECT_LT(ComputeSum(1, 1), ComputeSum(2, 2));
32
33 PW_TEST_ASSERT_EQ(ComputeSum(0, 0), 0);
34 PW_TEST_EXPECT_EQ(ComputeSum(-123, 0), -123) << "Additive identity";
35 });
36
37 } // namespace
38 // DOCSTAG[pw_unit_test-constexpr]
39
40 namespace {
41
42 PW_CONSTEXPR_TEST(PwConstexprTest, AllMacros, {
43 PW_TEST_EXPECT_TRUE(true) << "";
44 PW_TEST_EXPECT_FALSE(false) << "";
45
46 PW_TEST_EXPECT_EQ(0, 0) << "";
47 PW_TEST_EXPECT_NE(0, 1) << "";
48 PW_TEST_EXPECT_GT(1, 0) << "";
49 PW_TEST_EXPECT_GE(0, 0) << "";
50 PW_TEST_EXPECT_LT(-1, 0) << "";
51 PW_TEST_EXPECT_LE(0, 0) << "";
52
53 PW_TEST_EXPECT_NEAR(0, 0, 1) << "";
54 PW_TEST_EXPECT_FLOAT_EQ(0.0f, 0.0f) << "";
55 PW_TEST_EXPECT_DOUBLE_EQ(0.0, 0.0) << "";
56
57 PW_TEST_ASSERT_STREQ("", "") << "";
58 PW_TEST_ASSERT_STRNE("", "a") << "";
59
60 PW_TEST_ASSERT_TRUE(true) << "";
61 PW_TEST_ASSERT_FALSE(false) << "";
62
63 PW_TEST_ASSERT_EQ(0, 0) << "";
64 PW_TEST_ASSERT_NE(0, 1) << "";
65 PW_TEST_ASSERT_GT(1, 0) << "";
66 PW_TEST_ASSERT_GE(0, 0) << "";
67 PW_TEST_ASSERT_LT(-1, 0) << "";
68 PW_TEST_ASSERT_LE(0, 0) << "";
69
70 PW_TEST_ASSERT_NEAR(0, 0, 1) << "";
71 PW_TEST_ASSERT_FLOAT_EQ(0.0f, 0.0f) << "";
72 PW_TEST_ASSERT_DOUBLE_EQ(0.0, 0.0) << "";
73
74 PW_TEST_ASSERT_STREQ("", "") << "";
75 PW_TEST_ASSERT_STRNE("", "a") << "";
76 });
77
78 PW_CONSTEXPR_TEST(PwConstexprTest, CommasOutsideMacrosExpandCorrectly, {
79 int a = 1, b = 2, c = 3;
80 PW_TEST_EXPECT_LT(a, b);
81 PW_TEST_EXPECT_EQ(b + 1, c);
82
83 int sum = ComputeSum(a, b);
84 PW_TEST_EXPECT_EQ(sum, c);
85 });
86
87 // block-submission: disable
88 // DOCSTAG[pw_unit_test-constexpr-skip]
89 // Subsequent PW_CONSTEXPR_TESTs will skip the constexpr portion of the test.
90 // This allows you to use the richer GoogleTest-style output to debug failures.
91 #define SKIP_CONSTEXPR_TESTS_DONT_SUBMIT
92
NotConstexpr()93 void NotConstexpr() {}
94
95 PW_CONSTEXPR_TEST(PwConstexprTest, NotConstexprButDisabledByMacro, {
96 // This test is not constexpr, but the constexpr test is skipped because the
97 // SKIP_CONSTEXPR_TESTS macro is defined.
98 NotConstexpr();
99 PW_TEST_EXPECT_TRUE(true);
100 });
101
102 // Now, constexpr tests will no longer be skipped, and the same test will fail.
103 #undef SKIP_CONSTEXPR_TESTS_DONT_SUBMIT
104 // DOCSTAG[pw_unit_test-constexpr-skip]
105 // block-submission: enable
106
107 #if PW_NC_TEST(NonConstexprFailsToCompile)
108 PW_NC_EXPECT("NotConstexpr");
109
110 PW_CONSTEXPR_TEST(PwConstexprTest, NotConstexpr, {
111 NotConstexpr();
112 PW_TEST_EXPECT_TRUE(true);
113 });
114 #endif // PW_NC_TEST
115
116 #if PW_NC_TEST(FailingTestFailsToCompile)
117 PW_NC_EXPECT("EXPECT_TRUE_FAILED");
118
119 PW_CONSTEXPR_TEST(PwConstexprTest, FailingTest, {
120 PW_TEST_EXPECT_TRUE(false);
121 });
122 #endif // PW_NC_TEST
123
124 } // namespace
125