Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(176)

Side by Side Diff: base/logging_unittest.cc

Issue 2515283002: logging: Provide a specific MakeCheckOpValueString overload for functions (Closed)
Patch Set: Use actual member functions Created 4 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « base/logging.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
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
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 struct MemberFunctions {
273 void MemberFunction1() {
274 // See the comment in DcheckEmptyFunction1().
275 LOG(INFO) << "Do not merge with MemberFunction2.";
276 }
277 void MemberFunction2() {}
278 };
279 void (MemberFunctions::*mp1)() = &MemberFunctions::MemberFunction1;
280 void (MemberFunctions::*mp2)() = &MemberFunctions::MemberFunction2;
281 void (*fp1)() = DcheckEmptyFunction1;
282 void (*fp2)() = DcheckEmptyFunction2;
283 void (*fp3)() = DcheckEmptyFunction1;
284 DCHECK_EQ(fp1, fp3);
285 EXPECT_EQ(0, log_sink_call_count);
286 DCHECK_EQ(mp1, &MemberFunctions::MemberFunction1);
287 EXPECT_EQ(0, log_sink_call_count);
288 DCHECK_EQ(mp2, &MemberFunctions::MemberFunction2);
289 EXPECT_EQ(0, log_sink_call_count);
290 DCHECK_EQ(fp1, fp2);
291 EXPECT_EQ(DCHECK_IS_ON() ? 1 : 0, log_sink_call_count);
292 DCHECK_EQ(mp2, &MemberFunctions::MemberFunction1);
293 EXPECT_EQ(DCHECK_IS_ON() ? 2 : 0, log_sink_call_count);
261 } 294 }
262 295
263 TEST_F(LoggingTest, DcheckReleaseBehavior) { 296 TEST_F(LoggingTest, DcheckReleaseBehavior) {
264 int some_variable = 1; 297 int some_variable = 1;
265 // These should still reference |some_variable| so we don't get 298 // These should still reference |some_variable| so we don't get
266 // unused variable warnings. 299 // unused variable warnings.
267 DCHECK(some_variable) << "test"; 300 DCHECK(some_variable) << "test";
268 DPCHECK(some_variable) << "test"; 301 DPCHECK(some_variable) << "test";
269 DCHECK_EQ(some_variable, 1) << "test"; 302 DCHECK_EQ(some_variable, 1) << "test";
270 } 303 }
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
308 std::wstring wstr = L"Hello World"; 341 std::wstring wstr = L"Hello World";
309 std::ostringstream ostr; 342 std::ostringstream ostr;
310 ostr << wstr; 343 ostr << wstr;
311 EXPECT_EQ("Hello World", ostr.str()); 344 EXPECT_EQ("Hello World", ostr.str());
312 } 345 }
313 } // namespace nested_test 346 } // namespace nested_test
314 347
315 } // namespace 348 } // namespace
316 349
317 } // namespace logging 350 } // namespace logging
OLDNEW
« no previous file with comments | « base/logging.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698