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 series of "tests" is more a compile test to verify that the assert |
| 16 | // backend is able to compile the constructs promised by the assert facade. |
| 17 | // Truly testing the backend in a general way from the facade is impossible |
| 18 | // since the device will go down when an assert triggers, and so that must be |
| 19 | // handled inside the individual backends. |
Keir Mierle | 3cee879 | 2020-01-22 17:08:13 -0800 | [diff] [blame] | 20 | // |
Keir Mierle | 8d2a84f | 2020-04-14 22:00:00 -0700 | [diff] [blame] | 21 | // NOTE: While these tests are not intended to run, it *is* possible to run |
| 22 | // them with the assert_basic backend, in a special mode where the assert |
| 23 | // statements fall through instead of aborting. |
| 24 | // |
| 25 | // To run these "tests" for pw_assert_basic, you must modify two things: |
| 26 | // |
| 27 | // (1) Set DISABLE_ASSERT_TEST_EXECUTION 0 in assert_backend_compile_test.cc |
| 28 | // (2) Set DISABLE_ASSERT_TEST_EXECUTION 0 in assert_backend_compile_test.c |
| 29 | // (3) Set PW_ASSERT_BASIC_DISABLE_NORETURN 1 in assert_basic.h |
| 30 | // (4) Compile and run the resulting binary, paying attention to the |
| 31 | // displayed error messages. If "FAIL IF DISPLAYED" is printed, the |
| 32 | // test has failed. If any "FAIL_IF_HIDDEN" asserts are not displayed, |
| 33 | // the test has failed. Obviously manually verifying these is a pain |
| 34 | // and so this is not a suitable test for production. |
| 35 | // |
| 36 | // TODO(pwbug/88): Add verification of the actually recorded asserts statements. |
Keir Mierle | 3cee879 | 2020-01-22 17:08:13 -0800 | [diff] [blame] | 37 | |
| 38 | // clang-format off |
Keir Mierle | 0743cc5 | 2020-02-07 16:08:33 -0800 | [diff] [blame] | 39 | #define PW_ASSERT_USE_SHORT_NAMES 1 |
Keir Mierle | 3cee879 | 2020-01-22 17:08:13 -0800 | [diff] [blame] | 40 | #include "pw_assert/assert.h" |
| 41 | // clang-format on |
| 42 | |
Keir Mierle | 0fa7f7d | 2020-05-07 12:34:00 -0700 | [diff] [blame] | 43 | #include "pw_status/status.h" |
Keir Mierle | 3cee879 | 2020-01-22 17:08:13 -0800 | [diff] [blame] | 44 | #include "gtest/gtest.h" |
| 45 | |
| 46 | // This is a global constant to feed into the formatter for tests. |
| 47 | // Intended to pair with FAIL_IF_DISPLAYED_ARGS or FAIL_IF_HIDDEN_ARGS. |
| 48 | static const int z = 10; |
| 49 | |
| 50 | // At some point in the future when there is a proper test system in place for |
| 51 | // crashing, the below strings can help indicate pass/fail for a check. |
| 52 | |
| 53 | #define FAIL_IF_DISPLAYED "FAIL IF DISPLAYED" |
| 54 | #define FAIL_IF_DISPLAYED_ARGS "FAIL IF DISPLAYED: %d" |
| 55 | |
| 56 | #define FAIL_IF_HIDDEN "FAIL IF HIDDEN" |
| 57 | #define FAIL_IF_HIDDEN_ARGS "FAIL IF HIDDEN: %d" |
| 58 | |
| 59 | // This switch exists to support compiling and/or running the tests. |
| 60 | #define DISABLE_ASSERT_TEST_EXECUTION 1 |
| 61 | #if DISABLE_ASSERT_TEST_EXECUTION |
| 62 | #define MAYBE_SKIP_TEST return |
| 63 | #else |
| 64 | #define MAYBE_SKIP_TEST ; |
| 65 | #endif |
| 66 | |
| 67 | namespace { |
| 68 | |
| 69 | TEST(Crash, WithAndWithoutMessageArguments) { |
| 70 | MAYBE_SKIP_TEST; |
| 71 | PW_CRASH(FAIL_IF_HIDDEN); |
| 72 | PW_CRASH(FAIL_IF_HIDDEN_ARGS, z); |
| 73 | } |
| 74 | |
| 75 | TEST(Check, NoMessage) { |
| 76 | MAYBE_SKIP_TEST; |
| 77 | PW_CHECK(true); |
| 78 | PW_CHECK(false); |
| 79 | } |
| 80 | |
| 81 | TEST(Check, WithMessageAndArgs) { |
| 82 | MAYBE_SKIP_TEST; |
| 83 | PW_CHECK(true, FAIL_IF_DISPLAYED); |
| 84 | PW_CHECK(true, FAIL_IF_DISPLAYED_ARGS, z); |
| 85 | |
| 86 | PW_CHECK(false, FAIL_IF_HIDDEN); |
| 87 | PW_CHECK(false, FAIL_IF_HIDDEN_ARGS, z); |
| 88 | } |
| 89 | |
| 90 | TEST(Check, IntComparison) { |
| 91 | MAYBE_SKIP_TEST; |
| 92 | int x_int = 50; |
| 93 | int y_int = 66; |
| 94 | |
| 95 | PW_CHECK_INT_LE(x_int, y_int); |
| 96 | PW_CHECK_INT_LE(x_int, y_int, "INT: " FAIL_IF_DISPLAYED); |
| 97 | PW_CHECK_INT_LE(x_int, y_int, "INT: " FAIL_IF_DISPLAYED_ARGS, z); |
| 98 | |
| 99 | PW_CHECK_INT_GE(x_int, y_int); |
| 100 | PW_CHECK_INT_GE(x_int, y_int, "INT: " FAIL_IF_HIDDEN); |
| 101 | PW_CHECK_INT_GE(x_int, y_int, "INT: " FAIL_IF_HIDDEN_ARGS, z); |
| 102 | } |
| 103 | |
| 104 | TEST(Check, UintComparison) { |
| 105 | MAYBE_SKIP_TEST; |
| 106 | unsigned int x_uint = 50; |
| 107 | unsigned int y_uint = 66; |
| 108 | |
| 109 | PW_CHECK_UINT_LE(x_uint, y_uint); |
| 110 | PW_CHECK_UINT_LE(x_uint, y_uint, "UINT: " FAIL_IF_DISPLAYED); |
| 111 | PW_CHECK_UINT_LE(x_uint, y_uint, "UINT: " FAIL_IF_DISPLAYED_ARGS, z); |
| 112 | |
| 113 | PW_CHECK_UINT_GE(x_uint, y_uint); |
| 114 | PW_CHECK_UINT_GE(x_uint, y_uint, "UINT: " FAIL_IF_HIDDEN); |
| 115 | PW_CHECK_UINT_GE(x_uint, y_uint, "UINT: " FAIL_IF_HIDDEN_ARGS, z); |
| 116 | } |
| 117 | |
Keir Mierle | b9b8816 | 2020-04-15 20:43:09 -0700 | [diff] [blame] | 118 | TEST(Check, PtrComparison) { |
| 119 | MAYBE_SKIP_TEST; |
| 120 | void* x_ptr = reinterpret_cast<void*>(50); |
| 121 | void* y_ptr = reinterpret_cast<void*>(66); |
| 122 | |
| 123 | PW_CHECK_PTR_EQ(x_ptr, y_ptr); |
| 124 | PW_CHECK_PTR_LE(x_ptr, y_ptr, "PTR: " FAIL_IF_DISPLAYED); |
| 125 | PW_CHECK_PTR_LE(x_ptr, y_ptr, "PTR: " FAIL_IF_DISPLAYED_ARGS, z); |
| 126 | |
| 127 | PW_CHECK_PTR_GE(x_ptr, y_ptr); |
| 128 | PW_CHECK_PTR_GE(x_ptr, y_ptr, "PTR: " FAIL_IF_HIDDEN); |
| 129 | PW_CHECK_PTR_GE(x_ptr, y_ptr, "PTR: " FAIL_IF_HIDDEN_ARGS, z); |
| 130 | } |
| 131 | |
Keir Mierle | 3cee879 | 2020-01-22 17:08:13 -0800 | [diff] [blame] | 132 | TEST(Check, FloatComparison) { |
| 133 | MAYBE_SKIP_TEST; |
| 134 | float x_float = 50.5; |
| 135 | float y_float = 66.5; |
| 136 | |
| 137 | PW_CHECK_FLOAT_LE(x_float, y_float); |
| 138 | PW_CHECK_FLOAT_LE(x_float, y_float, "FLOAT: " FAIL_IF_DISPLAYED); |
| 139 | PW_CHECK_FLOAT_LE(x_float, y_float, "FLOAT: " FAIL_IF_DISPLAYED_ARGS, z); |
| 140 | |
| 141 | PW_CHECK_FLOAT_GE(x_float, y_float); |
| 142 | PW_CHECK_FLOAT_GE(x_float, y_float, "FLOAT: " FAIL_IF_HIDDEN); |
| 143 | PW_CHECK_FLOAT_GE(x_float, y_float, "FLOAT: " FAIL_IF_HIDDEN_ARGS, z); |
| 144 | } |
| 145 | |
Keir Mierle | b9b8816 | 2020-04-15 20:43:09 -0700 | [diff] [blame] | 146 | // Don't exhaustively test the DCHECKs but have a sampling of them. |
| 147 | TEST(DCheck, Sampling) { |
| 148 | MAYBE_SKIP_TEST; |
| 149 | PW_DCHECK(5 == 10); |
| 150 | PW_DCHECK(5 == 10, "Message"); |
| 151 | PW_DCHECK(5 == 10, "Message: %d", 5); |
| 152 | PW_DCHECK_INT_LE(5.4, 10.0); |
| 153 | PW_DCHECK_FLOAT_EQ(5.4, 10.0, "Message"); |
| 154 | } |
| 155 | |
Keir Mierle | 3cee879 | 2020-01-22 17:08:13 -0800 | [diff] [blame] | 156 | static int Add3(int a, int b, int c) { return a + b + c; } |
| 157 | |
| 158 | TEST(Check, ComparisonArgumentsWithCommas) { |
| 159 | MAYBE_SKIP_TEST; |
| 160 | int x_int = 50; |
| 161 | int y_int = 66; |
| 162 | |
| 163 | PW_CHECK_INT_LE(Add3(1, 2, 3), y_int); |
| 164 | PW_CHECK_INT_LE(x_int, Add3(1, 2, 3)); |
| 165 | |
| 166 | PW_CHECK_INT_LE(Add3(1, 2, 3), y_int, FAIL_IF_DISPLAYED); |
| 167 | PW_CHECK_INT_LE(x_int, Add3(1, 2, 3), FAIL_IF_DISPLAYED_ARGS, z); |
| 168 | |
| 169 | PW_CHECK_INT_LE(Add3(1, 2, 3), Add3(1, 2, 3), "INT: " FAIL_IF_DISPLAYED); |
| 170 | PW_CHECK_INT_LE(x_int, y_int, "INT: " FAIL_IF_DISPLAYED_ARGS, z); |
| 171 | } |
| 172 | |
Keir Mierle | 0743cc5 | 2020-02-07 16:08:33 -0800 | [diff] [blame] | 173 | // Note: This requires enabling PW_ASSERT_USE_SHORT_NAMES 1 above. |
| 174 | TEST(Check, ShortNamesWork) { |
| 175 | MAYBE_SKIP_TEST; |
| 176 | |
| 177 | // Crash |
| 178 | CRASH(FAIL_IF_HIDDEN); |
| 179 | CRASH(FAIL_IF_HIDDEN_ARGS, z); |
| 180 | |
| 181 | // Check |
| 182 | CHECK(true, FAIL_IF_DISPLAYED); |
| 183 | CHECK(true, FAIL_IF_DISPLAYED_ARGS, z); |
| 184 | CHECK(false, FAIL_IF_HIDDEN); |
| 185 | CHECK(false, FAIL_IF_HIDDEN_ARGS, z); |
| 186 | |
| 187 | // Check with binary comparison |
| 188 | int x_int = 50; |
| 189 | int y_int = 66; |
| 190 | |
| 191 | CHECK_INT_LE(Add3(1, 2, 3), y_int); |
| 192 | CHECK_INT_LE(x_int, Add3(1, 2, 3)); |
| 193 | |
| 194 | CHECK_INT_LE(Add3(1, 2, 3), y_int, FAIL_IF_DISPLAYED); |
| 195 | CHECK_INT_LE(x_int, Add3(1, 2, 3), FAIL_IF_DISPLAYED_ARGS, z); |
| 196 | |
| 197 | CHECK_INT_LE(Add3(1, 2, 3), Add3(1, 2, 3), "INT: " FAIL_IF_DISPLAYED); |
| 198 | CHECK_INT_LE(x_int, y_int, "INT: " FAIL_IF_DISPLAYED_ARGS, z); |
| 199 | } |
Keir Mierle | 8d2a84f | 2020-04-14 22:00:00 -0700 | [diff] [blame] | 200 | |
Keir Mierle | 0fa7f7d | 2020-05-07 12:34:00 -0700 | [diff] [blame] | 201 | pw::Status MakeStatus(pw::Status status) { return status; } |
| 202 | |
| 203 | TEST(Check, CheckOkMacrosCompile) { |
| 204 | MAYBE_SKIP_TEST; |
| 205 | pw::Status status = pw::Status::UNKNOWN; |
| 206 | |
| 207 | // Typical case with long names. |
| 208 | PW_CHECK_OK(status); |
| 209 | PW_CHECK_OK(status, "msg"); |
| 210 | PW_CHECK_OK(status, "msg: %d", 5); |
| 211 | |
| 212 | // Short names. |
| 213 | CHECK_OK(status); |
| 214 | CHECK_OK(status, "msg"); |
| 215 | CHECK_OK(status, "msg: %d", 5); |
| 216 | |
| 217 | // Status from a literal. |
| 218 | PW_CHECK_OK(pw::Status::OK); |
| 219 | |
| 220 | // Status from a function. |
| 221 | PW_CHECK_OK(MakeStatus(pw::Status::OK)); |
| 222 | |
| 223 | // Status from C enums. |
| 224 | PW_CHECK_OK(PW_STATUS_OK); |
| 225 | } |
| 226 | |
Keir Mierle | 8d2a84f | 2020-04-14 22:00:00 -0700 | [diff] [blame] | 227 | // These are defined in assert_test.c, to test C compatibility. |
| 228 | extern "C" void AssertBackendCompileTestsInC(); |
| 229 | |
| 230 | TEST(Check, AssertBackendCompileTestsInC) { |
| 231 | MAYBE_SKIP_TEST; |
| 232 | AssertBackendCompileTestsInC(); |
| 233 | } |
| 234 | |
Keir Mierle | 3cee879 | 2020-01-22 17:08:13 -0800 | [diff] [blame] | 235 | } // namespace |