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