brintos

brintos / llvm-project-archived public Read only

0
0
Text · 3.3 KiB · f6f407b Raw
101 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// <string_view>11 12//  template <class It, class End>13//  constexpr basic_string_view(It begin, End end)14 15#include <string_view>16#include <cassert>17#include <iterator>18 19#include "make_string.h"20#include "test_iterators.h"21 22template <class It, class Sentinel, class CharT>23constexpr void test_construction(std::basic_string_view<CharT> val) {24  auto sv = std::basic_string_view<CharT>(It(val.data()), Sentinel(It(val.data() + val.size())));25  assert(sv.data() == val.data());26  assert(sv.size() == val.size());27}28 29template <class CharT>30constexpr void test_with_char() {31  const auto val = MAKE_STRING_VIEW(CharT, "test");32  test_construction<CharT*, CharT*>(val);33  test_construction<CharT*, const CharT*>(val);34  test_construction<const CharT*, CharT*>(val);35  test_construction<const CharT*, sized_sentinel<const CharT*>>(val);36  test_construction<contiguous_iterator<const CharT*>, contiguous_iterator<const CharT*>>(val);37  test_construction<contiguous_iterator<const CharT*>, sized_sentinel<contiguous_iterator<const CharT*>>>(val);38}39 40constexpr bool test() {41  test_with_char<char>();42#ifndef TEST_HAS_NO_WIDE_CHARACTERS43  test_with_char<wchar_t>();44#endif45  test_with_char<char8_t>();46  test_with_char<char16_t>();47  test_with_char<char32_t>();48 49  return true;50}51 52#ifndef TEST_HAS_NO_EXCEPTIONS53template <class CharT>54struct ThrowingSentinel {55  friend bool operator==(const CharT*, ThrowingSentinel) noexcept { return true; }56  friend std::iter_difference_t<const CharT*> operator-(const CharT*, ThrowingSentinel) noexcept { return {}; }57  friend std::iter_difference_t<const CharT*> operator-(ThrowingSentinel, const CharT*) { throw 42; }58};59 60template <class CharT>61void test_throwing() {62  auto val = MAKE_STRING_VIEW(CharT, "test");63  try {64    (void)std::basic_string_view<CharT>(val.data(), ThrowingSentinel<CharT>());65    assert(false);66  } catch (int i) {67    assert(i == 42);68  }69}70 71void test_throwing() {72  test_throwing<char>();73#  ifndef TEST_HAS_NO_WIDE_CHARACTERS74  test_throwing<wchar_t>();75#  endif76  test_throwing<char8_t>();77  test_throwing<char16_t>();78  test_throwing<char32_t>();79}80#endif81 82static_assert(std::is_constructible_v<std::string_view, const char*, char*>);83static_assert(std::is_constructible_v<std::string_view, char*, const char*>);84static_assert(!std::is_constructible_v<std::string_view, char*, void*>);               // not a sentinel85static_assert(!std::is_constructible_v<std::string_view, signed char*, signed char*>); // wrong char type86static_assert(!std::is_constructible_v<std::string_view,87                                       random_access_iterator<char*>,88                                       random_access_iterator<char*>>); // not contiguous89static_assert(std::is_constructible_v<std::string_view, contiguous_iterator<char*>, contiguous_iterator<char*>>);90 91int main(int, char**) {92  test();93  static_assert(test());94 95#ifndef TEST_HAS_NO_EXCEPTIONS96  test_throwing();97#endif98 99  return 0;100}101