[libcxx-commits] [libcxx] c87a4a4 - [libc++][test][NFC] Add tests for std::vector comparisons

Ruslan Arutyunyan via libcxx-commits libcxx-commits at lists.llvm.org
Fri Oct 22 08:13:38 PDT 2021


Author: Konstantin Boyarinov
Date: 2021-10-22T18:11:04+03:00
New Revision: c87a4a46b2175bf0b00b9e1ffec5a7ce2e81df3b

URL: https://github.com/llvm/llvm-project/commit/c87a4a46b2175bf0b00b9e1ffec5a7ce2e81df3b
DIFF: https://github.com/llvm/llvm-project/commit/c87a4a46b2175bf0b00b9e1ffec5a7ce2e81df3b.diff

LOG: [libc++][test][NFC] Add tests for std::vector comparisons

Add missing tests for std::vector operator==, !=, <, <=, >, >=

Reviewed By: ldionne, rarutyun, Quuxplusone, Mordante, #libc

Differential Revision: https://reviews.llvm.org/D111738

Added: 
    libcxx/test/std/containers/sequences/vector/compare.pass.cpp

Modified: 
    libcxx/test/support/test_comparisons.h

Removed: 
    


################################################################################
diff  --git a/libcxx/test/std/containers/sequences/vector/compare.pass.cpp b/libcxx/test/std/containers/sequences/vector/compare.pass.cpp
new file mode 100644
index 0000000000000..47d85a33c02e1
--- /dev/null
+++ b/libcxx/test/std/containers/sequences/vector/compare.pass.cpp
@@ -0,0 +1,120 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+// <vector>
+
+// bool operator==(const vector& lhs, const vector& rhs);
+// bool operator!=(const vector& lhs, const vector& rhs);
+// bool operator< (const vector& lhs, const vector& rhs);
+// bool operator<=(const vector& lhs, const vector& rhs);
+// bool operator> (const vector& lhs, const vector& rhs);
+// bool operator>=(const vector& lhs, const vector& rhs);
+
+#include <vector>
+#include <cassert>
+
+#include "test_comparisons.h"
+
+int main(int, char**) {
+    {
+        const std::vector<int> c1, c2;
+        assert(testComparisons6(c1, c2, true, false));
+    }
+    {
+        const std::vector<int> c1(1, 1), c2(1, 2);
+        assert(testComparisons6(c1, c2, false, true));
+    }
+    {
+        const std::vector<int> c1, c2(1, 2);
+        assert(testComparisons6(c1, c2, false, true));
+    }
+    {
+        int items1[3] = {1, 2, 1};
+        int items2[3] = {1, 2, 2};
+        const std::vector<int> c1(items1, items1 + 3);
+        const std::vector<int> c2(items2, items2 + 3);
+        assert(testComparisons6(c1, c2, false, true));
+    }
+    {
+        int items1[3] = {3, 2, 3};
+        int items2[3] = {3, 1, 3};
+        const std::vector<int> c1(items1, items1 + 3);
+        const std::vector<int> c2(items2, items2 + 3);
+
+        assert(testComparisons6(c1, c2, false, false));
+    }
+    {
+        int items1[2] = {1, 2};
+        int items2[3] = {1, 2, 0};
+        const std::vector<int> c1(items1, items1 + 2);
+        const std::vector<int> c2(items2, items2 + 3);
+        assert(testComparisons6(c1, c2, false, true));
+    }
+    {
+        int items1[3] = {1, 2, 0};
+        const std::vector<int> c1(items1, items1 + 3);
+        const std::vector<int> c2(1, 3);
+        assert(testComparisons6(c1, c2, false, true));
+    }
+    {
+        const std::vector<LessAndEqComp> c1, c2;
+        assert(testComparisons6(c1, c2, true, false));
+    }
+    {
+        const std::vector<LessAndEqComp> c1(1, LessAndEqComp(1));
+        const std::vector<LessAndEqComp> c2(1, LessAndEqComp(1));
+        assert(testComparisons6(c1, c2, true, false));
+    }
+    {
+        const std::vector<LessAndEqComp> c1(1, LessAndEqComp(1));
+        const std::vector<LessAndEqComp> c2(1, LessAndEqComp(2));
+        assert(testComparisons6(c1, c2, false, true));
+    }
+    {
+        const std::vector<LessAndEqComp> c1;
+        const std::vector<LessAndEqComp> c2(1, LessAndEqComp(2));
+        assert(testComparisons6(c1, c2, false, true));
+    }
+    {
+        LessAndEqComp items1[3] = {LessAndEqComp(1), LessAndEqComp(2), LessAndEqComp(2)};
+        LessAndEqComp items2[3] = {LessAndEqComp(1), LessAndEqComp(2), LessAndEqComp(1)};
+        const std::vector<LessAndEqComp> c1(items1, items1 + 3);
+        const std::vector<LessAndEqComp> c2(items2, items2 + 3);
+        assert(testComparisons6(c1, c2, false, false));
+    }
+    {
+        LessAndEqComp items1[3] = {LessAndEqComp(3), LessAndEqComp(3), LessAndEqComp(3)};
+        LessAndEqComp items2[3] = {LessAndEqComp(3), LessAndEqComp(2), LessAndEqComp(3)};
+        const std::vector<LessAndEqComp> c1(items1, items1 + 3);
+        const std::vector<LessAndEqComp> c2(items2, items2 + 3);
+        assert(testComparisons6(c1, c2, false, false));
+    }
+    {
+        LessAndEqComp items1[2] = {LessAndEqComp(1), LessAndEqComp(2)};
+        LessAndEqComp items2[3] = {LessAndEqComp(1), LessAndEqComp(2), LessAndEqComp(0)};
+        const std::vector<LessAndEqComp> c1(items1, items1 + 2);
+        const std::vector<LessAndEqComp> c2(items2, items2 + 3);
+        assert(testComparisons6(c1, c2, false, true));
+    }
+    {
+        LessAndEqComp items1[3] = {LessAndEqComp(1), LessAndEqComp(2), LessAndEqComp(0)};
+        const std::vector<LessAndEqComp> c1(items1, items1 + 3);
+        const std::vector<LessAndEqComp> c2(1, LessAndEqComp(3));
+        assert(testComparisons6(c1, c2, false, true));
+    }
+    {
+        assert((std::vector<int>() == std::vector<int>()));
+        assert(!(std::vector<int>() != std::vector<int>()));
+        assert(!(std::vector<int>() < std::vector<int>()));
+        assert((std::vector<int>() <= std::vector<int>()));
+        assert(!(std::vector<int>() > std::vector<int>()));
+        assert((std::vector<int>() >= std::vector<int>()));
+    }
+
+    return 0;
+}

