blob: a23c58b36214761f5916db9469dd93caac2824fc [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 is a compile test that verifies that the assert macros compile in a C
16// context. They are not correctness checks.
Keir Mierle3cee8792020-01-22 17:08:13 -080017//
Keir Mierle8d2a84f2020-04-14 22:00:00 -070018// Note: These tests cannot be run with a normal assert backend, since they
19// will abort. However, the assert_basic backend supports non-aborting assert;
20// see the note in assert_backend_compile_test.cc.
21
22// The compile tests verifies that the short macros compile, so enable them.
23#undef PW_ASSERT_USE_SHORT_NAMES
24#define PW_ASSERT_USE_SHORT_NAMES 1
Keir Mierle3cee8792020-01-22 17:08:13 -080025
Wyatt Heplerf298de42021-03-19 15:06:36 -070026#include "pw_assert/check.h"
Keir Mierlea84fd8d2020-08-07 21:29:26 -070027
Wyatt Hepler06f98fc2021-03-04 16:25:27 -080028static void EnsureNullIsIncluded(void) {
Keir Mierlea84fd8d2020-08-07 21:29:26 -070029 // This is a compile check to ensure NULL is defined. It comes before the
30 // status.h include to ensure we don't accidentally get NULL from status.h.
31 PW_CHECK_NOTNULL(0xa);
32 PW_CHECK_NOTNULL(0x0);
33}
34
Wyatt Heplerdd3e8812020-09-29 11:14:28 -070035#include <stdbool.h>
36
Wyatt Heplerf298de42021-03-19 15:06:36 -070037#include "pw_assert/assert.h"
Keir Mierle0fa7f7d2020-05-07 12:34:00 -070038#include "pw_status/status.h"
Keir Mierle3cee8792020-01-22 17:08:13 -080039
40#ifdef __cplusplus
41#error "This file must be compiled as plain C to verify C compilation works."
42#endif // __cplusplus
43
44// This is a global constant to feed into the formatter for tests.
45// Intended to pair with FAIL_IF_DISPLAYED_ARGS or FAIL_IF_HIDDEN_ARGS.
46static const int z = 10;
47
48// At some point in the future when there is a proper test system in place for
49// crashing, the below strings can help indicate pass/fail for a check.
50
51#define FAIL_IF_DISPLAYED "FAIL IF DISPLAYED"
52#define FAIL_IF_DISPLAYED_ARGS "FAIL IF DISPLAYED: %d"
53
54#define FAIL_IF_HIDDEN "FAIL IF HIDDEN"
55#define FAIL_IF_HIDDEN_ARGS "FAIL IF HIDDEN: %d"
56
57// This switch exists to support compiling and/or running the tests.
58#define DISABLE_ASSERT_TEST_EXECUTION 1
59#if DISABLE_ASSERT_TEST_EXECUTION
60#define MAYBE_SKIP_TEST return
61#else
62#define MAYBE_SKIP_TEST ;
63#endif
64
65static int Add3(int a, int b, int c) { return a + b + c; }
66
Wyatt Hepler06f98fc2021-03-04 16:25:27 -080067void AssertBackendCompileTestsInC(void) {
Keir Mierle3cee8792020-01-22 17:08:13 -080068 { // TEST(Crash, WithAndWithoutMessageArguments)
69 MAYBE_SKIP_TEST;
70 PW_CRASH(FAIL_IF_HIDDEN);
71 PW_CRASH(FAIL_IF_HIDDEN_ARGS, z);
72 }
73
74 { // TEST(Check, NoMessage)
75 MAYBE_SKIP_TEST;
76 PW_CHECK(1);
77 PW_CHECK(0);
78 }
79
80 { // TEST(Check, WithMessageAndArgs)
81 MAYBE_SKIP_TEST;
82 PW_CHECK(1, FAIL_IF_DISPLAYED);
83 PW_CHECK(1, FAIL_IF_DISPLAYED_ARGS, z);
84
85 PW_CHECK(0, FAIL_IF_HIDDEN);
86 PW_CHECK(0, FAIL_IF_HIDDEN_ARGS, z);
87 }
88
89 { // TEST(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
103 { // TEST(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 -0700117 { // TEST(Check, PtrComparison)
118 MAYBE_SKIP_TEST;
119 void* x_ptr = (void*)(50);
120 void* y_ptr = (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);
Keir Mierlea84fd8d2020-08-07 21:29:26 -0700129
130 PW_CHECK_NOTNULL(0xa);
131 PW_CHECK_NOTNULL(0x0);
Keir Mierleb9b88162020-04-15 20:43:09 -0700132 }
133
Keir Mierle3cee8792020-01-22 17:08:13 -0800134 { // TEST(Check, FloatComparison)
135 MAYBE_SKIP_TEST;
136 float x_float = 50.5;
137 float y_float = 66.5;
138
Ewout van Bekkum9e97cfd2020-07-16 13:57:24 -0700139 PW_CHECK_FLOAT_EXACT_LE(x_float, y_float);
140 PW_CHECK_FLOAT_EXACT_LE(x_float, y_float, "FLOAT: " FAIL_IF_DISPLAYED);
141 PW_CHECK_FLOAT_EXACT_LE(
142 x_float, y_float, "FLOAT: " FAIL_IF_DISPLAYED_ARGS, z);
Keir Mierle3cee8792020-01-22 17:08:13 -0800143
Ewout van Bekkum9e97cfd2020-07-16 13:57:24 -0700144 PW_CHECK_FLOAT_EXACT_GE(x_float, y_float);
145 PW_CHECK_FLOAT_EXACT_GE(x_float, y_float, "FLOAT: " FAIL_IF_HIDDEN);
146 PW_CHECK_FLOAT_EXACT_GE(x_float, y_float, "FLOAT: " FAIL_IF_HIDDEN_ARGS, z);
Keir Mierle3cee8792020-01-22 17:08:13 -0800147 }
148
Keir Mierleb9b88162020-04-15 20:43:09 -0700149 // Don't exhaustively test the DCHECKs but have a sampling of them.
150 { // TEST(DCheck, Sampling)
151 MAYBE_SKIP_TEST;
152 PW_DCHECK(5 == 10);
153 PW_DCHECK(5 == 10, "Message");
154 PW_DCHECK(5 == 10, "Message: %d", 5);
155 PW_DCHECK_INT_LE(5.4, 10.0);
Ewout van Bekkum9e97cfd2020-07-16 13:57:24 -0700156 PW_DCHECK_FLOAT_EXACT_EQ(5.4, 10.0, "Message");
Keir Mierleb9b88162020-04-15 20:43:09 -0700157 }
158
Keir Mierle3cee8792020-01-22 17:08:13 -0800159 { // 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 }
Keir Mierle8d2a84f2020-04-14 22:00:00 -0700173
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(1, FAIL_IF_DISPLAYED);
184 CHECK(1, FAIL_IF_DISPLAYED_ARGS, z);
185 CHECK(0, FAIL_IF_HIDDEN);
186 CHECK(0, 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 Mierle0fa7f7d2020-05-07 12:34:00 -0700201
Wyatt Heplerdd3e8812020-09-29 11:14:28 -0700202 { // Compile tests for PW_CHECK_OK().
Keir Mierle0fa7f7d2020-05-07 12:34:00 -0700203 PW_CHECK_OK(PW_STATUS_OK);
204 PW_CHECK_OK(PW_STATUS_OK, "msg");
205 PW_CHECK_OK(PW_STATUS_OK, "msg: %d", 5);
206 PW_DCHECK_OK(PW_STATUS_OK);
207 PW_DCHECK_OK(PW_STATUS_OK, "msg");
208 PW_DCHECK_OK(PW_STATUS_OK, "msg: %d", 5);
209 }
Keir Mierlea84fd8d2020-08-07 21:29:26 -0700210
Wyatt Heplerdd3e8812020-09-29 11:14:28 -0700211 { // TEST(Assert, Basic)
212 MAYBE_SKIP_TEST;
213 PW_ASSERT(false);
214 PW_ASSERT(123 == 456);
215 }
216
217 { // Compile tests for PW_ASSERT().
218 PW_ASSERT(true);
219 PW_ASSERT(123 != 456);
220
221 PW_DASSERT(true);
222 PW_DASSERT(123 != 456);
223 }
224
Keir Mierlea84fd8d2020-08-07 21:29:26 -0700225 EnsureNullIsIncluded();
Keir Mierle3cee8792020-01-22 17:08:13 -0800226}