brintos

brintos / llvm-project-archived public Read only

0
0
Text · 1.5 KiB · d48c2f9 Raw
74 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 9// UNSUPPORTED: !stdlib=libc++ && (c++03 || c++11 || c++14)10 11// <string_view>12 13// constexpr basic_string_view(const _CharT* _s)14//    : __data (_s), __size(_Traits::length(_s)) {}15 16#include <string_view>17#include <string>18#include <cassert>19 20#include "test_macros.h"21#include "constexpr_char_traits.h"22 23template <typename CharT>24size_t StrLen(const CharT* s) {25  std::size_t retVal = 0;26  while (*s != 0) {27    ++retVal;28    ++s;29  }30  return retVal;31}32 33template <typename CharT>34void test(const CharT* s) {35  typedef std::basic_string_view<CharT> SV;36  //  I'd love to do this, but it would require traits::length() to be noexcept37  //  LIBCPP_ASSERT_NOEXCEPT(SV(s));38 39  SV sv1(s);40  assert(sv1.size() == StrLen(s));41  assert(sv1.data() == s);42}43 44int main(int, char**) {45  test("QBCDE");46  test("A");47  test("");48 49#ifndef TEST_HAS_NO_WIDE_CHARACTERS50  test(L"QBCDE");51  test(L"A");52  test(L"");53#endif54 55#if TEST_STD_VER >= 1156  test(u"QBCDE");57  test(u"A");58  test(u"");59 60  test(U"QBCDE");61  test(U"A");62  test(U"");63#endif64 65#if TEST_STD_VER > 1166  {67    constexpr std::basic_string_view<char, constexpr_char_traits<char>> sv1("ABCDE");68    static_assert(sv1.size() == 5, "");69  }70#endif71 72  return 0;73}74