Skip to content
Snippets Groups Projects
Commit c87a4a46 authored by Konstantin Boyarinov's avatar Konstantin Boyarinov Committed by Ruslan Arutyunyan
Browse files

[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
parent 3085e678
No related branches found
No related tags found
No related merge requests found
//===----------------------------------------------------------------------===//
//
// 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;
}
......@@ -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
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment