blob: fb81a9493038cd0a1a7721919162f510aabb5998 [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
125 PW_CHECK_FLOAT_LE(x_float, y_float);
126 PW_CHECK_FLOAT_LE(x_float, y_float, "FLOAT: " FAIL_IF_DISPLAYED);
127 PW_CHECK_FLOAT_LE(x_float, y_float, "FLOAT: " FAIL_IF_DISPLAYED_ARGS, z);
128
129 PW_CHECK_FLOAT_GE(x_float, y_float);
130 PW_CHECK_FLOAT_GE(x_float, y_float, "FLOAT: " FAIL_IF_HIDDEN);
131 PW_CHECK_FLOAT_GE(x_float, y_float, "FLOAT: " FAIL_IF_HIDDEN_ARGS, z);
132 }
133
Keir Mierleb9b88162020-04-15 20:43:09 -0700134 // Don't exhaustively test the DCHECKs but have a sampling of them.
135 { // TEST(DCheck, Sampling)
136 MAYBE_SKIP_TEST;
137 PW_DCHECK(5 == 10);
138 PW_DCHECK(5 == 10, "Message");
139 PW_DCHECK(5 == 10, "Message: %d", 5);
140 PW_DCHECK_INT_LE(5.4, 10.0);
141 PW_DCHECK_FLOAT_EQ(5.4, 10.0, "Message");
142 }
143
Keir Mierle3cee8792020-01-22 17:08:13 -0800144 { // TEST(Check, ComparisonArgumentsWithCommas)
145 MAYBE_SKIP_TEST;
146 int x_int = 50;
147 int y_int = 66;
148
149 PW_CHECK_INT_LE(Add3(1, 2, 3), y_int);
150 PW_CHECK_INT_LE(x_int, Add3(1, 2, 3));
151
152 PW_CHECK_INT_LE(Add3(1, 2, 3), y_int, FAIL_IF_DISPLAYED);
153 PW_CHECK_INT_LE(x_int, Add3(1, 2, 3), FAIL_IF_DISPLAYED_ARGS, z);
154
155 PW_CHECK_INT_LE(Add3(1, 2, 3), Add3(1, 2, 3), "INT: " FAIL_IF_DISPLAYED);
156 PW_CHECK_INT_LE(x_int, y_int, "INT: " FAIL_IF_DISPLAYED_ARGS, z);
157 }
Keir Mierle8d2a84f2020-04-14 22:00:00 -0700158
159 // Note: This requires enabling PW_ASSERT_USE_SHORT_NAMES 1 above.
160 { // TEST(Check, ShortNamesWork) {
161 MAYBE_SKIP_TEST;
162
163 // Crash
164 CRASH(FAIL_IF_HIDDEN);
165 CRASH(FAIL_IF_HIDDEN_ARGS, z);
166
167 // Check
168 CHECK(1, FAIL_IF_DISPLAYED);
169 CHECK(1, FAIL_IF_DISPLAYED_ARGS, z);
170 CHECK(0, FAIL_IF_HIDDEN);
171 CHECK(0, FAIL_IF_HIDDEN_ARGS, z);
172
173 // Check with binary comparison
174 int x_int = 50;
175 int y_int = 66;
176
177 CHECK_INT_LE(Add3(1, 2, 3), y_int);
178 CHECK_INT_LE(x_int, Add3(1, 2, 3));
179
180 CHECK_INT_LE(Add3(1, 2, 3), y_int, FAIL_IF_DISPLAYED);
181 CHECK_INT_LE(x_int, Add3(1, 2, 3), FAIL_IF_DISPLAYED_ARGS, z);
182
183 CHECK_INT_LE(Add3(1, 2, 3), Add3(1, 2, 3), "INT: " FAIL_IF_DISPLAYED);
184 CHECK_INT_LE(x_int, y_int, "INT: " FAIL_IF_DISPLAYED_ARGS, z);
185 }
Keir Mierle0fa7f7d2020-05-07 12:34:00 -0700186
187 { // Compile tests for PW_ASSERT_OK().
188 PW_CHECK_OK(PW_STATUS_OK);
189 PW_CHECK_OK(PW_STATUS_OK, "msg");
190 PW_CHECK_OK(PW_STATUS_OK, "msg: %d", 5);
191 PW_DCHECK_OK(PW_STATUS_OK);
192 PW_DCHECK_OK(PW_STATUS_OK, "msg");
193 PW_DCHECK_OK(PW_STATUS_OK, "msg: %d", 5);
194 }
Keir Mierle3cee8792020-01-22 17:08:13 -0800195}