From e0370601612275628850e6a06ddf5a3e3e77a2ee Mon Sep 17 00:00:00 2001 From: danilchap Date: Thu, 20 Oct 2016 00:58:15 -0700 Subject: [PATCH] Add to rtc::Optional equality/unequality comparisions with object BUG=None Review-Url: https://codereview.webrtc.org/2432393002 Cr-Commit-Position: refs/heads/master@{#14690} --- webrtc/base/optional.h | 16 +++++++++-- webrtc/base/optional_unittest.cc | 48 ++++++++++++++++++++++++++++++++ 2 files changed, 62 insertions(+), 2 deletions(-) diff --git a/webrtc/base/optional.h b/webrtc/base/optional.h index 728ed9e737..ec33470f26 100644 --- a/webrtc/base/optional.h +++ b/webrtc/base/optional.h @@ -234,16 +234,28 @@ class Optional final { } // Equality tests. Two Optionals are equal if they contain equivalent values, - // or - // if they're both empty. + // or if they're both empty. friend bool operator==(const Optional& m1, const Optional& m2) { return m1.has_value_ && m2.has_value_ ? m1.value_ == m2.value_ : m1.has_value_ == m2.has_value_; } + friend bool operator==(const Optional& opt, const T& value) { + return opt.has_value_ && opt.value_ == value; + } + friend bool operator==(const T& value, const Optional& opt) { + return opt.has_value_ && value == opt.value_; + } + friend bool operator!=(const Optional& m1, const Optional& m2) { return m1.has_value_ && m2.has_value_ ? m1.value_ != m2.value_ : m1.has_value_ != m2.has_value_; } + friend bool operator!=(const Optional& opt, const T& value) { + return !opt.has_value_ || opt.value_ != value; + } + friend bool operator!=(const T& value, const Optional& opt) { + return !opt.has_value_ || value != opt.value_; + } private: // Tell sanitizers that value_ shouldn't be touched. diff --git a/webrtc/base/optional_unittest.cc b/webrtc/base/optional_unittest.cc index 492db4bb08..c1ae9c00d4 100644 --- a/webrtc/base/optional_unittest.cc +++ b/webrtc/base/optional_unittest.cc @@ -656,6 +656,54 @@ TEST(OptionalTest, TestEquality) { *log); } +TEST(OptionalTest, TestEqualityWithObject) { + auto log = Logger::Setup(); + { + Logger a(17), b(42); + Optional ma(a), me; + // Using operator== and operator!= explicetly instead of EXPECT_EQ/EXPECT_NE + // macros because those operators are under test. + log->push_back("---"); + + EXPECT_TRUE(ma == a); + EXPECT_TRUE(a == ma); + EXPECT_FALSE(ma == b); + EXPECT_FALSE(b == ma); + EXPECT_FALSE(me == a); + EXPECT_FALSE(a == me); + + EXPECT_FALSE(ma != a); + EXPECT_FALSE(a != ma); + EXPECT_TRUE(ma != b); + EXPECT_TRUE(b != ma); + EXPECT_TRUE(me != a); + EXPECT_TRUE(a != me); + + log->push_back("---"); + } + // clang-format off + EXPECT_EQ(V("0:17. explicit constructor", + "1:42. explicit constructor", + "2:17. copy constructor (from 0:17)", + "---", + "operator== 2:17, 0:17", + "operator== 0:17, 2:17", + "operator== 2:17, 1:42", + "operator== 1:42, 2:17", + // No operator should be called when comparing to empty. + "operator!= 2:17, 0:17", + "operator!= 0:17, 2:17", + "operator!= 2:17, 1:42", + "operator!= 1:42, 2:17", + // No operator should be called when comparing to empty. + "---", + "2:17. destructor", + "1:42. destructor", + "0:17. destructor"), + *log); + // clang-format on +} + TEST(OptionalTest, TestSwap) { auto log = Logger::Setup(); {