[sanitizer] Add operator== and operator!= for InternalMmapVectorNoCtor
authorVitaly Buka <vitalybuka@google.com>
Mon, 7 May 2018 06:14:12 +0000 (06:14 +0000)
committerVitaly Buka <vitalybuka@google.com>
Mon, 7 May 2018 06:14:12 +0000 (06:14 +0000)
llvm-svn: 331619

compiler-rt/lib/sanitizer_common/sanitizer_common.h
compiler-rt/lib/sanitizer_common/tests/sanitizer_common_test.cc

index 9b90a19..cd47028 100644 (file)
@@ -528,6 +528,19 @@ class InternalMmapVectorNoCtor {
   uptr size_;
 };
 
+template <typename T>
+bool operator==(const InternalMmapVectorNoCtor<T> &lhs,
+                const InternalMmapVectorNoCtor<T> &rhs) {
+  if (lhs.size() != rhs.size()) return false;
+  return internal_memcmp(lhs.data(), rhs.data(), lhs.size() * sizeof(T)) == 0;
+}
+
+template <typename T>
+bool operator!=(const InternalMmapVectorNoCtor<T> &lhs,
+                const InternalMmapVectorNoCtor<T> &rhs) {
+  return !(lhs == rhs);
+}
+
 template<typename T>
 class InternalMmapVector : public InternalMmapVectorNoCtor<T> {
  public:
index 5fa80ef..ff5c632 100644 (file)
@@ -107,6 +107,29 @@ TEST(SanitizerCommon, InternalMmapVector) {
   CHECK_EQ(0U, empty_vector.size());
 }
 
+TEST(SanitizerCommon, InternalMmapVectorEq) {
+  InternalMmapVector<uptr> vector1;
+  InternalMmapVector<uptr> vector2;
+  for (uptr i = 0; i < 100; i++) {
+    vector1.push_back(i);
+    vector2.push_back(i);
+  }
+  EXPECT_TRUE(vector1 == vector2);
+  EXPECT_FALSE(vector1 != vector2);
+
+  vector1.push_back(1);
+  EXPECT_FALSE(vector1 == vector2);
+  EXPECT_TRUE(vector1 != vector2);
+
+  vector2.push_back(1);
+  EXPECT_TRUE(vector1 == vector2);
+  EXPECT_FALSE(vector1 != vector2);
+
+  vector1[55] = 1;
+  EXPECT_FALSE(vector1 == vector2);
+  EXPECT_TRUE(vector1 != vector2);
+}
+
 void TestThreadInfo(bool main) {
   uptr stk_addr = 0;
   uptr stk_size = 0;