Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "base/compiler_specific.h" | 5 #include "base/compiler_specific.h" |
| 6 #include "base/logging.h" | 6 #include "base/logging.h" |
| 7 #include "base/macros.h" | 7 #include "base/macros.h" |
| 8 | 8 |
| 9 #include "testing/gmock/include/gmock/gmock.h" | 9 #include "testing/gmock/include/gmock/gmock.h" |
| 10 #include "testing/gtest/include/gtest/gtest.h" | 10 #include "testing/gtest/include/gtest/gtest.h" |
| (...skipping 199 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 210 DCHECK_EQ(0, 0) << mock_log_source.Log(); | 210 DCHECK_EQ(0, 0) << mock_log_source.Log(); |
| 211 #else | 211 #else |
| 212 DCHECK(mock_log_source.Log()) << mock_log_source.Log(); | 212 DCHECK(mock_log_source.Log()) << mock_log_source.Log(); |
| 213 DPCHECK(mock_log_source.Log()) << mock_log_source.Log(); | 213 DPCHECK(mock_log_source.Log()) << mock_log_source.Log(); |
| 214 DCHECK_EQ(0, 0) << mock_log_source.Log(); | 214 DCHECK_EQ(0, 0) << mock_log_source.Log(); |
| 215 DCHECK_EQ(mock_log_source.Log(), static_cast<const char*>(NULL)) | 215 DCHECK_EQ(mock_log_source.Log(), static_cast<const char*>(NULL)) |
| 216 << mock_log_source.Log(); | 216 << mock_log_source.Log(); |
| 217 #endif | 217 #endif |
| 218 } | 218 } |
| 219 | 219 |
| 220 void DcheckEmptyFunction1() { | |
| 221 // Provide a body so that Release builds do not cause the compiler to | |
| 222 // optimize DcheckEmptyFunction1 and DcheckEmptyFunction2 as a single | |
| 223 // function, which breaks the Dcheck tests below. | |
| 224 LOG(INFO) << "DcheckEmptyFunction1"; | |
| 225 } | |
| 226 void DcheckEmptyFunction2() {} | |
|
Nico
2016/11/23 18:09:26
nit: newline after this
| |
| 220 TEST_F(LoggingTest, Dcheck) { | 227 TEST_F(LoggingTest, Dcheck) { |
| 221 #if defined(NDEBUG) && !defined(DCHECK_ALWAYS_ON) | 228 #if defined(NDEBUG) && !defined(DCHECK_ALWAYS_ON) |
| 222 // Release build. | 229 // Release build. |
| 223 EXPECT_FALSE(DCHECK_IS_ON()); | 230 EXPECT_FALSE(DCHECK_IS_ON()); |
| 224 EXPECT_FALSE(DLOG_IS_ON(DCHECK)); | 231 EXPECT_FALSE(DLOG_IS_ON(DCHECK)); |
| 225 #elif defined(NDEBUG) && defined(DCHECK_ALWAYS_ON) | 232 #elif defined(NDEBUG) && defined(DCHECK_ALWAYS_ON) |
| 226 // Release build with real DCHECKS. | 233 // Release build with real DCHECKS. |
| 227 SetLogAssertHandler(&LogSink); | 234 SetLogAssertHandler(&LogSink); |
| 228 EXPECT_TRUE(DCHECK_IS_ON()); | 235 EXPECT_TRUE(DCHECK_IS_ON()); |
| 229 EXPECT_TRUE(DLOG_IS_ON(DCHECK)); | 236 EXPECT_TRUE(DLOG_IS_ON(DCHECK)); |
| (...skipping 21 matching lines...) Expand all Loading... | |
| 251 DCHECK_NE(p_not_null, nullptr); | 258 DCHECK_NE(p_not_null, nullptr); |
| 252 DCHECK_NE(nullptr, p_not_null); | 259 DCHECK_NE(nullptr, p_not_null); |
| 253 EXPECT_EQ(0, log_sink_call_count); | 260 EXPECT_EQ(0, log_sink_call_count); |
| 254 | 261 |
| 255 // Test DCHECK on a scoped enum. | 262 // Test DCHECK on a scoped enum. |
| 256 enum class Animal { DOG, CAT }; | 263 enum class Animal { DOG, CAT }; |
| 257 DCHECK_EQ(Animal::DOG, Animal::DOG); | 264 DCHECK_EQ(Animal::DOG, Animal::DOG); |
| 258 EXPECT_EQ(0, log_sink_call_count); | 265 EXPECT_EQ(0, log_sink_call_count); |
| 259 DCHECK_EQ(Animal::DOG, Animal::CAT); | 266 DCHECK_EQ(Animal::DOG, Animal::CAT); |
| 260 EXPECT_EQ(DCHECK_IS_ON() ? 1 : 0, log_sink_call_count); | 267 EXPECT_EQ(DCHECK_IS_ON() ? 1 : 0, log_sink_call_count); |
| 268 | |
| 269 // Test DCHECK on functions and function pointers. | |
| 270 void (*fp1)() = DcheckEmptyFunction1; | |
| 271 void (*fp2)() = DcheckEmptyFunction2; | |
| 272 void (*fp3)() = DcheckEmptyFunction1; | |
| 273 DCHECK_EQ(fp1, fp3); | |
| 274 EXPECT_EQ(DCHECK_IS_ON() ? 1 : 0, log_sink_call_count); | |
|
Nico
2016/11/23 18:09:26
This is super confusing – it looks like fp1 and fp
| |
| 275 DCHECK_EQ(fp1, fp2); | |
| 276 EXPECT_EQ(DCHECK_IS_ON() ? 2 : 0, log_sink_call_count); | |
| 277 struct MemberFunctions { | |
| 278 static void MemberFunction1(){}; | |
| 279 static void TestDCHECK() { | |
| 280 void (*fp)() = MemberFunction1; | |
| 281 DCHECK_EQ(fp, MemberFunction1); | |
| 282 EXPECT_EQ(DCHECK_IS_ON() ? 2 : 0, log_sink_call_count); | |
| 283 DCHECK_EQ(fp, TestDCHECK); | |
| 284 EXPECT_EQ(DCHECK_IS_ON() ? 3 : 0, log_sink_call_count); | |
| 285 } | |
| 286 }; | |
| 287 MemberFunctions::TestDCHECK(); | |
| 261 } | 288 } |
| 262 | 289 |
| 263 TEST_F(LoggingTest, DcheckReleaseBehavior) { | 290 TEST_F(LoggingTest, DcheckReleaseBehavior) { |
| 264 int some_variable = 1; | 291 int some_variable = 1; |
| 265 // These should still reference |some_variable| so we don't get | 292 // These should still reference |some_variable| so we don't get |
| 266 // unused variable warnings. | 293 // unused variable warnings. |
| 267 DCHECK(some_variable) << "test"; | 294 DCHECK(some_variable) << "test"; |
| 268 DPCHECK(some_variable) << "test"; | 295 DPCHECK(some_variable) << "test"; |
| 269 DCHECK_EQ(some_variable, 1) << "test"; | 296 DCHECK_EQ(some_variable, 1) << "test"; |
| 270 } | 297 } |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 308 std::wstring wstr = L"Hello World"; | 335 std::wstring wstr = L"Hello World"; |
| 309 std::ostringstream ostr; | 336 std::ostringstream ostr; |
| 310 ostr << wstr; | 337 ostr << wstr; |
| 311 EXPECT_EQ("Hello World", ostr.str()); | 338 EXPECT_EQ("Hello World", ostr.str()); |
| 312 } | 339 } |
| 313 } // namespace nested_test | 340 } // namespace nested_test |
| 314 | 341 |
| 315 } // namespace | 342 } // namespace |
| 316 | 343 |
| 317 } // namespace logging | 344 } // namespace logging |
| OLD | NEW |