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