Index: libcxx/test/std/containers/sequences/vector/compare.fail.cpp =================================================================== --- /dev/null +++ libcxx/test/std/containers/sequences/vector/compare.fail.cpp @@ -0,0 +1,54 @@ +//===----------------------------------------------------------------------===// +// +// 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 "test_comparisons.h" +#include "test_macros.h" + +int main(int, char**) { + { + std::vector> c; + // expected-error@*:* {{invalid operands to binary expression}} + TEST_IGNORE_NODISCARD (c == c); + } + { + std::vector> c; + // expected-error@*:* {{invalid operands to binary expression}} + TEST_IGNORE_NODISCARD (c != c); + } + { + std::vector> c; + // expected-error@*:* {{invalid operands to binary expression}} + TEST_IGNORE_NODISCARD (c < c); + } + { + std::vector> c; + // expected-error@*:* {{invalid operands to binary expression}} + TEST_IGNORE_NODISCARD (c <= c); + } + { + std::vector> c; + // expected-error@*:* {{invalid operands to binary expression}} + TEST_IGNORE_NODISCARD (c > c); + } + { + std::vector> c; + // expected-error@*:* {{invalid operands to binary expression}} + TEST_IGNORE_NODISCARD (c >= c); + } +} Index: libcxx/test/std/containers/sequences/vector/compare.pass.cpp =================================================================== --- /dev/null +++ libcxx/test/std/containers/sequences/vector/compare.pass.cpp @@ -0,0 +1,97 @@ +//===----------------------------------------------------------------------===// +// +// 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}, c2{2}; + assert(testComparisons6(c1, c2, false, true)); + } + { + const std::vector c1, c2{2}; + assert(testComparisons6(c1, c2, false, true)); + } + { + const std::vector c1{1, 2, 1}, c2{1, 2, 2}; + assert(testComparisons6(c1, c2, false, true)); + } + { + const std::vector c1{3, 2, 3}, c2{3, 1, 3}; + assert(testComparisons6(c1, c2, false, false)); + } + { + const std::vector c1{1, 2}, c2{1, 2, 0}; + assert(testComparisons6(c1, c2, false, true)); + } + { + const std::vector c1{1, 2, 0}, c2{3}; + assert(testComparisons6(c1, c2, false, true)); + } + { + const std::vector c1, c2; + assert(testComparisons6(c1, c2, true, false)); + } + { + const std::vector c1 = {{1, 2}}; + const std::vector c2 = {{1, 2}}; + 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{{2, 2}}; + assert(testComparisons6(c1, c2, false, true)); + } + { + const std::vector c1{{1, 1}, {2, 2}, {2, 0}}; + const std::vector c2{{1, 1}, {2, 2}, {1, 0}}; + assert(testComparisons6(c1, c2, false, false)); + } + { + const std::vector c1{{3, 3}, {3, 3}, {3, 3}}; + const std::vector c2{{3, 3}, {2, 2}, {3, 3}}; + assert(testComparisons6(c1, c2, false, false)); + } + { + const std::vector c1{{1, 1}, {2, 2}}; + const std::vector c2{{1, 1}, {2, 2}, {0, 0}}; + assert(testComparisons6(c1, c2, false, true)); + } + { + const std::vector c1 = {{1, 1}, {2, 2}, {0, 0}}; + const std::vector c2 = {{3, 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())); + } +} Index: libcxx/test/support/test_comparisons.h =================================================================== --- libcxx/test/support/test_comparisons.h +++ 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,31 @@ static_assert((std::is_convertible() != std::declval()), bool>::value), ""); } +template +struct EqCompOnly { + friend bool operator==(const EqCompOnly&, const EqCompOnly&) { + return true; + } +}; + +template +struct LessCompOnly { + friend bool operator<(const LessCompOnly&, const LessCompOnly&) { + return true; + } +}; + +struct LessAndEqComp { + int first; + int second; + + friend bool operator<(const LessAndEqComp& lhs, const LessAndEqComp& rhs) { + return (lhs.first == rhs.first) ? (lhs.second < rhs.second) : (lhs.first < rhs.first); + } + + friend bool operator==(const LessAndEqComp& lhs, const LessAndEqComp& rhs) { + return (lhs.first == rhs.first) && (lhs.second == rhs.second); + } +}; + #endif // TEST_COMPARISONS_H