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() {} | |
227 | |
220 TEST_F(LoggingTest, Dcheck) { | 228 TEST_F(LoggingTest, Dcheck) { |
221 #if defined(NDEBUG) && !defined(DCHECK_ALWAYS_ON) | 229 #if defined(NDEBUG) && !defined(DCHECK_ALWAYS_ON) |
222 // Release build. | 230 // Release build. |
223 EXPECT_FALSE(DCHECK_IS_ON()); | 231 EXPECT_FALSE(DCHECK_IS_ON()); |
224 EXPECT_FALSE(DLOG_IS_ON(DCHECK)); | 232 EXPECT_FALSE(DLOG_IS_ON(DCHECK)); |
225 #elif defined(NDEBUG) && defined(DCHECK_ALWAYS_ON) | 233 #elif defined(NDEBUG) && defined(DCHECK_ALWAYS_ON) |
226 // Release build with real DCHECKS. | 234 // Release build with real DCHECKS. |
227 SetLogAssertHandler(&LogSink); | 235 SetLogAssertHandler(&LogSink); |
228 EXPECT_TRUE(DCHECK_IS_ON()); | 236 EXPECT_TRUE(DCHECK_IS_ON()); |
229 EXPECT_TRUE(DLOG_IS_ON(DCHECK)); | 237 EXPECT_TRUE(DLOG_IS_ON(DCHECK)); |
(...skipping 21 matching lines...) Expand all Loading... | |
251 DCHECK_NE(p_not_null, nullptr); | 259 DCHECK_NE(p_not_null, nullptr); |
252 DCHECK_NE(nullptr, p_not_null); | 260 DCHECK_NE(nullptr, p_not_null); |
253 EXPECT_EQ(0, log_sink_call_count); | 261 EXPECT_EQ(0, log_sink_call_count); |
254 | 262 |
255 // Test DCHECK on a scoped enum. | 263 // Test DCHECK on a scoped enum. |
256 enum class Animal { DOG, CAT }; | 264 enum class Animal { DOG, CAT }; |
257 DCHECK_EQ(Animal::DOG, Animal::DOG); | 265 DCHECK_EQ(Animal::DOG, Animal::DOG); |
258 EXPECT_EQ(0, log_sink_call_count); | 266 EXPECT_EQ(0, log_sink_call_count); |
259 DCHECK_EQ(Animal::DOG, Animal::CAT); | 267 DCHECK_EQ(Animal::DOG, Animal::CAT); |
260 EXPECT_EQ(DCHECK_IS_ON() ? 1 : 0, log_sink_call_count); | 268 EXPECT_EQ(DCHECK_IS_ON() ? 1 : 0, log_sink_call_count); |
269 | |
270 // Test DCHECK on functions and function pointers. | |
271 log_sink_call_count = 0; | |
272 void (*fp1)() = DcheckEmptyFunction1; | |
273 void (*fp2)() = DcheckEmptyFunction2; | |
274 void (*fp3)() = DcheckEmptyFunction1; | |
275 DCHECK_EQ(fp1, fp3); | |
276 EXPECT_EQ(DCHECK_IS_ON() ? 0 : 0, log_sink_call_count); | |
Nico
2016/11/23 18:21:10
Err, replace `DCHECK_IS_ON() ? 0 : 0` with `0`? :-
Raphael Kubo da Costa (rakuco)
2016/11/23 18:58:09
Brain fart :(
| |
277 DCHECK_EQ(fp1, fp2); | |
278 EXPECT_EQ(DCHECK_IS_ON() ? 1 : 0, log_sink_call_count); | |
279 struct MemberFunctions { | |
280 static void MemberFunction1(){}; | |
Nico
2016/11/23 18:21:10
Actually, this isn't a member function, this is a
Raphael Kubo da Costa (rakuco)
2016/11/23 18:58:09
That works too; the reason I went for static funct
| |
281 static void TestDCHECK() { | |
282 void (*fp)() = MemberFunction1; | |
283 DCHECK_EQ(fp, MemberFunction1); | |
284 EXPECT_EQ(DCHECK_IS_ON() ? 1 : 0, log_sink_call_count); | |
285 DCHECK_EQ(fp, TestDCHECK); | |
286 EXPECT_EQ(DCHECK_IS_ON() ? 2 : 0, log_sink_call_count); | |
287 } | |
288 }; | |
289 MemberFunctions::TestDCHECK(); | |
261 } | 290 } |
262 | 291 |
263 TEST_F(LoggingTest, DcheckReleaseBehavior) { | 292 TEST_F(LoggingTest, DcheckReleaseBehavior) { |
264 int some_variable = 1; | 293 int some_variable = 1; |
265 // These should still reference |some_variable| so we don't get | 294 // These should still reference |some_variable| so we don't get |
266 // unused variable warnings. | 295 // unused variable warnings. |
267 DCHECK(some_variable) << "test"; | 296 DCHECK(some_variable) << "test"; |
268 DPCHECK(some_variable) << "test"; | 297 DPCHECK(some_variable) << "test"; |
269 DCHECK_EQ(some_variable, 1) << "test"; | 298 DCHECK_EQ(some_variable, 1) << "test"; |
270 } | 299 } |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
308 std::wstring wstr = L"Hello World"; | 337 std::wstring wstr = L"Hello World"; |
309 std::ostringstream ostr; | 338 std::ostringstream ostr; |
310 ostr << wstr; | 339 ostr << wstr; |
311 EXPECT_EQ("Hello World", ostr.str()); | 340 EXPECT_EQ("Hello World", ostr.str()); |
312 } | 341 } |
313 } // namespace nested_test | 342 } // namespace nested_test |
314 | 343 |
315 } // namespace | 344 } // namespace |
316 | 345 |
317 } // namespace logging | 346 } // namespace logging |
OLD | NEW |