Skip to content

Commit c87a4a4

Browse files
kboyarinovrarutyun
authored andcommitted
[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
1 parent 3085e67 commit c87a4a4

File tree

2 files changed

+135
-0
lines changed

2 files changed

+135
-0
lines changed
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
// <vector>
10+
11+
// bool operator==(const vector& lhs, const vector& rhs);
12+
// bool operator!=(const vector& lhs, const vector& rhs);
13+
// bool operator< (const vector& lhs, const vector& rhs);
14+
// bool operator<=(const vector& lhs, const vector& rhs);
15+
// bool operator> (const vector& lhs, const vector& rhs);
16+
// bool operator>=(const vector& lhs, const vector& rhs);
17+
18+
#include <vector>
19+
#include <cassert>
20+
21+
#include "test_comparisons.h"
22+
23+
int main(int, char**) {
24+
{
25+
const std::vector<int> c1, c2;
26+
assert(testComparisons6(c1, c2, true, false));
27+
}
28+
{
29+
const std::vector<int> c1(1, 1), c2(1, 2);
30+
assert(testComparisons6(c1, c2, false, true));
31+
}
32+
{
33+
const std::vector<int> c1, c2(1, 2);
34+
assert(testComparisons6(c1, c2, false, true));
35+
}
36+
{
37+
int items1[3] = {1, 2, 1};
38+
int items2[3] = {1, 2, 2};
39+
const std::vector<int> c1(items1, items1 + 3);
40+
const std::vector<int> c2(items2, items2 + 3);
41+
assert(testComparisons6(c1, c2, false, true));
42+
}
43+
{
44+
int items1[3] = {3, 2, 3};
45+
int items2[3] = {3, 1, 3};
46+
const std::vector<int> c1(items1, items1 + 3);
47+
const std::vector<int> c2(items2, items2 + 3);
48+
49+
assert(testComparisons6(c1, c2, false, false));
50+
}
51+
{
52+
int items1[2] = {1, 2};
53+
int items2[3] = {1, 2, 0};
54+
const std::vector<int> c1(items1, items1 + 2);
55+
const std::vector<int> c2(items2, items2 + 3);
56+
assert(testComparisons6(c1, c2, false, true));
57+
}
58+
{
59+
int items1[3] = {1, 2, 0};
60+
const std::vector<int> c1(items1, items1 + 3);
61+
const std::vector<int> c2(1, 3);
62+
assert(testComparisons6(c1, c2, false, true));
63+
}
64+
{
65+
const std::vector<LessAndEqComp> c1, c2;
66+
assert(testComparisons6(c1, c2, true, false));
67+
}
68+
{
69+
const std::vector<LessAndEqComp> c1(1, LessAndEqComp(1));
70+
const std::vector<LessAndEqComp> c2(1, LessAndEqComp(1));
71+
assert(testComparisons6(c1, c2, true, false));
72+
}
73+
{
74+
const std::vector<LessAndEqComp> c1(1, LessAndEqComp(1));
75+
const std::vector<LessAndEqComp> c2(1, LessAndEqComp(2));
76+
assert(testComparisons6(c1, c2, false, true));
77+
}
78+
{
79+
const std::vector<LessAndEqComp> c1;
80+
const std::vector<LessAndEqComp> c2(1, LessAndEqComp(2));
81+
assert(testComparisons6(c1, c2, false, true));
82+
}
83+
{
84+
LessAndEqComp items1[3] = {LessAndEqComp(1), LessAndEqComp(2), LessAndEqComp(2)};
85+
LessAndEqComp items2[3] = {LessAndEqComp(1), LessAndEqComp(2), LessAndEqComp(1)};
86+
const std::vector<LessAndEqComp> c1(items1, items1 + 3);
87+
const std::vector<LessAndEqComp> c2(items2, items2 + 3);
88+
assert(testComparisons6(c1, c2, false, false));
89+
}
90+
{
91+
LessAndEqComp items1[3] = {LessAndEqComp(3), LessAndEqComp(3), LessAndEqComp(3)};
92+
LessAndEqComp items2[3] = {LessAndEqComp(3), LessAndEqComp(2), LessAndEqComp(3)};
93+
const std::vector<LessAndEqComp> c1(items1, items1 + 3);
94+
const std::vector<LessAndEqComp> c2(items2, items2 + 3);
95+
assert(testComparisons6(c1, c2, false, false));
96+
}
97+
{
98+
LessAndEqComp items1[2] = {LessAndEqComp(1), LessAndEqComp(2)};
99+
LessAndEqComp items2[3] = {LessAndEqComp(1), LessAndEqComp(2), LessAndEqComp(0)};
100+
const std::vector<LessAndEqComp> c1(items1, items1 + 2);
101+
const std::vector<LessAndEqComp> c2(items2, items2 + 3);
102+
assert(testComparisons6(c1, c2, false, true));
103+
}
104+
{
105+
LessAndEqComp items1[3] = {LessAndEqComp(1), LessAndEqComp(2), LessAndEqComp(0)};
106+
const std::vector<LessAndEqComp> c1(items1, items1 + 3);
107+
const std::vector<LessAndEqComp> c2(1, LessAndEqComp(3));
108+
assert(testComparisons6(c1, c2, false, true));
109+
}
110+
{
111+
assert((std::vector<int>() == std::vector<int>()));
112+
assert(!(std::vector<int>() != std::vector<int>()));
113+
assert(!(std::vector<int>() < std::vector<int>()));
114+
assert((std::vector<int>() <= std::vector<int>()));
115+
assert(!(std::vector<int>() > std::vector<int>()));
116+
assert((std::vector<int>() >= std::vector<int>()));
117+
}
118+
119+
return 0;
120+
}

libcxx/test/support/test_comparisons.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,14 @@
1919
#define TEST_COMPARISONS_H
2020

2121
#include <type_traits>
22+
#include <cassert>
2223
#include "test_macros.h"
2324

2425
// Test all six comparison operations for sanity
2526
template <class T, class U = T>
2627
TEST_CONSTEXPR_CXX14 bool testComparisons6(const T& t1, const U& t2, bool isEqual, bool isLess)
2728
{
29+
assert(!(isEqual && isLess) && "isEqual and isLess cannot be both true");
2830
if (isEqual)
2931
{
3032
if (!(t1 == t2)) return false;
@@ -171,4 +173,17 @@ void AssertComparisons2ConvertibleToBool()
171173
static_assert((std::is_convertible<decltype(std::declval<const T&>() != std::declval<const U&>()), bool>::value), "");
172174
}
173175

176+
struct LessAndEqComp {
177+
int value;
178+
179+
LessAndEqComp(int v) : value(v) {}
180+
181+
friend bool operator<(const LessAndEqComp& lhs, const LessAndEqComp& rhs) {
182+
return lhs.value < rhs.value;
183+
}
184+
185+
friend bool operator==(const LessAndEqComp& lhs, const LessAndEqComp& rhs) {
186+
return lhs.value == rhs.value;
187+
}
188+
};
174189
#endif // TEST_COMPARISONS_H

0 commit comments

Comments
 (0)