summaryrefslogtreecommitdiffstats
path: root/Tests/CMakeLib
diff options
context:
space:
mode:
authorKyle Edwards <kyle.edwards@kitware.com>2020-08-29 18:05:53 (GMT)
committerKyle Edwards <kyle.edwards@kitware.com>2020-09-07 12:50:09 (GMT)
commit7e1304c6e61a25aac6fcf1bee47a719ac39075e5 (patch)
tree695135b12da603c1a971f2f09d73786c794b8c90 /Tests/CMakeLib
parentc854e9eba57841fdbcb2258adbfabee9df7bab03 (diff)
downloadCMake-7e1304c6e61a25aac6fcf1bee47a719ac39075e5.zip
CMake-7e1304c6e61a25aac6fcf1bee47a719ac39075e5.tar.gz
CMake-7e1304c6e61a25aac6fcf1bee47a719ac39075e5.tar.bz2
cm::optional: Add comparison operators
Diffstat (limited to 'Tests/CMakeLib')
-rw-r--r--Tests/CMakeLib/testOptional.cxx158
1 files changed, 158 insertions, 0 deletions
diff --git a/Tests/CMakeLib/testOptional.cxx b/Tests/CMakeLib/testOptional.cxx
index 4c3d088..de09c0f 100644
--- a/Tests/CMakeLib/testOptional.cxx
+++ b/Tests/CMakeLib/testOptional.cxx
@@ -29,6 +29,13 @@ public:
CONST_RVALUE_REFERENCE,
SWAP,
+
+ COMPARE_EE_EQ,
+ COMPARE_EE_NE,
+ COMPARE_EE_LT,
+ COMPARE_EE_LE,
+ COMPARE_EE_GT,
+ COMPARE_EE_GE,
};
EventType Type;
@@ -161,6 +168,42 @@ EventLogger& EventLogger::operator=(int value)
return *this;
}
+bool operator==(const EventLogger& lhs, const EventLogger& rhs)
+{
+ events.push_back({ Event::COMPARE_EE_EQ, &lhs, &rhs, lhs.Value });
+ return lhs.Value == rhs.Value;
+}
+
+bool operator!=(const EventLogger& lhs, const EventLogger& rhs)
+{
+ events.push_back({ Event::COMPARE_EE_NE, &lhs, &rhs, lhs.Value });
+ return lhs.Value != rhs.Value;
+}
+
+bool operator<(const EventLogger& lhs, const EventLogger& rhs)
+{
+ events.push_back({ Event::COMPARE_EE_LT, &lhs, &rhs, lhs.Value });
+ return lhs.Value < rhs.Value;
+}
+
+bool operator<=(const EventLogger& lhs, const EventLogger& rhs)
+{
+ events.push_back({ Event::COMPARE_EE_LE, &lhs, &rhs, lhs.Value });
+ return lhs.Value <= rhs.Value;
+}
+
+bool operator>(const EventLogger& lhs, const EventLogger& rhs)
+{
+ events.push_back({ Event::COMPARE_EE_GT, &lhs, &rhs, lhs.Value });
+ return lhs.Value > rhs.Value;
+}
+
+bool operator>=(const EventLogger& lhs, const EventLogger& rhs)
+{
+ events.push_back({ Event::COMPARE_EE_GE, &lhs, &rhs, lhs.Value });
+ return lhs.Value >= rhs.Value;
+}
+
void EventLogger::Reference() &
{
events.push_back({ Event::REFERENCE, this, nullptr, this->Value });
@@ -452,6 +495,120 @@ static bool testValueOr()
return true;
}
+static bool testComparison(std::vector<Event>& expected)
+{
+ const cm::optional<EventLogger> o1{ 1 };
+ const cm::optional<EventLogger> o2{ 2 };
+ const cm::optional<EventLogger> o3{ 2 };
+ const cm::optional<EventLogger> o4{};
+ const cm::optional<EventLogger> o5{};
+ const EventLogger e1{ 2 };
+
+ ASSERT_TRUE(!(o1 == o2) && o1 != o2);
+ ASSERT_TRUE(o1 < o2 && !(o1 >= o2));
+ ASSERT_TRUE(!(o1 > o2) && o1 <= o2);
+
+ ASSERT_TRUE(o2 == o3 && !(o2 != o3));
+ ASSERT_TRUE(!(o2 < o3) && o2 >= o3);
+ ASSERT_TRUE(!(o2 > o3) && o2 <= o3);
+
+ ASSERT_TRUE(!(o3 == o4) && o3 != o4);
+ ASSERT_TRUE(!(o3 < o4) && o3 >= o4);
+ ASSERT_TRUE(o3 > o4 && !(o3 <= o4));
+
+ ASSERT_TRUE(o4 == o5 && !(o4 != o5));
+ ASSERT_TRUE(!(o4 < o5) && o4 >= o5);
+ ASSERT_TRUE(!(o4 > o5) && o4 <= o5);
+
+ ASSERT_TRUE(!(o1 == cm::nullopt) && o1 != cm::nullopt);
+ ASSERT_TRUE(!(o1 < cm::nullopt) && o1 >= cm::nullopt);
+ ASSERT_TRUE(o1 > cm::nullopt && !(o1 <= cm::nullopt));
+
+ ASSERT_TRUE(!(cm::nullopt == o1) && cm::nullopt != o1);
+ ASSERT_TRUE(cm::nullopt < o1 && !(cm::nullopt >= o1));
+ ASSERT_TRUE(!(cm::nullopt > o1) && cm::nullopt <= o1);
+
+ ASSERT_TRUE(o4 == cm::nullopt && !(o4 != cm::nullopt));
+ ASSERT_TRUE(!(o4 < cm::nullopt) && o4 >= cm::nullopt);
+ ASSERT_TRUE(!(o4 > cm::nullopt) && o4 <= cm::nullopt);
+
+ ASSERT_TRUE(cm::nullopt == o4 && !(cm::nullopt != o4));
+ ASSERT_TRUE(!(cm::nullopt < o4) && cm::nullopt >= o4);
+ ASSERT_TRUE(!(cm::nullopt > o4) && cm::nullopt <= o4);
+
+ ASSERT_TRUE(!(o1 == e1) && o1 != e1);
+ ASSERT_TRUE(o1 < e1 && !(o1 >= e1));
+ ASSERT_TRUE(!(o1 > e1) && o1 <= e1);
+
+ ASSERT_TRUE(o2 == e1 && !(o2 != e1));
+ ASSERT_TRUE(!(o2 < e1) && o2 >= e1);
+ ASSERT_TRUE(!(o2 > e1) && o2 <= e1);
+
+ ASSERT_TRUE(!(o4 == e1) && o4 != e1);
+ ASSERT_TRUE(o4 < e1 && !(o4 >= e1));
+ ASSERT_TRUE(!(o4 > e1) && o4 <= e1);
+
+ ASSERT_TRUE(!(e1 == o1) && e1 != o1);
+ ASSERT_TRUE(!(e1 < o1) && e1 >= o1);
+ ASSERT_TRUE(e1 > o1 && !(e1 <= o1));
+
+ ASSERT_TRUE(e1 == o2 && !(e1 != o2));
+ ASSERT_TRUE(!(e1 < o2) && e1 >= o2);
+ ASSERT_TRUE(!(e1 > o2) && e1 <= o2);
+
+ ASSERT_TRUE(!(e1 == o4) && e1 != o4);
+ ASSERT_TRUE(!(e1 < o4) && e1 >= o4);
+ ASSERT_TRUE(e1 > o4 && !(e1 <= o4));
+
+ expected = {
+ { Event::VALUE_CONSTRUCT, &*o1, nullptr, 1 },
+ { Event::VALUE_CONSTRUCT, &*o2, nullptr, 2 },
+ { Event::VALUE_CONSTRUCT, &*o3, nullptr, 2 },
+ { Event::VALUE_CONSTRUCT, &e1, nullptr, 2 },
+ { Event::COMPARE_EE_EQ, &*o1, &*o2, 1 },
+ { Event::COMPARE_EE_NE, &*o1, &*o2, 1 },
+ { Event::COMPARE_EE_LT, &*o1, &*o2, 1 },
+ { Event::COMPARE_EE_GE, &*o1, &*o2, 1 },
+ { Event::COMPARE_EE_GT, &*o1, &*o2, 1 },
+ { Event::COMPARE_EE_LE, &*o1, &*o2, 1 },
+ { Event::COMPARE_EE_EQ, &*o2, &*o3, 2 },
+ { Event::COMPARE_EE_NE, &*o2, &*o3, 2 },
+ { Event::COMPARE_EE_LT, &*o2, &*o3, 2 },
+ { Event::COMPARE_EE_GE, &*o2, &*o3, 2 },
+ { Event::COMPARE_EE_GT, &*o2, &*o3, 2 },
+ { Event::COMPARE_EE_LE, &*o2, &*o3, 2 },
+ { Event::COMPARE_EE_EQ, &*o1, &e1, 1 },
+ { Event::COMPARE_EE_NE, &*o1, &e1, 1 },
+ { Event::COMPARE_EE_LT, &*o1, &e1, 1 },
+ { Event::COMPARE_EE_GE, &*o1, &e1, 1 },
+ { Event::COMPARE_EE_GT, &*o1, &e1, 1 },
+ { Event::COMPARE_EE_LE, &*o1, &e1, 1 },
+ { Event::COMPARE_EE_EQ, &*o2, &e1, 2 },
+ { Event::COMPARE_EE_NE, &*o2, &e1, 2 },
+ { Event::COMPARE_EE_LT, &*o2, &e1, 2 },
+ { Event::COMPARE_EE_GE, &*o2, &e1, 2 },
+ { Event::COMPARE_EE_GT, &*o2, &e1, 2 },
+ { Event::COMPARE_EE_LE, &*o2, &e1, 2 },
+ { Event::COMPARE_EE_EQ, &e1, &*o1, 2 },
+ { Event::COMPARE_EE_NE, &e1, &*o1, 2 },
+ { Event::COMPARE_EE_LT, &e1, &*o1, 2 },
+ { Event::COMPARE_EE_GE, &e1, &*o1, 2 },
+ { Event::COMPARE_EE_GT, &e1, &*o1, 2 },
+ { Event::COMPARE_EE_LE, &e1, &*o1, 2 },
+ { Event::COMPARE_EE_EQ, &e1, &*o2, 2 },
+ { Event::COMPARE_EE_NE, &e1, &*o2, 2 },
+ { Event::COMPARE_EE_LT, &e1, &*o2, 2 },
+ { Event::COMPARE_EE_GE, &e1, &*o2, 2 },
+ { Event::COMPARE_EE_GT, &e1, &*o2, 2 },
+ { Event::COMPARE_EE_LE, &e1, &*o2, 2 },
+ { Event::DESTRUCT, &e1, nullptr, 2 },
+ { Event::DESTRUCT, &*o3, nullptr, 2 },
+ { Event::DESTRUCT, &*o2, nullptr, 2 },
+ { Event::DESTRUCT, &*o1, nullptr, 1 },
+ };
+ return true;
+}
+
static bool testSwap(std::vector<Event>& expected)
{
cm::optional<EventLogger> o1{ 4 };
@@ -612,6 +769,7 @@ int testOptional(int /*unused*/, char* /*unused*/ [])
DO_EVENT_TEST(testHasValue);
DO_EVENT_TEST(testValue);
DO_TEST(testValueOr);
+ DO_EVENT_TEST(testComparison);
DO_EVENT_TEST(testSwap);
DO_EVENT_TEST(testReset);
DO_EVENT_TEST(testEmplace);