diff  --git a/libcxx/test/support/test_comparisons.h b/libcxx/test/support/test_comparisons.h
index 96a75039f3e28..9d666545abdf8 100644
--- a/libcxx/test/support/test_comparisons.h
+++ b/libcxx/test/support/test_comparisons.h
@@ -19,12 +19,14 @@
 #define TEST_COMPARISONS_H
 
 #include <type_traits>
+#include <cassert>
 #include "test_macros.h"
 
 //  Test all six comparison operations for sanity
 template <class T, class U = T>
 TEST_CONSTEXPR_CXX14 bool testComparisons6(const T& t1, const U& t2, bool isEqual, bool isLess)
 {
+    assert(!(isEqual && isLess) && "isEqual and isLess cannot be both true");
     if (isEqual)
         {
         if (!(t1 == t2)) return false;
@@ -171,4 +173,17 @@ void AssertComparisons2ConvertibleToBool()
     static_assert((std::is_convertible<decltype(std::declval<const T&>() != std::declval<const U&>()), bool>::value), "");
 }
 
+struct LessAndEqComp {
+  int value;
+
+  LessAndEqComp(int v) : value(v) {}
+
+  friend bool operator<(const LessAndEqComp& lhs, const LessAndEqComp& rhs) {
+    return lhs.value < rhs.value;
+  }
+
+  friend bool operator==(const LessAndEqComp& lhs, const LessAndEqComp& rhs) {
+    return lhs.value == rhs.value;
+  }
+};
 #endif // TEST_COMPARISONS_H


        


More information about the libcxx-commits mailing list