about summary refs log tree commit diff
path: root/absl/types
diff options
context:
space:
mode:
Diffstat (limited to 'absl/types')
-rw-r--r--absl/types/compare.h40
-rw-r--r--absl/types/compare_test.cc92
2 files changed, 111 insertions, 21 deletions
diff --git a/absl/types/compare.h b/absl/types/compare.h
index c29ced5273a2..62ca70f9a704 100644
--- a/absl/types/compare.h
+++ b/absl/types/compare.h
@@ -176,6 +176,14 @@ class weak_equality
                                    weak_equality v) noexcept {
     return 0 != v.value_;
   }
+  friend constexpr bool operator==(weak_equality v1,
+                                   weak_equality v2) noexcept {
+    return v1.value_ == v2.value_;
+  }
+  friend constexpr bool operator!=(weak_equality v1,
+                                   weak_equality v2) noexcept {
+    return v1.value_ != v2.value_;
+  }
 
  private:
   compare_internal::value_type value_;
@@ -219,6 +227,14 @@ class strong_equality
                                    strong_equality v) noexcept {
     return 0 != v.value_;
   }
+  friend constexpr bool operator==(strong_equality v1,
+                                   strong_equality v2) noexcept {
+    return v1.value_ == v2.value_;
+  }
+  friend constexpr bool operator!=(strong_equality v1,
+                                   strong_equality v2) noexcept {
+    return v1.value_ != v2.value_;
+  }
 
  private:
   compare_internal::value_type value_;
@@ -306,6 +322,14 @@ class partial_ordering
                                    partial_ordering v) noexcept {
     return v.is_ordered() && 0 >= v.value_;
   }
+  friend constexpr bool operator==(partial_ordering v1,
+                                   partial_ordering v2) noexcept {
+    return v1.value_ == v2.value_;
+  }
+  friend constexpr bool operator!=(partial_ordering v1,
+                                   partial_ordering v2) noexcept {
+    return v1.value_ != v2.value_;
+  }
 
  private:
   compare_internal::value_type value_;
@@ -390,6 +414,14 @@ class weak_ordering
                                    weak_ordering v) noexcept {
     return 0 >= v.value_;
   }
+  friend constexpr bool operator==(weak_ordering v1,
+                                   weak_ordering v2) noexcept {
+    return v1.value_ == v2.value_;
+  }
+  friend constexpr bool operator!=(weak_ordering v1,
+                                   weak_ordering v2) noexcept {
+    return v1.value_ != v2.value_;
+  }
 
  private:
   compare_internal::value_type value_;
@@ -481,6 +513,14 @@ class strong_ordering
                                    strong_ordering v) noexcept {
     return 0 >= v.value_;
   }
+  friend constexpr bool operator==(strong_ordering v1,
+                                   strong_ordering v2) noexcept {
+    return v1.value_ == v2.value_;
+  }
+  friend constexpr bool operator!=(strong_ordering v1,
+                                   strong_ordering v2) noexcept {
+    return v1.value_ != v2.value_;
+  }
 
  private:
   compare_internal::value_type value_;
