brintos

brintos / llvm-project-archived public Read only

0
0
Text · 3.5 KiB · b781754 Raw
121 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// REQUIRES: std-at-least-c++2610 11// <string_view>12 13// constexpr basic_string_view subview(size_type pos = 0,14//                                     size_type n = npos) const;      // freestanding-deleted15 16#include <cassert>17#include <concepts>18#include <string_view>19#include <utility>20 21#include "constexpr_char_traits.h"22#include "make_string.h"23#include "test_macros.h"24#include "type_algorithms.h"25 26#define CS(S) MAKE_CSTRING(CharT, S)27 28template <typename CharT, typename TraitsT>29constexpr void test() {30  std::basic_string_view<CharT, TraitsT> sv{CS("Hello cruel world!")};31 32  { // With a default position and a character length.33 34    // Check if subview() is a const-qualified.35    assert(std::as_const(sv).subview() == CS("Hello cruel world!"));36 37    // Check if the return type of subview() is correct.38    std::same_as<std::basic_string_view<CharT, TraitsT>> decltype(auto) subsv = sv.subview();39    assert(subsv == CS("Hello cruel world!"));40  }41 42  { // Check with different position and length.43 44    // With a explict position and a character length.45    assert(sv.subview(6, 5) == CS("cruel"));46 47    // From the beginning of the string with a explicit character length.48    assert(sv.subview(0, 5) == CS("Hello"));49 50    // To the end of string with the default character length.51    assert(sv.subview(12) == CS("world!"));52 53    // From the beginning to the end of the string with explicit values.54    assert(sv.subview(0, sv.size()) == CS("Hello cruel world!"));55  }56 57  // Test if exceptions are thrown correctly.58#ifndef TEST_HAS_NO_EXCEPTIONS59  if (!std::is_constant_evaluated()) {60    { // With a position that is out of range.61      try {62        std::ignore = sv.subview(sv.size() + 1);63        assert(false);64      } catch ([[maybe_unused]] const std::out_of_range& ex) {65        LIBCPP_ASSERT(std::string(ex.what()) == "string_view::substr");66      } catch (...) {67        assert(false);68      }69    }70 71    { // With a position that is out of range and a 0 character length.72      try {73        std::ignore = sv.subview(sv.size() + 1, 0);74        assert(false);75      } catch ([[maybe_unused]] const std::out_of_range& ex) {76        LIBCPP_ASSERT(std::string(ex.what()) == "string_view::substr");77      } catch (...) {78        assert(false);79      }80    }81 82    { // With a position that is out of range and a some character length.83      try {84        std::ignore = sv.subview(sv.size() + 1, 1);85        assert(false);86      } catch ([[maybe_unused]] const std::out_of_range& ex) {87        LIBCPP_ASSERT(std::string(ex.what()) == "string_view::substr");88      } catch (...) {89        assert(false);90      }91    }92  }93#endif94}95 96template <typename CharT>97constexpr void test() {98  test<CharT, std::char_traits<CharT>>();99  test<CharT, std::char_traits<CharT>>();100  test<CharT, std::char_traits<CharT>>();101  test<CharT, std::char_traits<CharT>>();102 103  test<CharT, constexpr_char_traits<CharT>>();104  test<CharT, constexpr_char_traits<CharT>>();105  test<CharT, constexpr_char_traits<CharT>>();106  test<CharT, constexpr_char_traits<CharT>>();107}108 109constexpr bool test() {110  types::for_each(types::character_types(), []<typename CharT> { test<CharT>(); });111 112  return true;113}114 115int main(int, char**) {116  test();117  static_assert(test());118 119  return 0;120}121