• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 // constexpr index_type size_bytes() const noexcept;
15 //
16 //  Effects: Equivalent to: return size() * sizeof(element_type);
17 
18 
19 #include <span>
20 #include <cassert>
21 #include <string>
22 
23 #include "test_macros.h"
24 
25 
26 template <typename Span>
testConstexprSpan(Span sp,ptrdiff_t sz)27 constexpr bool testConstexprSpan(Span sp, ptrdiff_t sz)
28 {
29     ASSERT_NOEXCEPT(sp.size_bytes());
30     return (size_t) sp.size_bytes() == sz * sizeof(typename Span::element_type);
31 }
32 
33 
34 template <typename Span>
testRuntimeSpan(Span sp,ptrdiff_t sz)35 void testRuntimeSpan(Span sp, ptrdiff_t sz)
36 {
37     ASSERT_NOEXCEPT(sp.size_bytes());
38     assert((size_t) sp.size_bytes() == sz * sizeof(typename Span::element_type));
39 }
40 
41 struct A{};
42 constexpr int iArr1[] = { 0,  1,  2,  3,  4,  5,  6,  7,  8,  9};
43           int iArr2[] = {10, 11, 12, 13, 14, 15, 16, 17, 18, 19};
44 
main()45 int main ()
46 {
47     static_assert(testConstexprSpan(std::span<int>(), 0),            "");
48     static_assert(testConstexprSpan(std::span<long>(), 0),           "");
49     static_assert(testConstexprSpan(std::span<double>(), 0),         "");
50     static_assert(testConstexprSpan(std::span<A>(), 0),              "");
51     static_assert(testConstexprSpan(std::span<std::string>(), 0),    "");
52 
53     static_assert(testConstexprSpan(std::span<int, 0>(), 0),         "");
54     static_assert(testConstexprSpan(std::span<long, 0>(), 0),        "");
55     static_assert(testConstexprSpan(std::span<double, 0>(), 0),      "");
56     static_assert(testConstexprSpan(std::span<A, 0>(), 0),           "");
57     static_assert(testConstexprSpan(std::span<std::string, 0>(), 0), "");
58 
59     static_assert(testConstexprSpan(std::span<const int>(iArr1, 1), 1),    "");
60     static_assert(testConstexprSpan(std::span<const int>(iArr1, 2), 2),    "");
61     static_assert(testConstexprSpan(std::span<const int>(iArr1, 3), 3),    "");
62     static_assert(testConstexprSpan(std::span<const int>(iArr1, 4), 4),    "");
63     static_assert(testConstexprSpan(std::span<const int>(iArr1, 5), 5),    "");
64 
65     testRuntimeSpan(std::span<int>        (), 0);
66     testRuntimeSpan(std::span<long>       (), 0);
67     testRuntimeSpan(std::span<double>     (), 0);
68     testRuntimeSpan(std::span<A>          (), 0);
69     testRuntimeSpan(std::span<std::string>(), 0);
70 
71     testRuntimeSpan(std::span<int, 0>        (), 0);
72     testRuntimeSpan(std::span<long, 0>       (), 0);
73     testRuntimeSpan(std::span<double, 0>     (), 0);
74     testRuntimeSpan(std::span<A, 0>          (), 0);
75     testRuntimeSpan(std::span<std::string, 0>(), 0);
76 
77     testRuntimeSpan(std::span<int>(iArr2, 1), 1);
78     testRuntimeSpan(std::span<int>(iArr2, 2), 2);
79     testRuntimeSpan(std::span<int>(iArr2, 3), 3);
80     testRuntimeSpan(std::span<int>(iArr2, 4), 4);
81     testRuntimeSpan(std::span<int>(iArr2, 5), 5);
82 
83     testRuntimeSpan(std::span<int, 1>(iArr2 + 5, 1), 1);
84     testRuntimeSpan(std::span<int, 2>(iArr2 + 4, 2), 2);
85     testRuntimeSpan(std::span<int, 3>(iArr2 + 3, 3), 3);
86     testRuntimeSpan(std::span<int, 4>(iArr2 + 2, 4), 4);
87     testRuntimeSpan(std::span<int, 5>(iArr2 + 1, 5), 5);
88 
89     std::string s;
90     testRuntimeSpan(std::span<std::string>(&s, (std::ptrdiff_t) 0), 0);
91     testRuntimeSpan(std::span<std::string>(&s, 1), 1);
92 }
93