blob: 946d3ac15f582ecb818fa6c1021e4f6956fe2c9e [file] [log] [blame]
Keir Mierle3cee8792020-01-22 17:08:13 -08001// 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
Keir Mierle8d2a84f2020-04-14 22:00:00 -070015// 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.
Keir Mierle3cee8792020-01-22 17:08:13 -080020//
Keir Mierle8d2a84f2020-04-14 22:00:00 -070021// 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.
Keir Mierle3cee8792020-01-22 17:08:13 -080037
38// clang-format off
Keir Mierle0743cc52020-02-07 16:08:33 -080039#define PW_ASSERT_USE_SHORT_NAMES 1
Wyatt Heplerf298de42021-03-19 15:06:36 -070040#include "pw_assert/check.h"
Keir Mierle3cee8792020-01-22 17:08:13 -080041// clang-format on
42
43#include "gtest/gtest.h"
Rob Mohr98ff5c22021-04-28 20:31:40 -070044#include "pw_status/status.h"
Keir Mierle3cee8792020-01-22 17:08:13 -080045
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.
48static 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
67namespace {
68
69TEST(Crash, WithAndWithoutMessageArguments) {
70 MAYBE_SKIP_TEST;
71 PW_CRASH(FAIL_IF_HIDDEN);
72 PW_CRASH(FAIL_IF_HIDDEN_ARGS, z);
73}
74
75TEST(Check, NoMessage) {
76 MAYBE_SKIP_TEST;
77 PW_CHECK(true);
78 PW_CHECK(false);
79}
80
81TEST(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
90TEST(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
104TEST(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
Keir Mierleb9b88162020-04-15 20:43:09 -0700118TEST(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
Keir Mierle3cee8792020-01-22 17:08:13 -0800132TEST(Check, FloatComparison) {
133 MAYBE_SKIP_TEST;
134 float x_float = 50.5;
135 float y_float = 66.5;
136
Ewout van Bekkum9e97cfd2020-07-16 13:57:24 -0700137 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);
Keir Mierle3cee8792020-01-22 17:08:13 -0800141
Ewout van Bekkum9e97cfd2020-07-16 13:57:24 -0700142 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);
Keir Mierle3cee8792020-01-22 17:08:13 -0800145}
146
Keir Mierleb9b88162020-04-15 20:43:09 -0700147// Don't exhaustively test the DCHECKs but have a sampling of them.
148TEST(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);
Ewout van Bekkum9e97cfd2020-07-16 13:57:24 -0700154 PW_DCHECK_FLOAT_EXACT_EQ(5.4, 10.0, "Message");
Keir Mierleb9b88162020-04-15 20:43:09 -0700155}
156
Keir Mierle3cee8792020-01-22 17:08:13 -0800157static int Add3(int a, int b, int c) { return a + b + c; }
158
159TEST(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
Keir Mierle0743cc52020-02-07 16:08:33 -0800174// Note: This requires enabling PW_ASSERT_USE_SHORT_NAMES 1 above.
175TEST(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}
Keir Mierle8d2a84f2020-04-14 22:00:00 -0700201
Keir Mierle0fa7f7d2020-05-07 12:34:00 -0700202pw::Status MakeStatus(pw::Status status) { return status; }
203
204TEST(Check, CheckOkMacrosCompile) {
205 MAYBE_SKIP_TEST;
Wyatt Heplerd78f7c62020-09-28 14:27:32 -0700206 pw::Status status = pw::Status::Unknown();
Keir Mierle0fa7f7d2020-05-07 12:34:00 -0700207
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.
Wyatt Hepler1b3da3a2021-01-07 13:26:57 -0800219 PW_CHECK_OK(pw::OkStatus());
Keir Mierle0fa7f7d2020-05-07 12:34:00 -0700220
221 // Status from a function.
Wyatt Hepler1b3da3a2021-01-07 13:26:57 -0800222 PW_CHECK_OK(MakeStatus(pw::OkStatus()));
Keir Mierle0fa7f7d2020-05-07 12:34:00 -0700223
224 // Status from C enums.
225 PW_CHECK_OK(PW_STATUS_OK);
226}
227
Keir Mierle8d2a84f2020-04-14 22:00:00 -0700228// These are defined in assert_test.c, to test C compatibility.
229extern "C" void AssertBackendCompileTestsInC();
230
231TEST(Check, AssertBackendCompileTestsInC) {
232 MAYBE_SKIP_TEST;
233 AssertBackendCompileTestsInC();
234}
235
Keir Mierle3cee8792020-01-22 17:08:13 -0800236} // namespace