Commit 7997579b authored by Bartek Nowierski's avatar Bartek Nowierski Committed by Commit Bot

Add bool operator vs. implicit cast unit test

Bug: 1073933
Change-Id: I12c279457aa360cbb0cd9ea53a4ae54db4d4d051
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2226003
Commit-Queue: Daniel Cheng <dcheng@chromium.org>
Reviewed-by: default avatarDaniel Cheng <dcheng@chromium.org>
Auto-Submit: Bartek Nowierski <bartekn@chromium.org>
Cr-Commit-Position: refs/heads/master@{#774051}
parent 7b0d9469
...@@ -167,6 +167,45 @@ TEST_F(CheckedPtrTest, NullCmpBool) { ...@@ -167,6 +167,45 @@ TEST_F(CheckedPtrTest, NullCmpBool) {
EXPECT_EQ(g_get_for_dereference_cnt, 0); EXPECT_EQ(g_get_for_dereference_cnt, 0);
} }
bool IsValidNoCast(CountingCheckedPtr<int> ptr) {
return !!ptr; // !! to avoid implicit cast
}
bool IsValidNoCast2(CountingCheckedPtr<int> ptr) {
return ptr && true;
}
TEST_F(CheckedPtrTest, BoolOpNotCast) {
CountingCheckedPtr<int> ptr = nullptr;
volatile bool is_valid = !!ptr; // !! to avoid implicit cast
is_valid = ptr || is_valid; // volatile, so won't be optimized
if (ptr)
is_valid = true;
bool is_not_valid = !ptr;
if (!ptr)
is_not_valid = true;
std::ignore = IsValidNoCast(ptr);
std::ignore = IsValidNoCast2(ptr);
// No need to unwrap pointer, just compare against 0.
EXPECT_EQ(g_get_for_comparison_cnt, 0);
EXPECT_EQ(g_get_for_extraction_cnt, 0);
EXPECT_EQ(g_get_for_dereference_cnt, 0);
}
bool IsValidWithCast(CountingCheckedPtr<int> ptr) {
return ptr;
}
// This test is mostly for documentation purposes, demonstrating that a more
// costly cast can be invoked if we aren't careful.
TEST_F(CheckedPtrTest, CastNotBoolOp) {
CountingCheckedPtr<int> ptr = nullptr;
bool is_valid = ptr;
is_valid = IsValidWithCast(ptr);
EXPECT_EQ(g_get_for_comparison_cnt, 0);
EXPECT_EQ(g_get_for_extraction_cnt, 2);
EXPECT_EQ(g_get_for_dereference_cnt, 0);
}
TEST_F(CheckedPtrTest, StarDereference) { TEST_F(CheckedPtrTest, StarDereference) {
int foo = 42; int foo = 42;
CountingCheckedPtr<int> ptr = &foo; CountingCheckedPtr<int> ptr = &foo;
......
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