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 |
Wyatt Hepler | a59998f | 2021-03-19 14:35:10 -0700 | [diff] [blame] | 25 | #include "pw_assert/internal/check_impl.h" |
Keir Mierle | 8d2a84f | 2020-04-14 22:00:00 -0700 | [diff] [blame] | 26 | // clang-format on |
| 27 | |
| 28 | #include "gtest/gtest.h" |
| 29 | |
| 30 | namespace { |
| 31 | |
| 32 | #define EXPECT_MESSAGE(expected_assert_message) \ |
| 33 | do { \ |
| 34 | EXPECT_STREQ(pw_captured_assert.message, expected_assert_message); \ |
| 35 | } while (0) |
| 36 | |
| 37 | class AssertFail : public ::testing::Test { |
| 38 | protected: |
| 39 | void SetUp() override { pw_captured_assert.triggered = 0; } |
| 40 | void TearDown() override { EXPECT_EQ(pw_captured_assert.triggered, 1); } |
| 41 | }; |
| 42 | |
| 43 | class AssertPass : public ::testing::Test { |
| 44 | protected: |
| 45 | void SetUp() override { pw_captured_assert.triggered = 0; } |
| 46 | void TearDown() override { EXPECT_EQ(pw_captured_assert.triggered, 0); } |
| 47 | }; |
| 48 | |
| 49 | // PW_CRASH(...) |
| 50 | TEST_F(AssertFail, CrashMessageNoArguments) { |
| 51 | PW_CRASH("Goodbye"); |
| 52 | EXPECT_MESSAGE("Goodbye"); |
| 53 | } |
| 54 | TEST_F(AssertFail, CrashMessageWithArguments) { |
| 55 | PW_CRASH("Goodbye cruel %s", "world"); |
| 56 | EXPECT_MESSAGE("Goodbye cruel world"); |
| 57 | } |
| 58 | |
| 59 | // PW_CHECK(...) - No message |
| 60 | TEST_F(AssertPass, CheckNoMessage) { PW_CHECK(true); } |
| 61 | TEST_F(AssertFail, CheckNoMessage) { |
| 62 | PW_CHECK(false); |
| 63 | EXPECT_MESSAGE("Check failed: false. "); |
| 64 | } |
| 65 | TEST_F(AssertPass, CheckNoMessageComplexExpression) { PW_CHECK(2 == 2); } |
| 66 | TEST_F(AssertFail, CheckNoMessageComplexExpression) { |
| 67 | PW_CHECK(1 == 2); |
| 68 | EXPECT_MESSAGE("Check failed: 1 == 2. "); |
| 69 | } |
| 70 | |
| 71 | // PW_CHECK(..., msg) - With message; with and without arguments. |
| 72 | TEST_F(AssertPass, CheckMessageNoArguments) { PW_CHECK(true, "Hello"); } |
| 73 | TEST_F(AssertFail, CheckMessageNoArguments) { |
| 74 | PW_CHECK(false, "Hello"); |
| 75 | EXPECT_MESSAGE("Check failed: false. Hello"); |
| 76 | } |
| 77 | TEST_F(AssertPass, CheckMessageWithArguments) { PW_CHECK(true, "Hello %d", 5); } |
| 78 | TEST_F(AssertFail, CheckMessageWithArguments) { |
| 79 | PW_CHECK(false, "Hello %d", 5); |
| 80 | EXPECT_MESSAGE("Check failed: false. Hello 5"); |
| 81 | } |
| 82 | |
| 83 | // PW_CHECK_INT_*(...) |
Keir Mierle | fa8f89d | 2020-04-15 16:20:02 -0700 | [diff] [blame] | 84 | // Binary checks with ints, comparisons: <, <=, =, !=, >, >=. |
Keir Mierle | 8d2a84f | 2020-04-14 22:00:00 -0700 | [diff] [blame] | 85 | |
| 86 | // Test message formatting separate from the triggering. |
| 87 | // Only test formatting for the type once. |
| 88 | TEST_F(AssertFail, IntLessThanNoMessageNoArguments) { |
| 89 | PW_CHECK_INT_LT(5, -2); |
| 90 | EXPECT_MESSAGE("Check failed: 5 (=5) < -2 (=-2). "); |
| 91 | } |
| 92 | TEST_F(AssertFail, IntLessThanMessageNoArguments) { |
| 93 | PW_CHECK_INT_LT(5, -2, "msg"); |
| 94 | EXPECT_MESSAGE("Check failed: 5 (=5) < -2 (=-2). msg"); |
| 95 | } |
| 96 | TEST_F(AssertFail, IntLessThanMessageArguments) { |
| 97 | PW_CHECK_INT_LT(5, -2, "msg: %d", 6); |
| 98 | EXPECT_MESSAGE("Check failed: 5 (=5) < -2 (=-2). msg: 6"); |
| 99 | } |
| 100 | |
| 101 | // Test comparison boundaries. |
| 102 | |
| 103 | // INT < |
| 104 | TEST_F(AssertPass, IntLt1) { PW_CHECK_INT_LT(-1, 2); } |
| 105 | TEST_F(AssertPass, IntLt2) { PW_CHECK_INT_LT(1, 2); } |
| 106 | TEST_F(AssertFail, IntLt3) { PW_CHECK_INT_LT(-1, -2); } |
| 107 | TEST_F(AssertFail, IntLt4) { PW_CHECK_INT_LT(1, 1); } |
| 108 | |
| 109 | // INT <= |
| 110 | TEST_F(AssertPass, IntLe1) { PW_CHECK_INT_LE(-1, 2); } |
| 111 | TEST_F(AssertPass, IntLe2) { PW_CHECK_INT_LE(1, 2); } |
| 112 | TEST_F(AssertFail, IntLe3) { PW_CHECK_INT_LE(-1, -2); } |
| 113 | TEST_F(AssertPass, IntLe4) { PW_CHECK_INT_LE(1, 1); } |
| 114 | |
| 115 | // INT == |
| 116 | TEST_F(AssertFail, IntEq1) { PW_CHECK_INT_EQ(-1, 2); } |
| 117 | TEST_F(AssertFail, IntEq2) { PW_CHECK_INT_EQ(1, 2); } |
| 118 | TEST_F(AssertFail, IntEq3) { PW_CHECK_INT_EQ(-1, -2); } |
| 119 | TEST_F(AssertPass, IntEq4) { PW_CHECK_INT_EQ(1, 1); } |
| 120 | |
Keir Mierle | fa8f89d | 2020-04-15 16:20:02 -0700 | [diff] [blame] | 121 | // INT != |
| 122 | TEST_F(AssertPass, IntNe1) { PW_CHECK_INT_NE(-1, 2); } |
| 123 | TEST_F(AssertPass, IntNe2) { PW_CHECK_INT_NE(1, 2); } |
| 124 | TEST_F(AssertPass, IntNe3) { PW_CHECK_INT_NE(-1, -2); } |
| 125 | TEST_F(AssertFail, IntNe4) { PW_CHECK_INT_NE(1, 1); } |
| 126 | |
Keir Mierle | 8d2a84f | 2020-04-14 22:00:00 -0700 | [diff] [blame] | 127 | // INT > |
| 128 | TEST_F(AssertFail, IntGt1) { PW_CHECK_INT_GT(-1, 2); } |
| 129 | TEST_F(AssertFail, IntGt2) { PW_CHECK_INT_GT(1, 2); } |
| 130 | TEST_F(AssertPass, IntGt3) { PW_CHECK_INT_GT(-1, -2); } |
| 131 | TEST_F(AssertFail, IntGt4) { PW_CHECK_INT_GT(1, 1); } |
| 132 | |
| 133 | // INT >= |
| 134 | TEST_F(AssertFail, IntGe1) { PW_CHECK_INT_GE(-1, 2); } |
| 135 | TEST_F(AssertFail, IntGe2) { PW_CHECK_INT_GE(1, 2); } |
| 136 | TEST_F(AssertPass, IntGe3) { PW_CHECK_INT_GE(-1, -2); } |
| 137 | TEST_F(AssertPass, IntGe4) { PW_CHECK_INT_GE(1, 1); } |
| 138 | |
| 139 | // PW_CHECK_UINT_*(...) |
Keir Mierle | fa8f89d | 2020-04-15 16:20:02 -0700 | [diff] [blame] | 140 | // Binary checks with uints, comparisons: <, <=, =, !=, >, >=. |
Keir Mierle | 8d2a84f | 2020-04-14 22:00:00 -0700 | [diff] [blame] | 141 | |
| 142 | // Test message formatting separate from the triggering. |
| 143 | // Only test formatting for the type once. |
| 144 | TEST_F(AssertFail, UintLessThanNoMessageNoArguments) { |
| 145 | PW_CHECK_UINT_LT(5, 2); |
| 146 | EXPECT_MESSAGE("Check failed: 5 (=5) < 2 (=2). "); |
| 147 | } |
| 148 | TEST_F(AssertFail, UintLessThanMessageNoArguments) { |
| 149 | PW_CHECK_UINT_LT(5, 2, "msg"); |
| 150 | EXPECT_MESSAGE("Check failed: 5 (=5) < 2 (=2). msg"); |
| 151 | } |
| 152 | TEST_F(AssertFail, UintLessThanMessageArguments) { |
| 153 | PW_CHECK_UINT_LT(5, 2, "msg: %d", 6); |
| 154 | EXPECT_MESSAGE("Check failed: 5 (=5) < 2 (=2). msg: 6"); |
| 155 | } |
| 156 | |
| 157 | // Test comparison boundaries. |
| 158 | |
| 159 | // UINT < |
| 160 | TEST_F(AssertPass, UintLt1) { PW_CHECK_UINT_LT(1, 2); } |
| 161 | TEST_F(AssertFail, UintLt2) { PW_CHECK_UINT_LT(2, 2); } |
| 162 | TEST_F(AssertFail, UintLt3) { PW_CHECK_UINT_LT(2, 1); } |
| 163 | |
| 164 | // UINT <= |
| 165 | TEST_F(AssertPass, UintLe1) { PW_CHECK_UINT_LE(1, 2); } |
| 166 | TEST_F(AssertPass, UintLe2) { PW_CHECK_UINT_LE(2, 2); } |
| 167 | TEST_F(AssertFail, UintLe3) { PW_CHECK_UINT_LE(2, 1); } |
| 168 | |
| 169 | // UINT == |
| 170 | TEST_F(AssertFail, UintEq1) { PW_CHECK_UINT_EQ(1, 2); } |
| 171 | TEST_F(AssertPass, UintEq2) { PW_CHECK_UINT_EQ(2, 2); } |
| 172 | TEST_F(AssertFail, UintEq3) { PW_CHECK_UINT_EQ(2, 1); } |
| 173 | |
Keir Mierle | fa8f89d | 2020-04-15 16:20:02 -0700 | [diff] [blame] | 174 | // UINT != |
| 175 | TEST_F(AssertPass, UintNe1) { PW_CHECK_UINT_NE(1, 2); } |
| 176 | TEST_F(AssertFail, UintNe2) { PW_CHECK_UINT_NE(2, 2); } |
| 177 | TEST_F(AssertPass, UintNe3) { PW_CHECK_UINT_NE(2, 1); } |
| 178 | |
Keir Mierle | 8d2a84f | 2020-04-14 22:00:00 -0700 | [diff] [blame] | 179 | // UINT > |
| 180 | TEST_F(AssertFail, UintGt1) { PW_CHECK_UINT_GT(1, 2); } |
| 181 | TEST_F(AssertFail, UintGt2) { PW_CHECK_UINT_GT(2, 2); } |
| 182 | TEST_F(AssertPass, UintGt3) { PW_CHECK_UINT_GT(2, 1); } |
| 183 | |
| 184 | // UINT >= |
| 185 | TEST_F(AssertFail, UintGe1) { PW_CHECK_UINT_GE(1, 2); } |
| 186 | TEST_F(AssertPass, UintGe2) { PW_CHECK_UINT_GE(2, 2); } |
| 187 | TEST_F(AssertPass, UintGe3) { PW_CHECK_UINT_GE(2, 1); } |
| 188 | |
Keir Mierle | fa8f89d | 2020-04-15 16:20:02 -0700 | [diff] [blame] | 189 | // PW_CHECK_PTR_*(...) |
| 190 | // Binary checks with uints, comparisons: <, <=, =, !=, >, >=. |
| 191 | // Note: The format checks are skipped since they're not portable. |
| 192 | |
| 193 | // Test comparison boundaries. |
| 194 | |
| 195 | // PTR < |
| 196 | TEST_F(AssertPass, PtrLt1) { PW_CHECK_PTR_LT(0xa, 0xb); } |
| 197 | TEST_F(AssertFail, PtrLt2) { PW_CHECK_PTR_LT(0xb, 0xb); } |
| 198 | TEST_F(AssertFail, PtrLt3) { PW_CHECK_PTR_LT(0xb, 0xa); } |
| 199 | |
| 200 | // PTR <= |
| 201 | TEST_F(AssertPass, PtrLe1) { PW_CHECK_PTR_LE(0xa, 0xb); } |
| 202 | TEST_F(AssertPass, PtrLe2) { PW_CHECK_PTR_LE(0xb, 0xb); } |
| 203 | TEST_F(AssertFail, PtrLe3) { PW_CHECK_PTR_LE(0xb, 0xa); } |
| 204 | |
| 205 | // PTR == |
| 206 | TEST_F(AssertFail, PtrEq1) { PW_CHECK_PTR_EQ(0xa, 0xb); } |
| 207 | TEST_F(AssertPass, PtrEq2) { PW_CHECK_PTR_EQ(0xb, 0xb); } |
| 208 | TEST_F(AssertFail, PtrEq3) { PW_CHECK_PTR_EQ(0xb, 0xa); } |
| 209 | |
| 210 | // PTR != |
| 211 | TEST_F(AssertPass, PtrNe1) { PW_CHECK_PTR_NE(0xa, 0xb); } |
| 212 | TEST_F(AssertFail, PtrNe2) { PW_CHECK_PTR_NE(0xb, 0xb); } |
| 213 | TEST_F(AssertPass, PtrNe3) { PW_CHECK_PTR_NE(0xb, 0xa); } |
| 214 | |
| 215 | // PTR > |
| 216 | TEST_F(AssertFail, PtrGt1) { PW_CHECK_PTR_GT(0xa, 0xb); } |
| 217 | TEST_F(AssertFail, PtrGt2) { PW_CHECK_PTR_GT(0xb, 0xb); } |
| 218 | TEST_F(AssertPass, PtrGt3) { PW_CHECK_PTR_GT(0xb, 0xa); } |
| 219 | |
| 220 | // PTR >= |
| 221 | TEST_F(AssertFail, PtrGe1) { PW_CHECK_PTR_GE(0xa, 0xb); } |
| 222 | TEST_F(AssertPass, PtrGe2) { PW_CHECK_PTR_GE(0xb, 0xb); } |
| 223 | TEST_F(AssertPass, PtrGe3) { PW_CHECK_PTR_GE(0xb, 0xa); } |
| 224 | |
| 225 | // NOTNULL |
| 226 | TEST_F(AssertPass, PtrNotNull) { PW_CHECK_NOTNULL(0xa); } |
| 227 | TEST_F(AssertFail, PtrNotNull) { PW_CHECK_NOTNULL(0x0); } |
| 228 | |
Keir Mierle | a84fd8d | 2020-08-07 21:29:26 -0700 | [diff] [blame] | 229 | // Note: Due to platform inconsistencies, the below test for the NOTNULL |
| 230 | // message doesn't work. Some platforms print NULL formatted as %p as "(nil)", |
| 231 | // others "0x0". Leaving this here for reference. |
| 232 | // |
| 233 | // TEST_F(AssertFail, PtrNotNullDescription) { |
| 234 | // intptr_t intptr = 0; |
| 235 | // PW_CHECK_NOTNULL(intptr); |
| 236 | // EXPECT_MESSAGE("Check failed: intptr (=0x0) != nullptr (=0x0). "); |
| 237 | // } |
| 238 | |
Keir Mierle | 8d2a84f | 2020-04-14 22:00:00 -0700 | [diff] [blame] | 239 | // PW_CHECK_FLOAT_*(...) |
Ewout van Bekkum | 9e97cfd | 2020-07-16 13:57:24 -0700 | [diff] [blame] | 240 | // Binary checks with floats, comparisons: EXACT_LT, EXACT_LE, NEAR, EXACT_EQ, |
| 241 | // EXACT_NE, EXACT_GE, EXACT_GT. |
Keir Mierle | 8d2a84f | 2020-04-14 22:00:00 -0700 | [diff] [blame] | 242 | |
| 243 | // Test message formatting separate from the triggering. |
| 244 | // Only test formatting for the type once. |
| 245 | TEST_F(AssertFail, FloatLessThanNoMessageNoArguments) { |
Ewout van Bekkum | 9e97cfd | 2020-07-16 13:57:24 -0700 | [diff] [blame] | 246 | PW_CHECK_FLOAT_EXACT_LT(5.2, 2.3); |
Keir Mierle | 8d2a84f | 2020-04-14 22:00:00 -0700 | [diff] [blame] | 247 | EXPECT_MESSAGE("Check failed: 5.2 (=5.200000) < 2.3 (=2.300000). "); |
| 248 | } |
| 249 | TEST_F(AssertFail, FloatLessThanMessageNoArguments) { |
Ewout van Bekkum | 9e97cfd | 2020-07-16 13:57:24 -0700 | [diff] [blame] | 250 | PW_CHECK_FLOAT_EXACT_LT(5.2, 2.3, "msg"); |
Keir Mierle | 8d2a84f | 2020-04-14 22:00:00 -0700 | [diff] [blame] | 251 | EXPECT_MESSAGE("Check failed: 5.2 (=5.200000) < 2.3 (=2.300000). msg"); |
| 252 | } |
| 253 | TEST_F(AssertFail, FloatLessThanMessageArguments) { |
Ewout van Bekkum | 9e97cfd | 2020-07-16 13:57:24 -0700 | [diff] [blame] | 254 | PW_CHECK_FLOAT_EXACT_LT(5.2, 2.3, "msg: %d", 6); |
Keir Mierle | 8d2a84f | 2020-04-14 22:00:00 -0700 | [diff] [blame] | 255 | EXPECT_MESSAGE("Check failed: 5.2 (=5.200000) < 2.3 (=2.300000). msg: 6"); |
| 256 | } |
Ewout van Bekkum | 9e97cfd | 2020-07-16 13:57:24 -0700 | [diff] [blame] | 257 | // Check float NEAR both above and below the permitted range. |
| 258 | TEST_F(AssertFail, FloatNearAboveNoMessageNoArguments) { |
| 259 | PW_CHECK_FLOAT_NEAR(5.2, 2.3, 0.1); |
| 260 | EXPECT_MESSAGE( |
| 261 | "Check failed: 5.2 (=5.200000) <= 2.3 + abs_tolerance (=2.400000). "); |
| 262 | } |
| 263 | TEST_F(AssertFail, FloatNearAboveMessageNoArguments) { |
| 264 | PW_CHECK_FLOAT_NEAR(5.2, 2.3, 0.1, "msg"); |
| 265 | EXPECT_MESSAGE( |
| 266 | "Check failed: 5.2 (=5.200000) <= 2.3 + abs_tolerance (=2.400000). msg"); |
| 267 | } |
| 268 | TEST_F(AssertFail, FloatNearAboveMessageArguments) { |
| 269 | PW_CHECK_FLOAT_NEAR(5.2, 2.3, 0.1, "msg: %d", 6); |
| 270 | EXPECT_MESSAGE( |
| 271 | "Check failed: 5.2 (=5.200000) <= 2.3 + abs_tolerance (=2.400000). msg: " |
| 272 | "6"); |
| 273 | } |
| 274 | TEST_F(AssertFail, FloatNearBelowNoMessageNoArguments) { |
| 275 | PW_CHECK_FLOAT_NEAR(1.2, 2.3, 0.1); |
| 276 | EXPECT_MESSAGE( |
| 277 | "Check failed: 1.2 (=1.200000) >= 2.3 - abs_tolerance (=2.200000). "); |
| 278 | } |
| 279 | TEST_F(AssertFail, FloatNearBelowMessageNoArguments) { |
| 280 | PW_CHECK_FLOAT_NEAR(1.2, 2.3, 0.1, "msg"); |
| 281 | EXPECT_MESSAGE( |
| 282 | "Check failed: 1.2 (=1.200000) >= 2.3 - abs_tolerance (=2.200000). msg"); |
| 283 | } |
| 284 | TEST_F(AssertFail, FloatNearBelowMessageArguments) { |
| 285 | PW_CHECK_FLOAT_NEAR(1.2, 2.3, 0.1, "msg: %d", 6); |
| 286 | EXPECT_MESSAGE( |
| 287 | "Check failed: 1.2 (=1.200000) >= 2.3 - abs_tolerance (=2.200000). msg: " |
| 288 | "6"); |
| 289 | } |
Keir Mierle | 8d2a84f | 2020-04-14 22:00:00 -0700 | [diff] [blame] | 290 | // Test comparison boundaries. |
| 291 | // Note: The below example numbers all round to integer 1, to detect accidental |
| 292 | // integer conversions in the asserts. |
| 293 | |
| 294 | // FLOAT < |
Ewout van Bekkum | 9e97cfd | 2020-07-16 13:57:24 -0700 | [diff] [blame] | 295 | TEST_F(AssertPass, FloatLt1) { PW_CHECK_FLOAT_EXACT_LT(1.1, 1.2); } |
| 296 | TEST_F(AssertFail, FloatLt2) { PW_CHECK_FLOAT_EXACT_LT(1.2, 1.2); } |
| 297 | 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] | 298 | |
| 299 | // FLOAT <= |
Ewout van Bekkum | 9e97cfd | 2020-07-16 13:57:24 -0700 | [diff] [blame] | 300 | TEST_F(AssertPass, FloatLe1) { PW_CHECK_FLOAT_EXACT_LE(1.1, 1.2); } |
| 301 | TEST_F(AssertPass, FloatLe2) { PW_CHECK_FLOAT_EXACT_LE(1.2, 1.2); } |
| 302 | TEST_F(AssertFail, FloatLe3) { PW_CHECK_FLOAT_EXACT_LE(1.2, 1.1); } |
| 303 | |
| 304 | // FLOAT ~= based on absolute error. |
| 305 | TEST_F(AssertFail, FloatNearAbs1) { PW_CHECK_FLOAT_NEAR(1.09, 1.2, 0.1); } |
| 306 | TEST_F(AssertPass, FloatNearAbs2) { PW_CHECK_FLOAT_NEAR(1.1, 1.2, 0.1); } |
| 307 | TEST_F(AssertPass, FloatNearAbs3) { PW_CHECK_FLOAT_NEAR(1.2, 1.2, 0.1); } |
| 308 | TEST_F(AssertPass, FloatNearAbs4) { PW_CHECK_FLOAT_NEAR(1.2, 1.1, 0.1); } |
| 309 | TEST_F(AssertFail, FloatNearAbs5) { PW_CHECK_FLOAT_NEAR(1.21, 1.1, 0.1); } |
| 310 | // Make sure the abs_tolerance is asserted to be >= 0. |
| 311 | TEST_F(AssertFail, FloatNearAbs6) { PW_CHECK_FLOAT_NEAR(1.2, 1.2, -0.1); } |
| 312 | 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] | 313 | |
| 314 | // FLOAT == |
Ewout van Bekkum | 9e97cfd | 2020-07-16 13:57:24 -0700 | [diff] [blame] | 315 | TEST_F(AssertFail, FloatEq1) { PW_CHECK_FLOAT_EXACT_EQ(1.1, 1.2); } |
| 316 | TEST_F(AssertPass, FloatEq2) { PW_CHECK_FLOAT_EXACT_EQ(1.2, 1.2); } |
| 317 | 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] | 318 | |
Keir Mierle | fa8f89d | 2020-04-15 16:20:02 -0700 | [diff] [blame] | 319 | // FLOAT != |
Ewout van Bekkum | 9e97cfd | 2020-07-16 13:57:24 -0700 | [diff] [blame] | 320 | TEST_F(AssertPass, FloatNe1) { PW_CHECK_FLOAT_EXACT_NE(1.1, 1.2); } |
| 321 | TEST_F(AssertFail, FloatNe2) { PW_CHECK_FLOAT_EXACT_NE(1.2, 1.2); } |
| 322 | 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] | 323 | |
Keir Mierle | 8d2a84f | 2020-04-14 22:00:00 -0700 | [diff] [blame] | 324 | // FLOAT > |
Ewout van Bekkum | 9e97cfd | 2020-07-16 13:57:24 -0700 | [diff] [blame] | 325 | TEST_F(AssertFail, FloatGt1) { PW_CHECK_FLOAT_EXACT_GT(1.1, 1.2); } |
| 326 | TEST_F(AssertFail, FloatGt2) { PW_CHECK_FLOAT_EXACT_GT(1.2, 1.2); } |
| 327 | 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] | 328 | |
| 329 | // FLOAT >= |
Ewout van Bekkum | 9e97cfd | 2020-07-16 13:57:24 -0700 | [diff] [blame] | 330 | TEST_F(AssertFail, FloatGe1) { PW_CHECK_FLOAT_EXACT_GE(1.1, 1.2); } |
| 331 | TEST_F(AssertPass, FloatGe2) { PW_CHECK_FLOAT_EXACT_GE(1.2, 1.2); } |
| 332 | 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] | 333 | |
| 334 | // Nested comma handling. |
| 335 | static int Add3(int a, int b, int c) { return a + b + c; } |
| 336 | |
| 337 | TEST_F(AssertFail, CommaHandlingLeftSide) { |
| 338 | PW_CHECK_INT_EQ(Add3(1, 2, 3), 4); |
| 339 | EXPECT_MESSAGE("Check failed: Add3(1, 2, 3) (=6) == 4 (=4). "); |
| 340 | } |
| 341 | TEST_F(AssertFail, CommaHandlingRightSide) { |
| 342 | PW_CHECK_INT_EQ(4, Add3(1, 2, 3)); |
| 343 | EXPECT_MESSAGE("Check failed: 4 (=4) == Add3(1, 2, 3) (=6). "); |
| 344 | } |
| 345 | |
| 346 | // Verify that the CHECK_*(x,y) macros only evaluate their arguments once. |
| 347 | static int global_state_for_multi_evaluate_test; |
| 348 | static int IncrementsGlobal() { |
| 349 | global_state_for_multi_evaluate_test++; |
| 350 | return 0; |
| 351 | } |
| 352 | |
| 353 | TEST(AssertPass, CheckSingleSideEffectingCall) { |
| 354 | global_state_for_multi_evaluate_test = 0; |
| 355 | PW_CHECK(IncrementsGlobal() == 0); |
| 356 | EXPECT_EQ(global_state_for_multi_evaluate_test, 1); |
| 357 | } |
| 358 | TEST(AssertFail, CheckSingleSideEffectingCall) { |
| 359 | global_state_for_multi_evaluate_test = 0; |
| 360 | PW_CHECK(IncrementsGlobal() == 1); |
| 361 | EXPECT_EQ(global_state_for_multi_evaluate_test, 1); |
| 362 | } |
| 363 | TEST(AssertPass, BinaryOpSingleSideEffectingCall) { |
| 364 | global_state_for_multi_evaluate_test = 0; |
| 365 | PW_CHECK_INT_EQ(0, IncrementsGlobal()); |
| 366 | EXPECT_EQ(global_state_for_multi_evaluate_test, 1); |
| 367 | } |
| 368 | TEST(AssertPass, BinaryOpTwoSideEffectingCalls) { |
| 369 | global_state_for_multi_evaluate_test = 0; |
| 370 | PW_CHECK_INT_EQ(IncrementsGlobal(), IncrementsGlobal()); |
| 371 | EXPECT_EQ(global_state_for_multi_evaluate_test, 2); |
| 372 | } |
| 373 | TEST(AssertFail, BinaryOpSingleSideEffectingCall) { |
| 374 | global_state_for_multi_evaluate_test = 0; |
| 375 | PW_CHECK_INT_EQ(12314, IncrementsGlobal()); |
| 376 | EXPECT_EQ(global_state_for_multi_evaluate_test, 1); |
| 377 | } |
| 378 | TEST(AssertFail, BinaryOpTwoSideEffectingCalls) { |
| 379 | global_state_for_multi_evaluate_test = 0; |
| 380 | PW_CHECK_INT_EQ(IncrementsGlobal() + 10, IncrementsGlobal()); |
| 381 | EXPECT_EQ(global_state_for_multi_evaluate_test, 2); |
| 382 | } |
| 383 | |
Keir Mierle | b9b8816 | 2020-04-15 20:43:09 -0700 | [diff] [blame] | 384 | // Verify side effects of debug checks work as expected. |
| 385 | // 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] | 386 | #if PW_ASSERT_ENABLE_DEBUG |
Keir Mierle | b9b8816 | 2020-04-15 20:43:09 -0700 | [diff] [blame] | 387 | // When DCHECKs are enabled, they behave the same as normal checks. |
| 388 | TEST(AssertPass, DCheckEnabledSingleSideEffectingCall) { |
| 389 | global_state_for_multi_evaluate_test = 0; |
| 390 | PW_DCHECK(IncrementsGlobal() == 0); |
| 391 | EXPECT_EQ(global_state_for_multi_evaluate_test, 1); |
| 392 | } |
| 393 | TEST(AssertFail, DCheckEnabledSingleSideEffectingCall) { |
| 394 | global_state_for_multi_evaluate_test = 0; |
| 395 | PW_DCHECK(IncrementsGlobal() == 1); |
| 396 | EXPECT_EQ(global_state_for_multi_evaluate_test, 1); |
| 397 | } |
| 398 | TEST(AssertPass, DCheckEnabledBinaryOpSingleSideEffectingCall) { |
| 399 | global_state_for_multi_evaluate_test = 0; |
| 400 | PW_DCHECK_INT_EQ(0, IncrementsGlobal()); |
| 401 | EXPECT_EQ(global_state_for_multi_evaluate_test, 1); |
| 402 | } |
| 403 | TEST(AssertPass, DCheckEnabledBinaryOpTwoSideEffectingCalls) { |
| 404 | global_state_for_multi_evaluate_test = 0; |
| 405 | PW_DCHECK_INT_EQ(IncrementsGlobal(), IncrementsGlobal()); |
| 406 | EXPECT_EQ(global_state_for_multi_evaluate_test, 2); |
| 407 | } |
| 408 | TEST(AssertFail, DCheckEnabledBinaryOpSingleSideEffectingCall) { |
| 409 | global_state_for_multi_evaluate_test = 0; |
| 410 | PW_DCHECK_INT_EQ(12314, IncrementsGlobal()); |
| 411 | EXPECT_EQ(global_state_for_multi_evaluate_test, 1); |
| 412 | } |
| 413 | TEST(AssertFail, DCheckEnabledBinaryOpTwoSideEffectingCalls) { |
| 414 | global_state_for_multi_evaluate_test = 0; |
| 415 | PW_DCHECK_INT_EQ(IncrementsGlobal() + 10, IncrementsGlobal()); |
| 416 | EXPECT_EQ(global_state_for_multi_evaluate_test, 2); |
| 417 | } |
| 418 | |
Keir Mierle | 854adec | 2020-09-03 14:07:19 -0700 | [diff] [blame] | 419 | #else // PW_ASSERT_ENABLE_DEBUG |
Keir Mierle | b9b8816 | 2020-04-15 20:43:09 -0700 | [diff] [blame] | 420 | |
| 421 | // When DCHECKs are disabled, they should not trip, and their arguments |
| 422 | // shouldn't be evaluated. |
Wyatt Hepler | d78f7c6 | 2020-09-28 14:27:32 -0700 | [diff] [blame] | 423 | TEST(AssertPass, DCheckDisabledSingleSideEffectingCall_1) { |
Keir Mierle | b9b8816 | 2020-04-15 20:43:09 -0700 | [diff] [blame] | 424 | global_state_for_multi_evaluate_test = 0; |
| 425 | PW_DCHECK(IncrementsGlobal() == 0); |
| 426 | EXPECT_EQ(global_state_for_multi_evaluate_test, 0); |
| 427 | } |
Wyatt Hepler | d78f7c6 | 2020-09-28 14:27:32 -0700 | [diff] [blame] | 428 | TEST(AssertPass, DCheckDisabledSingleSideEffectingCall_2) { |
Keir Mierle | b9b8816 | 2020-04-15 20:43:09 -0700 | [diff] [blame] | 429 | global_state_for_multi_evaluate_test = 0; |
| 430 | PW_DCHECK(IncrementsGlobal() == 1); |
| 431 | EXPECT_EQ(global_state_for_multi_evaluate_test, 0); |
| 432 | } |
Wyatt Hepler | d78f7c6 | 2020-09-28 14:27:32 -0700 | [diff] [blame] | 433 | TEST(AssertPass, DCheckDisabledBinaryOpSingleSideEffectingCall_1) { |
Keir Mierle | b9b8816 | 2020-04-15 20:43:09 -0700 | [diff] [blame] | 434 | global_state_for_multi_evaluate_test = 0; |
| 435 | PW_DCHECK_INT_EQ(0, IncrementsGlobal()); |
| 436 | EXPECT_EQ(global_state_for_multi_evaluate_test, 0); |
| 437 | } |
Wyatt Hepler | d78f7c6 | 2020-09-28 14:27:32 -0700 | [diff] [blame] | 438 | TEST(AssertPass, DCheckDisabledBinaryOpTwoSideEffectingCalls_1) { |
Keir Mierle | b9b8816 | 2020-04-15 20:43:09 -0700 | [diff] [blame] | 439 | global_state_for_multi_evaluate_test = 0; |
| 440 | PW_DCHECK_INT_EQ(IncrementsGlobal(), IncrementsGlobal()); |
| 441 | EXPECT_EQ(global_state_for_multi_evaluate_test, 0); |
| 442 | } |
Wyatt Hepler | d78f7c6 | 2020-09-28 14:27:32 -0700 | [diff] [blame] | 443 | TEST(AssertPass, DCheckDisabledBinaryOpSingleSideEffectingCall_2) { |
Keir Mierle | b9b8816 | 2020-04-15 20:43:09 -0700 | [diff] [blame] | 444 | global_state_for_multi_evaluate_test = 0; |
| 445 | PW_DCHECK_INT_EQ(12314, IncrementsGlobal()); |
| 446 | EXPECT_EQ(global_state_for_multi_evaluate_test, 0); |
| 447 | } |
Wyatt Hepler | d78f7c6 | 2020-09-28 14:27:32 -0700 | [diff] [blame] | 448 | TEST(AssertPass, DCheckDisabledBinaryOpTwoSideEffectingCalls_2) { |
Keir Mierle | b9b8816 | 2020-04-15 20:43:09 -0700 | [diff] [blame] | 449 | global_state_for_multi_evaluate_test = 0; |
| 450 | PW_DCHECK_INT_EQ(IncrementsGlobal() + 10, IncrementsGlobal()); |
| 451 | EXPECT_EQ(global_state_for_multi_evaluate_test, 0); |
| 452 | } |
Keir Mierle | 854adec | 2020-09-03 14:07:19 -0700 | [diff] [blame] | 453 | #endif // PW_ASSERT_ENABLE_DEBUG |
Keir Mierle | b9b8816 | 2020-04-15 20:43:09 -0700 | [diff] [blame] | 454 | |
Keir Mierle | 0fa7f7d | 2020-05-07 12:34:00 -0700 | [diff] [blame] | 455 | // Verify PW_CHECK_OK, including message handling. |
| 456 | TEST_F(AssertFail, StatusNotOK) { |
Wyatt Hepler | d78f7c6 | 2020-09-28 14:27:32 -0700 | [diff] [blame] | 457 | pw::Status status = pw::Status::Unknown(); |
Keir Mierle | 0fa7f7d | 2020-05-07 12:34:00 -0700 | [diff] [blame] | 458 | PW_CHECK_OK(status); |
Wyatt Hepler | 1b3da3a | 2021-01-07 13:26:57 -0800 | [diff] [blame] | 459 | EXPECT_MESSAGE("Check failed: status (=UNKNOWN) == OkStatus() (=OK). "); |
Keir Mierle | 0fa7f7d | 2020-05-07 12:34:00 -0700 | [diff] [blame] | 460 | } |
| 461 | |
| 462 | TEST_F(AssertFail, StatusNotOKMessageNoArguments) { |
Wyatt Hepler | d78f7c6 | 2020-09-28 14:27:32 -0700 | [diff] [blame] | 463 | pw::Status status = pw::Status::Unknown(); |
Keir Mierle | 0fa7f7d | 2020-05-07 12:34:00 -0700 | [diff] [blame] | 464 | PW_CHECK_OK(status, "msg"); |
Wyatt Hepler | 1b3da3a | 2021-01-07 13:26:57 -0800 | [diff] [blame] | 465 | EXPECT_MESSAGE("Check failed: status (=UNKNOWN) == OkStatus() (=OK). msg"); |
Keir Mierle | 0fa7f7d | 2020-05-07 12:34:00 -0700 | [diff] [blame] | 466 | } |
| 467 | |
| 468 | TEST_F(AssertFail, StatusNotOKMessageArguments) { |
Wyatt Hepler | d78f7c6 | 2020-09-28 14:27:32 -0700 | [diff] [blame] | 469 | pw::Status status = pw::Status::Unknown(); |
Keir Mierle | 0fa7f7d | 2020-05-07 12:34:00 -0700 | [diff] [blame] | 470 | PW_CHECK_OK(status, "msg: %d", 5); |
Wyatt Hepler | 1b3da3a | 2021-01-07 13:26:57 -0800 | [diff] [blame] | 471 | EXPECT_MESSAGE("Check failed: status (=UNKNOWN) == OkStatus() (=OK). msg: 5"); |
Keir Mierle | 0fa7f7d | 2020-05-07 12:34:00 -0700 | [diff] [blame] | 472 | } |
| 473 | |
| 474 | // Example expression for the test below. |
Wyatt Hepler | d78f7c6 | 2020-09-28 14:27:32 -0700 | [diff] [blame] | 475 | pw::Status DoTheThing() { return pw::Status::ResourceExhausted(); } |
Keir Mierle | 0fa7f7d | 2020-05-07 12:34:00 -0700 | [diff] [blame] | 476 | |
| 477 | TEST_F(AssertFail, NonTrivialExpression) { |
| 478 | PW_CHECK_OK(DoTheThing()); |
| 479 | EXPECT_MESSAGE( |
Wyatt Hepler | 1b3da3a | 2021-01-07 13:26:57 -0800 | [diff] [blame] | 480 | "Check failed: DoTheThing() (=RESOURCE_EXHAUSTED) == OkStatus() (=OK). "); |
Keir Mierle | 0fa7f7d | 2020-05-07 12:34:00 -0700 | [diff] [blame] | 481 | } |
| 482 | |
| 483 | // Note: This function seems pointless but it is not, since pw::Status::FOO |
| 484 | // constants are not actually status objects, but code objects. This way we can |
| 485 | // ensure the macros work with both real status objects and literals. |
Wyatt Hepler | 1b3da3a | 2021-01-07 13:26:57 -0800 | [diff] [blame] | 486 | TEST_F(AssertPass, Function) { PW_CHECK_OK(pw::OkStatus()); } |
Keir Mierle | 0fa7f7d | 2020-05-07 12:34:00 -0700 | [diff] [blame] | 487 | TEST_F(AssertPass, Enum) { PW_CHECK_OK(PW_STATUS_OK); } |
Wyatt Hepler | d78f7c6 | 2020-09-28 14:27:32 -0700 | [diff] [blame] | 488 | TEST_F(AssertFail, Function) { PW_CHECK_OK(pw::Status::Unknown()); } |
Keir Mierle | 0fa7f7d | 2020-05-07 12:34:00 -0700 | [diff] [blame] | 489 | TEST_F(AssertFail, Enum) { PW_CHECK_OK(PW_STATUS_UNKNOWN); } |
| 490 | |
Keir Mierle | 854adec | 2020-09-03 14:07:19 -0700 | [diff] [blame] | 491 | #if PW_ASSERT_ENABLE_DEBUG |
Keir Mierle | 0fa7f7d | 2020-05-07 12:34:00 -0700 | [diff] [blame] | 492 | |
| 493 | // In debug mode, the asserts should check their arguments. |
Wyatt Hepler | 1b3da3a | 2021-01-07 13:26:57 -0800 | [diff] [blame] | 494 | TEST_F(AssertPass, DCheckFunction) { PW_DCHECK_OK(pw::OkStatus()); } |
Wyatt Hepler | d78f7c6 | 2020-09-28 14:27:32 -0700 | [diff] [blame] | 495 | TEST_F(AssertPass, DCheckEnum) { PW_DCHECK_OK(PW_STATUS_OK); } |
| 496 | TEST_F(AssertFail, DCheckFunction) { PW_DCHECK_OK(pw::Status::Unknown()); } |
| 497 | TEST_F(AssertFail, DCheckEnum) { PW_DCHECK_OK(PW_STATUS_UNKNOWN); } |
Keir Mierle | 854adec | 2020-09-03 14:07:19 -0700 | [diff] [blame] | 498 | #else // PW_ASSERT_ENABLE_DEBUG |
Keir Mierle | 0fa7f7d | 2020-05-07 12:34:00 -0700 | [diff] [blame] | 499 | |
| 500 | // In release mode, all the asserts should pass. |
Wyatt Hepler | 1b3da3a | 2021-01-07 13:26:57 -0800 | [diff] [blame] | 501 | TEST_F(AssertPass, DCheckFunction_Ok) { PW_DCHECK_OK(pw::OkStatus()); } |
Wyatt Hepler | d78f7c6 | 2020-09-28 14:27:32 -0700 | [diff] [blame] | 502 | TEST_F(AssertPass, DCheckEnum_Ok) { PW_DCHECK_OK(PW_STATUS_OK); } |
| 503 | TEST_F(AssertPass, DCheckFunction_Err) { PW_DCHECK_OK(pw::Status::Unknown()); } |
| 504 | TEST_F(AssertPass, DCheckEnum_Err) { PW_DCHECK_OK(PW_STATUS_UNKNOWN); } |
Keir Mierle | 854adec | 2020-09-03 14:07:19 -0700 | [diff] [blame] | 505 | #endif // PW_ASSERT_ENABLE_DEBUG |
Keir Mierle | 0fa7f7d | 2020-05-07 12:34:00 -0700 | [diff] [blame] | 506 | |
Keir Mierle | 8d2a84f | 2020-04-14 22:00:00 -0700 | [diff] [blame] | 507 | // TODO: Figure out how to run some of these tests is C. |
| 508 | |
| 509 | } // namespace |