• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 // constexpr reference front() const noexcept;
14 //   Expects: empty() is false.
15 //   Effects: Equivalent to: return *data();
16 //
17 
18 
19 #include <span>
20 #include <cassert>
21 #include <string>
22 
23 #include "test_macros.h"
24 
25 
26 template <typename Span>
testConstexprSpan(Span sp)27 constexpr bool testConstexprSpan(Span sp)
28 {
29     LIBCPP_ASSERT(noexcept(sp.front()));
30     return std::addressof(sp.front()) == sp.data();
31 }
32 
33 
34 template <typename Span>
testRuntimeSpan(Span sp)35 void testRuntimeSpan(Span sp)
36 {
37     LIBCPP_ASSERT(noexcept(sp.front()));
38     assert(std::addressof(sp.front()) == sp.data());
39 }
40 
41 template <typename Span>
testEmptySpan(Span sp)42 void testEmptySpan(Span sp)
43 {
44     if (!sp.empty())
45         [[maybe_unused]] auto res = sp.front();
46 }
47 
48 struct A{};
49 constexpr int iArr1[] = { 0,  1,  2,  3,  4,  5,  6,  7,  8,  9};
50           int iArr2[] = {10, 11, 12, 13, 14, 15, 16, 17, 18, 19};
51 
main(int,char **)52 int main(int, char**)
53 {
54     static_assert(testConstexprSpan(std::span<const int>(iArr1, 1)), "");
55     static_assert(testConstexprSpan(std::span<const int>(iArr1, 2)), "");
56     static_assert(testConstexprSpan(std::span<const int>(iArr1, 3)), "");
57     static_assert(testConstexprSpan(std::span<const int>(iArr1, 4)), "");
58 
59     static_assert(testConstexprSpan(std::span<const int, 1>(iArr1, 1)), "");
60     static_assert(testConstexprSpan(std::span<const int, 2>(iArr1, 2)), "");
61     static_assert(testConstexprSpan(std::span<const int, 3>(iArr1, 3)), "");
62     static_assert(testConstexprSpan(std::span<const int, 4>(iArr1, 4)), "");
63 
64 
65     testRuntimeSpan(std::span<int>(iArr2, 1));
66     testRuntimeSpan(std::span<int>(iArr2, 2));
67     testRuntimeSpan(std::span<int>(iArr2, 3));
68     testRuntimeSpan(std::span<int>(iArr2, 4));
69 
70 
71     testRuntimeSpan(std::span<int, 1>(iArr2, 1));
72     testRuntimeSpan(std::span<int, 2>(iArr2, 2));
73     testRuntimeSpan(std::span<int, 3>(iArr2, 3));
74     testRuntimeSpan(std::span<int, 4>(iArr2, 4));
75 
76     std::string s;
77     testRuntimeSpan(std::span<std::string>   (&s, 1));
78     testRuntimeSpan(std::span<std::string, 1>(&s, 1));
79 
80     std::span<int, 0> sp;
81     testEmptySpan(sp);
82 
83     return 0;
84 }
85