xref: /llvm-project/libcxx/test/std/strings/basic.string/string.ops/string_substr/substr_rvalue.pass.cpp (revision 29378ab24b98137b4959034a0882c3bbd97c46e4)
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-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 // UNSUPPORTED: c++03, c++11, c++14, c++17, c++20
10 
11 // <string>
12 
13 // constexpr basic_string substr(size_type pos = 0, size_type n = npos) &&;
14 
15 #include <algorithm>
16 #include <string>
17 
18 #include "constexpr_char_traits.h"
19 #include "make_string.h"
20 #include "min_allocator.h"
21 #include "test_allocator.h"
22 
23 #define STR(string) MAKE_CSTRING(typename S::value_type, string)
24 
25 constexpr struct should_throw_exception_t {
26 } should_throw_exception;
27 
28 template <class S>
29 constexpr void test(S orig, size_t pos, ptrdiff_t n, S expected) {
30   S str = std::move(orig).substr(pos, n);
31   LIBCPP_ASSERT(orig.__invariants());
32   LIBCPP_ASSERT(str.__invariants());
33   assert(str == expected);
34 }
35 
36 template <class S>
37 constexpr void test(S orig, size_t pos, ptrdiff_t n, should_throw_exception_t) {
38 #ifndef TEST_HAS_NO_EXCEPTIONS
39   if (!std::is_constant_evaluated()) {
40     try {
41       S str = std::move(orig).substr(pos, n);
42       assert(false);
43     } catch (const std::out_of_range&) {
44     }
45   }
46 #else
47   (void)orig;
48   (void)pos;
49   (void)n;
50 #endif
51 }
52 
53 template <class S>
54 constexpr void test_string() {
55   test<S>(STR(""), 0, 0, STR(""));
56   test<S>(STR(""), 0, 1, STR(""));
57   test<S>(STR(""), 1, 0, should_throw_exception);
58   test<S>(STR(""), 1, 1, should_throw_exception);
59   test<S>(STR("short string"), 0, 1, STR("s"));
60   test<S>(STR("short string"), 5, 5, STR(" stri"));
61   test<S>(STR("short string"), 12, 5, STR(""));
62   test<S>(STR("short string"), 13, 5, should_throw_exception);
63   test<S>(STR("long long string so no SSO"), 0, 0, STR(""));
64   test<S>(STR("long long string so no SSO"), 0, 10, STR("long long "));
65   test<S>(STR("long long string so no SSO"), 10, 10, STR("string so "));
66   test<S>(STR("long long string so no SSO"), 20, 10, STR("no SSO"));
67   test<S>(STR("long long string so no SSO"), 26, 10, STR(""));
68   test<S>(STR("long long string so no SSO"), 27, 0, should_throw_exception);
69 }
70 
71 template <class CharT, class CharTraits>
72 constexpr void test_allocators() {
73   test_string<std::basic_string<CharT, CharTraits, std::allocator<CharT>>>();
74   test_string<std::basic_string<CharT, CharTraits, min_allocator<CharT>>>();
75   test_string<std::basic_string<CharT, CharTraits, test_allocator<CharT>>>();
76 }
77 
78 template <class CharT>
79 constexpr void test_char_traits() {
80   test_allocators<CharT, std::char_traits<CharT>>();
81   test_allocators<CharT, constexpr_char_traits<CharT>>();
82 }
83 
84 constexpr bool test() {
85   test_char_traits<char>();
86   test_char_traits<char16_t>();
87   test_char_traits<char32_t>();
88 #ifndef TEST_HAS_NO_WIDE_CHARACTERS
89   test_char_traits<wchar_t>();
90 #endif
91 #ifndef TEST_HAS_NO_CHAR8_T
92   test_char_traits<char8_t>();
93 #endif
94 
95   return true;
96 }
97 
98 int main(int, char**) {
99   test();
100   static_assert(test());
101 
102   return 0;
103 }
104