Skip to content

Commit 244ab9b

Browse files
committed
test view ctor of views
1 parent e4fd646 commit 244ab9b

File tree

1 file changed

+89
-0
lines changed

1 file changed

+89
-0
lines changed
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
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 explicit cartesian_product_view(First first_base, Vs... bases);
12+
13+
#include <ranges>
14+
#include <tuple>
15+
16+
#include "../range.zip/types.h"
17+
18+
template <class T>
19+
void conversion_test(T);
20+
21+
template <class T, class... Args>
22+
concept implicitly_constructible_from = requires(Args&&... args) { conversion_test<T>({std::move(args)...}); };
23+
24+
// test constructor is explicit
25+
static_assert(std::constructible_from<std::ranges::cartesian_product_view<SimpleCommon>, SimpleCommon>);
26+
static_assert(!implicitly_constructible_from<std::ranges::cartesian_product_view<SimpleCommon>, SimpleCommon>);
27+
28+
static_assert(std::constructible_from<std::ranges::cartesian_product_view<SimpleCommon, SimpleCommon>, SimpleCommon, SimpleCommon>);
29+
static_assert(
30+
!implicitly_constructible_from<std::ranges::cartesian_product_view<SimpleCommon, SimpleCommon>, SimpleCommon, SimpleCommon>);
31+
32+
struct MoveAwareView : std::ranges::view_base {
33+
int moves = 0;
34+
constexpr MoveAwareView() = default;
35+
constexpr MoveAwareView(MoveAwareView&& other) : moves(other.moves + 1) { other.moves = 1; }
36+
constexpr MoveAwareView& operator=(MoveAwareView&& other) {
37+
moves = other.moves + 1;
38+
other.moves = 0;
39+
return *this;
40+
}
41+
constexpr const int* begin() const { return &moves; }
42+
constexpr const int* end() const { return &moves + 1; }
43+
};
44+
45+
template <class View1, class View2>
46+
constexpr void constructorTest(auto&& buffer1, auto&& buffer2) {
47+
std::ranges::cartesian_product_view v{View1{buffer1}, View2{buffer2}};
48+
auto [i, j] = *v.begin();
49+
assert(i == buffer1[0]);
50+
assert(j == buffer2[0]);
51+
}
52+
53+
constexpr bool test() {
54+
55+
int buffer[] = {1, 2, 3, 4, 5, 6, 7, 8};
56+
int buffer2[] = {9, 8, 7, 6};
57+
58+
{ // constructor from views
59+
std::ranges::cartesian_product_view v(SizedRandomAccessView{buffer}, std::views::iota(0), std::ranges::single_view(2.));
60+
assert(*v.begin() == std::make_tuple(1, 0, 2.0));
61+
}
62+
63+
{ // arguments are moved once
64+
MoveAwareView mv;
65+
std::ranges::cartesian_product_view v{std::move(mv), MoveAwareView{}};
66+
auto [numMoves1, numMoves2] = *v.begin();
67+
assert(numMoves1 == 2); // one move from the local variable to parameter, one move from parameter to member
68+
assert(numMoves2 == 1);
69+
}
70+
71+
{ // input and forward
72+
constructorTest<InputCommonView, ForwardSizedView>(buffer, buffer2);
73+
}
74+
75+
{ // bidi and random_access
76+
constructorTest<BidiCommonView, SizedRandomAccessView>(buffer, buffer2);
77+
}
78+
79+
{ // contiguous
80+
constructorTest<ContiguousCommonView, ContiguousCommonView>(buffer, buffer2);
81+
}
82+
83+
return true;
84+
}
85+
86+
int main() {
87+
test();
88+
static_assert(test());
89+
}

0 commit comments

Comments
 (0)