| 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 | // class std::ranges::subrange; |
| 12 | |
| 13 | #include <cassert> |
| 14 | #include <ranges> |
| 15 | #include <utility> |
| 16 | |
| 17 | #include "test_macros.h" |
| 18 | |
| 19 | constexpr void test_sized_subrange() |
| 20 | { |
| 21 | int a[4] = {1,2,3,4}; |
| 22 | auto r = std::ranges::subrange(a, a+4); |
| 23 | assert(std::ranges::sized_range<decltype(r)>); |
| 24 | { |
| 25 | auto [first, last] = r; |
| 26 | assert(first == a); |
| 27 | assert(last == a+4); |
| 28 | } |
| 29 | { |
| 30 | auto [first, last] = std::move(r); |
| 31 | assert(first == a); |
| 32 | assert(last == a+4); |
| 33 | } |
| 34 | { |
| 35 | auto [first, last] = std::as_const(r); |
| 36 | assert(first == a); |
| 37 | assert(last == a+4); |
| 38 | } |
| 39 | { |
| 40 | auto [first, last] = std::move(std::as_const(r)); |
| 41 | assert(first == a); |
| 42 | assert(last == a+4); |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | constexpr void test_unsized_subrange() |
| 47 | { |
| 48 | int a[4] = {1,2,3,4}; |
| 49 | auto r = std::ranges::subrange(a, std::unreachable_sentinel); |
| 50 | assert(!std::ranges::sized_range<decltype(r)>); |
| 51 | { |
| 52 | auto [first, last] = r; |
| 53 | assert(first == a); |
| 54 | ASSERT_SAME_TYPE(decltype(last), std::unreachable_sentinel_t); |
| 55 | } |
| 56 | { |
| 57 | auto [first, last] = std::move(r); |
| 58 | assert(first == a); |
| 59 | ASSERT_SAME_TYPE(decltype(last), std::unreachable_sentinel_t); |
| 60 | } |
| 61 | { |
| 62 | auto [first, last] = std::as_const(r); |
| 63 | assert(first == a); |
| 64 | ASSERT_SAME_TYPE(decltype(last), std::unreachable_sentinel_t); |
| 65 | } |
| 66 | { |
| 67 | auto [first, last] = std::move(std::as_const(r)); |
| 68 | assert(first == a); |
| 69 | ASSERT_SAME_TYPE(decltype(last), std::unreachable_sentinel_t); |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | constexpr void test_copies_not_originals() |
| 74 | { |
| 75 | int a[4] = {1,2,3,4}; |
| 76 | { |
| 77 | auto r = std::ranges::subrange(a, a+4); |
| 78 | auto&& [first, last] = r; |
| 79 | ASSERT_SAME_TYPE(decltype(first), int*); |
| 80 | ASSERT_SAME_TYPE(decltype(last), int*); |
| 81 | first = a+2; |
| 82 | last = a+2; |
| 83 | assert(r.begin() == a); |
| 84 | assert(r.end() == a+4); |
| 85 | } |
| 86 | { |
| 87 | const auto r = std::ranges::subrange(a, a+4); |
| 88 | auto&& [first, last] = r; |
| 89 | ASSERT_SAME_TYPE(decltype(first), int*); |
| 90 | ASSERT_SAME_TYPE(decltype(last), int*); |
| 91 | first = a+2; |
| 92 | last = a+2; |
| 93 | assert(r.begin() == a); |
| 94 | assert(r.end() == a+4); |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | constexpr bool test() |
| 99 | { |
| 100 | test_sized_subrange(); |
| 101 | test_unsized_subrange(); |
| 102 | test_copies_not_originals(); |
| 103 | return true; |
| 104 | } |
| 105 | |
| 106 | int main(int, char**) |
| 107 | { |
| 108 | test(); |
| 109 | static_assert(test()); |
| 110 | |
| 111 | return 0; |
| 112 | } |
| 113 | |