blob: 376bdf5330403b3108f5212e49f72a4a6629826c [file] [log] [blame]
Keir Mierle8d2a84f2020-04-14 22:00:00 -07001// 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
15// This test directly verifies the facade logic, by leveraging a fake backend
16// that captures arguments and returns rather than aborting execution.
17
18#include "pw_assert_test/fake_backend.h"
19
20// This directly includes the assert facade implementation header rather than
21// going through the backend header indirection mechanism, to prevent the real
22// assert backend from triggering.
23//
24// clang-format off
25#define PW_ASSERT_USE_SHORT_NAMES 1
26#include "pw_assert/internal/assert_impl.h"
27// clang-format on
28
29#include "gtest/gtest.h"
30
31namespace {
32
33#define EXPECT_MESSAGE(expected_assert_message) \
34 do { \
35 EXPECT_STREQ(pw_captured_assert.message, expected_assert_message); \
36 } while (0)
37
38class AssertFail : public ::testing::Test {
39 protected:
40 void SetUp() override { pw_captured_assert.triggered = 0; }
41 void TearDown() override { EXPECT_EQ(pw_captured_assert.triggered, 1); }
42};
43
44class AssertPass : public ::testing::Test {
45 protected:
46 void SetUp() override { pw_captured_assert.triggered = 0; }
47 void TearDown() override { EXPECT_EQ(pw_captured_assert.triggered, 0); }
48};
49
50// PW_CRASH(...)
51TEST_F(AssertFail, CrashMessageNoArguments) {
52 PW_CRASH("Goodbye");
53 EXPECT_MESSAGE("Goodbye");
54}
55TEST_F(AssertFail, CrashMessageWithArguments) {
56 PW_CRASH("Goodbye cruel %s", "world");
57 EXPECT_MESSAGE("Goodbye cruel world");
58}
59
60// PW_CHECK(...) - No message
61TEST_F(AssertPass, CheckNoMessage) { PW_CHECK(true); }
62TEST_F(AssertFail, CheckNoMessage) {
63 PW_CHECK(false);
64 EXPECT_MESSAGE("Check failed: false. ");
65}
66TEST_F(AssertPass, CheckNoMessageComplexExpression) { PW_CHECK(2 == 2); }
67TEST_F(AssertFail, CheckNoMessageComplexExpression) {
68 PW_CHECK(1 == 2);
69 EXPECT_MESSAGE("Check failed: 1 == 2. ");
70}
71
72// PW_CHECK(..., msg) - With message; with and without arguments.
73TEST_F(AssertPass, CheckMessageNoArguments) { PW_CHECK(true, "Hello"); }
74TEST_F(AssertFail, CheckMessageNoArguments) {
75 PW_CHECK(false, "Hello");
76 EXPECT_MESSAGE("Check failed: false. Hello");
77}
78TEST_F(AssertPass, CheckMessageWithArguments) { PW_CHECK(true, "Hello %d", 5); }
79TEST_F(AssertFail, CheckMessageWithArguments) {
80 PW_CHECK(false, "Hello %d", 5);
81 EXPECT_MESSAGE("Check failed: false. Hello 5");
82}
83
84// PW_CHECK_INT_*(...)
Keir Mierlefa8f89d2020-04-15 16:20:02 -070085// Binary checks with ints, comparisons: <, <=, =, !=, >, >=.
Keir Mierle8d2a84f2020-04-14 22:00:00 -070086
87// Test message formatting separate from the triggering.
88// Only test formatting for the type once.
89TEST_F(AssertFail, IntLessThanNoMessageNoArguments) {
90 PW_CHECK_INT_LT(5, -2);
91 EXPECT_MESSAGE("Check failed: 5 (=5) < -2 (=-2). ");
92}
93TEST_F(AssertFail, IntLessThanMessageNoArguments) {
94 PW_CHECK_INT_LT(5, -2, "msg");
95 EXPECT_MESSAGE("Check failed: 5 (=5) < -2 (=-2). msg");
96}
97TEST_F(AssertFail, IntLessThanMessageArguments) {
98 PW_CHECK_INT_LT(5, -2, "msg: %d", 6);
99 EXPECT_MESSAGE("Check failed: 5 (=5) < -2 (=-2). msg: 6");
100}
101
102// Test comparison boundaries.
103
104// INT <
105TEST_F(AssertPass, IntLt1) { PW_CHECK_INT_LT(-1, 2); }
106TEST_F(AssertPass, IntLt2) { PW_CHECK_INT_LT(1, 2); }
107TEST_F(AssertFail, IntLt3) { PW_CHECK_INT_LT(-1, -2); }
108TEST_F(AssertFail, IntLt4) { PW_CHECK_INT_LT(1, 1); }
109
110// INT <=
111TEST_F(AssertPass, IntLe1) { PW_CHECK_INT_LE(-1, 2); }
112TEST_F(AssertPass, IntLe2) { PW_CHECK_INT_LE(1, 2); }
113TEST_F(AssertFail, IntLe3) { PW_CHECK_INT_LE(-1, -2); }
114TEST_F(AssertPass, IntLe4) { PW_CHECK_INT_LE(1, 1); }
115
116// INT ==
117TEST_F(AssertFail, IntEq1) { PW_CHECK_INT_EQ(-1, 2); }
118TEST_F(AssertFail, IntEq2) { PW_CHECK_INT_EQ(1, 2); }
119TEST_F(AssertFail, IntEq3) { PW_CHECK_INT_EQ(-1, -2); }
120TEST_F(AssertPass, IntEq4) { PW_CHECK_INT_EQ(1, 1); }
121
Keir Mierlefa8f89d2020-04-15 16:20:02 -0700122// INT !=
123TEST_F(AssertPass, IntNe1) { PW_CHECK_INT_NE(-1, 2); }
124TEST_F(AssertPass, IntNe2) { PW_CHECK_INT_NE(1, 2); }
125TEST_F(AssertPass, IntNe3) { PW_CHECK_INT_NE(-1, -2); }
126TEST_F(AssertFail, IntNe4) { PW_CHECK_INT_NE(1, 1); }
127
Keir Mierle8d2a84f2020-04-14 22:00:00 -0700128// INT >
129TEST_F(AssertFail, IntGt1) { PW_CHECK_INT_GT(-1, 2); }
130TEST_F(AssertFail, IntGt2) { PW_CHECK_INT_GT(1, 2); }
131TEST_F(AssertPass, IntGt3) { PW_CHECK_INT_GT(-1, -2); }
132TEST_F(AssertFail, IntGt4) { PW_CHECK_INT_GT(1, 1); }
133
134// INT >=
135TEST_F(AssertFail, IntGe1) { PW_CHECK_INT_GE(-1, 2); }
136TEST_F(AssertFail, IntGe2) { PW_CHECK_INT_GE(1, 2); }
137TEST_F(AssertPass, IntGe3) { PW_CHECK_INT_GE(-1, -2); }
138TEST_F(AssertPass, IntGe4) { PW_CHECK_INT_GE(1, 1); }
139
140// PW_CHECK_UINT_*(...)
Keir Mierlefa8f89d2020-04-15 16:20:02 -0700141// Binary checks with uints, comparisons: <, <=, =, !=, >, >=.
Keir Mierle8d2a84f2020-04-14 22:00:00 -0700142
143// Test message formatting separate from the triggering.
144// Only test formatting for the type once.
145TEST_F(AssertFail, UintLessThanNoMessageNoArguments) {
146 PW_CHECK_UINT_LT(5, 2);
147 EXPECT_MESSAGE("Check failed: 5 (=5) < 2 (=2). ");
148}
149TEST_F(AssertFail, UintLessThanMessageNoArguments) {
150 PW_CHECK_UINT_LT(5, 2, "msg");
151 EXPECT_MESSAGE("Check failed: 5 (=5) < 2 (=2). msg");
152}
153TEST_F(AssertFail, UintLessThanMessageArguments) {
154 PW_CHECK_UINT_LT(5, 2, "msg: %d", 6);
155 EXPECT_MESSAGE("Check failed: 5 (=5) < 2 (=2). msg: 6");
156}
157
158// Test comparison boundaries.
159
160// UINT <
161TEST_F(AssertPass, UintLt1) { PW_CHECK_UINT_LT(1, 2); }
162TEST_F(AssertFail, UintLt2) { PW_CHECK_UINT_LT(2, 2); }
163TEST_F(AssertFail, UintLt3) { PW_CHECK_UINT_LT(2, 1); }
164
165// UINT <=
166TEST_F(AssertPass, UintLe1) { PW_CHECK_UINT_LE(1, 2); }
167TEST_F(AssertPass, UintLe2) { PW_CHECK_UINT_LE(2, 2); }
168TEST_F(AssertFail, UintLe3) { PW_CHECK_UINT_LE(2, 1); }
169
170// UINT ==
171TEST_F(AssertFail, UintEq1) { PW_CHECK_UINT_EQ(1, 2); }
172TEST_F(AssertPass, UintEq2) { PW_CHECK_UINT_EQ(2, 2); }
173TEST_F(AssertFail, UintEq3) { PW_CHECK_UINT_EQ(2, 1); }
174
Keir Mierlefa8f89d2020-04-15 16:20:02 -0700175// UINT !=
176TEST_F(AssertPass, UintNe1) { PW_CHECK_UINT_NE(1, 2); }
177TEST_F(AssertFail, UintNe2) { PW_CHECK_UINT_NE(2, 2); }
178TEST_F(AssertPass, UintNe3) { PW_CHECK_UINT_NE(2, 1); }
179
Keir Mierle8d2a84f2020-04-14 22:00:00 -0700180// UINT >
181TEST_F(AssertFail, UintGt1) { PW_CHECK_UINT_GT(1, 2); }
182TEST_F(AssertFail, UintGt2) { PW_CHECK_UINT_GT(2, 2); }
183TEST_F(AssertPass, UintGt3) { PW_CHECK_UINT_GT(2, 1); }
184
185// UINT >=
186TEST_F(AssertFail, UintGe1) { PW_CHECK_UINT_GE(1, 2); }
187TEST_F(AssertPass, UintGe2) { PW_CHECK_UINT_GE(2, 2); }
188TEST_F(AssertPass, UintGe3) { PW_CHECK_UINT_GE(2, 1); }
189
Keir Mierlefa8f89d2020-04-15 16:20:02 -0700190// PW_CHECK_PTR_*(...)
191// Binary checks with uints, comparisons: <, <=, =, !=, >, >=.
192// Note: The format checks are skipped since they're not portable.
193
194// Test comparison boundaries.
195
196// PTR <
197TEST_F(AssertPass, PtrLt1) { PW_CHECK_PTR_LT(0xa, 0xb); }
198TEST_F(AssertFail, PtrLt2) { PW_CHECK_PTR_LT(0xb, 0xb); }
199TEST_F(AssertFail, PtrLt3) { PW_CHECK_PTR_LT(0xb, 0xa); }
200
201// PTR <=
202TEST_F(AssertPass, PtrLe1) { PW_CHECK_PTR_LE(0xa, 0xb); }
203TEST_F(AssertPass, PtrLe2) { PW_CHECK_PTR_LE(0xb, 0xb); }
204TEST_F(AssertFail, PtrLe3) { PW_CHECK_PTR_LE(0xb, 0xa); }
205
206// PTR ==
207TEST_F(AssertFail, PtrEq1) { PW_CHECK_PTR_EQ(0xa, 0xb); }
208TEST_F(AssertPass, PtrEq2) { PW_CHECK_PTR_EQ(0xb, 0xb); }
209TEST_F(AssertFail, PtrEq3) { PW_CHECK_PTR_EQ(0xb, 0xa); }
210
211// PTR !=
212TEST_F(AssertPass, PtrNe1) { PW_CHECK_PTR_NE(0xa, 0xb); }
213TEST_F(AssertFail, PtrNe2) { PW_CHECK_PTR_NE(0xb, 0xb); }
214TEST_F(AssertPass, PtrNe3) { PW_CHECK_PTR_NE(0xb, 0xa); }
215
216// PTR >
217TEST_F(AssertFail, PtrGt1) { PW_CHECK_PTR_GT(0xa, 0xb); }
218TEST_F(AssertFail, PtrGt2) { PW_CHECK_PTR_GT(0xb, 0xb); }
219TEST_F(AssertPass, PtrGt3) { PW_CHECK_PTR_GT(0xb, 0xa); }
220
221// PTR >=
222TEST_F(AssertFail, PtrGe1) { PW_CHECK_PTR_GE(0xa, 0xb); }
223TEST_F(AssertPass, PtrGe2) { PW_CHECK_PTR_GE(0xb, 0xb); }
224TEST_F(AssertPass, PtrGe3) { PW_CHECK_PTR_GE(0xb, 0xa); }
225
226// NOTNULL
227TEST_F(AssertPass, PtrNotNull) { PW_CHECK_NOTNULL(0xa); }
228TEST_F(AssertFail, PtrNotNull) { PW_CHECK_NOTNULL(0x0); }
229
Keir Mierlea84fd8d2020-08-07 21:29:26 -0700230// Note: Due to platform inconsistencies, the below test for the NOTNULL
231// message doesn't work. Some platforms print NULL formatted as %p as "(nil)",
232// others "0x0". Leaving this here for reference.
233//
234// TEST_F(AssertFail, PtrNotNullDescription) {
235// intptr_t intptr = 0;
236// PW_CHECK_NOTNULL(intptr);
237// EXPECT_MESSAGE("Check failed: intptr (=0x0) != nullptr (=0x0). ");
238// }
239
Keir Mierle8d2a84f2020-04-14 22:00:00 -0700240// PW_CHECK_FLOAT_*(...)
Ewout van Bekkum9e97cfd2020-07-16 13:57:24 -0700241// Binary checks with floats, comparisons: EXACT_LT, EXACT_LE, NEAR, EXACT_EQ,
242// EXACT_NE, EXACT_GE, EXACT_GT.
Keir Mierle8d2a84f2020-04-14 22:00:00 -0700243
244// Test message formatting separate from the triggering.
245// Only test formatting for the type once.
246TEST_F(AssertFail, FloatLessThanNoMessageNoArguments) {
Ewout van Bekkum9e97cfd2020-07-16 13:57:24 -0700247 PW_CHECK_FLOAT_EXACT_LT(5.2, 2.3);
Keir Mierle8d2a84f2020-04-14 22:00:00 -0700248 EXPECT_MESSAGE("Check failed: 5.2 (=5.200000) < 2.3 (=2.300000). ");
249}
250TEST_F(AssertFail, FloatLessThanMessageNoArguments) {
Ewout van Bekkum9e97cfd2020-07-16 13:57:24 -0700251 PW_CHECK_FLOAT_EXACT_LT(5.2, 2.3, "msg");
Keir Mierle8d2a84f2020-04-14 22:00:00 -0700252 EXPECT_MESSAGE("Check failed: 5.2 (=5.200000) < 2.3 (=2.300000). msg");
253}
254TEST_F(AssertFail, FloatLessThanMessageArguments) {
Ewout van Bekkum9e97cfd2020-07-16 13:57:24 -0700255 PW_CHECK_FLOAT_EXACT_LT(5.2, 2.3, "msg: %d", 6);
Keir Mierle8d2a84f2020-04-14 22:00:00 -0700256 EXPECT_MESSAGE("Check failed: 5.2 (=5.200000) < 2.3 (=2.300000). msg: 6");
257}
Ewout van Bekkum9e97cfd2020-07-16 13:57:24 -0700258// Check float NEAR both above and below the permitted range.
259TEST_F(AssertFail, FloatNearAboveNoMessageNoArguments) {
260 PW_CHECK_FLOAT_NEAR(5.2, 2.3, 0.1);
261 EXPECT_MESSAGE(
262 "Check failed: 5.2 (=5.200000) <= 2.3 + abs_tolerance (=2.400000). ");
263}
264TEST_F(AssertFail, FloatNearAboveMessageNoArguments) {
265 PW_CHECK_FLOAT_NEAR(5.2, 2.3, 0.1, "msg");
266 EXPECT_MESSAGE(
267 "Check failed: 5.2 (=5.200000) <= 2.3 + abs_tolerance (=2.400000). msg");
268}
269TEST_F(AssertFail, FloatNearAboveMessageArguments) {
270 PW_CHECK_FLOAT_NEAR(5.2, 2.3, 0.1, "msg: %d", 6);
271 EXPECT_MESSAGE(
272 "Check failed: 5.2 (=5.200000) <= 2.3 + abs_tolerance (=2.400000). msg: "
273 "6");
274}
275TEST_F(AssertFail, FloatNearBelowNoMessageNoArguments) {
276 PW_CHECK_FLOAT_NEAR(1.2, 2.3, 0.1);
277 EXPECT_MESSAGE(
278 "Check failed: 1.2 (=1.200000) >= 2.3 - abs_tolerance (=2.200000). ");
279}
280TEST_F(AssertFail, FloatNearBelowMessageNoArguments) {
281 PW_CHECK_FLOAT_NEAR(1.2, 2.3, 0.1, "msg");
282 EXPECT_MESSAGE(
283 "Check failed: 1.2 (=1.200000) >= 2.3 - abs_tolerance (=2.200000). msg");
284}
285TEST_F(AssertFail, FloatNearBelowMessageArguments) {
286 PW_CHECK_FLOAT_NEAR(1.2, 2.3, 0.1, "msg: %d", 6);
287 EXPECT_MESSAGE(
288 "Check failed: 1.2 (=1.200000) >= 2.3 - abs_tolerance (=2.200000). msg: "
289 "6");
290}
Keir Mierle8d2a84f2020-04-14 22:00:00 -0700291// Test comparison boundaries.
292// Note: The below example numbers all round to integer 1, to detect accidental
293// integer conversions in the asserts.
294
295// FLOAT <
Ewout van Bekkum9e97cfd2020-07-16 13:57:24 -0700296TEST_F(AssertPass, FloatLt1) { PW_CHECK_FLOAT_EXACT_LT(1.1, 1.2); }
297TEST_F(AssertFail, FloatLt2) { PW_CHECK_FLOAT_EXACT_LT(1.2, 1.2); }
298TEST_F(AssertFail, FloatLt3) { PW_CHECK_FLOAT_EXACT_LT(1.2, 1.1); }
Keir Mierle8d2a84f2020-04-14 22:00:00 -0700299
300// FLOAT <=
Ewout van Bekkum9e97cfd2020-07-16 13:57:24 -0700301TEST_F(AssertPass, FloatLe1) { PW_CHECK_FLOAT_EXACT_LE(1.1, 1.2); }
302TEST_F(AssertPass, FloatLe2) { PW_CHECK_FLOAT_EXACT_LE(1.2, 1.2); }
303TEST_F(AssertFail, FloatLe3) { PW_CHECK_FLOAT_EXACT_LE(1.2, 1.1); }
304
305// FLOAT ~= based on absolute error.
306TEST_F(AssertFail, FloatNearAbs1) { PW_CHECK_FLOAT_NEAR(1.09, 1.2, 0.1); }
307TEST_F(AssertPass, FloatNearAbs2) { PW_CHECK_FLOAT_NEAR(1.1, 1.2, 0.1); }
308TEST_F(AssertPass, FloatNearAbs3) { PW_CHECK_FLOAT_NEAR(1.2, 1.2, 0.1); }
309TEST_F(AssertPass, FloatNearAbs4) { PW_CHECK_FLOAT_NEAR(1.2, 1.1, 0.1); }
310TEST_F(AssertFail, FloatNearAbs5) { PW_CHECK_FLOAT_NEAR(1.21, 1.1, 0.1); }
311// Make sure the abs_tolerance is asserted to be >= 0.
312TEST_F(AssertFail, FloatNearAbs6) { PW_CHECK_FLOAT_NEAR(1.2, 1.2, -0.1); }
313TEST_F(AssertPass, FloatNearAbs7) { PW_CHECK_FLOAT_NEAR(1.2, 1.2, 0.0); }
Keir Mierle8d2a84f2020-04-14 22:00:00 -0700314
315// FLOAT ==
Ewout van Bekkum9e97cfd2020-07-16 13:57:24 -0700316TEST_F(AssertFail, FloatEq1) { PW_CHECK_FLOAT_EXACT_EQ(1.1, 1.2); }
317TEST_F(AssertPass, FloatEq2) { PW_CHECK_FLOAT_EXACT_EQ(1.2, 1.2); }
318TEST_F(AssertFail, FloatEq3) { PW_CHECK_FLOAT_EXACT_EQ(1.2, 1.1); }
Keir Mierle8d2a84f2020-04-14 22:00:00 -0700319
Keir Mierlefa8f89d2020-04-15 16:20:02 -0700320// FLOAT !=
Ewout van Bekkum9e97cfd2020-07-16 13:57:24 -0700321TEST_F(AssertPass, FloatNe1) { PW_CHECK_FLOAT_EXACT_NE(1.1, 1.2); }
322TEST_F(AssertFail, FloatNe2) { PW_CHECK_FLOAT_EXACT_NE(1.2, 1.2); }
323TEST_F(AssertPass, FloatNe3) { PW_CHECK_FLOAT_EXACT_NE(1.2, 1.1); }
Keir Mierlefa8f89d2020-04-15 16:20:02 -0700324
Keir Mierle8d2a84f2020-04-14 22:00:00 -0700325// FLOAT >
Ewout van Bekkum9e97cfd2020-07-16 13:57:24 -0700326TEST_F(AssertFail, FloatGt1) { PW_CHECK_FLOAT_EXACT_GT(1.1, 1.2); }
327TEST_F(AssertFail, FloatGt2) { PW_CHECK_FLOAT_EXACT_GT(1.2, 1.2); }
328TEST_F(AssertPass, FloatGt3) { PW_CHECK_FLOAT_EXACT_GT(1.2, 1.1); }
Keir Mierle8d2a84f2020-04-14 22:00:00 -0700329
330// FLOAT >=
Ewout van Bekkum9e97cfd2020-07-16 13:57:24 -0700331TEST_F(AssertFail, FloatGe1) { PW_CHECK_FLOAT_EXACT_GE(1.1, 1.2); }
332TEST_F(AssertPass, FloatGe2) { PW_CHECK_FLOAT_EXACT_GE(1.2, 1.2); }
333TEST_F(AssertPass, FloatGe3) { PW_CHECK_FLOAT_EXACT_GE(1.2, 1.1); }
Keir Mierle8d2a84f2020-04-14 22:00:00 -0700334
335// Nested comma handling.
336static int Add3(int a, int b, int c) { return a + b + c; }
337
338TEST_F(AssertFail, CommaHandlingLeftSide) {
339 PW_CHECK_INT_EQ(Add3(1, 2, 3), 4);
340 EXPECT_MESSAGE("Check failed: Add3(1, 2, 3) (=6) == 4 (=4). ");
341}
342TEST_F(AssertFail, CommaHandlingRightSide) {
343 PW_CHECK_INT_EQ(4, Add3(1, 2, 3));
344 EXPECT_MESSAGE("Check failed: 4 (=4) == Add3(1, 2, 3) (=6). ");
345}
346
347// Verify that the CHECK_*(x,y) macros only evaluate their arguments once.
348static int global_state_for_multi_evaluate_test;
349static int IncrementsGlobal() {
350 global_state_for_multi_evaluate_test++;
351 return 0;
352}
353
354TEST(AssertPass, CheckSingleSideEffectingCall) {
355 global_state_for_multi_evaluate_test = 0;
356 PW_CHECK(IncrementsGlobal() == 0);
357 EXPECT_EQ(global_state_for_multi_evaluate_test, 1);
358}
359TEST(AssertFail, CheckSingleSideEffectingCall) {
360 global_state_for_multi_evaluate_test = 0;
361 PW_CHECK(IncrementsGlobal() == 1);
362 EXPECT_EQ(global_state_for_multi_evaluate_test, 1);
363}
364TEST(AssertPass, BinaryOpSingleSideEffectingCall) {
365 global_state_for_multi_evaluate_test = 0;
366 PW_CHECK_INT_EQ(0, IncrementsGlobal());
367 EXPECT_EQ(global_state_for_multi_evaluate_test, 1);
368}
369TEST(AssertPass, BinaryOpTwoSideEffectingCalls) {
370 global_state_for_multi_evaluate_test = 0;
371 PW_CHECK_INT_EQ(IncrementsGlobal(), IncrementsGlobal());
372 EXPECT_EQ(global_state_for_multi_evaluate_test, 2);
373}
374TEST(AssertFail, BinaryOpSingleSideEffectingCall) {
375 global_state_for_multi_evaluate_test = 0;
376 PW_CHECK_INT_EQ(12314, IncrementsGlobal());
377 EXPECT_EQ(global_state_for_multi_evaluate_test, 1);
378}
379TEST(AssertFail, BinaryOpTwoSideEffectingCalls) {
380 global_state_for_multi_evaluate_test = 0;
381 PW_CHECK_INT_EQ(IncrementsGlobal() + 10, IncrementsGlobal());
382 EXPECT_EQ(global_state_for_multi_evaluate_test, 2);
383}
384
Keir Mierleb9b88162020-04-15 20:43:09 -0700385// Verify side effects of debug checks work as expected.
386// Only check a couple of cases, since the logic is all the same.
Keir Mierle854adec2020-09-03 14:07:19 -0700387#if PW_ASSERT_ENABLE_DEBUG
Keir Mierleb9b88162020-04-15 20:43:09 -0700388// When DCHECKs are enabled, they behave the same as normal checks.
389TEST(AssertPass, DCheckEnabledSingleSideEffectingCall) {
390 global_state_for_multi_evaluate_test = 0;
391 PW_DCHECK(IncrementsGlobal() == 0);
392 EXPECT_EQ(global_state_for_multi_evaluate_test, 1);
393}
394TEST(AssertFail, DCheckEnabledSingleSideEffectingCall) {
395 global_state_for_multi_evaluate_test = 0;
396 PW_DCHECK(IncrementsGlobal() == 1);
397 EXPECT_EQ(global_state_for_multi_evaluate_test, 1);
398}
399TEST(AssertPass, DCheckEnabledBinaryOpSingleSideEffectingCall) {
400 global_state_for_multi_evaluate_test = 0;
401 PW_DCHECK_INT_EQ(0, IncrementsGlobal());
402 EXPECT_EQ(global_state_for_multi_evaluate_test, 1);
403}
404TEST(AssertPass, DCheckEnabledBinaryOpTwoSideEffectingCalls) {
405 global_state_for_multi_evaluate_test = 0;
406 PW_DCHECK_INT_EQ(IncrementsGlobal(), IncrementsGlobal());
407 EXPECT_EQ(global_state_for_multi_evaluate_test, 2);
408}
409TEST(AssertFail, DCheckEnabledBinaryOpSingleSideEffectingCall) {
410 global_state_for_multi_evaluate_test = 0;
411 PW_DCHECK_INT_EQ(12314, IncrementsGlobal());
412 EXPECT_EQ(global_state_for_multi_evaluate_test, 1);
413}
414TEST(AssertFail, DCheckEnabledBinaryOpTwoSideEffectingCalls) {
415 global_state_for_multi_evaluate_test = 0;
416 PW_DCHECK_INT_EQ(IncrementsGlobal() + 10, IncrementsGlobal());
417 EXPECT_EQ(global_state_for_multi_evaluate_test, 2);
418}
419
Keir Mierle854adec2020-09-03 14:07:19 -0700420#else // PW_ASSERT_ENABLE_DEBUG
Keir Mierleb9b88162020-04-15 20:43:09 -0700421
422// When DCHECKs are disabled, they should not trip, and their arguments
423// shouldn't be evaluated.
Wyatt Heplerd78f7c62020-09-28 14:27:32 -0700424TEST(AssertPass, DCheckDisabledSingleSideEffectingCall_1) {
Keir Mierleb9b88162020-04-15 20:43:09 -0700425 global_state_for_multi_evaluate_test = 0;
426 PW_DCHECK(IncrementsGlobal() == 0);
427 EXPECT_EQ(global_state_for_multi_evaluate_test, 0);
428}
Wyatt Heplerd78f7c62020-09-28 14:27:32 -0700429TEST(AssertPass, DCheckDisabledSingleSideEffectingCall_2) {
Keir Mierleb9b88162020-04-15 20:43:09 -0700430 global_state_for_multi_evaluate_test = 0;
431 PW_DCHECK(IncrementsGlobal() == 1);
432 EXPECT_EQ(global_state_for_multi_evaluate_test, 0);
433}
Wyatt Heplerd78f7c62020-09-28 14:27:32 -0700434TEST(AssertPass, DCheckDisabledBinaryOpSingleSideEffectingCall_1) {
Keir Mierleb9b88162020-04-15 20:43:09 -0700435 global_state_for_multi_evaluate_test = 0;
436 PW_DCHECK_INT_EQ(0, IncrementsGlobal());
437 EXPECT_EQ(global_state_for_multi_evaluate_test, 0);
438}
Wyatt Heplerd78f7c62020-09-28 14:27:32 -0700439TEST(AssertPass, DCheckDisabledBinaryOpTwoSideEffectingCalls_1) {
Keir Mierleb9b88162020-04-15 20:43:09 -0700440 global_state_for_multi_evaluate_test = 0;
441 PW_DCHECK_INT_EQ(IncrementsGlobal(), IncrementsGlobal());
442 EXPECT_EQ(global_state_for_multi_evaluate_test, 0);
443}
Wyatt Heplerd78f7c62020-09-28 14:27:32 -0700444TEST(AssertPass, DCheckDisabledBinaryOpSingleSideEffectingCall_2) {
Keir Mierleb9b88162020-04-15 20:43:09 -0700445 global_state_for_multi_evaluate_test = 0;
446 PW_DCHECK_INT_EQ(12314, IncrementsGlobal());
447 EXPECT_EQ(global_state_for_multi_evaluate_test, 0);
448}
Wyatt Heplerd78f7c62020-09-28 14:27:32 -0700449TEST(AssertPass, DCheckDisabledBinaryOpTwoSideEffectingCalls_2) {
Keir Mierleb9b88162020-04-15 20:43:09 -0700450 global_state_for_multi_evaluate_test = 0;
451 PW_DCHECK_INT_EQ(IncrementsGlobal() + 10, IncrementsGlobal());
452 EXPECT_EQ(global_state_for_multi_evaluate_test, 0);
453}
Keir Mierle854adec2020-09-03 14:07:19 -0700454#endif // PW_ASSERT_ENABLE_DEBUG
Keir Mierleb9b88162020-04-15 20:43:09 -0700455
Keir Mierle8d2a84f2020-04-14 22:00:00 -0700456// Note: This requires enabling PW_ASSERT_USE_SHORT_NAMES 1 above.
457TEST(Check, ShortNamesWork) {
458 // Crash
459 CRASH("msg");
460 CRASH("msg: %d", 5);
461
462 // Check
463 CHECK(true);
464 CHECK(true, "msg");
465 CHECK(true, "msg: %d", 5);
466 CHECK(false);
467 CHECK(false, "msg");
468 CHECK(false, "msg: %d", 5);
469
470 // Check with binary comparison
471 CHECK_INT_LE(1, 2);
472 CHECK_INT_LE(1, 2, "msg");
473 CHECK_INT_LE(1, 2, "msg: %d", 5);
474}
475
Keir Mierle0fa7f7d2020-05-07 12:34:00 -0700476// Verify PW_CHECK_OK, including message handling.
477TEST_F(AssertFail, StatusNotOK) {
Wyatt Heplerd78f7c62020-09-28 14:27:32 -0700478 pw::Status status = pw::Status::Unknown();
Keir Mierle0fa7f7d2020-05-07 12:34:00 -0700479 PW_CHECK_OK(status);
Wyatt Hepler1b3da3a2021-01-07 13:26:57 -0800480 EXPECT_MESSAGE("Check failed: status (=UNKNOWN) == OkStatus() (=OK). ");
Keir Mierle0fa7f7d2020-05-07 12:34:00 -0700481}
482
483TEST_F(AssertFail, StatusNotOKMessageNoArguments) {
Wyatt Heplerd78f7c62020-09-28 14:27:32 -0700484 pw::Status status = pw::Status::Unknown();
Keir Mierle0fa7f7d2020-05-07 12:34:00 -0700485 PW_CHECK_OK(status, "msg");
Wyatt Hepler1b3da3a2021-01-07 13:26:57 -0800486 EXPECT_MESSAGE("Check failed: status (=UNKNOWN) == OkStatus() (=OK). msg");
Keir Mierle0fa7f7d2020-05-07 12:34:00 -0700487}
488
489TEST_F(AssertFail, StatusNotOKMessageArguments) {
Wyatt Heplerd78f7c62020-09-28 14:27:32 -0700490 pw::Status status = pw::Status::Unknown();
Keir Mierle0fa7f7d2020-05-07 12:34:00 -0700491 PW_CHECK_OK(status, "msg: %d", 5);
Wyatt Hepler1b3da3a2021-01-07 13:26:57 -0800492 EXPECT_MESSAGE("Check failed: status (=UNKNOWN) == OkStatus() (=OK). msg: 5");
Keir Mierle0fa7f7d2020-05-07 12:34:00 -0700493}
494
495// Example expression for the test below.
Wyatt Heplerd78f7c62020-09-28 14:27:32 -0700496pw::Status DoTheThing() { return pw::Status::ResourceExhausted(); }
Keir Mierle0fa7f7d2020-05-07 12:34:00 -0700497
498TEST_F(AssertFail, NonTrivialExpression) {
499 PW_CHECK_OK(DoTheThing());
500 EXPECT_MESSAGE(
Wyatt Hepler1b3da3a2021-01-07 13:26:57 -0800501 "Check failed: DoTheThing() (=RESOURCE_EXHAUSTED) == OkStatus() (=OK). ");
Keir Mierle0fa7f7d2020-05-07 12:34:00 -0700502}
503
504// Note: This function seems pointless but it is not, since pw::Status::FOO
505// constants are not actually status objects, but code objects. This way we can
506// ensure the macros work with both real status objects and literals.
Wyatt Hepler1b3da3a2021-01-07 13:26:57 -0800507TEST_F(AssertPass, Function) { PW_CHECK_OK(pw::OkStatus()); }
Keir Mierle0fa7f7d2020-05-07 12:34:00 -0700508TEST_F(AssertPass, Enum) { PW_CHECK_OK(PW_STATUS_OK); }
Wyatt Heplerd78f7c62020-09-28 14:27:32 -0700509TEST_F(AssertFail, Function) { PW_CHECK_OK(pw::Status::Unknown()); }
Keir Mierle0fa7f7d2020-05-07 12:34:00 -0700510TEST_F(AssertFail, Enum) { PW_CHECK_OK(PW_STATUS_UNKNOWN); }
511
Keir Mierle854adec2020-09-03 14:07:19 -0700512#if PW_ASSERT_ENABLE_DEBUG
Keir Mierle0fa7f7d2020-05-07 12:34:00 -0700513
514// In debug mode, the asserts should check their arguments.
Wyatt Hepler1b3da3a2021-01-07 13:26:57 -0800515TEST_F(AssertPass, DCheckFunction) { PW_DCHECK_OK(pw::OkStatus()); }
Wyatt Heplerd78f7c62020-09-28 14:27:32 -0700516TEST_F(AssertPass, DCheckEnum) { PW_DCHECK_OK(PW_STATUS_OK); }
517TEST_F(AssertFail, DCheckFunction) { PW_DCHECK_OK(pw::Status::Unknown()); }
518TEST_F(AssertFail, DCheckEnum) { PW_DCHECK_OK(PW_STATUS_UNKNOWN); }
Keir Mierle854adec2020-09-03 14:07:19 -0700519#else // PW_ASSERT_ENABLE_DEBUG
Keir Mierle0fa7f7d2020-05-07 12:34:00 -0700520
521// In release mode, all the asserts should pass.
Wyatt Hepler1b3da3a2021-01-07 13:26:57 -0800522TEST_F(AssertPass, DCheckFunction_Ok) { PW_DCHECK_OK(pw::OkStatus()); }
Wyatt Heplerd78f7c62020-09-28 14:27:32 -0700523TEST_F(AssertPass, DCheckEnum_Ok) { PW_DCHECK_OK(PW_STATUS_OK); }
524TEST_F(AssertPass, DCheckFunction_Err) { PW_DCHECK_OK(pw::Status::Unknown()); }
525TEST_F(AssertPass, DCheckEnum_Err) { PW_DCHECK_OK(PW_STATUS_UNKNOWN); }
Keir Mierle854adec2020-09-03 14:07:19 -0700526#endif // PW_ASSERT_ENABLE_DEBUG
Keir Mierle0fa7f7d2020-05-07 12:34:00 -0700527
Keir Mierle8d2a84f2020-04-14 22:00:00 -0700528// TODO: Figure out how to run some of these tests is C.
529
530} // namespace