brintos

brintos / llvm-project-archived public Read only

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