Skip to content

Commit b63d384

Browse files
committed
add tests
1 parent 6efbe3a commit b63d384

21 files changed

+1383
-13
lines changed

libcxx/test/std/ranges/range.adaptors/range.zip.transform/begin.pass.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@
1717

1818
#include <cassert>
1919
#include <concepts>
20-
#include <tuple>
21-
#include <utility>
2220

2321
#include "types.h"
2422

libcxx/test/std/ranges/range.adaptors/range.zip.transform/cpo.pass.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,10 @@
1212

1313
#include <ranges>
1414

15-
#include <array>
1615
#include <algorithm>
16+
#include <array>
1717
#include <cassert>
18-
#include <tuple>
1918
#include <type_traits>
20-
#include <utility>
2119

2220
struct NotMoveConstructible {
2321
NotMoveConstructible() = default;

libcxx/test/std/ranges/range.adaptors/range.zip.transform/ctad.compile.pass.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313

1414
#include <cassert>
1515
#include <ranges>
16-
#include <utility>
1716

1817
#include "types.h"
1918
struct Container {

libcxx/test/std/ranges/range.adaptors/range.zip.transform/ctor.default.pass.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414

1515
#include <cassert>
1616
#include <type_traits>
17-
#include <utility>
1817

1918
constexpr int buff[] = {1, 2, 3};
2019

libcxx/test/std/ranges/range.adaptors/range.zip.transform/ctor.views.pass.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
// constexpr explicit zip_transform_view(F, Views...)
1212

1313
#include <ranges>
14-
#include <tuple>
1514

1615
#include "types.h"
1716

libcxx/test/std/ranges/range.adaptors/range.zip.transform/end.pass.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
// regular_invocable<const F&, range_reference_t<const Views>...>;
1515

1616
#include <ranges>
17-
#include <tuple>
1817

1918
#include "types.h"
2019

Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
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+
// UNSUPPORTED: c++03, c++11, c++14, c++17, c++20
10+
11+
// constexpr iterator& operator+=(difference_type x) requires random_access_range<Base>;
12+
// constexpr iterator& operator-=(difference_type x) requires random_access_range<Base>;
13+
// friend constexpr iterator operator+(const iterator& i, difference_type n)
14+
// requires random_access_range<Base>;
15+
// friend constexpr iterator operator+(difference_type n, const iterator& i)
16+
// requires random_access_range<Base>;
17+
// friend constexpr iterator operator-(const iterator& i, difference_type n)
18+
// requires random_access_range<Base>;
19+
// friend constexpr difference_type operator-(const iterator& x, const iterator& y)
20+
// requires sized_sentinel_for<ziperator<Const>, ziperator<Const>>;
21+
22+
#include <ranges>
23+
24+
#include <array>
25+
#include <concepts>
26+
#include <functional>
27+
28+
#include "../types.h"
29+
30+
template <class T, class U>
31+
concept canPlusEqual = requires(T& t, U& u) { t += u; };
32+
33+
template <class T, class U>
34+
concept canPlus = requires(T& t, U& u) { t + u; };
35+
36+
template <class T, class U>
37+
concept canMinusEqual = requires(T& t, U& u) { t -= u; };
38+
39+
template <class T, class U>
40+
concept canMinus = requires(T& t, U& u) { t - u; };
41+
42+
constexpr bool test() {
43+
int buffer1[5] = {1, 2, 3, 4, 5};
44+
SizedRandomAccessView a{buffer1};
45+
static_assert(std::ranges::random_access_range<decltype(a)>);
46+
47+
std::array b{4.1, 3.2, 4.3, 0.1, 0.2};
48+
static_assert(std::ranges::contiguous_range<decltype(b)>);
49+
50+
{
51+
// operator+(x, n) and operator+=
52+
std::ranges::zip_transform_view v(MakeTuple{}, a, b);
53+
auto it1 = v.begin();
54+
using Iter = decltype(it1);
55+
56+
std::same_as<Iter> decltype(auto) it2 = it1 + 3;
57+
assert(*it2 == std::tuple(4, 0.1));
58+
59+
std::same_as<Iter> decltype(auto) it3 = 3 + it1;
60+
assert(*it3 == std::tuple(4, 0.1));
61+
62+
std::same_as<Iter&> decltype(auto) it1_ref = it1 += 3;
63+
assert(&it1_ref == &it1);
64+
assert(*it1_ref == std::tuple(4, 0.1));
65+
assert(*it1 == std::tuple(4, 0.1));
66+
67+
static_assert(canPlus<Iter, std::intptr_t>);
68+
static_assert(canPlusEqual<Iter, std::intptr_t>);
69+
}
70+
71+
{
72+
// operator-(x, n) and operator-=
73+
std::ranges::zip_transform_view v(MakeTuple{}, a, b);
74+
auto it1 = v.end();
75+
using Iter = decltype(it1);
76+
77+
std::same_as<Iter> decltype(auto) it2 = it1 - 3;
78+
assert(*it2 == std::tuple(3, 4.3));
79+
80+
std::same_as<Iter&> decltype(auto) it1_ref = it1 -= 3;
81+
assert(&it1_ref == &it1);
82+
assert(*it1_ref == std::tuple(3, 4.3));
83+
assert(*it1 == std::tuple(3, 4.3));
84+
85+
static_assert(canMinusEqual<Iter, std::intptr_t>);
86+
static_assert(canMinus<Iter, std::intptr_t>);
87+
}
88+
89+
{
90+
// operator-(x, y)
91+
std::ranges::zip_transform_view v(MakeTuple{}, a, b);
92+
assert((v.end() - v.begin()) == 5);
93+
94+
auto it1 = v.begin() + 2;
95+
auto it2 = v.end() - 1;
96+
97+
using Iter = decltype(it1);
98+
99+
std::same_as<std::iter_difference_t<Iter>> decltype(auto) n = it1 - it2;
100+
assert(n == -2);
101+
}
102+
103+
{
104+
// One of the ranges is not random access
105+
std::ranges::zip_transform_view v(MakeTuple{}, a, b, ForwardSizedView{buffer1});
106+
auto it1 = v.begin();
107+
using Iter = decltype(it1);
108+
static_assert(!canPlus<Iter, std::intptr_t>);
109+
static_assert(!canPlus<std::intptr_t, Iter>);
110+
static_assert(!canPlusEqual<Iter, std::intptr_t>);
111+
static_assert(!canMinus<Iter, std::intptr_t>);
112+
static_assert(canMinus<Iter, Iter>);
113+
static_assert(!canMinusEqual<Iter, std::intptr_t>);
114+
115+
auto it2 = ++v.begin();
116+
assert((it2 - it1) == 1);
117+
}
118+
119+
{
120+
// One of the ranges does not have sized sentinel
121+
std::ranges::zip_transform_view v(MakeTuple{}, a, b, InputCommonView{buffer1});
122+
using Iter = decltype(v.begin());
123+
static_assert(!canPlus<Iter, std::intptr_t>);
124+
static_assert(!canPlus<std::intptr_t, Iter>);
125+
static_assert(!canPlusEqual<Iter, std::intptr_t>);
126+
static_assert(!canMinus<Iter, std::intptr_t>);
127+
static_assert(!canMinus<Iter, Iter>);
128+
static_assert(!canMinusEqual<Iter, std::intptr_t>);
129+
}
130+
131+
return true;
132+
}
133+
134+
int main(int, char**) {
135+
test();
136+
static_assert(test());
137+
138+
return 0;
139+
}
Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
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+
// UNSUPPORTED: c++03, c++11, c++14, c++17, c++20
10+
11+
// friend constexpr bool operator==(const iterator& x, const iterator& y)
12+
// requires equality_comparable<ziperator<Const>>;
13+
14+
// friend constexpr auto operator<=>(const iterator& x, const iterator& y)
15+
// requires random_access_range<Base>;
16+
17+
#include <ranges>
18+
19+
#include <compare>
20+
21+
#include "test_iterators.h"
22+
#include "../types.h"
23+
24+
constexpr void compareOperatorTest(auto&& iter1, auto&& iter2) {
25+
assert(!(iter1 < iter1));
26+
assert(iter1 < iter2);
27+
assert(!(iter2 < iter1));
28+
assert(iter1 <= iter1);
29+
assert(iter1 <= iter2);
30+
assert(!(iter2 <= iter1));
31+
assert(!(iter1 > iter1));
32+
assert(!(iter1 > iter2));
33+
assert(iter2 > iter1);
34+
assert(iter1 >= iter1);
35+
assert(!(iter1 >= iter2));
36+
assert(iter2 >= iter1);
37+
assert(iter1 == iter1);
38+
assert(!(iter1 == iter2));
39+
assert(iter2 == iter2);
40+
assert(!(iter1 != iter1));
41+
assert(iter1 != iter2);
42+
assert(!(iter2 != iter2));
43+
}
44+
45+
constexpr void inequalityOperatorsDoNotExistTest(auto&& iter1, auto&& iter2) {
46+
using Iter1 = decltype(iter1);
47+
using Iter2 = decltype(iter2);
48+
static_assert(!std::is_invocable_v<std::less<>, Iter1, Iter2>);
49+
static_assert(!std::is_invocable_v<std::less_equal<>, Iter1, Iter2>);
50+
static_assert(!std::is_invocable_v<std::greater<>, Iter1, Iter2>);
51+
static_assert(!std::is_invocable_v<std::greater_equal<>, Iter1, Iter2>);
52+
}
53+
54+
constexpr bool test() {
55+
{
56+
// Test a new-school iterator with operator<=>; the iterator should also have operator<=>.
57+
using It = three_way_contiguous_iterator<int*>;
58+
using SubRange = std::ranges::subrange<It>;
59+
static_assert(std::three_way_comparable<It>);
60+
61+
int a[] = {1, 2, 3, 4};
62+
int b[] = {5, 6, 7, 8, 9};
63+
auto r = std::views::zip_transform(MakeTuple{}, SubRange(It(a), It(a + 4)), SubRange(It(b), It(b + 5)));
64+
auto iter1 = r.begin();
65+
auto iter2 = iter1 + 1;
66+
using Iter = decltype(iter1);
67+
static_assert(std::three_way_comparable<Iter>);
68+
compareOperatorTest(iter1, iter2);
69+
70+
assert((iter1 <=> iter2) == std::strong_ordering::less);
71+
assert((iter1 <=> iter1) == std::strong_ordering::equal);
72+
assert((iter2 <=> iter1) == std::strong_ordering::greater);
73+
}
74+
75+
{
76+
// Test an old-school iterator with no operator<=>; the transform iterator shouldn't have
77+
// operator<=> either.
78+
using It = random_access_iterator<int*>;
79+
using Subrange = std::ranges::subrange<It>;
80+
static_assert(!std::three_way_comparable<It>);
81+
82+
int a[] = {1, 2, 3, 4};
83+
int b[] = {5, 6, 7, 8, 9};
84+
auto r = std::views::zip_transform(MakeTuple{}, Subrange(It(a), It(a + 4)), Subrange(It(b), It(b + 5)));
85+
auto iter1 = r.begin();
86+
using Iter = decltype(iter1);
87+
#ifndef _LIBCPP_VERSION
88+
// libc++ hasn't implemented LWG-3692 "zip_transform_view::iterator's operator<=> is overconstrained"
89+
auto iter2 = iter1 + 1;
90+
91+
compareOperatorTest(iter1, iter2);
92+
static_assert(std::three_way_comparable<Iter>);
93+
assert((iter1 <=> iter2) == std::strong_ordering::less);
94+
assert((iter1 <=> iter1) == std::strong_ordering::equal);
95+
assert((iter2 <=> iter1) == std::strong_ordering::greater);
96+
#endif
97+
}
98+
99+
{
100+
// non random_access_range
101+
int buffer1[1] = {1};
102+
int buffer2[2] = {1, 2};
103+
104+
std::ranges::zip_transform_view v{MakeTuple{}, InputCommonView(buffer1), InputCommonView(buffer2)};
105+
using View = decltype(v);
106+
static_assert(!std::ranges::forward_range<View>);
107+
static_assert(std::ranges::input_range<View>);
108+
static_assert(std::ranges::common_range<View>);
109+
110+
auto it1 = v.begin();
111+
auto it2 = v.end();
112+
assert(it1 != it2);
113+
114+
++it1;
115+
assert(it1 == it2);
116+
117+
inequalityOperatorsDoNotExistTest(it1, it2);
118+
}
119+
120+
{
121+
// in this case sentinel is computed by getting each of the underlying sentinel, so only one
122+
// underlying iterator is comparing equal
123+
int buffer1[1] = {1};
124+
int buffer2[2] = {1, 2};
125+
std::ranges::zip_transform_view v{MakeTuple{}, ForwardSizedView(buffer1), ForwardSizedView(buffer2)};
126+
using View = decltype(v);
127+
static_assert(std::ranges::common_range<View>);
128+
static_assert(!std::ranges::bidirectional_range<View>);
129+
130+
auto it1 = v.begin();
131+
auto it2 = v.end();
132+
assert(it1 != it2);
133+
134+
++it1;
135+
// it1: <buffer1 + 1, buffer2 + 1>
136+
// it2: <buffer1 + 1, buffer2 + 2>
137+
assert(it1 == it2);
138+
139+
inequalityOperatorsDoNotExistTest(it1, it2);
140+
}
141+
142+
{
143+
// underlying iterator does not support ==
144+
using IterNoEqualView = BasicView<cpp20_input_iterator<int*>, sentinel_wrapper<cpp20_input_iterator<int*>>>;
145+
int buffer[] = {1};
146+
std::ranges::zip_transform_view r(MakeTuple{}, IterNoEqualView{buffer});
147+
auto it = r.begin();
148+
using Iter = decltype(it);
149+
static_assert(!std::invocable<std::equal_to<>, Iter, Iter>);
150+
inequalityOperatorsDoNotExistTest(it, it);
151+
}
152+
return true;
153+
}
154+
155+
int main(int, char**) {
156+
test();
157+
//static_assert(test());
158+
159+
return 0;
160+
}

0 commit comments

Comments
 (0)