brintos

brintos / llvm-project-archived public Read only

0
0
Text · 3.1 KiB · ca2b781 Raw
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       reverse_iterator  rend() const noexcept;13// constexpr const_reverse_iterator crend() 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::reverse_iterator e = s.rend();25  if (s.empty()) {26    ret = ret && (e == s.rbegin());27  } else {28    ret = ret && (e != s.rbegin());29  }30 31  ret = ret && (static_cast<std::size_t>(e - s.rbegin()) == s.size());32  return ret;33}34 35template <class Span>36void testRuntimeSpan(Span s) {37  typename Span::reverse_iterator e = s.rend();38  if (s.empty()) {39    assert(e == s.rbegin());40  } else {41    assert(e != s.rbegin());42  }43 44  assert(static_cast<std::size_t>(e - s.rbegin()) == s.size());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