blob: 4c8c85ef93ab6e6e74286bbb309850b143a8c8f9 [file] [log] [blame]
Marshall Clow02e16512019-02-27 15:41:37 +00001// -*- 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++98, c++03, c++11, c++14, c++17
10
11// <span>
12
13// template <size_t _Ip, ElementType, size_t Extent>
14// constexpr ElementType& get(span<ElementType, Extent> s) noexcept;
15//
16
17
18#include <span>
19#include <cassert>
20#include <string>
21
22#include "test_macros.h"
23
24
25template <size_t Idx, typename Span>
26constexpr bool testConstexprSpan(Span sp, typename Span::pointer ptr)
27{
28 ASSERT_NOEXCEPT(std::get<Idx>(sp));
29 return std::addressof(std::get<Idx>(sp)) == ptr;
30}
31
32
33template <size_t Idx, typename Span>
34void testRuntimeSpan(Span sp, typename Span::pointer ptr)
35{
36 ASSERT_NOEXCEPT(std::get<Idx>(sp));
37 assert(std::addressof(std::get<Idx>(sp)) == ptr);
38}
39
40struct A{};
41constexpr int iArr1[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
42 int iArr2[] = {10, 11, 12, 13, 14, 15, 16, 17, 18, 19};
43
44int main(int, char**)
45{
46
47// static size
48 static_assert(testConstexprSpan<0>(std::span<const int, 4>(iArr1, 1), iArr1 + 0), "");
49 static_assert(testConstexprSpan<1>(std::span<const int, 4>(iArr1, 2), iArr1 + 1), "");
50 static_assert(testConstexprSpan<2>(std::span<const int, 4>(iArr1, 3), iArr1 + 2), "");
51 static_assert(testConstexprSpan<3>(std::span<const int, 4>(iArr1, 4), iArr1 + 3), "");
52
53 static_assert(testConstexprSpan<0>(std::span<const int, 1>(iArr1 + 1, 1), iArr1 + 1), "");
54 static_assert(testConstexprSpan<1>(std::span<const int, 2>(iArr1 + 2, 2), iArr1 + 3), "");
55 static_assert(testConstexprSpan<2>(std::span<const int, 3>(iArr1 + 3, 3), iArr1 + 5), "");
56 static_assert(testConstexprSpan<3>(std::span<const int, 4>(iArr1 + 4, 4), iArr1 + 7), "");
57
58// static size
59 testRuntimeSpan<0>(std::span<int, 4>(iArr2, 4), iArr2);
60 testRuntimeSpan<1>(std::span<int, 4>(iArr2, 1), iArr2 + 1);
61 testRuntimeSpan<2>(std::span<int, 4>(iArr2, 2), iArr2 + 2);
62 testRuntimeSpan<3>(std::span<int, 4>(iArr2, 3), iArr2 + 3);
63
64 testRuntimeSpan<0>(std::span<int, 4>(iArr2 + 1, 1), iArr2 + 1);
65 testRuntimeSpan<1>(std::span<int, 4>(iArr2 + 2, 2), iArr2 + 3);
66 testRuntimeSpan<2>(std::span<int, 4>(iArr2 + 3, 3), iArr2 + 5);
67 testRuntimeSpan<3>(std::span<int, 4>(iArr2 + 4, 4), iArr2 + 7);
68
69
70 std::string s;
71 testRuntimeSpan<0>(std::span<std::string, 1>(&s, 1), &s);
72
73
74 return 0;
75}