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
10
11// views::iota
12
13#include <cassert>
14#include <concepts>
15#include <ranges>
16#include <type_traits>
17
18#include "test_macros.h"
19#include "types.h"
20
21template<class T, class U>
22constexpr void testType(U u) {
23 // Test that this generally does the right thing.
24 // Test with only one argument.
25 {
26 assert(*std::views::iota(T(0)).begin() == T(0));
27 }
28 {
29 const auto io = std::views::iota(T(10));
30 assert(*io.begin() == T(10));
31 }
32 // Test with two arguments.
33 {
34 assert(*std::views::iota(T(0), u).begin() == T(0));
35 }
36 {
37 const auto io = std::views::iota(T(10), u);
38 assert(*io.begin() == T(10));
39 }
40 // Test that we return the correct type.
41 {
42 ASSERT_SAME_TYPE(decltype(std::views::iota(T(10))), std::ranges::iota_view<T>);
43 ASSERT_SAME_TYPE(decltype(std::views::iota(T(10), u)), std::ranges::iota_view<T, U>);
44 }
45}
46
47struct X {};
48
49constexpr bool test() {
50 testType<SomeInt>(u: SomeInt(10));
51 testType<SomeInt>(u: IntComparableWith(SomeInt(10)));
52 testType<signed long>(u: IntComparableWith<signed long>(10));
53 testType<unsigned long>(u: IntComparableWith<unsigned long>(10));
54 testType<int>(u: IntComparableWith<int>(10));
55 testType<int>(u: int(10));
56 testType<unsigned>(u: unsigned(10));
57 testType<unsigned>(u: IntComparableWith<unsigned>(10));
58 testType<short>(u: short(10));
59 testType<short>(u: IntComparableWith<short>(10));
60 testType<unsigned short>(u: IntComparableWith<unsigned short>(10));
61
62 {
63 static_assert( std::is_invocable_v<decltype(std::views::iota), int>);
64 static_assert(!std::is_invocable_v<decltype(std::views::iota), X>);
65 static_assert( std::is_invocable_v<decltype(std::views::iota), int, int>);
66 static_assert(!std::is_invocable_v<decltype(std::views::iota), int, X>);
67 }
68 {
69 static_assert(std::same_as<decltype(std::views::iota), decltype(std::ranges::views::iota)>);
70 }
71
72 return true;
73}
74
75int main(int, char**) {
76 test();
77 static_assert(test());
78
79 return 0;
80}
81

source code of libcxx/test/std/ranges/range.factories/range.iota.view/views_iota.pass.cpp