96 lines · cpp
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-exception6//7//===----------------------------------------------------------------------===//8// UNSUPPORTED: c++03, c++11, c++14, c++179 10// <span>11 12// constexpr iterator begin() const noexcept;13// constexpr const_iterator cbegin() const noexcept;14 15#include <span>16#include <cassert>17#include <string>18 19#include "test_macros.h"20 21template <class Span>22constexpr bool testConstexprSpan(Span s) {23 bool ret = true;24 typename Span::iterator b = s.begin();25 26 if (s.empty()) {27 ret = ret && (b == s.end());28 } else {29 ret = ret && (*b == s[0]);30 ret = ret && (&*b == &s[0]);31 }32 return ret;33}34 35template <class Span>36void testRuntimeSpan(Span s) {37 typename Span::iterator b = s.begin();38 39 if (s.empty()) {40 assert(b == s.end());41 } else {42 assert(*b == s[0]);43 assert(&*b == &s[0]);44 }45}46 47struct A {};48bool operator==(A, A) { return true; }49 50constexpr int iArr1[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};51int iArr2[] = {10, 11, 12, 13, 14, 15, 16, 17, 18, 19};52 53int main(int, char**) {54 static_assert(testConstexprSpan(std::span<int>()), "");55 static_assert(testConstexprSpan(std::span<long>()), "");56 static_assert(testConstexprSpan(std::span<double>()), "");57 static_assert(testConstexprSpan(std::span<A>()), "");58 static_assert(testConstexprSpan(std::span<std::string>()), "");59 60 static_assert(testConstexprSpan(std::span<int, 0>()), "");61 static_assert(testConstexprSpan(std::span<long, 0>()), "");62 static_assert(testConstexprSpan(std::span<double, 0>()), "");63 static_assert(testConstexprSpan(std::span<A, 0>()), "");64 static_assert(testConstexprSpan(std::span<std::string, 0>()), "");65 66 static_assert(testConstexprSpan(std::span<const int>(iArr1, 1)), "");67 static_assert(testConstexprSpan(std::span<const int>(iArr1, 2)), "");68 static_assert(testConstexprSpan(std::span<const int>(iArr1, 3)), "");69 static_assert(testConstexprSpan(std::span<const int>(iArr1, 4)), "");70 static_assert(testConstexprSpan(std::span<const int>(iArr1, 5)), "");71 72 testRuntimeSpan(std::span<int>());73 testRuntimeSpan(std::span<long>());74 testRuntimeSpan(std::span<double>());75 testRuntimeSpan(std::span<A>());76 testRuntimeSpan(std::span<std::string>());77 78 testRuntimeSpan(std::span<int, 0>());79 testRuntimeSpan(std::span<long, 0>());80 testRuntimeSpan(std::span<double, 0>());81 testRuntimeSpan(std::span<A, 0>());82 testRuntimeSpan(std::span<std::string, 0>());83 84 testRuntimeSpan(std::span<int>(iArr2, 1));85 testRuntimeSpan(std::span<int>(iArr2, 2));86 testRuntimeSpan(std::span<int>(iArr2, 3));87 testRuntimeSpan(std::span<int>(iArr2, 4));88 testRuntimeSpan(std::span<int>(iArr2, 5));89 90 std::string s;91 testRuntimeSpan(std::span<std::string>(&s, (std::size_t)0));92 testRuntimeSpan(std::span<std::string>(&s, 1));93 94 return 0;95}96