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