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 //===----------------------------------------------------------------------===//
11 // basic_string substr(size_type pos = 0, size_type n = npos) const; // constexpr since C++20, removed in C++23
12 // basic_string substr(size_type pos = 0, size_type n = npos) const&; // since in C++23
19 #include "test_allocator.h"
20 #include "test_macros.h"
21 #include "min_allocator.h"
24 TEST_CONSTEXPR_CXX20
void test(const S
& s
, typename
S::size_type pos
, typename
S::size_type n
) {
25 if (pos
<= s
.size()) {
26 S str
= s
.substr(pos
, n
);
27 LIBCPP_ASSERT(str
.__invariants());
28 assert(pos
<= s
.size());
29 typename
S::size_type rlen
= std::min(n
, s
.size() - pos
);
30 assert(str
.size() == rlen
);
31 assert(S::traits_type::compare(s
.data() + pos
, str
.data(), rlen
) == 0);
33 #ifndef TEST_HAS_NO_EXCEPTIONS
34 else if (!TEST_IS_CONSTANT_EVALUATED
) {
36 S str
= s
.substr(pos
, n
);
38 } catch (std::out_of_range
&) {
39 assert(pos
> s
.size());
46 TEST_CONSTEXPR_CXX20
void test_string() {
49 test(S("pniot"), 0, 0);
50 test(S("htaob"), 0, 1);
51 test(S("fodgq"), 0, 2);
52 test(S("hpqia"), 0, 4);
53 test(S("qanej"), 0, 5);
54 test(S("dfkap"), 1, 0);
55 test(S("clbao"), 1, 1);
56 test(S("ihqrf"), 1, 2);
57 test(S("mekdn"), 1, 3);
58 test(S("ngtjf"), 1, 4);
59 test(S("srdfq"), 2, 0);
60 test(S("qkdrs"), 2, 1);
61 test(S("ikcrq"), 2, 2);
62 test(S("cdaih"), 2, 3);
63 test(S("dmajb"), 4, 0);
64 test(S("karth"), 4, 1);
65 test(S("lhcdo"), 5, 0);
66 test(S("acbsj"), 6, 0);
67 test(S("pbsjikaole"), 0, 0);
68 test(S("pcbahntsje"), 0, 1);
69 test(S("mprdjbeiak"), 0, 5);
70 test(S("fhepcrntko"), 0, 9);
71 test(S("eqmpaidtls"), 0, 10);
72 test(S("joidhalcmq"), 1, 0);
73 test(S("omigsphflj"), 1, 1);
74 test(S("kocgbphfji"), 1, 4);
75 test(S("onmjekafbi"), 1, 8);
76 test(S("fbslrjiqkm"), 1, 9);
77 test(S("oqmrjahnkg"), 5, 0);
78 test(S("jeidpcmalh"), 5, 1);
79 test(S("schfalibje"), 5, 2);
80 test(S("crliponbqe"), 5, 4);
81 test(S("igdscopqtm"), 5, 5);
82 test(S("qngpdkimlc"), 9, 0);
83 test(S("thdjgafrlb"), 9, 1);
84 test(S("hcjitbfapl"), 10, 0);
85 test(S("mgojkldsqh"), 11, 0);
86 test(S("gfshlcmdjreqipbontak"), 0, 0);
87 test(S("nadkhpfemgclosibtjrq"), 0, 1);
88 test(S("nkodajteqplrbifhmcgs"), 0, 10);
89 test(S("ofdrqmkeblthacpgijsn"), 0, 19);
90 test(S("gbmetiprqdoasckjfhln"), 0, 20);
91 test(S("bdfjqgatlksriohemnpc"), 1, 0);
92 test(S("crnklpmegdqfiashtojb"), 1, 1);
93 test(S("ejqcnahdrkfsmptilgbo"), 1, 9);
94 test(S("jsbtafedocnirgpmkhql"), 1, 18);
95 test(S("prqgnlbaejsmkhdctoif"), 1, 19);
96 test(S("qnmodrtkebhpasifgcjl"), 10, 0);
97 test(S("pejafmnokrqhtisbcdgl"), 10, 1);
98 test(S("cpebqsfmnjdolhkratgi"), 10, 5);
99 test(S("odnqkgijrhabfmcestlp"), 10, 9);
100 test(S("lmofqdhpkibagnrcjste"), 10, 10);
101 test(S("lgjqketopbfahrmnsicd"), 19, 0);
102 test(S("ktsrmnqagdecfhijpobl"), 19, 1);
103 test(S("lsaijeqhtrbgcdmpfkno"), 20, 0);
104 test(S("dplqartnfgejichmoskb"), 21, 0);
107 TEST_CONSTEXPR_CXX20
bool test() {
108 test_string
<std::string
>();
109 #if TEST_STD_VER >= 11
110 test_string
<std::basic_string
<char, std::char_traits
<char>, min_allocator
<char>>>();
116 TEST_CONSTEXPR_CXX20
bool test_alloc() {
118 using alloc
= test_allocator
<char>;
119 using string
= std::basic_string
<char, std::char_traits
<char>, alloc
>;
120 test_allocator_statistics stats
;
122 string str
= string(alloc(&stats
));
123 stats
= test_allocator_statistics();
125 assert(stats
.moved
== 0);
126 assert(stats
.copied
== 0);
129 string str
= string(alloc(&stats
));
130 stats
= test_allocator_statistics();
131 (void)std::move(str
).substr();
132 assert(stats
.moved
== 0);
133 assert(stats
.copied
== 0);
140 int main(int, char**) {
143 #if TEST_STD_VER > 17
144 static_assert(test());
145 static_assert(test_alloc());