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

source code of libcxx/test/std/containers/views/views.span/span.objectrep/as_writable_bytes.pass.cpp