diff options
author | akalin@chromium.org <akalin@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-11-01 19:49:22 +0000 |
---|---|---|
committer | akalin@chromium.org <akalin@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-11-01 19:49:22 +0000 |
commit | 5e9878091b352f454ea2692ca3e95e8ad7cdaee0 (patch) | |
tree | 38cebaf5d497fc446d19ee952be9c41cdf60f690 /base/logging_unittest.cc | |
parent | 98fd4c02c025721cc575239f725aaa16040ed7ac (diff) | |
download | chromium_src-5e9878091b352f454ea2692ca3e95e8ad7cdaee0.zip chromium_src-5e9878091b352f454ea2692ca3e95e8ad7cdaee0.tar.gz chromium_src-5e9878091b352f454ea2692ca3e95e8ad7cdaee0.tar.bz2 |
Fixed subtle difference in behavior between DCHECK and DCHECK_EQ et al.
BUG=58998
TEST=New unit tests
Review URL: http://codereview.chromium.org/4199005
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@64650 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'base/logging_unittest.cc')
-rw-r--r-- | base/logging_unittest.cc | 49 |
1 files changed, 44 insertions, 5 deletions
diff --git a/base/logging_unittest.cc b/base/logging_unittest.cc index fa28ec6..e03c45e 100644 --- a/base/logging_unittest.cc +++ b/base/logging_unittest.cc @@ -14,23 +14,35 @@ namespace { using ::testing::Return; +// Needs to be global since log assert handlers can't maintain state. +int log_sink_call_count = 0; + +void LogSink(const std::string& str) { + ++log_sink_call_count; +} + // Class to make sure any manipulations we do to the min log level are // contained (i.e., do not affect other unit tests). -class MinLogLevelSaver { +class LogStateSaver { public: - MinLogLevelSaver() : old_min_log_level_(GetMinLogLevel()) {} + LogStateSaver() : old_min_log_level_(GetMinLogLevel()) {} - ~MinLogLevelSaver() { SetMinLogLevel(old_min_log_level_); } + ~LogStateSaver() { + SetMinLogLevel(old_min_log_level_); + SetLogAssertHandler(NULL); + SetLogReportHandler(NULL); + log_sink_call_count = 0; + } private: int old_min_log_level_; - DISALLOW_COPY_AND_ASSIGN(MinLogLevelSaver); + DISALLOW_COPY_AND_ASSIGN(LogStateSaver); }; class LoggingTest : public testing::Test { private: - MinLogLevelSaver min_log_level_saver_; + LogStateSaver log_state_saver_; }; class MockLogSource { @@ -140,6 +152,33 @@ TEST_F(LoggingTest, DchecksAreLazy) { << mock_log_source.Log(); } +TEST_F(LoggingTest, Dcheck) { +#if defined(LOGGING_IS_OFFICIAL_BUILD) + // Official build. + EXPECT_FALSE(DCHECK_IS_ON()); + EXPECT_FALSE(DLOG_IS_ON(DCHECK)); +#elif defined(NDEBUG) + // Unofficial release build. + logging::g_enable_dcheck = true; + logging::SetLogReportHandler(&LogSink); + EXPECT_TRUE(DCHECK_IS_ON()); + EXPECT_FALSE(DLOG_IS_ON(DCHECK)); +#else + // Unofficial debug build. + logging::SetLogAssertHandler(&LogSink); + EXPECT_TRUE(DCHECK_IS_ON()); + EXPECT_TRUE(DLOG_IS_ON(DCHECK)); +#endif // defined(LOGGING_IS_OFFICIAL_BUILD) + + EXPECT_EQ(0, log_sink_call_count); + DCHECK(false); + EXPECT_EQ(DCHECK_IS_ON() ? 1 : 0, log_sink_call_count); + DPCHECK(false); + EXPECT_EQ(DCHECK_IS_ON() ? 2 : 0, log_sink_call_count); + DCHECK_EQ(0, 1); + EXPECT_EQ(DCHECK_IS_ON() ? 3 : 0, log_sink_call_count); +} + TEST_F(LoggingTest, DcheckReleaseBehavior) { int some_variable = 1; // These should still reference |some_variable| so we don't get |