Marshall Clow | 8a0794b | 2018-07-24 03:01:02 +0000 | [diff] [blame] | 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 | // template<size_t N> |
| 15 | // constexpr span(array<value_type, N>& arr) noexcept; |
| 16 | // template<size_t N> |
| 17 | // constexpr span(const array<value_type, N>& arr) noexcept; |
| 18 | // |
| 19 | // Remarks: These constructors shall not participate in overload resolution unless: |
| 20 | // — extent == dynamic_extent || N == extent is true, and |
| 21 | // — remove_pointer_t<decltype(data(arr))>(*)[] is convertible to ElementType(*)[]. |
| 22 | // |
| 23 | |
| 24 | |
| 25 | #include <span> |
| 26 | #include <cassert> |
| 27 | #include <string> |
| 28 | |
| 29 | #include "test_macros.h" |
| 30 | |
| 31 | |
| 32 | void checkCV() |
| 33 | { |
| 34 | std::array<int, 3> arr = {1,2,3}; |
| 35 | // STL says these are not cromulent |
| 36 | // std::array<const int,3> carr = {4,5,6}; |
| 37 | // std::array<volatile int, 3> varr = {7,8,9}; |
| 38 | // std::array<const volatile int, 3> cvarr = {1,3,5}; |
| 39 | |
| 40 | // Types the same (dynamic sized) |
| 41 | { |
| 42 | std::span< int> s1{ arr}; // a span< int> pointing at int. |
| 43 | } |
| 44 | |
| 45 | // Types the same (static sized) |
| 46 | { |
| 47 | std::span< int,3> s1{ arr}; // a span< int> pointing at int. |
| 48 | } |
| 49 | |
| 50 | |
| 51 | // types different (dynamic sized) |
| 52 | { |
| 53 | std::span<const int> s1{ arr}; // a span<const int> pointing at int. |
| 54 | std::span< volatile int> s2{ arr}; // a span< volatile int> pointing at int. |
| 55 | std::span< volatile int> s3{ arr}; // a span< volatile int> pointing at const int. |
| 56 | std::span<const volatile int> s4{ arr}; // a span<const volatile int> pointing at int. |
| 57 | } |
| 58 | |
| 59 | // types different (static sized) |
| 60 | { |
| 61 | std::span<const int,3> s1{ arr}; // a span<const int> pointing at int. |
| 62 | std::span< volatile int,3> s2{ arr}; // a span< volatile int> pointing at int. |
| 63 | std::span< volatile int,3> s3{ arr}; // a span< volatile int> pointing at const int. |
| 64 | std::span<const volatile int,3> s4{ arr}; // a span<const volatile int> pointing at int. |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | |
| 69 | template <typename T> |
| 70 | constexpr bool testConstexprSpan() |
| 71 | { |
| 72 | constexpr std::array<T,2> val = { T(), T() }; |
| 73 | ASSERT_NOEXCEPT(std::span<const T> {val}); |
| 74 | ASSERT_NOEXCEPT(std::span<const T, 2>{val}); |
| 75 | std::span<const T> s1{val}; |
| 76 | std::span<const T, 2> s2{val}; |
| 77 | return |
| 78 | s1.data() == &val[0] && s1.size() == 2 |
| 79 | && s2.data() == &val[0] && s2.size() == 2; |
| 80 | } |
| 81 | |
| 82 | |
| 83 | template <typename T> |
| 84 | void testRuntimeSpan() |
| 85 | { |
| 86 | std::array<T,2> val; |
| 87 | ASSERT_NOEXCEPT(std::span<T> {val}); |
| 88 | ASSERT_NOEXCEPT(std::span<T, 2>{val}); |
| 89 | std::span<T> s1{val}; |
| 90 | std::span<T, 2> s2{val}; |
| 91 | assert(s1.data() == &val[0] && s1.size() == 2); |
| 92 | assert(s2.data() == &val[0] && s2.size() == 2); |
| 93 | } |
| 94 | |
| 95 | struct A{}; |
| 96 | |
| 97 | int main () |
| 98 | { |
| 99 | static_assert(testConstexprSpan<int>(), ""); |
| 100 | static_assert(testConstexprSpan<long>(), ""); |
| 101 | static_assert(testConstexprSpan<double>(), ""); |
| 102 | static_assert(testConstexprSpan<A>(), ""); |
| 103 | |
| 104 | testRuntimeSpan<int>(); |
| 105 | testRuntimeSpan<long>(); |
| 106 | testRuntimeSpan<double>(); |
| 107 | testRuntimeSpan<std::string>(); |
| 108 | testRuntimeSpan<A>(); |
| 109 | |
| 110 | checkCV(); |
| 111 | } |