blob: 49666ebc11c8e682029d368ca26abf5d98f72946 [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
117TEST(Check, FloatComparison) {
118 MAYBE_SKIP_TEST;
119 float x_float = 50.5;
120 float y_float = 66.5;
121
122 PW_CHECK_FLOAT_LE(x_float, y_float);
123 PW_CHECK_FLOAT_LE(x_float, y_float, "FLOAT: " FAIL_IF_DISPLAYED);
124 PW_CHECK_FLOAT_LE(x_float, y_float, "FLOAT: " FAIL_IF_DISPLAYED_ARGS, z);
125
126 PW_CHECK_FLOAT_GE(x_float, y_float);
127 PW_CHECK_FLOAT_GE(x_float, y_float, "FLOAT: " FAIL_IF_HIDDEN);
128 PW_CHECK_FLOAT_GE(x_float, y_float, "FLOAT: " FAIL_IF_HIDDEN_ARGS, z);
129}
130
131static int Add3(int a, int b, int c) { return a + b + c; }
132
133TEST(Check, ComparisonArgumentsWithCommas) {
134 MAYBE_SKIP_TEST;
135 int x_int = 50;
136 int y_int = 66;
137
138 PW_CHECK_INT_LE(Add3(1, 2, 3), y_int);
139 PW_CHECK_INT_LE(x_int, Add3(1, 2, 3));
140
141 PW_CHECK_INT_LE(Add3(1, 2, 3), y_int, FAIL_IF_DISPLAYED);
142 PW_CHECK_INT_LE(x_int, Add3(1, 2, 3), FAIL_IF_DISPLAYED_ARGS, z);
143
144 PW_CHECK_INT_LE(Add3(1, 2, 3), Add3(1, 2, 3), "INT: " FAIL_IF_DISPLAYED);
145 PW_CHECK_INT_LE(x_int, y_int, "INT: " FAIL_IF_DISPLAYED_ARGS, z);
146}
147
Keir Mierle0743cc52020-02-07 16:08:33 -0800148// Note: This requires enabling PW_ASSERT_USE_SHORT_NAMES 1 above.
149TEST(Check, ShortNamesWork) {
150 MAYBE_SKIP_TEST;
151
152 // Crash
153 CRASH(FAIL_IF_HIDDEN);
154 CRASH(FAIL_IF_HIDDEN_ARGS, z);
155
156 // Check
157 CHECK(true, FAIL_IF_DISPLAYED);
158 CHECK(true, FAIL_IF_DISPLAYED_ARGS, z);
159 CHECK(false, FAIL_IF_HIDDEN);
160 CHECK(false, FAIL_IF_HIDDEN_ARGS, z);
161
162 // Check with binary comparison
163 int x_int = 50;
164 int y_int = 66;
165
166 CHECK_INT_LE(Add3(1, 2, 3), y_int);
167 CHECK_INT_LE(x_int, Add3(1, 2, 3));
168
169 CHECK_INT_LE(Add3(1, 2, 3), y_int, FAIL_IF_DISPLAYED);
170 CHECK_INT_LE(x_int, Add3(1, 2, 3), FAIL_IF_DISPLAYED_ARGS, z);
171
172 CHECK_INT_LE(Add3(1, 2, 3), Add3(1, 2, 3), "INT: " FAIL_IF_DISPLAYED);
173 CHECK_INT_LE(x_int, y_int, "INT: " FAIL_IF_DISPLAYED_ARGS, z);
174}
Keir Mierle8d2a84f2020-04-14 22:00:00 -0700175
176// These are defined in assert_test.c, to test C compatibility.
177extern "C" void AssertBackendCompileTestsInC();
178
179TEST(Check, AssertBackendCompileTestsInC) {
180 MAYBE_SKIP_TEST;
181 AssertBackendCompileTestsInC();
182}
183
Keir Mierle3cee8792020-01-22 17:08:13 -0800184} // namespace