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