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 span(pointer first, pointer last);
15 // Requires: [first, last) shall be a valid range.
16 // If extent is not equal to dynamic_extent, then last - first shall be equal to extent.
17 //
18
19 #include <span>
20 #include <cassert>
21 #include <string>
22
23 #include "test_macros.h"
24
checkCV()25 void checkCV()
26 {
27 int arr[] = {1,2,3};
28 const int carr[] = {4,5,6};
29 volatile int varr[] = {7,8,9};
30 const volatile int cvarr[] = {1,3,5};
31
32 // Types the same (dynamic sized)
33 {
34 std::span< int> s1{ arr, arr + 3}; // a span< int> pointing at int.
35 std::span<const int> s2{ carr, carr + 3}; // a span<const int> pointing at const int.
36 std::span< volatile int> s3{ varr, varr + 3}; // a span< volatile int> pointing at volatile int.
37 std::span<const volatile int> s4{cvarr, cvarr + 3}; // a span<const volatile int> pointing at const volatile int.
38 assert(s1.size() + s2.size() + s3.size() + s4.size() == 12);
39 }
40
41 // Types the same (static sized)
42 {
43 std::span< int,3> s1{ arr, arr + 3}; // a span< int> pointing at int.
44 std::span<const int,3> s2{ carr, carr + 3}; // a span<const int> pointing at const int.
45 std::span< volatile int,3> s3{ varr, varr + 3}; // a span< volatile int> pointing at volatile int.
46 std::span<const volatile int,3> s4{cvarr, cvarr + 3}; // a span<const volatile int> pointing at const volatile int.
47 assert(s1.size() + s2.size() + s3.size() + s4.size() == 12);
48 }
49
50
51 // types different (dynamic sized)
52 {
53 std::span<const int> s1{ arr, arr + 3}; // a span<const int> pointing at int.
54 std::span< volatile int> s2{ arr, arr + 3}; // a span< volatile int> pointing at int.
55 std::span< volatile int> s3{ arr, arr + 3}; // a span< volatile int> pointing at const int.
56 std::span<const volatile int> s4{ arr, arr + 3}; // a span<const volatile int> pointing at int.
57 std::span<const volatile int> s5{carr, carr + 3}; // a span<const volatile int> pointing at const int.
58 std::span<const volatile int> s6{varr, varr + 3}; // a span<const volatile int> pointing at volatile int.
59 assert(s1.size() + s2.size() + s3.size() + s4.size() + s5.size() + s6.size() == 18);
60 }
61
62 // types different (static sized)
63 {
64 std::span<const int,3> s1{ arr, arr + 3}; // a span<const int> pointing at int.
65 std::span< volatile int,3> s2{ arr, arr + 3}; // a span< volatile int> pointing at int.
66 std::span< volatile int,3> s3{ arr, arr + 3}; // a span< volatile int> pointing at const int.
67 std::span<const volatile int,3> s4{ arr, arr + 3}; // a span<const volatile int> pointing at int.
68 std::span<const volatile int,3> s5{carr, carr + 3}; // a span<const volatile int> pointing at const int.
69 std::span<const volatile int,3> s6{varr, varr + 3}; // a span<const volatile int> pointing at volatile int.
70 assert(s1.size() + s2.size() + s3.size() + s4.size() + s5.size() + s6.size() == 18);
71 }
72 }
73
74
75 template <typename T>
testConstexprSpan()76 constexpr bool testConstexprSpan()
77 {
78 constexpr T val[2] = {};
79 std::span<const T> s1{val, val+2};
80 std::span<const T,2> s2{val, val+2};
81 return
82 s1.data() == &val[0] && s1.size() == 2
83 && s2.data() == &val[0] && s2.size() == 2;
84 }
85
86
87 template <typename T>
testRuntimeSpan()88 void testRuntimeSpan()
89 {
90 T val[2] = {};
91 std::span<T> s1{val, val+2};
92 std::span<T,2> s2{val, val+2};
93 assert(s1.data() == &val[0] && s1.size() == 2);
94 assert(s2.data() == &val[0] && s2.size() == 2);
95 }
96
97 struct A{};
98
main()99 int main ()
100 {
101 static_assert(testConstexprSpan<int>(), "");
102 static_assert(testConstexprSpan<long>(), "");
103 static_assert(testConstexprSpan<double>(), "");
104 static_assert(testConstexprSpan<A>(), "");
105
106 testRuntimeSpan<int>();
107 testRuntimeSpan<long>();
108 testRuntimeSpan<double>();
109 testRuntimeSpan<std::string>();
110 testRuntimeSpan<A>();
111
112 checkCV();
113 }
114