blob: 5532f2ee9ecb2db8cbd5f376fb404c1caf0994eb [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
15// This is mostly a compile test to verify that the log backend is able to
16// compile the constructs promised by the logging facade; and that when run,
17// there is no crash.
18//
19// TODO(pwbug/88): Add verification of the actually logged statements.
20
21// clang-format off
Keir Mierle0743cc52020-02-07 16:08:33 -080022#define PW_ASSERT_USE_SHORT_NAMES 1
Keir Mierle3cee8792020-01-22 17:08:13 -080023#include "pw_assert/assert.h"
24// clang-format on
25
26#include "gtest/gtest.h"
27
28// This is a global constant to feed into the formatter for tests.
29// Intended to pair with FAIL_IF_DISPLAYED_ARGS or FAIL_IF_HIDDEN_ARGS.
30static const int z = 10;
31
32// At some point in the future when there is a proper test system in place for
33// crashing, the below strings can help indicate pass/fail for a check.
34
35#define FAIL_IF_DISPLAYED "FAIL IF DISPLAYED"
36#define FAIL_IF_DISPLAYED_ARGS "FAIL IF DISPLAYED: %d"
37
38#define FAIL_IF_HIDDEN "FAIL IF HIDDEN"
39#define FAIL_IF_HIDDEN_ARGS "FAIL IF HIDDEN: %d"
40
41// This switch exists to support compiling and/or running the tests.
42#define DISABLE_ASSERT_TEST_EXECUTION 1
43#if DISABLE_ASSERT_TEST_EXECUTION
44#define MAYBE_SKIP_TEST return
45#else
46#define MAYBE_SKIP_TEST ;
47#endif
48
49namespace {
50
51TEST(Crash, WithAndWithoutMessageArguments) {
52 MAYBE_SKIP_TEST;
53 PW_CRASH(FAIL_IF_HIDDEN);
54 PW_CRASH(FAIL_IF_HIDDEN_ARGS, z);
55}
56
57TEST(Check, NoMessage) {
58 MAYBE_SKIP_TEST;
59 PW_CHECK(true);
60 PW_CHECK(false);
61}
62
63TEST(Check, WithMessageAndArgs) {
64 MAYBE_SKIP_TEST;
65 PW_CHECK(true, FAIL_IF_DISPLAYED);
66 PW_CHECK(true, FAIL_IF_DISPLAYED_ARGS, z);
67
68 PW_CHECK(false, FAIL_IF_HIDDEN);
69 PW_CHECK(false, FAIL_IF_HIDDEN_ARGS, z);
70}
71
72TEST(Check, IntComparison) {
73 MAYBE_SKIP_TEST;
74 int x_int = 50;
75 int y_int = 66;
76
77 PW_CHECK_INT_LE(x_int, y_int);
78 PW_CHECK_INT_LE(x_int, y_int, "INT: " FAIL_IF_DISPLAYED);
79 PW_CHECK_INT_LE(x_int, y_int, "INT: " FAIL_IF_DISPLAYED_ARGS, z);
80
81 PW_CHECK_INT_GE(x_int, y_int);
82 PW_CHECK_INT_GE(x_int, y_int, "INT: " FAIL_IF_HIDDEN);
83 PW_CHECK_INT_GE(x_int, y_int, "INT: " FAIL_IF_HIDDEN_ARGS, z);
84}
85
86TEST(Check, UintComparison) {
87 MAYBE_SKIP_TEST;
88 unsigned int x_uint = 50;
89 unsigned int y_uint = 66;
90
91 PW_CHECK_UINT_LE(x_uint, y_uint);
92 PW_CHECK_UINT_LE(x_uint, y_uint, "UINT: " FAIL_IF_DISPLAYED);
93 PW_CHECK_UINT_LE(x_uint, y_uint, "UINT: " FAIL_IF_DISPLAYED_ARGS, z);
94
95 PW_CHECK_UINT_GE(x_uint, y_uint);
96 PW_CHECK_UINT_GE(x_uint, y_uint, "UINT: " FAIL_IF_HIDDEN);
97 PW_CHECK_UINT_GE(x_uint, y_uint, "UINT: " FAIL_IF_HIDDEN_ARGS, z);
98}
99
100TEST(Check, FloatComparison) {
101 MAYBE_SKIP_TEST;
102 float x_float = 50.5;
103 float y_float = 66.5;
104
105 PW_CHECK_FLOAT_LE(x_float, y_float);
106 PW_CHECK_FLOAT_LE(x_float, y_float, "FLOAT: " FAIL_IF_DISPLAYED);
107 PW_CHECK_FLOAT_LE(x_float, y_float, "FLOAT: " FAIL_IF_DISPLAYED_ARGS, z);
108
109 PW_CHECK_FLOAT_GE(x_float, y_float);
110 PW_CHECK_FLOAT_GE(x_float, y_float, "FLOAT: " FAIL_IF_HIDDEN);
111 PW_CHECK_FLOAT_GE(x_float, y_float, "FLOAT: " FAIL_IF_HIDDEN_ARGS, z);
112}
113
114static int Add3(int a, int b, int c) { return a + b + c; }
115
116TEST(Check, ComparisonArgumentsWithCommas) {
117 MAYBE_SKIP_TEST;
118 int x_int = 50;
119 int y_int = 66;
120
121 PW_CHECK_INT_LE(Add3(1, 2, 3), y_int);
122 PW_CHECK_INT_LE(x_int, Add3(1, 2, 3));
123
124 PW_CHECK_INT_LE(Add3(1, 2, 3), y_int, FAIL_IF_DISPLAYED);
125 PW_CHECK_INT_LE(x_int, Add3(1, 2, 3), FAIL_IF_DISPLAYED_ARGS, z);
126
127 PW_CHECK_INT_LE(Add3(1, 2, 3), Add3(1, 2, 3), "INT: " FAIL_IF_DISPLAYED);
128 PW_CHECK_INT_LE(x_int, y_int, "INT: " FAIL_IF_DISPLAYED_ARGS, z);
129}
130
131// These are defined in assert_test.c, to test C compatibility.
132extern "C" {
133void AssertTestsInC();
134} // extern "C"
135
136TEST(Check, AssertTestsInC) {
137 MAYBE_SKIP_TEST;
138 AssertTestsInC();
139}
140
141static int global_state_for_multi_evaluate_test;
142static int IncrementsGlobal() {
143 global_state_for_multi_evaluate_test++;
144 return 0;
145}
146
147// This test verifies that the binary CHECK_*(x,y) macros only
148// evaluate their arguments once.
149TEST(Check, BinaryOpOnlyEvaluatesOnce) {
150 MAYBE_SKIP_TEST;
151
152 global_state_for_multi_evaluate_test = 0;
153 PW_CHECK_INT_EQ(0, IncrementsGlobal());
154 EXPECT_EQ(global_state_for_multi_evaluate_test, 1);
155
156 global_state_for_multi_evaluate_test = 0;
157 PW_CHECK_INT_EQ(IncrementsGlobal(), IncrementsGlobal());
158 EXPECT_EQ(global_state_for_multi_evaluate_test, 2);
159
160 // Fails; should only evaluate IncrementGlobal() once.
161 global_state_for_multi_evaluate_test = 0;
162 PW_CHECK_INT_EQ(1, IncrementsGlobal());
163 EXPECT_EQ(global_state_for_multi_evaluate_test, 1);
164
165 global_state_for_multi_evaluate_test = 0;
166 PW_CHECK_INT_EQ(IncrementsGlobal(), 1 + IncrementsGlobal());
167 EXPECT_EQ(global_state_for_multi_evaluate_test, 2);
168}
169
Keir Mierle0743cc52020-02-07 16:08:33 -0800170// Note: This requires enabling PW_ASSERT_USE_SHORT_NAMES 1 above.
171TEST(Check, ShortNamesWork) {
172 MAYBE_SKIP_TEST;
173
174 // Crash
175 CRASH(FAIL_IF_HIDDEN);
176 CRASH(FAIL_IF_HIDDEN_ARGS, z);
177
178 // Check
179 CHECK(true, FAIL_IF_DISPLAYED);
180 CHECK(true, FAIL_IF_DISPLAYED_ARGS, z);
181 CHECK(false, FAIL_IF_HIDDEN);
182 CHECK(false, FAIL_IF_HIDDEN_ARGS, z);
183
184 // Check with binary comparison
185 int x_int = 50;
186 int y_int = 66;
187
188 CHECK_INT_LE(Add3(1, 2, 3), y_int);
189 CHECK_INT_LE(x_int, Add3(1, 2, 3));
190
191 CHECK_INT_LE(Add3(1, 2, 3), y_int, FAIL_IF_DISPLAYED);
192 CHECK_INT_LE(x_int, Add3(1, 2, 3), FAIL_IF_DISPLAYED_ARGS, z);
193
194 CHECK_INT_LE(Add3(1, 2, 3), Add3(1, 2, 3), "INT: " FAIL_IF_DISPLAYED);
195 CHECK_INT_LE(x_int, y_int, "INT: " FAIL_IF_DISPLAYED_ARGS, z);
196}
Keir Mierle3cee8792020-01-22 17:08:13 -0800197} // namespace