blob: b1bbe26dc44c8a8f8ca1be49d5120e48185f50b3 [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"
27
28#ifdef __cplusplus
29#error "This file must be compiled as plain C to verify C compilation works."
30#endif // __cplusplus
31
32// This is a global constant to feed into the formatter for tests.
33// Intended to pair with FAIL_IF_DISPLAYED_ARGS or FAIL_IF_HIDDEN_ARGS.
34static const int z = 10;
35
36// At some point in the future when there is a proper test system in place for
37// crashing, the below strings can help indicate pass/fail for a check.
38
39#define FAIL_IF_DISPLAYED "FAIL IF DISPLAYED"
40#define FAIL_IF_DISPLAYED_ARGS "FAIL IF DISPLAYED: %d"
41
42#define FAIL_IF_HIDDEN "FAIL IF HIDDEN"
43#define FAIL_IF_HIDDEN_ARGS "FAIL IF HIDDEN: %d"
44
45// This switch exists to support compiling and/or running the tests.
46#define DISABLE_ASSERT_TEST_EXECUTION 1
47#if DISABLE_ASSERT_TEST_EXECUTION
48#define MAYBE_SKIP_TEST return
49#else
50#define MAYBE_SKIP_TEST ;
51#endif
52
53static int Add3(int a, int b, int c) { return a + b + c; }
54
Keir Mierle8d2a84f2020-04-14 22:00:00 -070055void AssertBackendCompileTestsInC() {
Keir Mierle3cee8792020-01-22 17:08:13 -080056 { // TEST(Crash, WithAndWithoutMessageArguments)
57 MAYBE_SKIP_TEST;
58 PW_CRASH(FAIL_IF_HIDDEN);
59 PW_CRASH(FAIL_IF_HIDDEN_ARGS, z);
60 }
61
62 { // TEST(Check, NoMessage)
63 MAYBE_SKIP_TEST;
64 PW_CHECK(1);
65 PW_CHECK(0);
66 }
67
68 { // TEST(Check, WithMessageAndArgs)
69 MAYBE_SKIP_TEST;
70 PW_CHECK(1, FAIL_IF_DISPLAYED);
71 PW_CHECK(1, FAIL_IF_DISPLAYED_ARGS, z);
72
73 PW_CHECK(0, FAIL_IF_HIDDEN);
74 PW_CHECK(0, FAIL_IF_HIDDEN_ARGS, z);
75 }
76
77 { // TEST(Check, IntComparison)
78 MAYBE_SKIP_TEST;
79 int x_int = 50;
80 int y_int = 66;
81
82 PW_CHECK_INT_LE(x_int, y_int);
83 PW_CHECK_INT_LE(x_int, y_int, "INT: " FAIL_IF_DISPLAYED);
84 PW_CHECK_INT_LE(x_int, y_int, "INT: " FAIL_IF_DISPLAYED_ARGS, z);
85
86 PW_CHECK_INT_GE(x_int, y_int);
87 PW_CHECK_INT_GE(x_int, y_int, "INT: " FAIL_IF_HIDDEN);
88 PW_CHECK_INT_GE(x_int, y_int, "INT: " FAIL_IF_HIDDEN_ARGS, z);
89 }
90
91 { // TEST(Check, UintComparison)
92 MAYBE_SKIP_TEST;
93 unsigned int x_uint = 50;
94 unsigned int y_uint = 66;
95
96 PW_CHECK_UINT_LE(x_uint, y_uint);
97 PW_CHECK_UINT_LE(x_uint, y_uint, "UINT: " FAIL_IF_DISPLAYED);
98 PW_CHECK_UINT_LE(x_uint, y_uint, "UINT: " FAIL_IF_DISPLAYED_ARGS, z);
99
100 PW_CHECK_UINT_GE(x_uint, y_uint);
101 PW_CHECK_UINT_GE(x_uint, y_uint, "UINT: " FAIL_IF_HIDDEN);
102 PW_CHECK_UINT_GE(x_uint, y_uint, "UINT: " FAIL_IF_HIDDEN_ARGS, z);
103 }
104
105 { // TEST(Check, FloatComparison)
106 MAYBE_SKIP_TEST;
107 float x_float = 50.5;
108 float y_float = 66.5;
109
110 PW_CHECK_FLOAT_LE(x_float, y_float);
111 PW_CHECK_FLOAT_LE(x_float, y_float, "FLOAT: " FAIL_IF_DISPLAYED);
112 PW_CHECK_FLOAT_LE(x_float, y_float, "FLOAT: " FAIL_IF_DISPLAYED_ARGS, z);
113
114 PW_CHECK_FLOAT_GE(x_float, y_float);
115 PW_CHECK_FLOAT_GE(x_float, y_float, "FLOAT: " FAIL_IF_HIDDEN);
116 PW_CHECK_FLOAT_GE(x_float, y_float, "FLOAT: " FAIL_IF_HIDDEN_ARGS, z);
117 }
118
119 { // TEST(Check, ComparisonArgumentsWithCommas)
120 MAYBE_SKIP_TEST;
121 int x_int = 50;
122 int y_int = 66;
123
124 PW_CHECK_INT_LE(Add3(1, 2, 3), y_int);
125 PW_CHECK_INT_LE(x_int, Add3(1, 2, 3));
126
127 PW_CHECK_INT_LE(Add3(1, 2, 3), y_int, FAIL_IF_DISPLAYED);
128 PW_CHECK_INT_LE(x_int, Add3(1, 2, 3), FAIL_IF_DISPLAYED_ARGS, z);
129
130 PW_CHECK_INT_LE(Add3(1, 2, 3), Add3(1, 2, 3), "INT: " FAIL_IF_DISPLAYED);
131 PW_CHECK_INT_LE(x_int, y_int, "INT: " FAIL_IF_DISPLAYED_ARGS, z);
132 }
Keir Mierle8d2a84f2020-04-14 22:00:00 -0700133
134 // Note: This requires enabling PW_ASSERT_USE_SHORT_NAMES 1 above.
135 { // TEST(Check, ShortNamesWork) {
136 MAYBE_SKIP_TEST;
137
138 // Crash
139 CRASH(FAIL_IF_HIDDEN);
140 CRASH(FAIL_IF_HIDDEN_ARGS, z);
141
142 // Check
143 CHECK(1, FAIL_IF_DISPLAYED);
144 CHECK(1, FAIL_IF_DISPLAYED_ARGS, z);
145 CHECK(0, FAIL_IF_HIDDEN);
146 CHECK(0, FAIL_IF_HIDDEN_ARGS, z);
147
148 // Check with binary comparison
149 int x_int = 50;
150 int y_int = 66;
151
152 CHECK_INT_LE(Add3(1, 2, 3), y_int);
153 CHECK_INT_LE(x_int, Add3(1, 2, 3));
154
155 CHECK_INT_LE(Add3(1, 2, 3), y_int, FAIL_IF_DISPLAYED);
156 CHECK_INT_LE(x_int, Add3(1, 2, 3), FAIL_IF_DISPLAYED_ARGS, z);
157
158 CHECK_INT_LE(Add3(1, 2, 3), Add3(1, 2, 3), "INT: " FAIL_IF_DISPLAYED);
159 CHECK_INT_LE(x_int, y_int, "INT: " FAIL_IF_DISPLAYED_ARGS, z);
160 }
Keir Mierle3cee8792020-01-22 17:08:13 -0800161}