108 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: c++03, c++11, c++14, c++17, c++2010 11// <string>12 13// constexpr basic_string substr(size_type pos = 0, size_type n = npos) &&;14 15#include <algorithm>16#include <stdexcept>17#include <string>18 19#include "constexpr_char_traits.h"20#include "make_string.h"21#include "min_allocator.h"22#include "test_allocator.h"23#include "asan_testing.h"24 25#define STR(string) MAKE_CSTRING(typename S::value_type, string)26 27constexpr struct should_throw_exception_t {28} should_throw_exception;29 30template <class S>31constexpr void test(S orig, typename S::size_type pos, typename S::size_type n, const S expected) {32 S str = std::move(orig).substr(pos, n);33 LIBCPP_ASSERT(orig.__invariants());34 LIBCPP_ASSERT(str.__invariants());35 assert(str == expected);36 LIBCPP_ASSERT(is_string_asan_correct(orig));37 LIBCPP_ASSERT(is_string_asan_correct(str));38}39 40template <class S>41constexpr void test(S orig, typename S::size_type pos, typename S::size_type n, should_throw_exception_t) {42#ifndef TEST_HAS_NO_EXCEPTIONS43 if (!std::is_constant_evaluated()) {44 try {45 S str = std::move(orig).substr(pos, n);46 assert(false);47 } catch (const std::out_of_range&) {48 }49 }50#else51 (void)orig;52 (void)pos;53 (void)n;54#endif55}56 57template <class S>58constexpr void test_string() {59 test<S>(STR(""), 0, 0, STR(""));60 test<S>(STR(""), 0, 1, STR(""));61 test<S>(STR(""), 1, 0, should_throw_exception);62 test<S>(STR(""), 1, 1, should_throw_exception);63 test<S>(STR("short string"), 0, 1, STR("s"));64 test<S>(STR("short string"), 5, 5, STR(" stri"));65 test<S>(STR("short string"), 12, 5, STR(""));66 test<S>(STR("short string"), 13, 5, should_throw_exception);67 test<S>(STR("long long string so no SSO"), 0, 0, STR(""));68 test<S>(STR("long long string so no SSO"), 0, 10, STR("long long "));69 test<S>(STR("long long string so no SSO"), 10, 10, STR("string so "));70 test<S>(STR("long long string so no SSO"), 20, 10, STR("no SSO"));71 test<S>(STR("long long string so no SSO"), 26, 10, STR(""));72 test<S>(STR("long long string so no SSO"), 27, 0, should_throw_exception);73}74 75template <class CharT, class CharTraits>76constexpr void test_allocators() {77 test_string<std::basic_string<CharT, CharTraits, std::allocator<CharT>>>();78 test_string<std::basic_string<CharT, CharTraits, min_allocator<CharT>>>();79 test_string<std::basic_string<CharT, CharTraits, test_allocator<CharT>>>();80}81 82template <class CharT>83constexpr void test_char_traits() {84 test_allocators<CharT, std::char_traits<CharT>>();85 test_allocators<CharT, constexpr_char_traits<CharT>>();86}87 88constexpr bool test() {89 test_char_traits<char>();90 test_char_traits<char16_t>();91 test_char_traits<char32_t>();92#ifndef TEST_HAS_NO_WIDE_CHARACTERS93 test_char_traits<wchar_t>();94#endif95#ifndef TEST_HAS_NO_CHAR8_T96 test_char_traits<char8_t>();97#endif98 99 return true;100}101 102int main(int, char**) {103 test();104 static_assert(test());105 106 return 0;107}108