Keir Mierle | 3cee879 | 2020-01-22 17:08:13 -0800 | [diff] [blame] | 1 | // 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 Mierle | 8d2a84f | 2020-04-14 22:00:00 -0700 | [diff] [blame] | 15 | // This is a compile test that verifies that the assert macros compile in a C |
| 16 | // context. They are not correctness checks. |
Keir Mierle | 3cee879 | 2020-01-22 17:08:13 -0800 | [diff] [blame] | 17 | // |
Keir Mierle | 8d2a84f | 2020-04-14 22:00:00 -0700 | [diff] [blame] | 18 | // 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 Mierle | 3cee879 | 2020-01-22 17:08:13 -0800 | [diff] [blame] | 25 | |
Wyatt Hepler | f298de4 | 2021-03-19 15:06:36 -0700 | [diff] [blame] | 26 | #include "pw_assert/check.h" |
Keir Mierle | a84fd8d | 2020-08-07 21:29:26 -0700 | [diff] [blame] | 27 | |
Wyatt Hepler | 06f98fc | 2021-03-04 16:25:27 -0800 | [diff] [blame] | 28 | static void EnsureNullIsIncluded(void) { |
Keir Mierle | a84fd8d | 2020-08-07 21:29:26 -0700 | [diff] [blame] | 29 | // This is a compile check to ensure NULL is defined. It comes before the |
| 30 | // status.h include to ensure we don't accidentally get NULL from status.h. |
| 31 | PW_CHECK_NOTNULL(0xa); |
| 32 | PW_CHECK_NOTNULL(0x0); |
| 33 | } |
| 34 | |
Wyatt Hepler | dd3e881 | 2020-09-29 11:14:28 -0700 | [diff] [blame] | 35 | #include <stdbool.h> |
| 36 | |
Wyatt Hepler | f298de4 | 2021-03-19 15:06:36 -0700 | [diff] [blame] | 37 | #include "pw_assert/assert.h" |
Keir Mierle | 0fa7f7d | 2020-05-07 12:34:00 -0700 | [diff] [blame] | 38 | #include "pw_status/status.h" |
Keir Mierle | 3cee879 | 2020-01-22 17:08:13 -0800 | [diff] [blame] | 39 | |
| 40 | #ifdef __cplusplus |
| 41 | #error "This file must be compiled as plain C to verify C compilation works." |
| 42 | #endif // __cplusplus |
| 43 | |
| 44 | // This is a global constant to feed into the formatter for tests. |
| 45 | // Intended to pair with FAIL_IF_DISPLAYED_ARGS or FAIL_IF_HIDDEN_ARGS. |
| 46 | static const int z = 10; |
| 47 | |
| 48 | // At some point in the future when there is a proper test system in place for |
| 49 | // crashing, the below strings can help indicate pass/fail for a check. |
| 50 | |
| 51 | #define FAIL_IF_DISPLAYED "FAIL IF DISPLAYED" |
| 52 | #define FAIL_IF_DISPLAYED_ARGS "FAIL IF DISPLAYED: %d" |
| 53 | |
| 54 | #define FAIL_IF_HIDDEN "FAIL IF HIDDEN" |
| 55 | #define FAIL_IF_HIDDEN_ARGS "FAIL IF HIDDEN: %d" |
| 56 | |
| 57 | // This switch exists to support compiling and/or running the tests. |
| 58 | #define DISABLE_ASSERT_TEST_EXECUTION 1 |
| 59 | #if DISABLE_ASSERT_TEST_EXECUTION |
| 60 | #define MAYBE_SKIP_TEST return |
| 61 | #else |
| 62 | #define MAYBE_SKIP_TEST ; |
| 63 | #endif |
| 64 | |
| 65 | static int Add3(int a, int b, int c) { return a + b + c; } |
| 66 | |
Wyatt Hepler | 06f98fc | 2021-03-04 16:25:27 -0800 | [diff] [blame] | 67 | void AssertBackendCompileTestsInC(void) { |
Keir Mierle | 3cee879 | 2020-01-22 17:08:13 -0800 | [diff] [blame] | 68 | { // TEST(Crash, WithAndWithoutMessageArguments) |
| 69 | MAYBE_SKIP_TEST; |
| 70 | PW_CRASH(FAIL_IF_HIDDEN); |
| 71 | PW_CRASH(FAIL_IF_HIDDEN_ARGS, z); |
| 72 | } |
| 73 | |
| 74 | { // TEST(Check, NoMessage) |
| 75 | MAYBE_SKIP_TEST; |
| 76 | PW_CHECK(1); |
| 77 | PW_CHECK(0); |
| 78 | } |
| 79 | |
| 80 | { // TEST(Check, WithMessageAndArgs) |
| 81 | MAYBE_SKIP_TEST; |
| 82 | PW_CHECK(1, FAIL_IF_DISPLAYED); |
| 83 | PW_CHECK(1, FAIL_IF_DISPLAYED_ARGS, z); |
| 84 | |
| 85 | PW_CHECK(0, FAIL_IF_HIDDEN); |
| 86 | PW_CHECK(0, FAIL_IF_HIDDEN_ARGS, z); |
| 87 | } |
| 88 | |
| 89 | { // TEST(Check, IntComparison) |
| 90 | MAYBE_SKIP_TEST; |
| 91 | int x_int = 50; |
| 92 | int y_int = 66; |
| 93 | |
| 94 | PW_CHECK_INT_LE(x_int, y_int); |
| 95 | PW_CHECK_INT_LE(x_int, y_int, "INT: " FAIL_IF_DISPLAYED); |
| 96 | PW_CHECK_INT_LE(x_int, y_int, "INT: " FAIL_IF_DISPLAYED_ARGS, z); |
| 97 | |
| 98 | PW_CHECK_INT_GE(x_int, y_int); |
| 99 | PW_CHECK_INT_GE(x_int, y_int, "INT: " FAIL_IF_HIDDEN); |
| 100 | PW_CHECK_INT_GE(x_int, y_int, "INT: " FAIL_IF_HIDDEN_ARGS, z); |
| 101 | } |
| 102 | |
| 103 | { // TEST(Check, UintComparison) |
| 104 | MAYBE_SKIP_TEST; |
| 105 | unsigned int x_uint = 50; |
| 106 | unsigned int y_uint = 66; |
| 107 | |
| 108 | PW_CHECK_UINT_LE(x_uint, y_uint); |
| 109 | PW_CHECK_UINT_LE(x_uint, y_uint, "UINT: " FAIL_IF_DISPLAYED); |
| 110 | PW_CHECK_UINT_LE(x_uint, y_uint, "UINT: " FAIL_IF_DISPLAYED_ARGS, z); |
| 111 | |
| 112 | PW_CHECK_UINT_GE(x_uint, y_uint); |
| 113 | PW_CHECK_UINT_GE(x_uint, y_uint, "UINT: " FAIL_IF_HIDDEN); |
| 114 | PW_CHECK_UINT_GE(x_uint, y_uint, "UINT: " FAIL_IF_HIDDEN_ARGS, z); |
| 115 | } |
| 116 | |
Keir Mierle | b9b8816 | 2020-04-15 20:43:09 -0700 | [diff] [blame] | 117 | { // TEST(Check, PtrComparison) |
| 118 | MAYBE_SKIP_TEST; |
| 119 | void* x_ptr = (void*)(50); |
| 120 | void* y_ptr = (void*)(66); |
| 121 | |
| 122 | PW_CHECK_PTR_EQ(x_ptr, y_ptr); |
| 123 | PW_CHECK_PTR_LE(x_ptr, y_ptr, "PTR: " FAIL_IF_DISPLAYED); |
| 124 | PW_CHECK_PTR_LE(x_ptr, y_ptr, "PTR: " FAIL_IF_DISPLAYED_ARGS, z); |
| 125 | |
| 126 | PW_CHECK_PTR_GE(x_ptr, y_ptr); |
| 127 | PW_CHECK_PTR_GE(x_ptr, y_ptr, "PTR: " FAIL_IF_HIDDEN); |
| 128 | PW_CHECK_PTR_GE(x_ptr, y_ptr, "PTR: " FAIL_IF_HIDDEN_ARGS, z); |
Keir Mierle | a84fd8d | 2020-08-07 21:29:26 -0700 | [diff] [blame] | 129 | |
| 130 | PW_CHECK_NOTNULL(0xa); |
| 131 | PW_CHECK_NOTNULL(0x0); |
Keir Mierle | b9b8816 | 2020-04-15 20:43:09 -0700 | [diff] [blame] | 132 | } |
| 133 | |
Keir Mierle | 3cee879 | 2020-01-22 17:08:13 -0800 | [diff] [blame] | 134 | { // TEST(Check, FloatComparison) |
| 135 | MAYBE_SKIP_TEST; |
| 136 | float x_float = 50.5; |
| 137 | float y_float = 66.5; |
| 138 | |
Ewout van Bekkum | 9e97cfd | 2020-07-16 13:57:24 -0700 | [diff] [blame] | 139 | PW_CHECK_FLOAT_EXACT_LE(x_float, y_float); |
| 140 | PW_CHECK_FLOAT_EXACT_LE(x_float, y_float, "FLOAT: " FAIL_IF_DISPLAYED); |
| 141 | PW_CHECK_FLOAT_EXACT_LE( |
| 142 | x_float, y_float, "FLOAT: " FAIL_IF_DISPLAYED_ARGS, z); |
Keir Mierle | 3cee879 | 2020-01-22 17:08:13 -0800 | [diff] [blame] | 143 | |
Ewout van Bekkum | 9e97cfd | 2020-07-16 13:57:24 -0700 | [diff] [blame] | 144 | PW_CHECK_FLOAT_EXACT_GE(x_float, y_float); |
| 145 | PW_CHECK_FLOAT_EXACT_GE(x_float, y_float, "FLOAT: " FAIL_IF_HIDDEN); |
| 146 | PW_CHECK_FLOAT_EXACT_GE(x_float, y_float, "FLOAT: " FAIL_IF_HIDDEN_ARGS, z); |
Keir Mierle | 3cee879 | 2020-01-22 17:08:13 -0800 | [diff] [blame] | 147 | } |
| 148 | |
Keir Mierle | b9b8816 | 2020-04-15 20:43:09 -0700 | [diff] [blame] | 149 | // Don't exhaustively test the DCHECKs but have a sampling of them. |
| 150 | { // TEST(DCheck, Sampling) |
| 151 | MAYBE_SKIP_TEST; |
| 152 | PW_DCHECK(5 == 10); |
| 153 | PW_DCHECK(5 == 10, "Message"); |
| 154 | PW_DCHECK(5 == 10, "Message: %d", 5); |
| 155 | PW_DCHECK_INT_LE(5.4, 10.0); |
Ewout van Bekkum | 9e97cfd | 2020-07-16 13:57:24 -0700 | [diff] [blame] | 156 | PW_DCHECK_FLOAT_EXACT_EQ(5.4, 10.0, "Message"); |
Keir Mierle | b9b8816 | 2020-04-15 20:43:09 -0700 | [diff] [blame] | 157 | } |
| 158 | |
Keir Mierle | 3cee879 | 2020-01-22 17:08:13 -0800 | [diff] [blame] | 159 | { // TEST(Check, ComparisonArgumentsWithCommas) |
| 160 | MAYBE_SKIP_TEST; |
| 161 | int x_int = 50; |
| 162 | int y_int = 66; |
| 163 | |
| 164 | PW_CHECK_INT_LE(Add3(1, 2, 3), y_int); |
| 165 | PW_CHECK_INT_LE(x_int, Add3(1, 2, 3)); |
| 166 | |
| 167 | PW_CHECK_INT_LE(Add3(1, 2, 3), y_int, FAIL_IF_DISPLAYED); |
| 168 | PW_CHECK_INT_LE(x_int, Add3(1, 2, 3), FAIL_IF_DISPLAYED_ARGS, z); |
| 169 | |
| 170 | PW_CHECK_INT_LE(Add3(1, 2, 3), Add3(1, 2, 3), "INT: " FAIL_IF_DISPLAYED); |
| 171 | PW_CHECK_INT_LE(x_int, y_int, "INT: " FAIL_IF_DISPLAYED_ARGS, z); |
| 172 | } |
Keir Mierle | 8d2a84f | 2020-04-14 22:00:00 -0700 | [diff] [blame] | 173 | |
| 174 | // Note: This requires enabling PW_ASSERT_USE_SHORT_NAMES 1 above. |
| 175 | { // TEST(Check, ShortNamesWork) { |
| 176 | MAYBE_SKIP_TEST; |
| 177 | |
| 178 | // Crash |
| 179 | CRASH(FAIL_IF_HIDDEN); |
| 180 | CRASH(FAIL_IF_HIDDEN_ARGS, z); |
| 181 | |
| 182 | // Check |
| 183 | CHECK(1, FAIL_IF_DISPLAYED); |
| 184 | CHECK(1, FAIL_IF_DISPLAYED_ARGS, z); |
| 185 | CHECK(0, FAIL_IF_HIDDEN); |
| 186 | CHECK(0, FAIL_IF_HIDDEN_ARGS, z); |
| 187 | |
| 188 | // Check with binary comparison |
| 189 | int x_int = 50; |
| 190 | int y_int = 66; |
| 191 | |
| 192 | CHECK_INT_LE(Add3(1, 2, 3), y_int); |
| 193 | CHECK_INT_LE(x_int, Add3(1, 2, 3)); |
| 194 | |
| 195 | CHECK_INT_LE(Add3(1, 2, 3), y_int, FAIL_IF_DISPLAYED); |
| 196 | CHECK_INT_LE(x_int, Add3(1, 2, 3), FAIL_IF_DISPLAYED_ARGS, z); |
| 197 | |
| 198 | CHECK_INT_LE(Add3(1, 2, 3), Add3(1, 2, 3), "INT: " FAIL_IF_DISPLAYED); |
| 199 | CHECK_INT_LE(x_int, y_int, "INT: " FAIL_IF_DISPLAYED_ARGS, z); |
| 200 | } |
Keir Mierle | 0fa7f7d | 2020-05-07 12:34:00 -0700 | [diff] [blame] | 201 | |
Wyatt Hepler | dd3e881 | 2020-09-29 11:14:28 -0700 | [diff] [blame] | 202 | { // Compile tests for PW_CHECK_OK(). |
Keir Mierle | 0fa7f7d | 2020-05-07 12:34:00 -0700 | [diff] [blame] | 203 | PW_CHECK_OK(PW_STATUS_OK); |
| 204 | PW_CHECK_OK(PW_STATUS_OK, "msg"); |
| 205 | PW_CHECK_OK(PW_STATUS_OK, "msg: %d", 5); |
| 206 | PW_DCHECK_OK(PW_STATUS_OK); |
| 207 | PW_DCHECK_OK(PW_STATUS_OK, "msg"); |
| 208 | PW_DCHECK_OK(PW_STATUS_OK, "msg: %d", 5); |
| 209 | } |
Keir Mierle | a84fd8d | 2020-08-07 21:29:26 -0700 | [diff] [blame] | 210 | |
Wyatt Hepler | dd3e881 | 2020-09-29 11:14:28 -0700 | [diff] [blame] | 211 | { // TEST(Assert, Basic) |
| 212 | MAYBE_SKIP_TEST; |
| 213 | PW_ASSERT(false); |
| 214 | PW_ASSERT(123 == 456); |
| 215 | } |
| 216 | |
| 217 | { // Compile tests for PW_ASSERT(). |
| 218 | PW_ASSERT(true); |
| 219 | PW_ASSERT(123 != 456); |
| 220 | |
| 221 | PW_DASSERT(true); |
| 222 | PW_DASSERT(123 != 456); |
| 223 | } |
| 224 | |
Keir Mierle | a84fd8d | 2020-08-07 21:29:26 -0700 | [diff] [blame] | 225 | EnsureNullIsIncluded(); |
Keir Mierle | 3cee879 | 2020-01-22 17:08:13 -0800 | [diff] [blame] | 226 | } |