Keir Mierle | 8d2a84f | 2020-04-14 22:00:00 -0700 | [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 | |
| 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 | |
| 31 | namespace { |
| 32 | |
| 33 | #define EXPECT_MESSAGE(expected_assert_message) \ |
| 34 | do { \ |
| 35 | EXPECT_STREQ(pw_captured_assert.message, expected_assert_message); \ |
| 36 | } while (0) |
| 37 | |
| 38 | class 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 | |
| 44 | class 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(...) |
| 51 | TEST_F(AssertFail, CrashMessageNoArguments) { |
| 52 | PW_CRASH("Goodbye"); |
| 53 | EXPECT_MESSAGE("Goodbye"); |
| 54 | } |
| 55 | TEST_F(AssertFail, CrashMessageWithArguments) { |
| 56 | PW_CRASH("Goodbye cruel %s", "world"); |
| 57 | EXPECT_MESSAGE("Goodbye cruel world"); |
| 58 | } |
| 59 | |
| 60 | // PW_CHECK(...) - No message |
| 61 | TEST_F(AssertPass, CheckNoMessage) { PW_CHECK(true); } |
| 62 | TEST_F(AssertFail, CheckNoMessage) { |
| 63 | PW_CHECK(false); |
| 64 | EXPECT_MESSAGE("Check failed: false. "); |
| 65 | } |
| 66 | TEST_F(AssertPass, CheckNoMessageComplexExpression) { PW_CHECK(2 == 2); } |
| 67 | TEST_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. |
| 73 | TEST_F(AssertPass, CheckMessageNoArguments) { PW_CHECK(true, "Hello"); } |
| 74 | TEST_F(AssertFail, CheckMessageNoArguments) { |
| 75 | PW_CHECK(false, "Hello"); |
| 76 | EXPECT_MESSAGE("Check failed: false. Hello"); |
| 77 | } |
| 78 | TEST_F(AssertPass, CheckMessageWithArguments) { PW_CHECK(true, "Hello %d", 5); } |
| 79 | TEST_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 Mierle | fa8f89d | 2020-04-15 16:20:02 -0700 | [diff] [blame] | 85 | // Binary checks with ints, comparisons: <, <=, =, !=, >, >=. |
Keir Mierle | 8d2a84f | 2020-04-14 22:00:00 -0700 | [diff] [blame] | 86 | |
| 87 | // Test message formatting separate from the triggering. |
| 88 | // Only test formatting for the type once. |
| 89 | TEST_F(AssertFail, IntLessThanNoMessageNoArguments) { |
| 90 | PW_CHECK_INT_LT(5, -2); |
| 91 | EXPECT_MESSAGE("Check failed: 5 (=5) < -2 (=-2). "); |
| 92 | } |
| 93 | TEST_F(AssertFail, IntLessThanMessageNoArguments) { |
| 94 | PW_CHECK_INT_LT(5, -2, "msg"); |
| 95 | EXPECT_MESSAGE("Check failed: 5 (=5) < -2 (=-2). msg"); |
| 96 | } |
| 97 | TEST_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 < |
| 105 | TEST_F(AssertPass, IntLt1) { PW_CHECK_INT_LT(-1, 2); } |
| 106 | TEST_F(AssertPass, IntLt2) { PW_CHECK_INT_LT(1, 2); } |
| 107 | TEST_F(AssertFail, IntLt3) { PW_CHECK_INT_LT(-1, -2); } |
| 108 | TEST_F(AssertFail, IntLt4) { PW_CHECK_INT_LT(1, 1); } |
| 109 | |
| 110 | // INT <= |
| 111 | TEST_F(AssertPass, IntLe1) { PW_CHECK_INT_LE(-1, 2); } |
| 112 | TEST_F(AssertPass, IntLe2) { PW_CHECK_INT_LE(1, 2); } |
| 113 | TEST_F(AssertFail, IntLe3) { PW_CHECK_INT_LE(-1, -2); } |
| 114 | TEST_F(AssertPass, IntLe4) { PW_CHECK_INT_LE(1, 1); } |
| 115 | |
| 116 | // INT == |
| 117 | TEST_F(AssertFail, IntEq1) { PW_CHECK_INT_EQ(-1, 2); } |
| 118 | TEST_F(AssertFail, IntEq2) { PW_CHECK_INT_EQ(1, 2); } |
| 119 | TEST_F(AssertFail, IntEq3) { PW_CHECK_INT_EQ(-1, -2); } |
| 120 | TEST_F(AssertPass, IntEq4) { PW_CHECK_INT_EQ(1, 1); } |
| 121 | |
Keir Mierle | fa8f89d | 2020-04-15 16:20:02 -0700 | [diff] [blame] | 122 | // INT != |
| 123 | TEST_F(AssertPass, IntNe1) { PW_CHECK_INT_NE(-1, 2); } |
| 124 | TEST_F(AssertPass, IntNe2) { PW_CHECK_INT_NE(1, 2); } |
| 125 | TEST_F(AssertPass, IntNe3) { PW_CHECK_INT_NE(-1, -2); } |
| 126 | TEST_F(AssertFail, IntNe4) { PW_CHECK_INT_NE(1, 1); } |
| 127 | |
Keir Mierle | 8d2a84f | 2020-04-14 22:00:00 -0700 | [diff] [blame] | 128 | // INT > |
| 129 | TEST_F(AssertFail, IntGt1) { PW_CHECK_INT_GT(-1, 2); } |
| 130 | TEST_F(AssertFail, IntGt2) { PW_CHECK_INT_GT(1, 2); } |
| 131 | TEST_F(AssertPass, IntGt3) { PW_CHECK_INT_GT(-1, -2); } |
| 132 | TEST_F(AssertFail, IntGt4) { PW_CHECK_INT_GT(1, 1); } |
| 133 | |
| 134 | // INT >= |
| 135 | TEST_F(AssertFail, IntGe1) { PW_CHECK_INT_GE(-1, 2); } |
| 136 | TEST_F(AssertFail, IntGe2) { PW_CHECK_INT_GE(1, 2); } |
| 137 | TEST_F(AssertPass, IntGe3) { PW_CHECK_INT_GE(-1, -2); } |
| 138 | TEST_F(AssertPass, IntGe4) { PW_CHECK_INT_GE(1, 1); } |
| 139 | |
| 140 | // PW_CHECK_UINT_*(...) |
Keir Mierle | fa8f89d | 2020-04-15 16:20:02 -0700 | [diff] [blame] | 141 | // Binary checks with uints, comparisons: <, <=, =, !=, >, >=. |
Keir Mierle | 8d2a84f | 2020-04-14 22:00:00 -0700 | [diff] [blame] | 142 | |
| 143 | // Test message formatting separate from the triggering. |
| 144 | // Only test formatting for the type once. |
| 145 | TEST_F(AssertFail, UintLessThanNoMessageNoArguments) { |
| 146 | PW_CHECK_UINT_LT(5, 2); |
| 147 | EXPECT_MESSAGE("Check failed: 5 (=5) < 2 (=2). "); |
| 148 | } |
| 149 | TEST_F(AssertFail, UintLessThanMessageNoArguments) { |
| 150 | PW_CHECK_UINT_LT(5, 2, "msg"); |
| 151 | EXPECT_MESSAGE("Check failed: 5 (=5) < 2 (=2). msg"); |
| 152 | } |
| 153 | TEST_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 < |
| 161 | TEST_F(AssertPass, UintLt1) { PW_CHECK_UINT_LT(1, 2); } |
| 162 | TEST_F(AssertFail, UintLt2) { PW_CHECK_UINT_LT(2, 2); } |
| 163 | TEST_F(AssertFail, UintLt3) { PW_CHECK_UINT_LT(2, 1); } |
| 164 | |
| 165 | // UINT <= |
| 166 | TEST_F(AssertPass, UintLe1) { PW_CHECK_UINT_LE(1, 2); } |
| 167 | TEST_F(AssertPass, UintLe2) { PW_CHECK_UINT_LE(2, 2); } |
| 168 | TEST_F(AssertFail, UintLe3) { PW_CHECK_UINT_LE(2, 1); } |
| 169 | |
| 170 | // UINT == |
| 171 | TEST_F(AssertFail, UintEq1) { PW_CHECK_UINT_EQ(1, 2); } |
| 172 | TEST_F(AssertPass, UintEq2) { PW_CHECK_UINT_EQ(2, 2); } |
| 173 | TEST_F(AssertFail, UintEq3) { PW_CHECK_UINT_EQ(2, 1); } |
| 174 | |
Keir Mierle | fa8f89d | 2020-04-15 16:20:02 -0700 | [diff] [blame] | 175 | // UINT != |
| 176 | TEST_F(AssertPass, UintNe1) { PW_CHECK_UINT_NE(1, 2); } |
| 177 | TEST_F(AssertFail, UintNe2) { PW_CHECK_UINT_NE(2, 2); } |
| 178 | TEST_F(AssertPass, UintNe3) { PW_CHECK_UINT_NE(2, 1); } |
| 179 | |
Keir Mierle | 8d2a84f | 2020-04-14 22:00:00 -0700 | [diff] [blame] | 180 | // UINT > |
| 181 | TEST_F(AssertFail, UintGt1) { PW_CHECK_UINT_GT(1, 2); } |
| 182 | TEST_F(AssertFail, UintGt2) { PW_CHECK_UINT_GT(2, 2); } |
| 183 | TEST_F(AssertPass, UintGt3) { PW_CHECK_UINT_GT(2, 1); } |
| 184 | |
| 185 | // UINT >= |
| 186 | TEST_F(AssertFail, UintGe1) { PW_CHECK_UINT_GE(1, 2); } |
| 187 | TEST_F(AssertPass, UintGe2) { PW_CHECK_UINT_GE(2, 2); } |
| 188 | TEST_F(AssertPass, UintGe3) { PW_CHECK_UINT_GE(2, 1); } |
| 189 | |
Keir Mierle | fa8f89d | 2020-04-15 16:20:02 -0700 | [diff] [blame] | 190 | // 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 < |
| 197 | TEST_F(AssertPass, PtrLt1) { PW_CHECK_PTR_LT(0xa, 0xb); } |
| 198 | TEST_F(AssertFail, PtrLt2) { PW_CHECK_PTR_LT(0xb, 0xb); } |
| 199 | TEST_F(AssertFail, PtrLt3) { PW_CHECK_PTR_LT(0xb, 0xa); } |
| 200 | |
| 201 | // PTR <= |
| 202 | TEST_F(AssertPass, PtrLe1) { PW_CHECK_PTR_LE(0xa, 0xb); } |
| 203 | TEST_F(AssertPass, PtrLe2) { PW_CHECK_PTR_LE(0xb, 0xb); } |
| 204 | TEST_F(AssertFail, PtrLe3) { PW_CHECK_PTR_LE(0xb, 0xa); } |
| 205 | |
| 206 | // PTR == |
| 207 | TEST_F(AssertFail, PtrEq1) { PW_CHECK_PTR_EQ(0xa, 0xb); } |
| 208 | TEST_F(AssertPass, PtrEq2) { PW_CHECK_PTR_EQ(0xb, 0xb); } |
| 209 | TEST_F(AssertFail, PtrEq3) { PW_CHECK_PTR_EQ(0xb, 0xa); } |
| 210 | |
| 211 | // PTR != |
| 212 | TEST_F(AssertPass, PtrNe1) { PW_CHECK_PTR_NE(0xa, 0xb); } |
| 213 | TEST_F(AssertFail, PtrNe2) { PW_CHECK_PTR_NE(0xb, 0xb); } |
| 214 | TEST_F(AssertPass, PtrNe3) { PW_CHECK_PTR_NE(0xb, 0xa); } |
| 215 | |
| 216 | // PTR > |
| 217 | TEST_F(AssertFail, PtrGt1) { PW_CHECK_PTR_GT(0xa, 0xb); } |
| 218 | TEST_F(AssertFail, PtrGt2) { PW_CHECK_PTR_GT(0xb, 0xb); } |
| 219 | TEST_F(AssertPass, PtrGt3) { PW_CHECK_PTR_GT(0xb, 0xa); } |
| 220 | |
| 221 | // PTR >= |
| 222 | TEST_F(AssertFail, PtrGe1) { PW_CHECK_PTR_GE(0xa, 0xb); } |
| 223 | TEST_F(AssertPass, PtrGe2) { PW_CHECK_PTR_GE(0xb, 0xb); } |
| 224 | TEST_F(AssertPass, PtrGe3) { PW_CHECK_PTR_GE(0xb, 0xa); } |
| 225 | |
| 226 | // NOTNULL |
| 227 | TEST_F(AssertPass, PtrNotNull) { PW_CHECK_NOTNULL(0xa); } |
| 228 | TEST_F(AssertFail, PtrNotNull) { PW_CHECK_NOTNULL(0x0); } |
| 229 | |
Keir Mierle | a84fd8d | 2020-08-07 21:29:26 -0700 | [diff] [blame] | 230 | // 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 Mierle | 8d2a84f | 2020-04-14 22:00:00 -0700 | [diff] [blame] | 240 | // PW_CHECK_FLOAT_*(...) |
Ewout van Bekkum | 9e97cfd | 2020-07-16 13:57:24 -0700 | [diff] [blame] | 241 | // Binary checks with floats, comparisons: EXACT_LT, EXACT_LE, NEAR, EXACT_EQ, |
| 242 | // EXACT_NE, EXACT_GE, EXACT_GT. |
Keir Mierle | 8d2a84f | 2020-04-14 22:00:00 -0700 | [diff] [blame] | 243 | |
| 244 | // Test message formatting separate from the triggering. |
| 245 | // Only test formatting for the type once. |
| 246 | TEST_F(AssertFail, FloatLessThanNoMessageNoArguments) { |
Ewout van Bekkum | 9e97cfd | 2020-07-16 13:57:24 -0700 | [diff] [blame] | 247 | PW_CHECK_FLOAT_EXACT_LT(5.2, 2.3); |
Keir Mierle | 8d2a84f | 2020-04-14 22:00:00 -0700 | [diff] [blame] | 248 | EXPECT_MESSAGE("Check failed: 5.2 (=5.200000) < 2.3 (=2.300000). "); |
| 249 | } |
| 250 | TEST_F(AssertFail, FloatLessThanMessageNoArguments) { |
Ewout van Bekkum | 9e97cfd | 2020-07-16 13:57:24 -0700 | [diff] [blame] | 251 | PW_CHECK_FLOAT_EXACT_LT(5.2, 2.3, "msg"); |
Keir Mierle | 8d2a84f | 2020-04-14 22:00:00 -0700 | [diff] [blame] | 252 | EXPECT_MESSAGE("Check failed: 5.2 (=5.200000) < 2.3 (=2.300000). msg"); |
| 253 | } |
| 254 | TEST_F(AssertFail, FloatLessThanMessageArguments) { |
Ewout van Bekkum | 9e97cfd | 2020-07-16 13:57:24 -0700 | [diff] [blame] | 255 | PW_CHECK_FLOAT_EXACT_LT(5.2, 2.3, "msg: %d", 6); |
Keir Mierle | 8d2a84f | 2020-04-14 22:00:00 -0700 | [diff] [blame] | 256 | EXPECT_MESSAGE("Check failed: 5.2 (=5.200000) < 2.3 (=2.300000). msg: 6"); |
| 257 | } |
Ewout van Bekkum | 9e97cfd | 2020-07-16 13:57:24 -0700 | [diff] [blame] | 258 | // Check float NEAR both above and below the permitted range. |
| 259 | TEST_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 | } |
| 264 | TEST_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 | } |
| 269 | TEST_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 | } |
| 275 | TEST_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 | } |
| 280 | TEST_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 | } |
| 285 | TEST_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 Mierle | 8d2a84f | 2020-04-14 22:00:00 -0700 | [diff] [blame] | 291 | // 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 Bekkum | 9e97cfd | 2020-07-16 13:57:24 -0700 | [diff] [blame] | 296 | TEST_F(AssertPass, FloatLt1) { PW_CHECK_FLOAT_EXACT_LT(1.1, 1.2); } |
| 297 | TEST_F(AssertFail, FloatLt2) { PW_CHECK_FLOAT_EXACT_LT(1.2, 1.2); } |
| 298 | TEST_F(AssertFail, FloatLt3) { PW_CHECK_FLOAT_EXACT_LT(1.2, 1.1); } |
Keir Mierle | 8d2a84f | 2020-04-14 22:00:00 -0700 | [diff] [blame] | 299 | |
| 300 | // FLOAT <= |
Ewout van Bekkum | 9e97cfd | 2020-07-16 13:57:24 -0700 | [diff] [blame] | 301 | TEST_F(AssertPass, FloatLe1) { PW_CHECK_FLOAT_EXACT_LE(1.1, 1.2); } |
| 302 | TEST_F(AssertPass, FloatLe2) { PW_CHECK_FLOAT_EXACT_LE(1.2, 1.2); } |
| 303 | TEST_F(AssertFail, FloatLe3) { PW_CHECK_FLOAT_EXACT_LE(1.2, 1.1); } |
| 304 | |
| 305 | // FLOAT ~= based on absolute error. |
| 306 | TEST_F(AssertFail, FloatNearAbs1) { PW_CHECK_FLOAT_NEAR(1.09, 1.2, 0.1); } |
| 307 | TEST_F(AssertPass, FloatNearAbs2) { PW_CHECK_FLOAT_NEAR(1.1, 1.2, 0.1); } |
| 308 | TEST_F(AssertPass, FloatNearAbs3) { PW_CHECK_FLOAT_NEAR(1.2, 1.2, 0.1); } |
| 309 | TEST_F(AssertPass, FloatNearAbs4) { PW_CHECK_FLOAT_NEAR(1.2, 1.1, 0.1); } |
| 310 | TEST_F(AssertFail, FloatNearAbs5) { PW_CHECK_FLOAT_NEAR(1.21, 1.1, 0.1); } |
| 311 | // Make sure the abs_tolerance is asserted to be >= 0. |
| 312 | TEST_F(AssertFail, FloatNearAbs6) { PW_CHECK_FLOAT_NEAR(1.2, 1.2, -0.1); } |
| 313 | TEST_F(AssertPass, FloatNearAbs7) { PW_CHECK_FLOAT_NEAR(1.2, 1.2, 0.0); } |
Keir Mierle | 8d2a84f | 2020-04-14 22:00:00 -0700 | [diff] [blame] | 314 | |
| 315 | // FLOAT == |
Ewout van Bekkum | 9e97cfd | 2020-07-16 13:57:24 -0700 | [diff] [blame] | 316 | TEST_F(AssertFail, FloatEq1) { PW_CHECK_FLOAT_EXACT_EQ(1.1, 1.2); } |
| 317 | TEST_F(AssertPass, FloatEq2) { PW_CHECK_FLOAT_EXACT_EQ(1.2, 1.2); } |
| 318 | TEST_F(AssertFail, FloatEq3) { PW_CHECK_FLOAT_EXACT_EQ(1.2, 1.1); } |
Keir Mierle | 8d2a84f | 2020-04-14 22:00:00 -0700 | [diff] [blame] | 319 | |
Keir Mierle | fa8f89d | 2020-04-15 16:20:02 -0700 | [diff] [blame] | 320 | // FLOAT != |
Ewout van Bekkum | 9e97cfd | 2020-07-16 13:57:24 -0700 | [diff] [blame] | 321 | TEST_F(AssertPass, FloatNe1) { PW_CHECK_FLOAT_EXACT_NE(1.1, 1.2); } |
| 322 | TEST_F(AssertFail, FloatNe2) { PW_CHECK_FLOAT_EXACT_NE(1.2, 1.2); } |
| 323 | TEST_F(AssertPass, FloatNe3) { PW_CHECK_FLOAT_EXACT_NE(1.2, 1.1); } |
Keir Mierle | fa8f89d | 2020-04-15 16:20:02 -0700 | [diff] [blame] | 324 | |
Keir Mierle | 8d2a84f | 2020-04-14 22:00:00 -0700 | [diff] [blame] | 325 | // FLOAT > |
Ewout van Bekkum | 9e97cfd | 2020-07-16 13:57:24 -0700 | [diff] [blame] | 326 | TEST_F(AssertFail, FloatGt1) { PW_CHECK_FLOAT_EXACT_GT(1.1, 1.2); } |
| 327 | TEST_F(AssertFail, FloatGt2) { PW_CHECK_FLOAT_EXACT_GT(1.2, 1.2); } |
| 328 | TEST_F(AssertPass, FloatGt3) { PW_CHECK_FLOAT_EXACT_GT(1.2, 1.1); } |
Keir Mierle | 8d2a84f | 2020-04-14 22:00:00 -0700 | [diff] [blame] | 329 | |
| 330 | // FLOAT >= |
Ewout van Bekkum | 9e97cfd | 2020-07-16 13:57:24 -0700 | [diff] [blame] | 331 | TEST_F(AssertFail, FloatGe1) { PW_CHECK_FLOAT_EXACT_GE(1.1, 1.2); } |
| 332 | TEST_F(AssertPass, FloatGe2) { PW_CHECK_FLOAT_EXACT_GE(1.2, 1.2); } |
| 333 | TEST_F(AssertPass, FloatGe3) { PW_CHECK_FLOAT_EXACT_GE(1.2, 1.1); } |
Keir Mierle | 8d2a84f | 2020-04-14 22:00:00 -0700 | [diff] [blame] | 334 | |
| 335 | // Nested comma handling. |
| 336 | static int Add3(int a, int b, int c) { return a + b + c; } |
| 337 | |
| 338 | TEST_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 | } |
| 342 | TEST_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. |
| 348 | static int global_state_for_multi_evaluate_test; |
| 349 | static int IncrementsGlobal() { |
| 350 | global_state_for_multi_evaluate_test++; |
| 351 | return 0; |
| 352 | } |
| 353 | |
| 354 | TEST(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 | } |
| 359 | TEST(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 | } |
| 364 | TEST(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 | } |
| 369 | TEST(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 | } |
| 374 | TEST(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 | } |
| 379 | TEST(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 Mierle | b9b8816 | 2020-04-15 20:43:09 -0700 | [diff] [blame] | 385 | // Verify side effects of debug checks work as expected. |
| 386 | // Only check a couple of cases, since the logic is all the same. |
Keir Mierle | 854adec | 2020-09-03 14:07:19 -0700 | [diff] [blame] | 387 | #if PW_ASSERT_ENABLE_DEBUG |
Keir Mierle | b9b8816 | 2020-04-15 20:43:09 -0700 | [diff] [blame] | 388 | // When DCHECKs are enabled, they behave the same as normal checks. |
| 389 | TEST(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 | } |
| 394 | TEST(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 | } |
| 399 | TEST(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 | } |
| 404 | TEST(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 | } |
| 409 | TEST(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 | } |
| 414 | TEST(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 Mierle | 854adec | 2020-09-03 14:07:19 -0700 | [diff] [blame] | 420 | #else // PW_ASSERT_ENABLE_DEBUG |
Keir Mierle | b9b8816 | 2020-04-15 20:43:09 -0700 | [diff] [blame] | 421 | |
| 422 | // When DCHECKs are disabled, they should not trip, and their arguments |
| 423 | // shouldn't be evaluated. |
Wyatt Hepler | d78f7c6 | 2020-09-28 14:27:32 -0700 | [diff] [blame] | 424 | TEST(AssertPass, DCheckDisabledSingleSideEffectingCall_1) { |
Keir Mierle | b9b8816 | 2020-04-15 20:43:09 -0700 | [diff] [blame] | 425 | global_state_for_multi_evaluate_test = 0; |
| 426 | PW_DCHECK(IncrementsGlobal() == 0); |
| 427 | EXPECT_EQ(global_state_for_multi_evaluate_test, 0); |
| 428 | } |
Wyatt Hepler | d78f7c6 | 2020-09-28 14:27:32 -0700 | [diff] [blame] | 429 | TEST(AssertPass, DCheckDisabledSingleSideEffectingCall_2) { |
Keir Mierle | b9b8816 | 2020-04-15 20:43:09 -0700 | [diff] [blame] | 430 | global_state_for_multi_evaluate_test = 0; |
| 431 | PW_DCHECK(IncrementsGlobal() == 1); |
| 432 | EXPECT_EQ(global_state_for_multi_evaluate_test, 0); |
| 433 | } |
Wyatt Hepler | d78f7c6 | 2020-09-28 14:27:32 -0700 | [diff] [blame] | 434 | TEST(AssertPass, DCheckDisabledBinaryOpSingleSideEffectingCall_1) { |
Keir Mierle | b9b8816 | 2020-04-15 20:43:09 -0700 | [diff] [blame] | 435 | 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 Hepler | d78f7c6 | 2020-09-28 14:27:32 -0700 | [diff] [blame] | 439 | TEST(AssertPass, DCheckDisabledBinaryOpTwoSideEffectingCalls_1) { |
Keir Mierle | b9b8816 | 2020-04-15 20:43:09 -0700 | [diff] [blame] | 440 | 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 Hepler | d78f7c6 | 2020-09-28 14:27:32 -0700 | [diff] [blame] | 444 | TEST(AssertPass, DCheckDisabledBinaryOpSingleSideEffectingCall_2) { |
Keir Mierle | b9b8816 | 2020-04-15 20:43:09 -0700 | [diff] [blame] | 445 | 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 Hepler | d78f7c6 | 2020-09-28 14:27:32 -0700 | [diff] [blame] | 449 | TEST(AssertPass, DCheckDisabledBinaryOpTwoSideEffectingCalls_2) { |
Keir Mierle | b9b8816 | 2020-04-15 20:43:09 -0700 | [diff] [blame] | 450 | 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 Mierle | 854adec | 2020-09-03 14:07:19 -0700 | [diff] [blame] | 454 | #endif // PW_ASSERT_ENABLE_DEBUG |
Keir Mierle | b9b8816 | 2020-04-15 20:43:09 -0700 | [diff] [blame] | 455 | |
Keir Mierle | 8d2a84f | 2020-04-14 22:00:00 -0700 | [diff] [blame] | 456 | // Note: This requires enabling PW_ASSERT_USE_SHORT_NAMES 1 above. |
| 457 | TEST(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 Mierle | 0fa7f7d | 2020-05-07 12:34:00 -0700 | [diff] [blame] | 476 | // Verify PW_CHECK_OK, including message handling. |
| 477 | TEST_F(AssertFail, StatusNotOK) { |
Wyatt Hepler | d78f7c6 | 2020-09-28 14:27:32 -0700 | [diff] [blame] | 478 | pw::Status status = pw::Status::Unknown(); |
Keir Mierle | 0fa7f7d | 2020-05-07 12:34:00 -0700 | [diff] [blame] | 479 | PW_CHECK_OK(status); |
Wyatt Hepler | 1b3da3a | 2021-01-07 13:26:57 -0800 | [diff] [blame] | 480 | EXPECT_MESSAGE("Check failed: status (=UNKNOWN) == OkStatus() (=OK). "); |
Keir Mierle | 0fa7f7d | 2020-05-07 12:34:00 -0700 | [diff] [blame] | 481 | } |
| 482 | |
| 483 | TEST_F(AssertFail, StatusNotOKMessageNoArguments) { |
Wyatt Hepler | d78f7c6 | 2020-09-28 14:27:32 -0700 | [diff] [blame] | 484 | pw::Status status = pw::Status::Unknown(); |
Keir Mierle | 0fa7f7d | 2020-05-07 12:34:00 -0700 | [diff] [blame] | 485 | PW_CHECK_OK(status, "msg"); |
Wyatt Hepler | 1b3da3a | 2021-01-07 13:26:57 -0800 | [diff] [blame] | 486 | EXPECT_MESSAGE("Check failed: status (=UNKNOWN) == OkStatus() (=OK). msg"); |
Keir Mierle | 0fa7f7d | 2020-05-07 12:34:00 -0700 | [diff] [blame] | 487 | } |
| 488 | |
| 489 | TEST_F(AssertFail, StatusNotOKMessageArguments) { |
Wyatt Hepler | d78f7c6 | 2020-09-28 14:27:32 -0700 | [diff] [blame] | 490 | pw::Status status = pw::Status::Unknown(); |
Keir Mierle | 0fa7f7d | 2020-05-07 12:34:00 -0700 | [diff] [blame] | 491 | PW_CHECK_OK(status, "msg: %d", 5); |
Wyatt Hepler | 1b3da3a | 2021-01-07 13:26:57 -0800 | [diff] [blame] | 492 | EXPECT_MESSAGE("Check failed: status (=UNKNOWN) == OkStatus() (=OK). msg: 5"); |
Keir Mierle | 0fa7f7d | 2020-05-07 12:34:00 -0700 | [diff] [blame] | 493 | } |
| 494 | |
| 495 | // Example expression for the test below. |
Wyatt Hepler | d78f7c6 | 2020-09-28 14:27:32 -0700 | [diff] [blame] | 496 | pw::Status DoTheThing() { return pw::Status::ResourceExhausted(); } |
Keir Mierle | 0fa7f7d | 2020-05-07 12:34:00 -0700 | [diff] [blame] | 497 | |
| 498 | TEST_F(AssertFail, NonTrivialExpression) { |
| 499 | PW_CHECK_OK(DoTheThing()); |
| 500 | EXPECT_MESSAGE( |
Wyatt Hepler | 1b3da3a | 2021-01-07 13:26:57 -0800 | [diff] [blame] | 501 | "Check failed: DoTheThing() (=RESOURCE_EXHAUSTED) == OkStatus() (=OK). "); |
Keir Mierle | 0fa7f7d | 2020-05-07 12:34:00 -0700 | [diff] [blame] | 502 | } |
| 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 Hepler | 1b3da3a | 2021-01-07 13:26:57 -0800 | [diff] [blame] | 507 | TEST_F(AssertPass, Function) { PW_CHECK_OK(pw::OkStatus()); } |
Keir Mierle | 0fa7f7d | 2020-05-07 12:34:00 -0700 | [diff] [blame] | 508 | TEST_F(AssertPass, Enum) { PW_CHECK_OK(PW_STATUS_OK); } |
Wyatt Hepler | d78f7c6 | 2020-09-28 14:27:32 -0700 | [diff] [blame] | 509 | TEST_F(AssertFail, Function) { PW_CHECK_OK(pw::Status::Unknown()); } |
Keir Mierle | 0fa7f7d | 2020-05-07 12:34:00 -0700 | [diff] [blame] | 510 | TEST_F(AssertFail, Enum) { PW_CHECK_OK(PW_STATUS_UNKNOWN); } |
| 511 | |
Keir Mierle | 854adec | 2020-09-03 14:07:19 -0700 | [diff] [blame] | 512 | #if PW_ASSERT_ENABLE_DEBUG |
Keir Mierle | 0fa7f7d | 2020-05-07 12:34:00 -0700 | [diff] [blame] | 513 | |
| 514 | // In debug mode, the asserts should check their arguments. |
Wyatt Hepler | 1b3da3a | 2021-01-07 13:26:57 -0800 | [diff] [blame] | 515 | TEST_F(AssertPass, DCheckFunction) { PW_DCHECK_OK(pw::OkStatus()); } |
Wyatt Hepler | d78f7c6 | 2020-09-28 14:27:32 -0700 | [diff] [blame] | 516 | TEST_F(AssertPass, DCheckEnum) { PW_DCHECK_OK(PW_STATUS_OK); } |
| 517 | TEST_F(AssertFail, DCheckFunction) { PW_DCHECK_OK(pw::Status::Unknown()); } |
| 518 | TEST_F(AssertFail, DCheckEnum) { PW_DCHECK_OK(PW_STATUS_UNKNOWN); } |
Keir Mierle | 854adec | 2020-09-03 14:07:19 -0700 | [diff] [blame] | 519 | #else // PW_ASSERT_ENABLE_DEBUG |
Keir Mierle | 0fa7f7d | 2020-05-07 12:34:00 -0700 | [diff] [blame] | 520 | |
| 521 | // In release mode, all the asserts should pass. |
Wyatt Hepler | 1b3da3a | 2021-01-07 13:26:57 -0800 | [diff] [blame] | 522 | TEST_F(AssertPass, DCheckFunction_Ok) { PW_DCHECK_OK(pw::OkStatus()); } |
Wyatt Hepler | d78f7c6 | 2020-09-28 14:27:32 -0700 | [diff] [blame] | 523 | TEST_F(AssertPass, DCheckEnum_Ok) { PW_DCHECK_OK(PW_STATUS_OK); } |
| 524 | TEST_F(AssertPass, DCheckFunction_Err) { PW_DCHECK_OK(pw::Status::Unknown()); } |
| 525 | TEST_F(AssertPass, DCheckEnum_Err) { PW_DCHECK_OK(PW_STATUS_UNKNOWN); } |
Keir Mierle | 854adec | 2020-09-03 14:07:19 -0700 | [diff] [blame] | 526 | #endif // PW_ASSERT_ENABLE_DEBUG |
Keir Mierle | 0fa7f7d | 2020-05-07 12:34:00 -0700 | [diff] [blame] | 527 | |
Keir Mierle | 8d2a84f | 2020-04-14 22:00:00 -0700 | [diff] [blame] | 528 | // TODO: Figure out how to run some of these tests is C. |
| 529 | |
| 530 | } // namespace |