1 //===----------------------------------------------------------------------===//
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-exception
7 //===----------------------------------------------------------------------===//
9 // UNSUPPORTED: c++03, c++11, c++14, c++17, c++20
13 // constexpr basic_string substr(size_type pos = 0, size_type n = npos) &&;
19 #include "constexpr_char_traits.h"
20 #include "make_string.h"
21 #include "min_allocator.h"
22 #include "test_allocator.h"
24 #define STR(string) MAKE_CSTRING(typename S::value_type, string)
26 constexpr struct should_throw_exception_t
{
27 } should_throw_exception
;
30 constexpr void test(S orig
, typename
S::size_type pos
, typename
S::size_type n
, const S expected
) {
31 S str
= std::move(orig
).substr(pos
, n
);
32 LIBCPP_ASSERT(orig
.__invariants());
33 LIBCPP_ASSERT(str
.__invariants());
34 assert(str
== expected
);
38 constexpr void test(S orig
, typename
S::size_type pos
, typename
S::size_type n
, should_throw_exception_t
) {
39 #ifndef TEST_HAS_NO_EXCEPTIONS
40 if (!std::is_constant_evaluated()) {
42 S str
= std::move(orig
).substr(pos
, n
);
44 } catch (const std::out_of_range
&) {
55 constexpr void test_string() {
56 test
<S
>(STR(""), 0, 0, STR(""));
57 test
<S
>(STR(""), 0, 1, STR(""));
58 test
<S
>(STR(""), 1, 0, should_throw_exception
);
59 test
<S
>(STR(""), 1, 1, should_throw_exception
);
60 test
<S
>(STR("short string"), 0, 1, STR("s"));
61 test
<S
>(STR("short string"), 5, 5, STR(" stri"));
62 test
<S
>(STR("short string"), 12, 5, STR(""));
63 test
<S
>(STR("short string"), 13, 5, should_throw_exception
);
64 test
<S
>(STR("long long string so no SSO"), 0, 0, STR(""));
65 test
<S
>(STR("long long string so no SSO"), 0, 10, STR("long long "));
66 test
<S
>(STR("long long string so no SSO"), 10, 10, STR("string so "));
67 test
<S
>(STR("long long string so no SSO"), 20, 10, STR("no SSO"));
68 test
<S
>(STR("long long string so no SSO"), 26, 10, STR(""));
69 test
<S
>(STR("long long string so no SSO"), 27, 0, should_throw_exception
);
72 template <class CharT
, class CharTraits
>
73 constexpr void test_allocators() {
74 test_string
<std::basic_string
<CharT
, CharTraits
, std::allocator
<CharT
>>>();
75 test_string
<std::basic_string
<CharT
, CharTraits
, min_allocator
<CharT
>>>();
76 test_string
<std::basic_string
<CharT
, CharTraits
, test_allocator
<CharT
>>>();
79 template <class CharT
>
80 constexpr void test_char_traits() {
81 test_allocators
<CharT
, std::char_traits
<CharT
>>();
82 test_allocators
<CharT
, constexpr_char_traits
<CharT
>>();
85 constexpr bool test() {
86 test_char_traits
<char>();
87 test_char_traits
<char16_t
>();
88 test_char_traits
<char32_t
>();
89 #ifndef TEST_HAS_NO_WIDE_CHARACTERS
90 test_char_traits
<wchar_t>();
92 #ifndef TEST_HAS_NO_CHAR8_T
93 test_char_traits
<char8_t
>();
99 int main(int, char**) {
101 static_assert(test());