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 --- /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 +// +//===----------------------------------------------------------------------===// + +// + +// 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 +#include + +#include "test_comparisons.h" + +int main(int, char**) { + { + const std::vector c1, c2; + assert(testComparisons6(c1, c2, true, false)); + } + { + const std::vector c1(1, 1), c2(1, 2); + assert(testComparisons6(c1, c2, false, true)); + } + { + const std::vector 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 c1(items1, items1 + 3); + const std::vector 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 c1(items1, items1 + 3); + const std::vector c2(items2, items2 + 3); + + assert(testComparisons6(c1, c2, false, false)); + } + { + int items1[2] = {1, 2}; + int items2[3] = {1, 2, 0}; + const std::vector c1(items1, items1 + 2); + const std::vector c2(items2, items2 + 3); + assert(testComparisons6(c1, c2, false, true)); + } + { + int items1[3] = {1, 2, 0}; + const std::vector c1(items1, items1 + 3); + const std::vector c2(1, 3); + assert(testComparisons6(c1, c2, false, true)); + } + { + const std::vector c1, c2; + assert(testComparisons6(c1, c2, true, false)); + } + { + const std::vector c1(1, LessAndEqComp(1)); + const std::vector c2(1, LessAndEqComp(1)); + assert(testComparisons6(c1, c2, true, false)); + } + { + const std::vector c1(1, LessAndEqComp(1)); + const std::vector c2(1, LessAndEqComp(2)); + assert(testComparisons6(c1, c2, false, true)); + } + { + const std::vector c1; + const std::vector 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 c1(items1, items1 + 3); + const std::vector 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 c1(items1, items1 + 3); + const std::vector 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 c1(items1, items1 + 2); + const std::vector c2(items2, items2 + 3); + assert(testComparisons6(c1, c2, false, true)); + } + { + LessAndEqComp items1[3] = {LessAndEqComp(1), LessAndEqComp(2), LessAndEqComp(0)}; + const std::vector c1(items1, items1 + 3); + const std::vector c2(1, LessAndEqComp(3)); + assert(testComparisons6(c1, c2, false, true)); + } + { + assert((std::vector() == std::vector())); + assert(!(std::vector() != std::vector())); + assert(!(std::vector() < std::vector())); + assert((std::vector() <= std::vector())); + assert(!(std::vector() > std::vector())); + assert((std::vector() >= std::vector())); + } + + return 0; +} diff --git a/libcxx/test/support/test_comparisons.h b/libcxx/test/support/test_comparisons.h --- a/libcxx/test/support/test_comparisons.h +++ b/libcxx/test/support/test_comparisons.h @@ -19,12 +19,14 @@ #define TEST_COMPARISONS_H #include +#include #include "test_macros.h" // Test all six comparison operations for sanity template 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 @@ static_assert((std::is_convertible() != std::declval()), 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