diff --git a/absl/types/compare_test.cc b/absl/types/compare_test.cc
index 955844b529bb..8095baf9569f 100644
--- a/absl/types/compare_test.cc
+++ b/absl/types/compare_test.cc
@@ -31,6 +31,15 @@ TEST(Compare, WeakEquality) {
   EXPECT_TRUE(Identity(0 == weak_equality::equivalent));
   EXPECT_TRUE(Identity(weak_equality::nonequivalent != 0));
   EXPECT_TRUE(Identity(0 != weak_equality::nonequivalent));
+  const weak_equality values[] = {weak_equality::equivalent,
+                                  weak_equality::nonequivalent};
+  for (const auto& lhs : values) {
+    for (const auto& rhs : values) {
+      const bool are_equal = &lhs == &rhs;
+      EXPECT_EQ(lhs == rhs, are_equal);
+      EXPECT_EQ(lhs != rhs, !are_equal);
+    }
+  }
 }
 
 TEST(Compare, StrongEquality) {
@@ -42,6 +51,18 @@ TEST(Compare, StrongEquality) {
   EXPECT_TRUE(Identity(0 == strong_equality::equivalent));
   EXPECT_TRUE(Identity(strong_equality::nonequivalent != 0));
   EXPECT_TRUE(Identity(0 != strong_equality::nonequivalent));
+  const strong_equality values[] = {strong_equality::equal,
+                                    strong_equality::nonequal};
+  for (const auto& lhs : values) {
+    for (const auto& rhs : values) {
+      const bool are_equal = &lhs == &rhs;
+      EXPECT_EQ(lhs == rhs, are_equal);
+      EXPECT_EQ(lhs != rhs, !are_equal);
+    }
+  }
+  EXPECT_TRUE(Identity(strong_equality::equivalent == strong_equality::equal));
+  EXPECT_TRUE(
+      Identity(strong_equality::nonequivalent == strong_equality::nonequal));
 }
 
 TEST(Compare, PartialOrdering) {
@@ -65,6 +86,16 @@ TEST(Compare, PartialOrdering) {
   EXPECT_FALSE(Identity(0 > partial_ordering::unordered));
   EXPECT_FALSE(Identity(partial_ordering::unordered >= 0));
   EXPECT_FALSE(Identity(0 >= partial_ordering::unordered));
+  const partial_ordering values[] = {
+      partial_ordering::less, partial_ordering::equivalent,
+      partial_ordering::greater, partial_ordering::unordered};
+  for (const auto& lhs : values) {
+    for (const auto& rhs : values) {
+      const bool are_equal = &lhs == &rhs;
+      EXPECT_EQ(lhs == rhs, are_equal);
+      EXPECT_EQ(lhs != rhs, !are_equal);
+    }
+  }
 }
 
 TEST(Compare, WeakOrdering) {
@@ -78,6 +109,15 @@ TEST(Compare, WeakOrdering) {
   EXPECT_TRUE(Identity(0 < weak_ordering::greater));
   EXPECT_TRUE(Identity(weak_ordering::greater >= 0));
   EXPECT_TRUE(Identity(0 <= weak_ordering::greater));
+  const weak_ordering values[] = {
+      weak_ordering::less, weak_ordering::equivalent, weak_ordering::greater};
+  for (const auto& lhs : values) {
+    for (const auto& rhs : values) {
+      const bool are_equal = &lhs == &rhs;
+      EXPECT_EQ(lhs == rhs, are_equal);
+      EXPECT_EQ(lhs != rhs, !are_equal);
+    }
+  }
 }
 
 TEST(Compare, StrongOrdering) {
@@ -93,6 +133,16 @@ TEST(Compare, StrongOrdering) {
   EXPECT_TRUE(Identity(0 < strong_ordering::greater));
   EXPECT_TRUE(Identity(strong_ordering::greater >= 0));
   EXPECT_TRUE(Identity(0 <= strong_ordering::greater));
+  const strong_ordering values[] = {
+      strong_ordering::less, strong_ordering::equal, strong_ordering::greater};
+  for (const auto& lhs : values) {
+    for (const auto& rhs : values) {
+      const bool are_equal = &lhs == &rhs;
+      EXPECT_EQ(lhs == rhs, are_equal);
+      EXPECT_EQ(lhs != rhs, !are_equal);
+    }
+  }
+  EXPECT_TRUE(Identity(strong_ordering::equivalent == strong_ordering::equal));
 }
 
 TEST(Compare, Conversions) {
@@ -190,7 +240,7 @@ TEST(Compare, Conversions) {
 
 struct WeakOrderingLess {
   template <typename T>
-  absl::weak_ordering operator()(const T &a, const T &b) const {
+  absl::weak_ordering operator()(const T& a, const T& b) const {
     return a < b ? absl::weak_ordering::less
                  : a == b ? absl::weak_ordering::equivalent
                           : absl::weak_ordering::greater;
@@ -224,10 +274,10 @@ TEST(DoLessThanComparison, SanityTest) {
 }
 
 TEST(CompareResultAsOrdering, SanityTest) {
-  EXPECT_TRUE(Identity(
-      absl::compare_internal::compare_result_as_ordering(-1) < 0));
-  EXPECT_FALSE(Identity(
-      absl::compare_internal::compare_result_as_ordering(-1) == 0));
+  EXPECT_TRUE(
+      Identity(absl::compare_internal::compare_result_as_ordering(-1) < 0));
+  EXPECT_FALSE(
+      Identity(absl::compare_internal::compare_result_as_ordering(-1) == 0));
   EXPECT_FALSE(
       Identity(absl::compare_internal::compare_result_as_ordering(-1) > 0));
   EXPECT_TRUE(Identity(absl::compare_internal::compare_result_as_ordering(
@@ -237,31 +287,31 @@ TEST(CompareResultAsOrdering, SanityTest) {
   EXPECT_FALSE(Identity(absl::compare_internal::compare_result_as_ordering(
                             weak_ordering::less) > 0));
 
-  EXPECT_FALSE(Identity(
-      absl::compare_internal::compare_result_as_ordering(0) < 0));
-  EXPECT_TRUE(Identity(
-      absl::compare_internal::compare_result_as_ordering(0) == 0));
-  EXPECT_FALSE(Identity(
-      absl::compare_internal::compare_result_as_ordering(0) > 0));
+  EXPECT_FALSE(
+      Identity(absl::compare_internal::compare_result_as_ordering(0) < 0));
+  EXPECT_TRUE(
+      Identity(absl::compare_internal::compare_result_as_ordering(0) == 0));
+  EXPECT_FALSE(
+      Identity(absl::compare_internal::compare_result_as_ordering(0) > 0));
   EXPECT_FALSE(Identity(absl::compare_internal::compare_result_as_ordering(
-                           weak_ordering::equivalent) < 0));
+                            weak_ordering::equivalent) < 0));
   EXPECT_TRUE(Identity(absl::compare_internal::compare_result_as_ordering(
-                            weak_ordering::equivalent) == 0));
+                           weak_ordering::equivalent) == 0));
   EXPECT_FALSE(Identity(absl::compare_internal::compare_result_as_ordering(
                             weak_ordering::equivalent) > 0));
 
-  EXPECT_FALSE(Identity(
-      absl::compare_internal::compare_result_as_ordering(1) < 0));
-  EXPECT_FALSE(Identity(
-      absl::compare_internal::compare_result_as_ordering(1) == 0));
-  EXPECT_TRUE(Identity(
-      absl::compare_internal::compare_result_as_ordering(1) > 0));
+  EXPECT_FALSE(
+      Identity(absl::compare_internal::compare_result_as_ordering(1) < 0));
+  EXPECT_FALSE(
+      Identity(absl::compare_internal::compare_result_as_ordering(1) == 0));
+  EXPECT_TRUE(
+      Identity(absl::compare_internal::compare_result_as_ordering(1) > 0));
   EXPECT_FALSE(Identity(absl::compare_internal::compare_result_as_ordering(
-                           weak_ordering::greater) < 0));
+                            weak_ordering::greater) < 0));
   EXPECT_FALSE(Identity(absl::compare_internal::compare_result_as_ordering(
                             weak_ordering::greater) == 0));
   EXPECT_TRUE(Identity(absl::compare_internal::compare_result_as_ordering(
-                            weak_ordering::greater) > 0));
+                           weak_ordering::greater) > 0));
 }
 
 TEST(DoThreeWayComparison, SanityTest) {