xref: /llvm-project/libcxx/test/std/containers/views/views.span/span.elem/front.pass.cpp (revision a8cf78c73914f614d4ec339f7bd5bba8b20c3c29)
1 //===----------------------------------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 // UNSUPPORTED: c++03, c++11, c++14, c++17
9 
10 // <span>
11 
12 // constexpr reference front() const noexcept;
13 //   Expects: empty() is false.
14 //   Effects: Equivalent to: return *data();
15 //
16 
17 
18 #include <span>
19 #include <cassert>
20 #include <string>
21 
22 #include "test_macros.h"
23 
24 
25 template <typename Span>
testConstexprSpan(Span sp)26 constexpr bool testConstexprSpan(Span sp)
27 {
28     LIBCPP_ASSERT(noexcept(sp.front()));
29     return std::addressof(sp.front()) == sp.data();
30 }
31 
32 
33 template <typename Span>
testRuntimeSpan(Span sp)34 void testRuntimeSpan(Span sp)
35 {
36     LIBCPP_ASSERT(noexcept(sp.front()));
37     assert(std::addressof(sp.front()) == sp.data());
38 }
39 
40 template <typename Span>
testEmptySpan(Span sp)41 void testEmptySpan(Span sp)
42 {
43     if (!sp.empty())
44         [[maybe_unused]] auto res = sp.front();
45 }
46 
47 struct A{};
48 constexpr int iArr1[] = { 0,  1,  2,  3,  4,  5,  6,  7,  8,  9};
49           int iArr2[] = {10, 11, 12, 13, 14, 15, 16, 17, 18, 19};
50 
main(int,char **)51 int main(int, char**)
52 {
53     static_assert(testConstexprSpan(std::span<const int>(iArr1, 1)), "");
54     static_assert(testConstexprSpan(std::span<const int>(iArr1, 2)), "");
55     static_assert(testConstexprSpan(std::span<const int>(iArr1, 3)), "");
56     static_assert(testConstexprSpan(std::span<const int>(iArr1, 4)), "");
57 
58     static_assert(testConstexprSpan(std::span<const int, 1>(iArr1, 1)), "");
59     static_assert(testConstexprSpan(std::span<const int, 2>(iArr1, 2)), "");
60     static_assert(testConstexprSpan(std::span<const int, 3>(iArr1, 3)), "");
61     static_assert(testConstexprSpan(std::span<const int, 4>(iArr1, 4)), "");
62 
63 
64     testRuntimeSpan(std::span<int>(iArr2, 1));
65     testRuntimeSpan(std::span<int>(iArr2, 2));
66     testRuntimeSpan(std::span<int>(iArr2, 3));
67     testRuntimeSpan(std::span<int>(iArr2, 4));
68 
69 
70     testRuntimeSpan(std::span<int, 1>(iArr2, 1));
71     testRuntimeSpan(std::span<int, 2>(iArr2, 2));
72     testRuntimeSpan(std::span<int, 3>(iArr2, 3));
73     testRuntimeSpan(std::span<int, 4>(iArr2, 4));
74 
75     std::string s;
76     testRuntimeSpan(std::span<std::string>   (&s, 1));
77     testRuntimeSpan(std::span<std::string, 1>(&s, 1));
78 
79     std::span<int, 0> sp;
80     testEmptySpan(sp);
81 
82     return 0;
83 }
84