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<class T, size_t N>
15 //     span(T (&)[N]) -> span<T, N>;
16 //
17 //   template<class T, size_t N>
18 //     span(array<T, N>&) -> span<T, N>;
19 //
20 //   template<class T, size_t N>
21 //     span(const array<T, N>&) -> span<const T, N>;
22 //
23 //   template<class Container>
24 //     span(Container&) -> span<typename Container::value_type>;
25 //
26 //   template<class Container>
27 //     span(const Container&) -> span<const typename Container::value_type>;
28 
29 
30 
31 #include <span>
32 #include <algorithm>
33 #include <array>
34 #include <cassert>
35 #include <string>
36 #include <type_traits>
37 
38 #include "test_macros.h"
39 
40 // std::array is explicitly allowed to be initialized with A a = { init-list };.
41 // Disable the missing braces warning for this reason.
42 #include "disable_missing_braces_warning.h"
43 
main()44 int main ()
45 {
46     {
47     int arr[] = {1,2,3};
48     std::span s{arr};
49     using S = decltype(s);
50     ASSERT_SAME_TYPE(S, std::span<int, 3>);
51     assert((std::equal(std::begin(arr), std::end(arr), s.begin(), s.end())));
52     }
53 
54     {
55     std::array<double, 4> arr = {1.0, 2.0, 3.0, 4.0};
56     std::span s{arr};
57     using S = decltype(s);
58     ASSERT_SAME_TYPE(S, std::span<double, 4>);
59     assert((std::equal(std::begin(arr), std::end(arr), s.begin(), s.end())));
60     }
61 
62     {
63     const std::array<long, 5> arr = {4, 5, 6, 7, 8};
64     std::span s{arr};
65     using S = decltype(s);
66     ASSERT_SAME_TYPE(S, std::span<const long, 5>);
67     assert((std::equal(std::begin(arr), std::end(arr), s.begin(), s.end())));
68     }
69 
70     {
71     std::string str{"ABCDE"};
72     std::span s{str};
73     using S = decltype(s);
74     ASSERT_SAME_TYPE(S, std::span<char>);
75     assert((size_t)s.size() == str.size());
76     assert((std::equal(s.begin(), s.end(), std::begin(s), std::end(s))));
77     }
78 
79     {
80     const std::string str{"QWERTYUIOP"};
81     std::span s{str};
82     using S = decltype(s);
83     ASSERT_SAME_TYPE(S, std::span<const char>);
84     assert((size_t)s.size() == str.size());
85     assert((std::equal(s.begin(), s.end(), std::begin(s), std::end(s))));
86     }
87 }
88