Commit ad9b27ef authored by Bartek Nowierski's avatar Bartek Nowierski Committed by Commit Bot

Add one more case to cast vs. bool operator tests

Bug: 1073933
Change-Id: Ie039b080b8c76fa29dfd5b52f5f1135ed53a40b1
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2259535
Commit-Queue: Bartek Nowierski <bartekn@chromium.org>
Auto-Submit: Bartek Nowierski <bartekn@chromium.org>
Reviewed-by: default avatarDaniel Cheng <dcheng@chromium.org>
Cr-Commit-Position: refs/heads/master@{#781279}
parent afe1fab8
...@@ -167,6 +167,8 @@ TEST_F(CheckedPtrTest, NullCmpBool) { ...@@ -167,6 +167,8 @@ TEST_F(CheckedPtrTest, NullCmpBool) {
EXPECT_EQ(g_get_for_dereference_cnt, 0); EXPECT_EQ(g_get_for_dereference_cnt, 0);
} }
void FuncThatAcceptsBool(bool b) {}
bool IsValidNoCast(CountingCheckedPtr<int> ptr) { bool IsValidNoCast(CountingCheckedPtr<int> ptr) {
return !!ptr; // !! to avoid implicit cast return !!ptr; // !! to avoid implicit cast
} }
...@@ -185,6 +187,7 @@ TEST_F(CheckedPtrTest, BoolOpNotCast) { ...@@ -185,6 +187,7 @@ TEST_F(CheckedPtrTest, BoolOpNotCast) {
is_not_valid = true; is_not_valid = true;
std::ignore = IsValidNoCast(ptr); std::ignore = IsValidNoCast(ptr);
std::ignore = IsValidNoCast2(ptr); std::ignore = IsValidNoCast2(ptr);
FuncThatAcceptsBool(!ptr);
// No need to unwrap pointer, just compare against 0. // No need to unwrap pointer, just compare against 0.
EXPECT_EQ(g_get_for_comparison_cnt, 0); EXPECT_EQ(g_get_for_comparison_cnt, 0);
EXPECT_EQ(g_get_for_extraction_cnt, 0); EXPECT_EQ(g_get_for_extraction_cnt, 0);
...@@ -195,14 +198,17 @@ bool IsValidWithCast(CountingCheckedPtr<int> ptr) { ...@@ -195,14 +198,17 @@ bool IsValidWithCast(CountingCheckedPtr<int> ptr) {
return ptr; return ptr;
} }
// This test is mostly for documentation purposes, demonstrating that a more // This test is mostly for documentation purposes. It demonstrates cases where
// costly cast can be invoked if we aren't careful. // |operator T*| is called first and then the pointer is converted to bool,
// as opposed to calling |operator bool| directly. The former may be more
// costly, so the caller has to be careful not to trigger this path.
TEST_F(CheckedPtrTest, CastNotBoolOp) { TEST_F(CheckedPtrTest, CastNotBoolOp) {
CountingCheckedPtr<int> ptr = nullptr; CountingCheckedPtr<int> ptr = nullptr;
bool is_valid = ptr; bool is_valid = ptr;
is_valid = IsValidWithCast(ptr); is_valid = IsValidWithCast(ptr);
FuncThatAcceptsBool(ptr);
EXPECT_EQ(g_get_for_comparison_cnt, 0); EXPECT_EQ(g_get_for_comparison_cnt, 0);
EXPECT_EQ(g_get_for_extraction_cnt, 2); EXPECT_EQ(g_get_for_extraction_cnt, 3);
EXPECT_EQ(g_get_for_dereference_cnt, 0); EXPECT_EQ(g_get_for_dereference_cnt, 0);
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment