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}
This commit is contained in:
danilchap 2016-10-20 00:58:15 -07:00 committed by Commit bot
parent a34e796a1b
commit e037060161
2 changed files with 62 additions and 2 deletions

View File

@ -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.

View File

@ -656,6 +656,54 @@ TEST(OptionalTest, TestEquality) {
*log);
}
TEST(OptionalTest, TestEqualityWithObject) {
auto log = Logger::Setup();
{
Logger a(17), b(42);
Optional<Logger> 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();
{