about summary refs log tree commit diff
path: root/absl/container/internal
diff options
context:
space:
mode:
Diffstat (limited to 'absl/container/internal')
-rw-r--r--absl/container/internal/test_instance_tracker.cc1
-rw-r--r--absl/container/internal/test_instance_tracker.h52
-rw-r--r--absl/container/internal/test_instance_tracker_test.cc22
3 files changed, 70 insertions, 5 deletions
diff --git a/absl/container/internal/test_instance_tracker.cc b/absl/container/internal/test_instance_tracker.cc
index fe00aca8fb98..b18e0bb7a1ea 100644
--- a/absl/container/internal/test_instance_tracker.cc
+++ b/absl/container/internal/test_instance_tracker.cc
@@ -21,6 +21,7 @@ int BaseCountedInstance::num_live_instances_ = 0;
 int BaseCountedInstance::num_moves_ = 0;
 int BaseCountedInstance::num_copies_ = 0;
 int BaseCountedInstance::num_swaps_ = 0;
+int BaseCountedInstance::num_comparisons_ = 0;
 
 }  // namespace test_internal
 }  // namespace absl
diff --git a/absl/container/internal/test_instance_tracker.h b/absl/container/internal/test_instance_tracker.h
index cf8f3a531e62..ec45f574037f 100644
--- a/absl/container/internal/test_instance_tracker.h
+++ b/absl/container/internal/test_instance_tracker.h
@@ -22,8 +22,8 @@ namespace absl {
 namespace test_internal {
 
 // A type that counts number of occurences of the type, the live occurrences of
-// the type, as well as the number of copies, moves, and swaps that have
-// occurred on the type. This is used as a base class for the copyable,
+// the type, as well as the number of copies, moves, swaps, and comparisons that
+// have occurred on the type. This is used as a base class for the copyable,
 // copyable+movable, and movable types below that are used in actual tests. Use
 // InstanceTracker in tests to track the number of instances.
 class BaseCountedInstance {
@@ -66,6 +66,36 @@ class BaseCountedInstance {
     return *this;
   }
 
+  bool operator==(const BaseCountedInstance& x) const {
+    ++num_comparisons_;
+    return value_ == x.value_;
+  }
+
+  bool operator!=(const BaseCountedInstance& x) const {
+    ++num_comparisons_;
+    return value_ != x.value_;
+  }
+
+  bool operator<(const BaseCountedInstance& x) const {
+    ++num_comparisons_;
+    return value_ < x.value_;
+  }
+
+  bool operator>(const BaseCountedInstance& x) const {
+    ++num_comparisons_;
+    return value_ > x.value_;
+  }
+
+  bool operator<=(const BaseCountedInstance& x) const {
+    ++num_comparisons_;
+    return value_ <= x.value_;
+  }
+
+  bool operator>=(const BaseCountedInstance& x) const {
+    ++num_comparisons_;
+    return value_ >= x.value_;
+  }
+
   int value() const {
     if (!is_live_) std::abort();
     return value_;
@@ -108,6 +138,9 @@ class BaseCountedInstance {
 
   // Number of times that BaseCountedInstance objects were swapped.
   static int num_swaps_;
+
+  // Number of times that BaseCountedInstance objects were compared.
+  static int num_comparisons_;
 };
 
 // Helper to track the BaseCountedInstance instance counters. Expects that the
@@ -152,13 +185,21 @@ class InstanceTracker {
   // construction or the last call to ResetCopiesMovesSwaps().
   int swaps() const { return BaseCountedInstance::num_swaps_ - start_swaps_; }
 
-  // Resets the base values for moves, copies and swaps to the current values,
-  // so that subsequent Get*() calls for moves, copies and swaps will compare to
-  // the situation at the point of this call.
+  // Returns the number of comparisons on BaseCountedInstance objects since
+  // construction or the last call to ResetCopiesMovesSwaps().
+  int comparisons() const {
+    return BaseCountedInstance::num_comparisons_ - start_comparisons_;
+  }
+
+  // Resets the base values for moves, copies, comparisons, and swaps to the
+  // current values, so that subsequent Get*() calls for moves, copies,
+  // comparisons, and swaps will compare to the situation at the point of this
+  // call.
   void ResetCopiesMovesSwaps() {
     start_moves_ = BaseCountedInstance::num_moves_;
     start_copies_ = BaseCountedInstance::num_copies_;
     start_swaps_ = BaseCountedInstance::num_swaps_;
+    start_comparisons_ = BaseCountedInstance::num_comparisons_;
   }
 
  private:
@@ -167,6 +208,7 @@ class InstanceTracker {
   int start_moves_;
   int start_copies_;
   int start_swaps_;
+  int start_comparisons_;
 };
 
 // Copyable, not movable.
diff --git a/absl/container/internal/test_instance_tracker_test.cc b/absl/container/internal/test_instance_tracker_test.cc
index 9efb6771cf08..0ae57636df21 100644
--- a/absl/container/internal/test_instance_tracker_test.cc
+++ b/absl/container/internal/test_instance_tracker_test.cc
@@ -157,4 +157,26 @@ TEST(TestInstanceTracker, ExistingInstances) {
   EXPECT_EQ(1, tracker.moves());
 }
 
+TEST(TestInstanceTracker, Comparisons) {
+  InstanceTracker tracker;
+  MovableOnlyInstance one(1), two(2);
+
+  EXPECT_EQ(0, tracker.comparisons());
+  EXPECT_FALSE(one == two);
+  EXPECT_EQ(1, tracker.comparisons());
+  EXPECT_TRUE(one != two);
+  EXPECT_EQ(2, tracker.comparisons());
+  EXPECT_TRUE(one < two);
+  EXPECT_EQ(3, tracker.comparisons());
+  EXPECT_FALSE(one > two);
+  EXPECT_EQ(4, tracker.comparisons());
+  EXPECT_TRUE(one <= two);
+  EXPECT_EQ(5, tracker.comparisons());
+  EXPECT_FALSE(one >= two);
+  EXPECT_EQ(6, tracker.comparisons());
+
+  tracker.ResetCopiesMovesSwaps();
+  EXPECT_EQ(0, tracker.comparisons());
+}
+
 }  // namespace