Marshall Clow | 02e1651 | 2019-02-27 15:41:37 +0000 | [diff] [blame] | 1 | // -*- C++ -*- |
| 2 | //===------------------------------ span ---------------------------------===// |
| 3 | // |
| 4 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 5 | // See https://llvm.org/LICENSE.txt for license information. |
| 6 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 7 | // |
| 8 | //===---------------------------------------------------------------------===// |
| 9 | // UNSUPPORTED: c++98, c++03, c++11, c++14, c++17 |
| 10 | |
| 11 | // <span> |
| 12 | |
| 13 | // template <size_t _Ip, ElementType, size_t Extent> |
| 14 | // constexpr ElementType& get(span<ElementType, Extent> s) noexcept; |
| 15 | // |
| 16 | |
| 17 | |
| 18 | #include <span> |
| 19 | #include <cassert> |
| 20 | #include <string> |
| 21 | |
| 22 | #include "test_macros.h" |
| 23 | |
| 24 | |
| 25 | template <size_t Idx, typename Span> |
| 26 | constexpr bool testConstexprSpan(Span sp, typename Span::pointer ptr) |
| 27 | { |
| 28 | ASSERT_NOEXCEPT(std::get<Idx>(sp)); |
| 29 | return std::addressof(std::get<Idx>(sp)) == ptr; |
| 30 | } |
| 31 | |
| 32 | |
| 33 | template <size_t Idx, typename Span> |
| 34 | void testRuntimeSpan(Span sp, typename Span::pointer ptr) |
| 35 | { |
| 36 | ASSERT_NOEXCEPT(std::get<Idx>(sp)); |
| 37 | assert(std::addressof(std::get<Idx>(sp)) == ptr); |
| 38 | } |
| 39 | |
| 40 | struct A{}; |
| 41 | constexpr int iArr1[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; |
| 42 | int iArr2[] = {10, 11, 12, 13, 14, 15, 16, 17, 18, 19}; |
| 43 | |
| 44 | int main(int, char**) |
| 45 | { |
| 46 | |
| 47 | // static size |
| 48 | static_assert(testConstexprSpan<0>(std::span<const int, 4>(iArr1, 1), iArr1 + 0), ""); |
| 49 | static_assert(testConstexprSpan<1>(std::span<const int, 4>(iArr1, 2), iArr1 + 1), ""); |
| 50 | static_assert(testConstexprSpan<2>(std::span<const int, 4>(iArr1, 3), iArr1 + 2), ""); |
| 51 | static_assert(testConstexprSpan<3>(std::span<const int, 4>(iArr1, 4), iArr1 + 3), ""); |
| 52 | |
| 53 | static_assert(testConstexprSpan<0>(std::span<const int, 1>(iArr1 + 1, 1), iArr1 + 1), ""); |
| 54 | static_assert(testConstexprSpan<1>(std::span<const int, 2>(iArr1 + 2, 2), iArr1 + 3), ""); |
| 55 | static_assert(testConstexprSpan<2>(std::span<const int, 3>(iArr1 + 3, 3), iArr1 + 5), ""); |
| 56 | static_assert(testConstexprSpan<3>(std::span<const int, 4>(iArr1 + 4, 4), iArr1 + 7), ""); |
| 57 | |
| 58 | // static size |
| 59 | testRuntimeSpan<0>(std::span<int, 4>(iArr2, 4), iArr2); |
| 60 | testRuntimeSpan<1>(std::span<int, 4>(iArr2, 1), iArr2 + 1); |
| 61 | testRuntimeSpan<2>(std::span<int, 4>(iArr2, 2), iArr2 + 2); |
| 62 | testRuntimeSpan<3>(std::span<int, 4>(iArr2, 3), iArr2 + 3); |
| 63 | |
| 64 | testRuntimeSpan<0>(std::span<int, 4>(iArr2 + 1, 1), iArr2 + 1); |
| 65 | testRuntimeSpan<1>(std::span<int, 4>(iArr2 + 2, 2), iArr2 + 3); |
| 66 | testRuntimeSpan<2>(std::span<int, 4>(iArr2 + 3, 3), iArr2 + 5); |
| 67 | testRuntimeSpan<3>(std::span<int, 4>(iArr2 + 4, 4), iArr2 + 7); |
| 68 | |
| 69 | |
| 70 | std::string s; |
| 71 | testRuntimeSpan<0>(std::span<std::string, 1>(&s, 1), &s); |
| 72 | |
| 73 | |
| 74 | return 0; |
| 75 | } |