1 // -*- C++ -*-
2 //===------------------------------ span ---------------------------------===//
3 //
4 // The LLVM Compiler Infrastructure
5 //
6 // This file is dual licensed under the MIT and the University of Illinois Open
7 // Source Licenses. See LICENSE.TXT for details.
8 //
9 //===---------------------------------------------------------------------===//
10 // UNSUPPORTED: c++98, c++03, c++11, c++14, c++17
11
12 // <span>
13
14 // template <class ElementType, ptrdiff_t Extent>
15 // span<const byte,
16 // Extent == dynamic_extent
17 // ? dynamic_extent
18 // : static_cast<ptrdiff_t>(sizeof(ElementType)) * Extent>
19 // as_bytes(span<ElementType, Extent> s) noexcept;
20
21
22 #include <span>
23 #include <cassert>
24 #include <string>
25
26 #include "test_macros.h"
27
28 template<typename Span>
testRuntimeSpan(Span sp)29 void testRuntimeSpan(Span sp)
30 {
31 ASSERT_NOEXCEPT(std::as_bytes(sp));
32
33 auto spBytes = std::as_bytes(sp);
34 using SB = decltype(spBytes);
35 ASSERT_SAME_TYPE(const std::byte, typename SB::element_type);
36
37 if (sp.extent == std::dynamic_extent)
38 assert(spBytes.extent == std::dynamic_extent);
39 else
40 assert(spBytes.extent == static_cast<std::ptrdiff_t>(sizeof(typename Span::element_type)) * sp.extent);
41
42 assert((void *) spBytes.data() == (void *) sp.data());
43 assert(spBytes.size() == sp.size_bytes());
44 }
45
46 struct A{};
47 int iArr2[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
48
main()49 int main ()
50 {
51 testRuntimeSpan(std::span<int> ());
52 testRuntimeSpan(std::span<long> ());
53 testRuntimeSpan(std::span<double> ());
54 testRuntimeSpan(std::span<A> ());
55 testRuntimeSpan(std::span<std::string>());
56
57 testRuntimeSpan(std::span<int, 0> ());
58 testRuntimeSpan(std::span<long, 0> ());
59 testRuntimeSpan(std::span<double, 0> ());
60 testRuntimeSpan(std::span<A, 0> ());
61 testRuntimeSpan(std::span<std::string, 0>());
62
63 testRuntimeSpan(std::span<int>(iArr2, 1));
64 testRuntimeSpan(std::span<int>(iArr2, 2));
65 testRuntimeSpan(std::span<int>(iArr2, 3));
66 testRuntimeSpan(std::span<int>(iArr2, 4));
67 testRuntimeSpan(std::span<int>(iArr2, 5));
68
69 testRuntimeSpan(std::span<int, 1>(iArr2 + 5, 1));
70 testRuntimeSpan(std::span<int, 2>(iArr2 + 4, 2));
71 testRuntimeSpan(std::span<int, 3>(iArr2 + 3, 3));
72 testRuntimeSpan(std::span<int, 4>(iArr2 + 2, 4));
73 testRuntimeSpan(std::span<int, 5>(iArr2 + 1, 5));
74
75 std::string s;
76 testRuntimeSpan(std::span<std::string>(&s, (std::ptrdiff_t) 0));
77 testRuntimeSpan(std::span<std::string>(&s, 1));
78 }
79