| 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 |
// std::views::zip |
| 12 |
|
| 13 |
#include <ranges> |
| 14 |
|
| 15 |
#include <array> |
| 16 |
#include <cassert> |
| 17 |
#include <tuple> |
| 18 |
#include <type_traits> |
| 19 |
#include <utility> |
| 20 |
|
| 21 |
#include "types.h" |
| 22 |
|
| 23 |
static_assert(std::is_invocable_v<decltype((std::views::zip))>); |
| 24 |
static_assert(!std::is_invocable_v<decltype((std::views::zip)), int>); |
| 25 |
static_assert(std::is_invocable_v<decltype((std::views::zip)), SizedRandomAccessView>); |
| 26 |
static_assert( |
| 27 |
std::is_invocable_v<decltype((std::views::zip)), SizedRandomAccessView, std::ranges::iota_view<int, int>>); |
| 28 |
static_assert(!std::is_invocable_v<decltype((std::views::zip)), SizedRandomAccessView, int>); |
| 29 |
|
| 30 |
constexpr bool test() { |
| 31 |
{ |
| 32 |
// zip zero arguments |
| 33 |
auto v = std::views::zip(); |
| 34 |
assert(std::ranges::empty(v)); |
| 35 |
static_assert(std::is_same_v<decltype(v), std::ranges::empty_view<std::tuple<>>>); |
| 36 |
} |
| 37 |
|
| 38 |
{ |
| 39 |
// zip a view |
| 40 |
int buffer[8] = {1, 2, 3, 4, 5, 6, 7, 8}; |
| 41 |
std::same_as<std::ranges::zip_view<SizedRandomAccessView>> decltype(auto) v = |
| 42 |
std::views::zip(SizedRandomAccessView{buffer}); |
| 43 |
assert(std::ranges::size(v) == 8); |
| 44 |
static_assert(std::is_same_v<std::ranges::range_reference_t<decltype(v)>, std::tuple<int&>>); |
| 45 |
} |
| 46 |
|
| 47 |
{ |
| 48 |
// zip a viewable range |
| 49 |
std::array a{1, 2, 3}; |
| 50 |
std::same_as<std::ranges::zip_view<std::ranges::ref_view<std::array<int, 3>>>> decltype(auto) v = |
| 51 |
std::views::zip(a); |
| 52 |
assert(&(std::get<0>(*v.begin())) == &(a[0])); |
| 53 |
static_assert(std::is_same_v<std::ranges::range_reference_t<decltype(v)>, std::tuple<int&>>); |
| 54 |
} |
| 55 |
|
| 56 |
{ |
| 57 |
// zip the zip_view |
| 58 |
int buffer[8] = {1, 2, 3, 4, 5, 6, 7, 8}; |
| 59 |
std::same_as<std::ranges::zip_view<SizedRandomAccessView, SizedRandomAccessView>> decltype(auto) v = |
| 60 |
std::views::zip(SizedRandomAccessView{buffer}, SizedRandomAccessView{buffer}); |
| 61 |
|
| 62 |
std::same_as< |
| 63 |
std::ranges::zip_view<std::ranges::zip_view<SizedRandomAccessView, SizedRandomAccessView>>> decltype(auto) v2 = |
| 64 |
std::views::zip(v); |
| 65 |
|
| 66 |
#ifdef _LIBCPP_VERSION // libc++ doesn't implement P2165R4 yet |
| 67 |
static_assert(std::is_same_v<std::ranges::range_reference_t<decltype(v2)>, std::tuple<std::pair<int&, int&>>>); |
| 68 |
#else |
| 69 |
static_assert(std::is_same_v<std::ranges::range_reference_t<decltype(v2)>, std::tuple<std::tuple<int&, int&>>>); |
| 70 |
#endif |
| 71 |
} |
| 72 |
return true; |
| 73 |
} |
| 74 |
|
| 75 |
int main(int, char**) { |
| 76 |
test(); |
| 77 |
static_assert(test()); |
| 78 |
|
| 79 |
return 0; |
| 80 |
} |