Fix for x86_64 build fail
[platform/upstream/connectedhomeip.git] / third_party / pigweed / repo / pw_assert / assert_backend_compile_test.cc
1 // Copyright 2020 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 // This series of "tests" is more a compile test to verify that the assert
16 // backend is able to compile the constructs promised by the assert facade.
17 // Truly testing the backend in a general way from the facade is impossible
18 // since the device will go down when an assert triggers, and so that must be
19 // handled inside the individual backends.
20 //
21 // NOTE: While these tests are not intended to run, it *is* possible to run
22 // them with the assert_basic backend, in a special mode where the assert
23 // statements fall through instead of aborting.
24 //
25 // To run these "tests" for pw_assert_basic, you must modify two things:
26 //
27 //   (1) Set DISABLE_ASSERT_TEST_EXECUTION 0 in assert_backend_compile_test.cc
28 //   (2) Set DISABLE_ASSERT_TEST_EXECUTION 0 in assert_backend_compile_test.c
29 //   (3) Set PW_ASSERT_BASIC_DISABLE_NORETURN 1 in assert_basic.h
30 //   (4) Compile and run the resulting binary, paying attention to the
31 //       displayed error messages. If "FAIL IF DISPLAYED" is printed, the
32 //       test has failed. If any "FAIL_IF_HIDDEN" asserts are not displayed,
33 //       the test has failed. Obviously manually verifying these is a pain
34 //       and so this is not a suitable test for production.
35 //
36 // TODO(pwbug/88): Add verification of the actually recorded asserts statements.
37
38 // clang-format off
39 #define PW_ASSERT_USE_SHORT_NAMES 1
40 #include "pw_assert/assert.h"
41 // clang-format on
42
43 #include "pw_status/status.h"
44 #include "gtest/gtest.h"
45
46 // This is a global constant to feed into the formatter for tests.
47 // Intended to pair with FAIL_IF_DISPLAYED_ARGS or FAIL_IF_HIDDEN_ARGS.
48 static const int z = 10;
49
50 // At some point in the future when there is a proper test system in place for
51 // crashing, the below strings can help indicate pass/fail for a check.
52
53 #define FAIL_IF_DISPLAYED "FAIL IF DISPLAYED"
54 #define FAIL_IF_DISPLAYED_ARGS "FAIL IF DISPLAYED: %d"
55
56 #define FAIL_IF_HIDDEN "FAIL IF HIDDEN"
57 #define FAIL_IF_HIDDEN_ARGS "FAIL IF HIDDEN: %d"
58
59 // This switch exists to support compiling and/or running the tests.
60 #define DISABLE_ASSERT_TEST_EXECUTION 1
61 #if DISABLE_ASSERT_TEST_EXECUTION
62 #define MAYBE_SKIP_TEST return
63 #else
64 #define MAYBE_SKIP_TEST ;
65 #endif
66
67 namespace {
68
69 TEST(Crash, WithAndWithoutMessageArguments) {
70   MAYBE_SKIP_TEST;
71   PW_CRASH(FAIL_IF_HIDDEN);
72   PW_CRASH(FAIL_IF_HIDDEN_ARGS, z);
73 }
74
75 TEST(Check, NoMessage) {
76   MAYBE_SKIP_TEST;
77   PW_CHECK(true);
78   PW_CHECK(false);
79 }
80
81 TEST(Check, WithMessageAndArgs) {
82   MAYBE_SKIP_TEST;
83   PW_CHECK(true, FAIL_IF_DISPLAYED);
84   PW_CHECK(true, FAIL_IF_DISPLAYED_ARGS, z);
85
86   PW_CHECK(false, FAIL_IF_HIDDEN);
87   PW_CHECK(false, FAIL_IF_HIDDEN_ARGS, z);
88 }
89
90 TEST(Check, IntComparison) {
91   MAYBE_SKIP_TEST;
92   int x_int = 50;
93   int y_int = 66;
94
95   PW_CHECK_INT_LE(x_int, y_int);
96   PW_CHECK_INT_LE(x_int, y_int, "INT: " FAIL_IF_DISPLAYED);
97   PW_CHECK_INT_LE(x_int, y_int, "INT: " FAIL_IF_DISPLAYED_ARGS, z);
98
99   PW_CHECK_INT_GE(x_int, y_int);
100   PW_CHECK_INT_GE(x_int, y_int, "INT: " FAIL_IF_HIDDEN);
101   PW_CHECK_INT_GE(x_int, y_int, "INT: " FAIL_IF_HIDDEN_ARGS, z);
102 }
103
104 TEST(Check, UintComparison) {
105   MAYBE_SKIP_TEST;
106   unsigned int x_uint = 50;
107   unsigned int y_uint = 66;
108
109   PW_CHECK_UINT_LE(x_uint, y_uint);
110   PW_CHECK_UINT_LE(x_uint, y_uint, "UINT: " FAIL_IF_DISPLAYED);
111   PW_CHECK_UINT_LE(x_uint, y_uint, "UINT: " FAIL_IF_DISPLAYED_ARGS, z);
112
113   PW_CHECK_UINT_GE(x_uint, y_uint);
114   PW_CHECK_UINT_GE(x_uint, y_uint, "UINT: " FAIL_IF_HIDDEN);
115   PW_CHECK_UINT_GE(x_uint, y_uint, "UINT: " FAIL_IF_HIDDEN_ARGS, z);
116 }
117
118 TEST(Check, PtrComparison) {
119   MAYBE_SKIP_TEST;
120   void* x_ptr = reinterpret_cast<void*>(50);
121   void* y_ptr = reinterpret_cast<void*>(66);
122
123   PW_CHECK_PTR_EQ(x_ptr, y_ptr);
124   PW_CHECK_PTR_LE(x_ptr, y_ptr, "PTR: " FAIL_IF_DISPLAYED);
125   PW_CHECK_PTR_LE(x_ptr, y_ptr, "PTR: " FAIL_IF_DISPLAYED_ARGS, z);
126
127   PW_CHECK_PTR_GE(x_ptr, y_ptr);
128   PW_CHECK_PTR_GE(x_ptr, y_ptr, "PTR: " FAIL_IF_HIDDEN);
129   PW_CHECK_PTR_GE(x_ptr, y_ptr, "PTR: " FAIL_IF_HIDDEN_ARGS, z);
130 }
131
132 TEST(Check, FloatComparison) {
133   MAYBE_SKIP_TEST;
134   float x_float = 50.5;
135   float y_float = 66.5;
136
137   PW_CHECK_FLOAT_EXACT_LE(x_float, y_float);
138   PW_CHECK_FLOAT_EXACT_LE(x_float, y_float, "FLOAT: " FAIL_IF_DISPLAYED);
139   PW_CHECK_FLOAT_EXACT_LE(
140       x_float, y_float, "FLOAT: " FAIL_IF_DISPLAYED_ARGS, z);
141
142   PW_CHECK_FLOAT_EXACT_GE(x_float, y_float);
143   PW_CHECK_FLOAT_EXACT_GE(x_float, y_float, "FLOAT: " FAIL_IF_HIDDEN);
144   PW_CHECK_FLOAT_EXACT_GE(x_float, y_float, "FLOAT: " FAIL_IF_HIDDEN_ARGS, z);
145 }
146
147 // Don't exhaustively test the DCHECKs but have a sampling of them.
148 TEST(DCheck, Sampling) {
149   MAYBE_SKIP_TEST;
150   PW_DCHECK(5 == 10);
151   PW_DCHECK(5 == 10, "Message");
152   PW_DCHECK(5 == 10, "Message: %d", 5);
153   PW_DCHECK_INT_LE(5.4, 10.0);
154   PW_DCHECK_FLOAT_EXACT_EQ(5.4, 10.0, "Message");
155 }
156
157 static int Add3(int a, int b, int c) { return a + b + c; }
158
159 TEST(Check, ComparisonArgumentsWithCommas) {
160   MAYBE_SKIP_TEST;
161   int x_int = 50;
162   int y_int = 66;
163
164   PW_CHECK_INT_LE(Add3(1, 2, 3), y_int);
165   PW_CHECK_INT_LE(x_int, Add3(1, 2, 3));
166
167   PW_CHECK_INT_LE(Add3(1, 2, 3), y_int, FAIL_IF_DISPLAYED);
168   PW_CHECK_INT_LE(x_int, Add3(1, 2, 3), FAIL_IF_DISPLAYED_ARGS, z);
169
170   PW_CHECK_INT_LE(Add3(1, 2, 3), Add3(1, 2, 3), "INT: " FAIL_IF_DISPLAYED);
171   PW_CHECK_INT_LE(x_int, y_int, "INT: " FAIL_IF_DISPLAYED_ARGS, z);
172 }
173
174 // Note: This requires enabling PW_ASSERT_USE_SHORT_NAMES 1 above.
175 TEST(Check, ShortNamesWork) {
176   MAYBE_SKIP_TEST;
177
178   // Crash
179   CRASH(FAIL_IF_HIDDEN);
180   CRASH(FAIL_IF_HIDDEN_ARGS, z);
181
182   // Check
183   CHECK(true, FAIL_IF_DISPLAYED);
184   CHECK(true, FAIL_IF_DISPLAYED_ARGS, z);
185   CHECK(false, FAIL_IF_HIDDEN);
186   CHECK(false, FAIL_IF_HIDDEN_ARGS, z);
187
188   // Check with binary comparison
189   int x_int = 50;
190   int y_int = 66;
191
192   CHECK_INT_LE(Add3(1, 2, 3), y_int);
193   CHECK_INT_LE(x_int, Add3(1, 2, 3));
194
195   CHECK_INT_LE(Add3(1, 2, 3), y_int, FAIL_IF_DISPLAYED);
196   CHECK_INT_LE(x_int, Add3(1, 2, 3), FAIL_IF_DISPLAYED_ARGS, z);
197
198   CHECK_INT_LE(Add3(1, 2, 3), Add3(1, 2, 3), "INT: " FAIL_IF_DISPLAYED);
199   CHECK_INT_LE(x_int, y_int, "INT: " FAIL_IF_DISPLAYED_ARGS, z);
200 }
201
202 pw::Status MakeStatus(pw::Status status) { return status; }
203
204 TEST(Check, CheckOkMacrosCompile) {
205   MAYBE_SKIP_TEST;
206   pw::Status status = pw::Status::Unknown();
207
208   // Typical case with long names.
209   PW_CHECK_OK(status);
210   PW_CHECK_OK(status, "msg");
211   PW_CHECK_OK(status, "msg: %d", 5);
212
213   // Short names.
214   CHECK_OK(status);
215   CHECK_OK(status, "msg");
216   CHECK_OK(status, "msg: %d", 5);
217
218   // Status from a literal.
219   PW_CHECK_OK(pw::OkStatus());
220
221   // Status from a function.
222   PW_CHECK_OK(MakeStatus(pw::OkStatus()));
223
224   // Status from C enums.
225   PW_CHECK_OK(PW_STATUS_OK);
226 }
227
228 // These are defined in assert_test.c, to test C compatibility.
229 extern "C" void AssertBackendCompileTestsInC();
230
231 TEST(Check, AssertBackendCompileTestsInC) {
232   MAYBE_SKIP_TEST;
233   AssertBackendCompileTestsInC();
234 }
235
236 }  // namespace