1 // Copyright 2020 The Pigweed Authors
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
7 // https://www.apache.org/licenses/LICENSE-2.0
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
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.
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.
25 // To run these "tests" for pw_assert_basic, you must modify two things:
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.
36 // TODO(pwbug/88): Add verification of the actually recorded asserts statements.
39 #define PW_ASSERT_USE_SHORT_NAMES 1
40 #include "pw_assert/assert.h"
43 #include "pw_status/status.h"
44 #include "gtest/gtest.h"
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;
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.
53 #define FAIL_IF_DISPLAYED "FAIL IF DISPLAYED"
54 #define FAIL_IF_DISPLAYED_ARGS "FAIL IF DISPLAYED: %d"
56 #define FAIL_IF_HIDDEN "FAIL IF HIDDEN"
57 #define FAIL_IF_HIDDEN_ARGS "FAIL IF HIDDEN: %d"
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
64 #define MAYBE_SKIP_TEST ;
69 TEST(Crash, WithAndWithoutMessageArguments) {
71 PW_CRASH(FAIL_IF_HIDDEN);
72 PW_CRASH(FAIL_IF_HIDDEN_ARGS, z);
75 TEST(Check, NoMessage) {
81 TEST(Check, WithMessageAndArgs) {
83 PW_CHECK(true, FAIL_IF_DISPLAYED);
84 PW_CHECK(true, FAIL_IF_DISPLAYED_ARGS, z);
86 PW_CHECK(false, FAIL_IF_HIDDEN);
87 PW_CHECK(false, FAIL_IF_HIDDEN_ARGS, z);
90 TEST(Check, IntComparison) {
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);
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);
104 TEST(Check, UintComparison) {
106 unsigned int x_uint = 50;
107 unsigned int y_uint = 66;
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);
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);
118 TEST(Check, PtrComparison) {
120 void* x_ptr = reinterpret_cast<void*>(50);
121 void* y_ptr = reinterpret_cast<void*>(66);
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);
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);
132 TEST(Check, FloatComparison) {
134 float x_float = 50.5;
135 float y_float = 66.5;
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);
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);
147 // Don't exhaustively test the DCHECKs but have a sampling of them.
148 TEST(DCheck, Sampling) {
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");
157 static int Add3(int a, int b, int c) { return a + b + c; }
159 TEST(Check, ComparisonArgumentsWithCommas) {
164 PW_CHECK_INT_LE(Add3(1, 2, 3), y_int);
165 PW_CHECK_INT_LE(x_int, Add3(1, 2, 3));
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);
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);
174 // Note: This requires enabling PW_ASSERT_USE_SHORT_NAMES 1 above.
175 TEST(Check, ShortNamesWork) {
179 CRASH(FAIL_IF_HIDDEN);
180 CRASH(FAIL_IF_HIDDEN_ARGS, z);
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);
188 // Check with binary comparison
192 CHECK_INT_LE(Add3(1, 2, 3), y_int);
193 CHECK_INT_LE(x_int, Add3(1, 2, 3));
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);
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);
202 pw::Status MakeStatus(pw::Status status) { return status; }
204 TEST(Check, CheckOkMacrosCompile) {
206 pw::Status status = pw::Status::Unknown();
208 // Typical case with long names.
210 PW_CHECK_OK(status, "msg");
211 PW_CHECK_OK(status, "msg: %d", 5);
215 CHECK_OK(status, "msg");
216 CHECK_OK(status, "msg: %d", 5);
218 // Status from a literal.
219 PW_CHECK_OK(pw::OkStatus());
221 // Status from a function.
222 PW_CHECK_OK(MakeStatus(pw::OkStatus()));
224 // Status from C enums.
225 PW_CHECK_OK(PW_STATUS_OK);
228 // These are defined in assert_test.c, to test C compatibility.
229 extern "C" void AssertBackendCompileTestsInC();
231 TEST(Check, AssertBackendCompileTestsInC) {
233 AssertBackendCompileTestsInC();