Warning, /include/absl/log/check_test_impl.inc is written in an unsupported language. File is not indexed.
0001 //
0002 // Copyright 2022 The Abseil Authors.
0003 //
0004 // Licensed under the Apache License, Version 2.0 (the "License");
0005 // you may not use this file except in compliance with the License.
0006 // You may obtain a copy of the License at
0007 //
0008 // https://www.apache.org/licenses/LICENSE-2.0
0009 //
0010 // Unless required by applicable law or agreed to in writing, software
0011 // distributed under the License is distributed on an "AS IS" BASIS,
0012 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
0013 // See the License for the specific language governing permissions and
0014 // limitations under the License.
0015
0016 #ifndef ABSL_LOG_CHECK_TEST_IMPL_H_
0017 #define ABSL_LOG_CHECK_TEST_IMPL_H_
0018
0019 // Verify that both sets of macros behave identically by parameterizing the
0020 // entire test file.
0021 #ifndef ABSL_TEST_CHECK
0022 #error ABSL_TEST_CHECK must be defined for these tests to work.
0023 #endif
0024
0025 #include <ostream>
0026 #include <string>
0027
0028 #include "gmock/gmock.h"
0029 #include "gtest/gtest.h"
0030 #include "absl/base/attributes.h"
0031 #include "absl/base/config.h"
0032 #include "absl/log/internal/test_helpers.h"
0033 #include "absl/status/status.h"
0034 #include "absl/strings/string_view.h"
0035 #include "absl/strings/substitute.h"
0036
0037 // NOLINTBEGIN(misc-definitions-in-headers)
0038
0039 namespace absl_log_internal {
0040
0041 using ::testing::AllOf;
0042 using ::testing::HasSubstr;
0043 using ::testing::Not;
0044
0045 auto* test_env ABSL_ATTRIBUTE_UNUSED = ::testing::AddGlobalTestEnvironment(
0046 new absl::log_internal::LogTestEnvironment);
0047
0048 #if GTEST_HAS_DEATH_TEST
0049
0050 TEST(CHECKDeathTest, TestBasicValues) {
0051 ABSL_TEST_CHECK(true);
0052
0053 EXPECT_DEATH(ABSL_TEST_CHECK(false), "Check failed: false");
0054
0055 int i = 2;
0056 ABSL_TEST_CHECK(i != 3); // NOLINT
0057 }
0058
0059 #endif // GTEST_HAS_DEATH_TEST
0060
0061 TEST(CHECKTest, TestLogicExpressions) {
0062 int i = 5;
0063 ABSL_TEST_CHECK(i > 0 && i < 10);
0064 ABSL_TEST_CHECK(i < 0 || i > 3);
0065 }
0066
0067 #if ABSL_INTERNAL_CPLUSPLUS_LANG >= 201703L
0068 ABSL_CONST_INIT const auto global_var_check = [](int i) {
0069 ABSL_TEST_CHECK(i > 0); // NOLINT
0070 return i + 1;
0071 }(3);
0072
0073 ABSL_CONST_INIT const auto global_var = [](int i) {
0074 ABSL_TEST_CHECK_GE(i, 0); // NOLINT
0075 return i + 1;
0076 }(global_var_check);
0077 #endif // ABSL_INTERNAL_CPLUSPLUS_LANG
0078
0079 TEST(CHECKTest, TestPlacementsInCompoundStatements) {
0080 // check placement inside if/else clauses
0081 if (true) ABSL_TEST_CHECK(true);
0082
0083 if (false)
0084 ; // NOLINT
0085 else
0086 ABSL_TEST_CHECK(true);
0087
0088 switch (0)
0089 case 0:
0090 ABSL_TEST_CHECK(true); // NOLINT
0091
0092 #if ABSL_INTERNAL_CPLUSPLUS_LANG >= 201703L
0093 constexpr auto var = [](int i) {
0094 ABSL_TEST_CHECK(i > 0); // NOLINT
0095 return i + 1;
0096 }(global_var);
0097 (void)var;
0098 #endif // ABSL_INTERNAL_CPLUSPLUS_LANG
0099 }
0100
0101 TEST(CHECKTest, TestBoolConvertible) {
0102 struct Tester {
0103 } tester;
0104 ABSL_TEST_CHECK([&]() { return &tester; }());
0105 }
0106
0107 #if GTEST_HAS_DEATH_TEST
0108
0109 TEST(CHECKDeathTest, TestChecksWithSideEffects) {
0110 int var = 0;
0111 ABSL_TEST_CHECK([&var]() {
0112 ++var;
0113 return true;
0114 }());
0115 EXPECT_EQ(var, 1);
0116
0117 EXPECT_DEATH(ABSL_TEST_CHECK([&var]() {
0118 ++var;
0119 return false;
0120 }()) << var,
0121 "Check failed: .* 2");
0122 }
0123
0124 #endif // GTEST_HAS_DEATH_TEST
0125
0126 template <int a, int b>
0127 constexpr int sum() {
0128 return a + b;
0129 }
0130 #define MACRO_ONE 1
0131 #define TEMPLATE_SUM(a, b) sum<a, b>()
0132 #define CONCAT(a, b) a b
0133 #define IDENTITY(x) x
0134
0135 TEST(CHECKTest, TestPassingMacroExpansion) {
0136 ABSL_TEST_CHECK(IDENTITY(true));
0137 ABSL_TEST_CHECK_EQ(TEMPLATE_SUM(MACRO_ONE, 2), 3);
0138 ABSL_TEST_CHECK_STREQ(CONCAT("x", "y"), "xy");
0139 }
0140
0141 #if GTEST_HAS_DEATH_TEST
0142
0143 TEST(CHECKTest, TestMacroExpansionInMessage) {
0144 auto MessageGen = []() { ABSL_TEST_CHECK(IDENTITY(false)); };
0145 EXPECT_DEATH(MessageGen(), HasSubstr("IDENTITY(false)"));
0146 }
0147
0148 TEST(CHECKTest, TestNestedMacroExpansionInMessage) {
0149 EXPECT_DEATH(ABSL_TEST_CHECK(IDENTITY(false)), HasSubstr("IDENTITY(false)"));
0150 }
0151
0152 TEST(CHECKTest, TestMacroExpansionCompare) {
0153 EXPECT_DEATH(ABSL_TEST_CHECK_EQ(IDENTITY(false), IDENTITY(true)),
0154 HasSubstr("IDENTITY(false) == IDENTITY(true)"));
0155 EXPECT_DEATH(ABSL_TEST_CHECK_GT(IDENTITY(1), IDENTITY(2)),
0156 HasSubstr("IDENTITY(1) > IDENTITY(2)"));
0157 }
0158
0159 TEST(CHECKTest, TestMacroExpansionStrCompare) {
0160 EXPECT_DEATH(ABSL_TEST_CHECK_STREQ(IDENTITY("x"), IDENTITY("y")),
0161 HasSubstr("IDENTITY(\"x\") == IDENTITY(\"y\")"));
0162 EXPECT_DEATH(ABSL_TEST_CHECK_STRCASENE(IDENTITY("a"), IDENTITY("A")),
0163 HasSubstr("IDENTITY(\"a\") != IDENTITY(\"A\")"));
0164 }
0165
0166 TEST(CHECKTest, TestMacroExpansionStatus) {
0167 EXPECT_DEATH(
0168 ABSL_TEST_CHECK_OK(IDENTITY(absl::FailedPreconditionError("message"))),
0169 HasSubstr("IDENTITY(absl::FailedPreconditionError(\"message\"))"));
0170 }
0171
0172 TEST(CHECKTest, TestMacroExpansionComma) {
0173 EXPECT_DEATH(ABSL_TEST_CHECK(TEMPLATE_SUM(MACRO_ONE, 2) == 4),
0174 HasSubstr("TEMPLATE_SUM(MACRO_ONE, 2) == 4"));
0175 }
0176
0177 TEST(CHECKTest, TestMacroExpansionCommaCompare) {
0178 EXPECT_DEATH(
0179 ABSL_TEST_CHECK_EQ(TEMPLATE_SUM(2, MACRO_ONE), TEMPLATE_SUM(3, 2)),
0180 HasSubstr("TEMPLATE_SUM(2, MACRO_ONE) == TEMPLATE_SUM(3, 2)"));
0181 EXPECT_DEATH(
0182 ABSL_TEST_CHECK_GT(TEMPLATE_SUM(2, MACRO_ONE), TEMPLATE_SUM(3, 2)),
0183 HasSubstr("TEMPLATE_SUM(2, MACRO_ONE) > TEMPLATE_SUM(3, 2)"));
0184 }
0185
0186 TEST(CHECKTest, TestMacroExpansionCommaStrCompare) {
0187 EXPECT_DEATH(ABSL_TEST_CHECK_STREQ(CONCAT("x", "y"), "z"),
0188 HasSubstr("CONCAT(\"x\", \"y\") == \"z\""));
0189 EXPECT_DEATH(ABSL_TEST_CHECK_STRNE(CONCAT("x", "y"), "xy"),
0190 HasSubstr("CONCAT(\"x\", \"y\") != \"xy\""));
0191 }
0192
0193 #endif // GTEST_HAS_DEATH_TEST
0194
0195 #undef TEMPLATE_SUM
0196 #undef CONCAT
0197 #undef MACRO
0198 #undef ONE
0199
0200 #if GTEST_HAS_DEATH_TEST
0201
0202 TEST(CHECKDeachTest, TestOrderOfInvocationsBetweenCheckAndMessage) {
0203 int counter = 0;
0204
0205 auto GetStr = [&counter]() -> std::string {
0206 return counter++ == 0 ? "" : "non-empty";
0207 };
0208
0209 EXPECT_DEATH(ABSL_TEST_CHECK(!GetStr().empty()) << GetStr(),
0210 HasSubstr("non-empty"));
0211 }
0212
0213 TEST(CHECKTest, TestSecondaryFailure) {
0214 auto FailingRoutine = []() {
0215 ABSL_TEST_CHECK(false) << "Secondary";
0216 return false;
0217 };
0218 EXPECT_DEATH(ABSL_TEST_CHECK(FailingRoutine()) << "Primary",
0219 AllOf(HasSubstr("Secondary"), Not(HasSubstr("Primary"))));
0220 }
0221
0222 TEST(CHECKTest, TestSecondaryFailureInMessage) {
0223 auto MessageGen = []() {
0224 ABSL_TEST_CHECK(false) << "Secondary";
0225 return "Primary";
0226 };
0227 EXPECT_DEATH(ABSL_TEST_CHECK(false) << MessageGen(),
0228 AllOf(HasSubstr("Secondary"), Not(HasSubstr("Primary"))));
0229 }
0230
0231 #endif // GTEST_HAS_DEATH_TEST
0232
0233 TEST(CHECKTest, TestBinaryChecksWithPrimitives) {
0234 ABSL_TEST_CHECK_EQ(1, 1);
0235 ABSL_TEST_CHECK_NE(1, 2);
0236 ABSL_TEST_CHECK_GE(1, 1);
0237 ABSL_TEST_CHECK_GE(2, 1);
0238 ABSL_TEST_CHECK_LE(1, 1);
0239 ABSL_TEST_CHECK_LE(1, 2);
0240 ABSL_TEST_CHECK_GT(2, 1);
0241 ABSL_TEST_CHECK_LT(1, 2);
0242 }
0243
0244 // For testing using CHECK*() on anonymous enums.
0245 enum { CASE_A, CASE_B };
0246
0247 TEST(CHECKTest, TestBinaryChecksWithEnumValues) {
0248 // Tests using CHECK*() on anonymous enums.
0249 ABSL_TEST_CHECK_EQ(CASE_A, CASE_A);
0250 ABSL_TEST_CHECK_NE(CASE_A, CASE_B);
0251 ABSL_TEST_CHECK_GE(CASE_A, CASE_A);
0252 ABSL_TEST_CHECK_GE(CASE_B, CASE_A);
0253 ABSL_TEST_CHECK_LE(CASE_A, CASE_A);
0254 ABSL_TEST_CHECK_LE(CASE_A, CASE_B);
0255 ABSL_TEST_CHECK_GT(CASE_B, CASE_A);
0256 ABSL_TEST_CHECK_LT(CASE_A, CASE_B);
0257 }
0258
0259 TEST(CHECKTest, TestBinaryChecksWithNullptr) {
0260 const void* p_null = nullptr;
0261 const void* p_not_null = &p_null;
0262 ABSL_TEST_CHECK_EQ(p_null, nullptr);
0263 ABSL_TEST_CHECK_EQ(nullptr, p_null);
0264 ABSL_TEST_CHECK_NE(p_not_null, nullptr);
0265 ABSL_TEST_CHECK_NE(nullptr, p_not_null);
0266 }
0267
0268 #if GTEST_HAS_DEATH_TEST
0269
0270 // Test logging of various char-typed values by failing CHECK*().
0271 TEST(CHECKDeathTest, TestComparingCharsValues) {
0272 {
0273 char a = ';';
0274 char b = 'b';
0275 EXPECT_DEATH(ABSL_TEST_CHECK_EQ(a, b),
0276 "Check failed: a == b \\(';' vs. 'b'\\)");
0277 b = 1;
0278 EXPECT_DEATH(ABSL_TEST_CHECK_EQ(a, b),
0279 "Check failed: a == b \\(';' vs. char value 1\\)");
0280 }
0281 {
0282 signed char a = ';';
0283 signed char b = 'b';
0284 EXPECT_DEATH(ABSL_TEST_CHECK_EQ(a, b),
0285 "Check failed: a == b \\(';' vs. 'b'\\)");
0286 b = -128;
0287 EXPECT_DEATH(ABSL_TEST_CHECK_EQ(a, b),
0288 "Check failed: a == b \\(';' vs. signed char value -128\\)");
0289 }
0290 {
0291 unsigned char a = ';';
0292 unsigned char b = 'b';
0293 EXPECT_DEATH(ABSL_TEST_CHECK_EQ(a, b),
0294 "Check failed: a == b \\(';' vs. 'b'\\)");
0295 b = 128;
0296 EXPECT_DEATH(ABSL_TEST_CHECK_EQ(a, b),
0297 "Check failed: a == b \\(';' vs. unsigned char value 128\\)");
0298 }
0299 }
0300
0301 TEST(CHECKDeathTest, TestNullValuesAreReportedCleanly) {
0302 const char* a = nullptr;
0303 const char* b = nullptr;
0304 EXPECT_DEATH(ABSL_TEST_CHECK_NE(a, b),
0305 "Check failed: a != b \\(\\(null\\) vs. \\(null\\)\\)");
0306
0307 a = "xx";
0308 EXPECT_DEATH(ABSL_TEST_CHECK_EQ(a, b),
0309 "Check failed: a == b \\(xx vs. \\(null\\)\\)");
0310 EXPECT_DEATH(ABSL_TEST_CHECK_EQ(b, a),
0311 "Check failed: b == a \\(\\(null\\) vs. xx\\)");
0312
0313 std::nullptr_t n{};
0314 EXPECT_DEATH(ABSL_TEST_CHECK_NE(n, nullptr),
0315 "Check failed: n != nullptr \\(\\(null\\) vs. \\(null\\)\\)");
0316 }
0317
0318 #endif // GTEST_HAS_DEATH_TEST
0319
0320 TEST(CHECKTest, TestSTREQ) {
0321 ABSL_TEST_CHECK_STREQ("this", "this");
0322 ABSL_TEST_CHECK_STREQ(nullptr, nullptr);
0323 ABSL_TEST_CHECK_STRCASEEQ("this", "tHiS");
0324 ABSL_TEST_CHECK_STRCASEEQ(nullptr, nullptr);
0325 ABSL_TEST_CHECK_STRNE("this", "tHiS");
0326 ABSL_TEST_CHECK_STRNE("this", nullptr);
0327 ABSL_TEST_CHECK_STRCASENE("this", "that");
0328 ABSL_TEST_CHECK_STRCASENE(nullptr, "that");
0329 ABSL_TEST_CHECK_STREQ((std::string("a") + "b").c_str(), "ab");
0330 ABSL_TEST_CHECK_STREQ(std::string("test").c_str(),
0331 (std::string("te") + std::string("st")).c_str());
0332 }
0333
0334 TEST(CHECKTest, TestComparisonPlacementsInCompoundStatements) {
0335 // check placement inside if/else clauses
0336 if (true) ABSL_TEST_CHECK_EQ(1, 1);
0337 if (true) ABSL_TEST_CHECK_STREQ("c", "c");
0338
0339 if (false)
0340 ; // NOLINT
0341 else
0342 ABSL_TEST_CHECK_LE(0, 1);
0343
0344 if (false)
0345 ; // NOLINT
0346 else
0347 ABSL_TEST_CHECK_STRNE("a", "b");
0348
0349 switch (0)
0350 case 0:
0351 ABSL_TEST_CHECK_NE(1, 0);
0352
0353 switch (0)
0354 case 0:
0355 ABSL_TEST_CHECK_STRCASEEQ("A", "a");
0356
0357 #if ABSL_INTERNAL_CPLUSPLUS_LANG >= 201703L
0358 constexpr auto var = [](int i) {
0359 ABSL_TEST_CHECK_GT(i, 0);
0360 return i + 1;
0361 }(global_var);
0362 (void)var;
0363
0364 // CHECK_STR... checks are not supported in constexpr routines.
0365 // constexpr auto var2 = [](int i) {
0366 // ABSL_TEST_CHECK_STRNE("c", "d");
0367 // return i + 1;
0368 // }(global_var);
0369
0370 #if defined(__GNUC__)
0371 int var3 = (({ ABSL_TEST_CHECK_LE(1, 2); }), global_var < 10) ? 1 : 0;
0372 (void)var3;
0373
0374 int var4 = (({ ABSL_TEST_CHECK_STREQ("a", "a"); }), global_var < 10) ? 1 : 0;
0375 (void)var4;
0376 #endif // __GNUC__
0377 #endif // ABSL_INTERNAL_CPLUSPLUS_LANG
0378 }
0379
0380 TEST(CHECKTest, TestDCHECK) {
0381 #ifdef NDEBUG
0382 ABSL_TEST_DCHECK(1 == 2) << " DCHECK's shouldn't be compiled in normal mode";
0383 #endif
0384 ABSL_TEST_DCHECK(1 == 1); // NOLINT(readability/check)
0385 ABSL_TEST_DCHECK_EQ(1, 1);
0386 ABSL_TEST_DCHECK_NE(1, 2);
0387 ABSL_TEST_DCHECK_GE(1, 1);
0388 ABSL_TEST_DCHECK_GE(2, 1);
0389 ABSL_TEST_DCHECK_LE(1, 1);
0390 ABSL_TEST_DCHECK_LE(1, 2);
0391 ABSL_TEST_DCHECK_GT(2, 1);
0392 ABSL_TEST_DCHECK_LT(1, 2);
0393
0394 // Test DCHECK on std::nullptr_t
0395 const void* p_null = nullptr;
0396 const void* p_not_null = &p_null;
0397 ABSL_TEST_DCHECK_EQ(p_null, nullptr);
0398 ABSL_TEST_DCHECK_EQ(nullptr, p_null);
0399 ABSL_TEST_DCHECK_NE(p_not_null, nullptr);
0400 ABSL_TEST_DCHECK_NE(nullptr, p_not_null);
0401 }
0402
0403 TEST(CHECKTest, TestQCHECK) {
0404 // The tests that QCHECK does the same as CHECK
0405 ABSL_TEST_QCHECK(1 == 1); // NOLINT(readability/check)
0406 ABSL_TEST_QCHECK_EQ(1, 1);
0407 ABSL_TEST_QCHECK_NE(1, 2);
0408 ABSL_TEST_QCHECK_GE(1, 1);
0409 ABSL_TEST_QCHECK_GE(2, 1);
0410 ABSL_TEST_QCHECK_LE(1, 1);
0411 ABSL_TEST_QCHECK_LE(1, 2);
0412 ABSL_TEST_QCHECK_GT(2, 1);
0413 ABSL_TEST_QCHECK_LT(1, 2);
0414
0415 // Tests using QCHECK*() on anonymous enums.
0416 ABSL_TEST_QCHECK_EQ(CASE_A, CASE_A);
0417 ABSL_TEST_QCHECK_NE(CASE_A, CASE_B);
0418 ABSL_TEST_QCHECK_GE(CASE_A, CASE_A);
0419 ABSL_TEST_QCHECK_GE(CASE_B, CASE_A);
0420 ABSL_TEST_QCHECK_LE(CASE_A, CASE_A);
0421 ABSL_TEST_QCHECK_LE(CASE_A, CASE_B);
0422 ABSL_TEST_QCHECK_GT(CASE_B, CASE_A);
0423 ABSL_TEST_QCHECK_LT(CASE_A, CASE_B);
0424 }
0425
0426 TEST(CHECKTest, TestQCHECKPlacementsInCompoundStatements) {
0427 // check placement inside if/else clauses
0428 if (true) ABSL_TEST_QCHECK(true);
0429
0430 if (false)
0431 ; // NOLINT
0432 else
0433 ABSL_TEST_QCHECK(true);
0434
0435 if (false)
0436 ; // NOLINT
0437 else
0438 ABSL_TEST_QCHECK(true);
0439
0440 switch (0)
0441 case 0:
0442 ABSL_TEST_QCHECK(true);
0443
0444 #if ABSL_INTERNAL_CPLUSPLUS_LANG >= 201703L
0445 constexpr auto var = [](int i) {
0446 ABSL_TEST_QCHECK(i > 0); // NOLINT
0447 return i + 1;
0448 }(global_var);
0449 (void)var;
0450
0451 #if defined(__GNUC__)
0452 int var2 = (({ ABSL_TEST_CHECK_LE(1, 2); }), global_var < 10) ? 1 : 0;
0453 (void)var2;
0454 #endif // __GNUC__
0455 #endif // ABSL_INTERNAL_CPLUSPLUS_LANG
0456 }
0457
0458 class ComparableType {
0459 public:
0460 explicit ComparableType(int v) : v_(v) {}
0461
0462 void MethodWithCheck(int i) {
0463 ABSL_TEST_CHECK_EQ(*this, i);
0464 ABSL_TEST_CHECK_EQ(i, *this);
0465 }
0466
0467 int Get() const { return v_; }
0468
0469 private:
0470 friend bool operator==(const ComparableType& lhs, const ComparableType& rhs) {
0471 return lhs.v_ == rhs.v_;
0472 }
0473 friend bool operator!=(const ComparableType& lhs, const ComparableType& rhs) {
0474 return lhs.v_ != rhs.v_;
0475 }
0476 friend bool operator<(const ComparableType& lhs, const ComparableType& rhs) {
0477 return lhs.v_ < rhs.v_;
0478 }
0479 friend bool operator<=(const ComparableType& lhs, const ComparableType& rhs) {
0480 return lhs.v_ <= rhs.v_;
0481 }
0482 friend bool operator>(const ComparableType& lhs, const ComparableType& rhs) {
0483 return lhs.v_ > rhs.v_;
0484 }
0485 friend bool operator>=(const ComparableType& lhs, const ComparableType& rhs) {
0486 return lhs.v_ >= rhs.v_;
0487 }
0488 friend bool operator==(const ComparableType& lhs, int rhs) {
0489 return lhs.v_ == rhs;
0490 }
0491 friend bool operator==(int lhs, const ComparableType& rhs) {
0492 return lhs == rhs.v_;
0493 }
0494
0495 friend std::ostream& operator<<(std::ostream& out, const ComparableType& v) {
0496 return out << "ComparableType{" << v.Get() << "}";
0497 }
0498
0499 int v_;
0500 };
0501
0502 TEST(CHECKTest, TestUserDefinedCompOp) {
0503 ABSL_TEST_CHECK_EQ(ComparableType{0}, ComparableType{0});
0504 ABSL_TEST_CHECK_NE(ComparableType{1}, ComparableType{2});
0505 ABSL_TEST_CHECK_LT(ComparableType{1}, ComparableType{2});
0506 ABSL_TEST_CHECK_LE(ComparableType{1}, ComparableType{2});
0507 ABSL_TEST_CHECK_GT(ComparableType{2}, ComparableType{1});
0508 ABSL_TEST_CHECK_GE(ComparableType{2}, ComparableType{2});
0509 }
0510
0511 TEST(CHECKTest, TestCheckInMethod) {
0512 ComparableType v{1};
0513 v.MethodWithCheck(1);
0514 }
0515
0516 TEST(CHECKDeathTest, TestUserDefinedStreaming) {
0517 ComparableType v1{1};
0518 ComparableType v2{2};
0519
0520 EXPECT_DEATH(
0521 ABSL_TEST_CHECK_EQ(v1, v2),
0522 HasSubstr(
0523 "Check failed: v1 == v2 (ComparableType{1} vs. ComparableType{2})"));
0524 }
0525
0526 // A type that can be printed using AbslStringify.
0527 struct StringifiableType {
0528 int x = 0;
0529 explicit StringifiableType(int x) : x(x) {}
0530 friend bool operator==(const StringifiableType& lhs,
0531 const StringifiableType& rhs) {
0532 return lhs.x == rhs.x;
0533 }
0534 friend bool operator!=(const StringifiableType& lhs,
0535 const StringifiableType& rhs) {
0536 return lhs.x != rhs.x;
0537 }
0538 friend bool operator<(const StringifiableType& lhs,
0539 const StringifiableType& rhs) {
0540 return lhs.x < rhs.x;
0541 }
0542 friend bool operator>(const StringifiableType& lhs,
0543 const StringifiableType& rhs) {
0544 return lhs.x > rhs.x;
0545 }
0546 friend bool operator<=(const StringifiableType& lhs,
0547 const StringifiableType& rhs) {
0548 return lhs.x <= rhs.x;
0549 }
0550 friend bool operator>=(const StringifiableType& lhs,
0551 const StringifiableType& rhs) {
0552 return lhs.x >= rhs.x;
0553 }
0554 template <typename Sink>
0555 friend void AbslStringify(Sink& sink, const StringifiableType& obj) {
0556 absl::Format(&sink, "StringifiableType{%d}", obj.x);
0557 }
0558
0559 // Make sure no unintended copy happens.
0560 StringifiableType(const StringifiableType&) = delete;
0561 };
0562
0563 TEST(CHECKTest, TestUserDefinedAbslStringify) {
0564 const StringifiableType v1(1);
0565 const StringifiableType v2(2);
0566
0567 ABSL_TEST_CHECK_EQ(v1, v1);
0568 ABSL_TEST_CHECK_NE(v1, v2);
0569 ABSL_TEST_CHECK_LT(v1, v2);
0570 ABSL_TEST_CHECK_LE(v1, v2);
0571 ABSL_TEST_CHECK_GT(v2, v1);
0572 ABSL_TEST_CHECK_GE(v2, v1);
0573 }
0574
0575 TEST(CHECKDeathTest, TestUserDefinedAbslStringify) {
0576 const StringifiableType v1(1);
0577 const StringifiableType v2(2);
0578
0579 // Returns a matcher for the expected check failure message when comparing two
0580 // values.
0581 auto expected_output = [](int lhs, absl::string_view condition, int rhs) {
0582 return HasSubstr(
0583 absl::Substitute("Check failed: v$0 $1 v$2 (StringifiableType{$0} vs. "
0584 "StringifiableType{$2})",
0585 lhs, condition, rhs));
0586 };
0587 // Test comparisons where the check fails.
0588 EXPECT_DEATH(ABSL_TEST_CHECK_EQ(v1, v2), expected_output(1, "==", 2));
0589 EXPECT_DEATH(ABSL_TEST_CHECK_NE(v1, v1), expected_output(1, "!=", 1));
0590 EXPECT_DEATH(ABSL_TEST_CHECK_LT(v2, v1), expected_output(2, "<", 1));
0591 EXPECT_DEATH(ABSL_TEST_CHECK_LE(v2, v1), expected_output(2, "<=", 1));
0592 EXPECT_DEATH(ABSL_TEST_CHECK_GT(v1, v2), expected_output(1, ">", 2));
0593 EXPECT_DEATH(ABSL_TEST_CHECK_GE(v1, v2), expected_output(1, ">=", 2));
0594 }
0595
0596 // A type that can be printed using both AbslStringify and operator<<.
0597 struct StringifiableStreamableType {
0598 int x = 0;
0599 explicit StringifiableStreamableType(int x) : x(x) {}
0600
0601 friend bool operator==(const StringifiableStreamableType& lhs,
0602 const StringifiableStreamableType& rhs) {
0603 return lhs.x == rhs.x;
0604 }
0605 friend bool operator!=(const StringifiableStreamableType& lhs,
0606 const StringifiableStreamableType& rhs) {
0607 return lhs.x != rhs.x;
0608 }
0609 template <typename Sink>
0610 friend void AbslStringify(Sink& sink,
0611 const StringifiableStreamableType& obj) {
0612 absl::Format(&sink, "Strigified{%d}", obj.x);
0613 }
0614 friend std::ostream& operator<<(std::ostream& out,
0615 const StringifiableStreamableType& obj) {
0616 return out << "Streamed{" << obj.x << "}";
0617 }
0618
0619 // Avoid unintentional copy.
0620 StringifiableStreamableType(const StringifiableStreamableType&) = delete;
0621 };
0622
0623 TEST(CHECKDeathTest, TestStreamingPreferredOverAbslStringify) {
0624 StringifiableStreamableType v1(1);
0625 StringifiableStreamableType v2(2);
0626
0627 EXPECT_DEATH(
0628 ABSL_TEST_CHECK_EQ(v1, v2),
0629 HasSubstr("Check failed: v1 == v2 (Streamed{1} vs. Streamed{2})"));
0630 }
0631
0632 // A type whose pointer can be passed to AbslStringify.
0633 struct PointerIsStringifiable {};
0634 template <typename Sink>
0635 void AbslStringify(Sink& sink, const PointerIsStringifiable* var) {
0636 sink.Append("PointerIsStringifiable");
0637 }
0638
0639 // Verifies that a pointer is printed as a number despite having AbslStringify
0640 // defined. Users may implement AbslStringify that dereferences the pointer, and
0641 // doing so as part of DCHECK would not be good.
0642 TEST(CHECKDeathTest, TestPointerPrintedAsNumberDespiteAbslStringify) {
0643 const auto* p = reinterpret_cast<const PointerIsStringifiable*>(0x1234);
0644
0645 #ifdef _MSC_VER
0646 EXPECT_DEATH(
0647 ABSL_TEST_CHECK_EQ(p, nullptr),
0648 HasSubstr("Check failed: p == nullptr (0000000000001234 vs. (null))"));
0649 #else // _MSC_VER
0650 EXPECT_DEATH(ABSL_TEST_CHECK_EQ(p, nullptr),
0651 HasSubstr("Check failed: p == nullptr (0x1234 vs. (null))"));
0652 #endif // _MSC_VER
0653 }
0654
0655 // An uncopyable object with operator<<.
0656 struct Uncopyable {
0657 int x;
0658 explicit Uncopyable(int x) : x(x) {}
0659 Uncopyable(const Uncopyable&) = delete;
0660 friend bool operator==(const Uncopyable& lhs, const Uncopyable& rhs) {
0661 return lhs.x == rhs.x;
0662 }
0663 friend bool operator!=(const Uncopyable& lhs, const Uncopyable& rhs) {
0664 return lhs.x != rhs.x;
0665 }
0666 friend std::ostream& operator<<(std::ostream& os, const Uncopyable& obj) {
0667 return os << "Uncopyable{" << obj.x << "}";
0668 }
0669 };
0670
0671 // Test that an uncopyable object can be used.
0672 // Will catch us if implementation has an unintended copy.
0673 TEST(CHECKDeathTest, TestUncopyable) {
0674 const Uncopyable v1(1);
0675 const Uncopyable v2(2);
0676
0677 EXPECT_DEATH(
0678 ABSL_TEST_CHECK_EQ(v1, v2),
0679 HasSubstr("Check failed: v1 == v2 (Uncopyable{1} vs. Uncopyable{2})"));
0680 }
0681
0682 } // namespace absl_log_internal
0683
0684 // NOLINTEND(misc-definitions-in-headers)
0685
0686 #endif // ABSL_LOG_CHECK_TEST_IMPL_H_