| 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 | // constexpr bool empty() const; |
| 12 | |
| 13 | #include <cassert> |
| 14 | #include <concepts> |
| 15 | #include <ranges> |
| 16 | #include <utility> |
| 17 | #include <vector> |
| 18 | |
| 19 | #include "types.h" |
| 20 | |
| 21 | template <typename R> |
| 22 | concept HasEmpty = requires(const R r) { |
| 23 | std::ranges::empty(r); |
| 24 | { r.empty() } -> std::same_as<bool>; |
| 25 | }; |
| 26 | |
| 27 | constexpr void test_empty_iota_sfinae() { |
| 28 | std::vector<int> ev; |
| 29 | |
| 30 | auto iv = std::views::iota(std::ranges::begin(ev), std::ranges::end(ev)); |
| 31 | |
| 32 | static_assert(HasEmpty<decltype(iv)>); |
| 33 | static_assert(HasEmpty<decltype(std::as_const(iv))>); |
| 34 | } |
| 35 | |
| 36 | constexpr void test_nonempty_iota_sfinae() { |
| 37 | // Default ctr |
| 38 | { |
| 39 | std::ranges::iota_view<Int42<DefaultTo42>> iv; |
| 40 | |
| 41 | static_assert(HasEmpty<decltype(iv)>); |
| 42 | } |
| 43 | // Value pass |
| 44 | { |
| 45 | std::ranges::iota_view<SomeInt> iv(SomeInt(94)); |
| 46 | |
| 47 | static_assert(HasEmpty<decltype(iv)>); |
| 48 | } |
| 49 | |
| 50 | { |
| 51 | std::vector<char> v; |
| 52 | auto it = std::back_inserter(x&: v); |
| 53 | auto iv = std::views::iota(it); |
| 54 | |
| 55 | static_assert(HasEmpty<decltype(iv)>); |
| 56 | } |
| 57 | { |
| 58 | std::vector<char> v{'b', 'a', 'b', 'a', 'z', 'm', 't'}; |
| 59 | auto it = std::back_inserter(x&: v); |
| 60 | auto iv = std::views::iota(it); |
| 61 | |
| 62 | static_assert(HasEmpty<decltype(iv)>); |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | constexpr void test_empty_iota() { |
| 67 | std::vector<int> ev; |
| 68 | |
| 69 | auto iv = std::views::iota(std::ranges::begin(ev), std::ranges::end(ev)); |
| 70 | |
| 71 | assert(iv.empty()); |
| 72 | assert(std::as_const(iv).empty()); |
| 73 | } |
| 74 | |
| 75 | constexpr void test_nonempty_iota() { |
| 76 | // Default ctr |
| 77 | { |
| 78 | std::ranges::iota_view<Int42<DefaultTo42>> iv; |
| 79 | |
| 80 | assert(!iv.empty()); |
| 81 | } |
| 82 | // Value pass |
| 83 | { |
| 84 | std::ranges::iota_view<SomeInt> iv(SomeInt(94)); |
| 85 | |
| 86 | assert(!iv.empty()); |
| 87 | } |
| 88 | |
| 89 | { |
| 90 | std::vector<char> v; |
| 91 | auto it = std::back_inserter(x&: v); |
| 92 | auto iv = std::views::iota(it); |
| 93 | |
| 94 | assert(!iv.empty()); |
| 95 | } |
| 96 | { |
| 97 | std::vector<char> v{'b', 'a', 'b', 'a', 'z', 'm', 't'}; |
| 98 | auto it = std::back_inserter(x&: v); |
| 99 | auto iv = std::views::iota(it); |
| 100 | |
| 101 | assert(!iv.empty()); |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | constexpr bool test() { |
| 106 | test_empty_iota(); |
| 107 | test_nonempty_iota(); |
| 108 | |
| 109 | return true; |
| 110 | } |
| 111 | |
| 112 | int main(int, char**) { |
| 113 | test(); |
| 114 | static_assert(test()); |
| 115 | |
| 116 | return 0; |
| 117 | } |
| 118 | |