blob: 47df05696a2e81ae2acae5d7cf94fce01f998c21 [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
Keir Mierle3cee8792020-01-22 17:08:13 -080040#include "pw_assert/assert.h"
41// clang-format on
42
43#include "gtest/gtest.h"
44
45// This is a global constant to feed into the formatter for tests.
46// Intended to pair with FAIL_IF_DISPLAYED_ARGS or FAIL_IF_HIDDEN_ARGS.
47static const int z = 10;
48
49// At some point in the future when there is a proper test system in place for
50// crashing, the below strings can help indicate pass/fail for a check.
51
52#define FAIL_IF_DISPLAYED "FAIL IF DISPLAYED"
53#define FAIL_IF_DISPLAYED_ARGS "FAIL IF DISPLAYED: %d"
54
55#define FAIL_IF_HIDDEN "FAIL IF HIDDEN"
56#define FAIL_IF_HIDDEN_ARGS "FAIL IF HIDDEN: %d"
57
58// This switch exists to support compiling and/or running the tests.
59#define DISABLE_ASSERT_TEST_EXECUTION 1
60#if DISABLE_ASSERT_TEST_EXECUTION
61#define MAYBE_SKIP_TEST return
62#else
63#define MAYBE_SKIP_TEST ;
64#endif
65
66namespace {
67
68TEST(Crash, WithAndWithoutMessageArguments) {
69 MAYBE_SKIP_TEST;
70 PW_CRASH(FAIL_IF_HIDDEN);
71 PW_CRASH(FAIL_IF_HIDDEN_ARGS, z);
72}
73
74TEST(Check, NoMessage) {
75 MAYBE_SKIP_TEST;
76 PW_CHECK(true);
77 PW_CHECK(false);
78}
79
80TEST(Check, WithMessageAndArgs) {
81 MAYBE_SKIP_TEST;
82 PW_CHECK(true, FAIL_IF_DISPLAYED);
83 PW_CHECK(true, FAIL_IF_DISPLAYED_ARGS, z);
84
85 PW_CHECK(false, FAIL_IF_HIDDEN);
86 PW_CHECK(false, FAIL_IF_HIDDEN_ARGS, z);
87}
88
89TEST(Check, IntComparison) {
90 MAYBE_SKIP_TEST;
91 int x_int = 50;
92 int y_int = 66;
93
94 PW_CHECK_INT_LE(x_int, y_int);
95 PW_CHECK_INT_LE(x_int, y_int, "INT: " FAIL_IF_DISPLAYED);
96 PW_CHECK_INT_LE(x_int, y_int, "INT: " FAIL_IF_DISPLAYED_ARGS, z);
97
98 PW_CHECK_INT_GE(x_int, y_int);
99 PW_CHECK_INT_GE(x_int, y_int, "INT: " FAIL_IF_HIDDEN);
100 PW_CHECK_INT_GE(x_int, y_int, "INT: " FAIL_IF_HIDDEN_ARGS, z);
101}
102
103TEST(Check, UintComparison) {
104 MAYBE_SKIP_TEST;
105 unsigned int x_uint = 50;
106 unsigned int y_uint = 66;
107
108 PW_CHECK_UINT_LE(x_uint, y_uint);
109 PW_CHECK_UINT_LE(x_uint, y_uint, "UINT: " FAIL_IF_DISPLAYED);
110 PW_CHECK_UINT_LE(x_uint, y_uint, "UINT: " FAIL_IF_DISPLAYED_ARGS, z);
111
112 PW_CHECK_UINT_GE(x_uint, y_uint);
113 PW_CHECK_UINT_GE(x_uint, y_uint, "UINT: " FAIL_IF_HIDDEN);
114 PW_CHECK_UINT_GE(x_uint, y_uint, "UINT: " FAIL_IF_HIDDEN_ARGS, z);
115}
116
Keir Mierleb9b88162020-04-15 20:43:09 -0700117TEST(Check, PtrComparison) {
118 MAYBE_SKIP_TEST;
119 void* x_ptr = reinterpret_cast<void*>(50);
120 void* y_ptr = reinterpret_cast<void*>(66);
121
122 PW_CHECK_PTR_EQ(x_ptr, y_ptr);
123 PW_CHECK_PTR_LE(x_ptr, y_ptr, "PTR: " FAIL_IF_DISPLAYED);
124 PW_CHECK_PTR_LE(x_ptr, y_ptr, "PTR: " FAIL_IF_DISPLAYED_ARGS, z);
125
126 PW_CHECK_PTR_GE(x_ptr, y_ptr);
127 PW_CHECK_PTR_GE(x_ptr, y_ptr, "PTR: " FAIL_IF_HIDDEN);
128 PW_CHECK_PTR_GE(x_ptr, y_ptr, "PTR: " FAIL_IF_HIDDEN_ARGS, z);
129}
130
Keir Mierle3cee8792020-01-22 17:08:13 -0800131TEST(Check, FloatComparison) {
132 MAYBE_SKIP_TEST;
133 float x_float = 50.5;
134 float y_float = 66.5;
135
136 PW_CHECK_FLOAT_LE(x_float, y_float);
137 PW_CHECK_FLOAT_LE(x_float, y_float, "FLOAT: " FAIL_IF_DISPLAYED);
138 PW_CHECK_FLOAT_LE(x_float, y_float, "FLOAT: " FAIL_IF_DISPLAYED_ARGS, z);
139
140 PW_CHECK_FLOAT_GE(x_float, y_float);
141 PW_CHECK_FLOAT_GE(x_float, y_float, "FLOAT: " FAIL_IF_HIDDEN);
142 PW_CHECK_FLOAT_GE(x_float, y_float, "FLOAT: " FAIL_IF_HIDDEN_ARGS, z);
143}
144
Keir Mierleb9b88162020-04-15 20:43:09 -0700145// Don't exhaustively test the DCHECKs but have a sampling of them.
146TEST(DCheck, Sampling) {
147 MAYBE_SKIP_TEST;
148 PW_DCHECK(5 == 10);
149 PW_DCHECK(5 == 10, "Message");
150 PW_DCHECK(5 == 10, "Message: %d", 5);
151 PW_DCHECK_INT_LE(5.4, 10.0);
152 PW_DCHECK_FLOAT_EQ(5.4, 10.0, "Message");
153}
154
Keir Mierle3cee8792020-01-22 17:08:13 -0800155static int Add3(int a, int b, int c) { return a + b + c; }
156
157TEST(Check, ComparisonArgumentsWithCommas) {
158 MAYBE_SKIP_TEST;
159 int x_int = 50;
160 int y_int = 66;
161
162 PW_CHECK_INT_LE(Add3(1, 2, 3), y_int);
163 PW_CHECK_INT_LE(x_int, Add3(1, 2, 3));
164
165 PW_CHECK_INT_LE(Add3(1, 2, 3), y_int, FAIL_IF_DISPLAYED);
166 PW_CHECK_INT_LE(x_int, Add3(1, 2, 3), FAIL_IF_DISPLAYED_ARGS, z);
167
168 PW_CHECK_INT_LE(Add3(1, 2, 3), Add3(1, 2, 3), "INT: " FAIL_IF_DISPLAYED);
169 PW_CHECK_INT_LE(x_int, y_int, "INT: " FAIL_IF_DISPLAYED_ARGS, z);
170}
171
Keir Mierle0743cc52020-02-07 16:08:33 -0800172// Note: This requires enabling PW_ASSERT_USE_SHORT_NAMES 1 above.
173TEST(Check, ShortNamesWork) {
174 MAYBE_SKIP_TEST;
175
176 // Crash
177 CRASH(FAIL_IF_HIDDEN);
178 CRASH(FAIL_IF_HIDDEN_ARGS, z);
179
180 // Check
181 CHECK(true, FAIL_IF_DISPLAYED);
182 CHECK(true, FAIL_IF_DISPLAYED_ARGS, z);
183 CHECK(false, FAIL_IF_HIDDEN);
184 CHECK(false, FAIL_IF_HIDDEN_ARGS, z);
185
186 // Check with binary comparison
187 int x_int = 50;
188 int y_int = 66;
189
190 CHECK_INT_LE(Add3(1, 2, 3), y_int);
191 CHECK_INT_LE(x_int, Add3(1, 2, 3));
192
193 CHECK_INT_LE(Add3(1, 2, 3), y_int, FAIL_IF_DISPLAYED);
194 CHECK_INT_LE(x_int, Add3(1, 2, 3), FAIL_IF_DISPLAYED_ARGS, z);
195
196 CHECK_INT_LE(Add3(1, 2, 3), Add3(1, 2, 3), "INT: " FAIL_IF_DISPLAYED);
197 CHECK_INT_LE(x_int, y_int, "INT: " FAIL_IF_DISPLAYED_ARGS, z);
198}
Keir Mierle8d2a84f2020-04-14 22:00:00 -0700199
200// These are defined in assert_test.c, to test C compatibility.
201extern "C" void AssertBackendCompileTestsInC();
202
203TEST(Check, AssertBackendCompileTestsInC) {
204 MAYBE_SKIP_TEST;
205 AssertBackendCompileTestsInC();
206}
207
Keir Mierle3cee8792020-01-22 17:08:13 -0800208} // namespace