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"
23 #include "asan_testing.h"
25 #define STR(string) MAKE_CSTRING(typename S::value_type, string)
27 constexpr struct should_throw_exception_t
{
28 } should_throw_exception
;
31 constexpr 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
));
41 constexpr void test(S orig
, typename
S::size_type pos
, typename
S::size_type n
, should_throw_exception_t
) {
42 #ifndef TEST_HAS_NO_EXCEPTIONS
43 if (!std::is_constant_evaluated()) {
45 S str
= std::move(orig
).substr(pos
, n
);
47 } catch (const std::out_of_range
&) {
58 constexpr 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
);
75 template <class CharT
, class CharTraits
>
76 constexpr 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
>>>();
82 template <class CharT
>
83 constexpr void test_char_traits() {
84 test_allocators
<CharT
, std::char_traits
<CharT
>>();
85 test_allocators
<CharT
, constexpr_char_traits
<CharT
>>();
88 constexpr 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_CHARACTERS
93 test_char_traits
<wchar_t>();
95 #ifndef TEST_HAS_NO_CHAR8_T
96 test_char_traits
<char8_t
>();
102 int main(int, char**) {
104 static_assert(test());