| 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 | // UNSUPPORTED: c++03, c++11, c++14, c++17 |
| 9 | |
| 10 | // <span> |
| 11 | |
| 12 | // template <class ElementType, size_t Extent> |
| 13 | // span<const byte, |
| 14 | // Extent == dynamic_extent |
| 15 | // ? dynamic_extent |
| 16 | // : sizeof(ElementType) * Extent> |
| 17 | // as_bytes(span<ElementType, Extent> s) noexcept; |
| 18 | |
| 19 | #include <cassert> |
| 20 | #include <cstddef> |
| 21 | #include <span> |
| 22 | #include <string> |
| 23 | |
| 24 | #include "test_macros.h" |
| 25 | |
| 26 | template <typename Span> |
| 27 | void testRuntimeSpan(Span sp) { |
| 28 | ASSERT_NOEXCEPT(std::as_bytes(sp)); |
| 29 | |
| 30 | auto spBytes = std::as_bytes(sp); |
| 31 | using SB = decltype(spBytes); |
| 32 | ASSERT_SAME_TYPE(const std::byte, typename SB::element_type); |
| 33 | |
| 34 | if constexpr (sp.extent == std::dynamic_extent) |
| 35 | assert(spBytes.extent == std::dynamic_extent); |
| 36 | else |
| 37 | assert(spBytes.extent == sizeof(typename Span::element_type) * sp.extent); |
| 38 | |
| 39 | assert((void*)spBytes.data() == (void*)sp.data()); |
| 40 | assert(spBytes.size() == sp.size_bytes()); |
| 41 | } |
| 42 | |
| 43 | struct A {}; |
| 44 | int iArr2[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; |
| 45 | |
| 46 | int main(int, char**) { |
| 47 | testRuntimeSpan(std::span<int>()); |
| 48 | testRuntimeSpan(std::span<long>()); |
| 49 | testRuntimeSpan(std::span<double>()); |
| 50 | testRuntimeSpan(std::span<A>()); |
| 51 | testRuntimeSpan(std::span<std::string>()); |
| 52 | |
| 53 | testRuntimeSpan(std::span<int, 0>()); |
| 54 | testRuntimeSpan(std::span<long, 0>()); |
| 55 | testRuntimeSpan(std::span<double, 0>()); |
| 56 | testRuntimeSpan(std::span<A, 0>()); |
| 57 | testRuntimeSpan(std::span<std::string, 0>()); |
| 58 | |
| 59 | testRuntimeSpan(std::span<int>(iArr2, 1)); |
| 60 | testRuntimeSpan(std::span<int>(iArr2, 2)); |
| 61 | testRuntimeSpan(std::span<int>(iArr2, 3)); |
| 62 | testRuntimeSpan(std::span<int>(iArr2, 4)); |
| 63 | testRuntimeSpan(std::span<int>(iArr2, 5)); |
| 64 | |
| 65 | testRuntimeSpan(std::span<int, 1>(iArr2 + 5, 1)); |
| 66 | testRuntimeSpan(std::span<int, 2>(iArr2 + 4, 2)); |
| 67 | testRuntimeSpan(std::span<int, 3>(iArr2 + 3, 3)); |
| 68 | testRuntimeSpan(std::span<int, 4>(iArr2 + 2, 4)); |
| 69 | testRuntimeSpan(std::span<int, 5>(iArr2 + 1, 5)); |
| 70 | |
| 71 | std::string s; |
| 72 | testRuntimeSpan(std::span<std::string>(&s, (std::size_t)0)); |
| 73 | testRuntimeSpan(std::span<std::string>(&s, 1)); |
| 74 | |
| 75 | return 0; |
| 76 | } |
| 77 | |