blob: cecc756bd77f6617c2c0d38cd0ec3fe09bb2ed20 [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
26#include "pw_assert/assert.h"
Keir Mierle0fa7f7d2020-05-07 12:34:00 -070027#include "pw_status/status.h"
Keir Mierle3cee8792020-01-22 17:08:13 -080028
29#ifdef __cplusplus
30#error "This file must be compiled as plain C to verify C compilation works."
31#endif // __cplusplus
32
33// This is a global constant to feed into the formatter for tests.
34// Intended to pair with FAIL_IF_DISPLAYED_ARGS or FAIL_IF_HIDDEN_ARGS.
35static const int z = 10;
36
37// At some point in the future when there is a proper test system in place for
38// crashing, the below strings can help indicate pass/fail for a check.
39
40#define FAIL_IF_DISPLAYED "FAIL IF DISPLAYED"
41#define FAIL_IF_DISPLAYED_ARGS "FAIL IF DISPLAYED: %d"
42
43#define FAIL_IF_HIDDEN "FAIL IF HIDDEN"
44#define FAIL_IF_HIDDEN_ARGS "FAIL IF HIDDEN: %d"
45
46// This switch exists to support compiling and/or running the tests.
47#define DISABLE_ASSERT_TEST_EXECUTION 1
48#if DISABLE_ASSERT_TEST_EXECUTION
49#define MAYBE_SKIP_TEST return
50#else
51#define MAYBE_SKIP_TEST ;
52#endif
53
54static int Add3(int a, int b, int c) { return a + b + c; }
55
Keir Mierle8d2a84f2020-04-14 22:00:00 -070056void AssertBackendCompileTestsInC() {
Keir Mierle3cee8792020-01-22 17:08:13 -080057 { // TEST(Crash, WithAndWithoutMessageArguments)
58 MAYBE_SKIP_TEST;
59 PW_CRASH(FAIL_IF_HIDDEN);
60 PW_CRASH(FAIL_IF_HIDDEN_ARGS, z);
61 }
62
63 { // TEST(Check, NoMessage)
64 MAYBE_SKIP_TEST;
65 PW_CHECK(1);
66 PW_CHECK(0);
67 }
68
69 { // TEST(Check, WithMessageAndArgs)
70 MAYBE_SKIP_TEST;
71 PW_CHECK(1, FAIL_IF_DISPLAYED);
72 PW_CHECK(1, FAIL_IF_DISPLAYED_ARGS, z);
73
74 PW_CHECK(0, FAIL_IF_HIDDEN);
75 PW_CHECK(0, FAIL_IF_HIDDEN_ARGS, z);
76 }
77
78 { // TEST(Check, IntComparison)
79 MAYBE_SKIP_TEST;
80 int x_int = 50;
81 int y_int = 66;
82
83 PW_CHECK_INT_LE(x_int, y_int);
84 PW_CHECK_INT_LE(x_int, y_int, "INT: " FAIL_IF_DISPLAYED);
85 PW_CHECK_INT_LE(x_int, y_int, "INT: " FAIL_IF_DISPLAYED_ARGS, z);
86
87 PW_CHECK_INT_GE(x_int, y_int);
88 PW_CHECK_INT_GE(x_int, y_int, "INT: " FAIL_IF_HIDDEN);
89 PW_CHECK_INT_GE(x_int, y_int, "INT: " FAIL_IF_HIDDEN_ARGS, z);
90 }
91
92 { // TEST(Check, UintComparison)
93 MAYBE_SKIP_TEST;
94 unsigned int x_uint = 50;
95 unsigned int y_uint = 66;
96
97 PW_CHECK_UINT_LE(x_uint, y_uint);
98 PW_CHECK_UINT_LE(x_uint, y_uint, "UINT: " FAIL_IF_DISPLAYED);
99 PW_CHECK_UINT_LE(x_uint, y_uint, "UINT: " FAIL_IF_DISPLAYED_ARGS, z);
100
101 PW_CHECK_UINT_GE(x_uint, y_uint);
102 PW_CHECK_UINT_GE(x_uint, y_uint, "UINT: " FAIL_IF_HIDDEN);
103 PW_CHECK_UINT_GE(x_uint, y_uint, "UINT: " FAIL_IF_HIDDEN_ARGS, z);
104 }
105
Keir Mierleb9b88162020-04-15 20:43:09 -0700106 { // TEST(Check, PtrComparison)
107 MAYBE_SKIP_TEST;
108 void* x_ptr = (void*)(50);
109 void* y_ptr = (void*)(66);
110
111 PW_CHECK_PTR_EQ(x_ptr, y_ptr);
112 PW_CHECK_PTR_LE(x_ptr, y_ptr, "PTR: " FAIL_IF_DISPLAYED);
113 PW_CHECK_PTR_LE(x_ptr, y_ptr, "PTR: " FAIL_IF_DISPLAYED_ARGS, z);
114
115 PW_CHECK_PTR_GE(x_ptr, y_ptr);
116 PW_CHECK_PTR_GE(x_ptr, y_ptr, "PTR: " FAIL_IF_HIDDEN);
117 PW_CHECK_PTR_GE(x_ptr, y_ptr, "PTR: " FAIL_IF_HIDDEN_ARGS, z);
118 }
119
Keir Mierle3cee8792020-01-22 17:08:13 -0800120 { // TEST(Check, FloatComparison)
121 MAYBE_SKIP_TEST;
122 float x_float = 50.5;
123 float y_float = 66.5;
124
Ewout van Bekkum9e97cfd2020-07-16 13:57:24 -0700125 PW_CHECK_FLOAT_EXACT_LE(x_float, y_float);
126 PW_CHECK_FLOAT_EXACT_LE(x_float, y_float, "FLOAT: " FAIL_IF_DISPLAYED);
127 PW_CHECK_FLOAT_EXACT_LE(
128 x_float, y_float, "FLOAT: " FAIL_IF_DISPLAYED_ARGS, z);
Keir Mierle3cee8792020-01-22 17:08:13 -0800129
Ewout van Bekkum9e97cfd2020-07-16 13:57:24 -0700130 PW_CHECK_FLOAT_EXACT_GE(x_float, y_float);
131 PW_CHECK_FLOAT_EXACT_GE(x_float, y_float, "FLOAT: " FAIL_IF_HIDDEN);
132 PW_CHECK_FLOAT_EXACT_GE(x_float, y_float, "FLOAT: " FAIL_IF_HIDDEN_ARGS, z);
Keir Mierle3cee8792020-01-22 17:08:13 -0800133 }
134
Keir Mierleb9b88162020-04-15 20:43:09 -0700135 // Don't exhaustively test the DCHECKs but have a sampling of them.
136 { // TEST(DCheck, Sampling)
137 MAYBE_SKIP_TEST;
138 PW_DCHECK(5 == 10);
139 PW_DCHECK(5 == 10, "Message");
140 PW_DCHECK(5 == 10, "Message: %d", 5);
141 PW_DCHECK_INT_LE(5.4, 10.0);
Ewout van Bekkum9e97cfd2020-07-16 13:57:24 -0700142 PW_DCHECK_FLOAT_EXACT_EQ(5.4, 10.0, "Message");
Keir Mierleb9b88162020-04-15 20:43:09 -0700143 }
144
Keir Mierle3cee8792020-01-22 17:08:13 -0800145 { // TEST(Check, ComparisonArgumentsWithCommas)
146 MAYBE_SKIP_TEST;
147 int x_int = 50;
148 int y_int = 66;
149
150 PW_CHECK_INT_LE(Add3(1, 2, 3), y_int);
151 PW_CHECK_INT_LE(x_int, Add3(1, 2, 3));
152
153 PW_CHECK_INT_LE(Add3(1, 2, 3), y_int, FAIL_IF_DISPLAYED);
154 PW_CHECK_INT_LE(x_int, Add3(1, 2, 3), FAIL_IF_DISPLAYED_ARGS, z);
155
156 PW_CHECK_INT_LE(Add3(1, 2, 3), Add3(1, 2, 3), "INT: " FAIL_IF_DISPLAYED);
157 PW_CHECK_INT_LE(x_int, y_int, "INT: " FAIL_IF_DISPLAYED_ARGS, z);
158 }
Keir Mierle8d2a84f2020-04-14 22:00:00 -0700159
160 // Note: This requires enabling PW_ASSERT_USE_SHORT_NAMES 1 above.
161 { // TEST(Check, ShortNamesWork) {
162 MAYBE_SKIP_TEST;
163
164 // Crash
165 CRASH(FAIL_IF_HIDDEN);
166 CRASH(FAIL_IF_HIDDEN_ARGS, z);
167
168 // Check
169 CHECK(1, FAIL_IF_DISPLAYED);
170 CHECK(1, FAIL_IF_DISPLAYED_ARGS, z);
171 CHECK(0, FAIL_IF_HIDDEN);
172 CHECK(0, FAIL_IF_HIDDEN_ARGS, z);
173
174 // Check with binary comparison
175 int x_int = 50;
176 int y_int = 66;
177
178 CHECK_INT_LE(Add3(1, 2, 3), y_int);
179 CHECK_INT_LE(x_int, Add3(1, 2, 3));
180
181 CHECK_INT_LE(Add3(1, 2, 3), y_int, FAIL_IF_DISPLAYED);
182 CHECK_INT_LE(x_int, Add3(1, 2, 3), FAIL_IF_DISPLAYED_ARGS, z);
183
184 CHECK_INT_LE(Add3(1, 2, 3), Add3(1, 2, 3), "INT: " FAIL_IF_DISPLAYED);
185 CHECK_INT_LE(x_int, y_int, "INT: " FAIL_IF_DISPLAYED_ARGS, z);
186 }
Keir Mierle0fa7f7d2020-05-07 12:34:00 -0700187
188 { // Compile tests for PW_ASSERT_OK().
189 PW_CHECK_OK(PW_STATUS_OK);
190 PW_CHECK_OK(PW_STATUS_OK, "msg");
191 PW_CHECK_OK(PW_STATUS_OK, "msg: %d", 5);
192 PW_DCHECK_OK(PW_STATUS_OK);
193 PW_DCHECK_OK(PW_STATUS_OK, "msg");
194 PW_DCHECK_OK(PW_STATUS_OK, "msg: %d", 5);
195 }
Keir Mierle3cee8792020-01-22 17:08:13 -0800196}