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 // <string>
10
11 // basic_string substr(size_type pos = 0, size_type n = npos) const; // constexpr since C++20
12
13 #include <string>
14 #include <stdexcept>
15 #include <algorithm>
16 #include <cassert>
17
18 #include "test_macros.h"
19 #include "min_allocator.h"
20
21 template <class S>
22 TEST_CONSTEXPR_CXX20 void
test(const S & s,typename S::size_type pos,typename S::size_type n)23 test(const S& s, typename S::size_type pos, typename S::size_type n)
24 {
25 if (pos <= s.size())
26 {
27 S str = s.substr(pos, n);
28 LIBCPP_ASSERT(str.__invariants());
29 assert(pos <= s.size());
30 typename S::size_type rlen = std::min(n, s.size() - pos);
31 assert(str.size() == rlen);
32 assert(S::traits_type::compare(s.data()+pos, str.data(), rlen) == 0);
33 }
34 #ifndef TEST_HAS_NO_EXCEPTIONS
35 else if (!TEST_IS_CONSTANT_EVALUATED)
36 {
37 try
38 {
39 S str = s.substr(pos, n);
40 assert(false);
41 }
42 catch (std::out_of_range&)
43 {
44 assert(pos > s.size());
45 }
46 }
47 #endif
48 }
49
test()50 TEST_CONSTEXPR_CXX20 bool test() {
51 {
52 typedef std::string S;
53 test(S(""), 0, 0);
54 test(S(""), 1, 0);
55 test(S("pniot"), 0, 0);
56 test(S("htaob"), 0, 1);
57 test(S("fodgq"), 0, 2);
58 test(S("hpqia"), 0, 4);
59 test(S("qanej"), 0, 5);
60 test(S("dfkap"), 1, 0);
61 test(S("clbao"), 1, 1);
62 test(S("ihqrf"), 1, 2);
63 test(S("mekdn"), 1, 3);
64 test(S("ngtjf"), 1, 4);
65 test(S("srdfq"), 2, 0);
66 test(S("qkdrs"), 2, 1);
67 test(S("ikcrq"), 2, 2);
68 test(S("cdaih"), 2, 3);
69 test(S("dmajb"), 4, 0);
70 test(S("karth"), 4, 1);
71 test(S("lhcdo"), 5, 0);
72 test(S("acbsj"), 6, 0);
73 test(S("pbsjikaole"), 0, 0);
74 test(S("pcbahntsje"), 0, 1);
75 test(S("mprdjbeiak"), 0, 5);
76 test(S("fhepcrntko"), 0, 9);
77 test(S("eqmpaidtls"), 0, 10);
78 test(S("joidhalcmq"), 1, 0);
79 test(S("omigsphflj"), 1, 1);
80 test(S("kocgbphfji"), 1, 4);
81 test(S("onmjekafbi"), 1, 8);
82 test(S("fbslrjiqkm"), 1, 9);
83 test(S("oqmrjahnkg"), 5, 0);
84 test(S("jeidpcmalh"), 5, 1);
85 test(S("schfalibje"), 5, 2);
86 test(S("crliponbqe"), 5, 4);
87 test(S("igdscopqtm"), 5, 5);
88 test(S("qngpdkimlc"), 9, 0);
89 test(S("thdjgafrlb"), 9, 1);
90 test(S("hcjitbfapl"), 10, 0);
91 test(S("mgojkldsqh"), 11, 0);
92 test(S("gfshlcmdjreqipbontak"), 0, 0);
93 test(S("nadkhpfemgclosibtjrq"), 0, 1);
94 test(S("nkodajteqplrbifhmcgs"), 0, 10);
95 test(S("ofdrqmkeblthacpgijsn"), 0, 19);
96 test(S("gbmetiprqdoasckjfhln"), 0, 20);
97 test(S("bdfjqgatlksriohemnpc"), 1, 0);
98 test(S("crnklpmegdqfiashtojb"), 1, 1);
99 test(S("ejqcnahdrkfsmptilgbo"), 1, 9);
100 test(S("jsbtafedocnirgpmkhql"), 1, 18);
101 test(S("prqgnlbaejsmkhdctoif"), 1, 19);
102 test(S("qnmodrtkebhpasifgcjl"), 10, 0);
103 test(S("pejafmnokrqhtisbcdgl"), 10, 1);
104 test(S("cpebqsfmnjdolhkratgi"), 10, 5);
105 test(S("odnqkgijrhabfmcestlp"), 10, 9);
106 test(S("lmofqdhpkibagnrcjste"), 10, 10);
107 test(S("lgjqketopbfahrmnsicd"), 19, 0);
108 test(S("ktsrmnqagdecfhijpobl"), 19, 1);
109 test(S("lsaijeqhtrbgcdmpfkno"), 20, 0);
110 test(S("dplqartnfgejichmoskb"), 21, 0);
111 }
112 #if TEST_STD_VER >= 11
113 {
114 typedef std::basic_string<char, std::char_traits<char>, min_allocator<char>> S;
115 test(S(""), 0, 0);
116 test(S(""), 1, 0);
117 test(S("pniot"), 0, 0);
118 test(S("htaob"), 0, 1);
119 test(S("fodgq"), 0, 2);
120 test(S("hpqia"), 0, 4);
121 test(S("qanej"), 0, 5);
122 test(S("dfkap"), 1, 0);
123 test(S("clbao"), 1, 1);
124 test(S("ihqrf"), 1, 2);
125 test(S("mekdn"), 1, 3);
126 test(S("ngtjf"), 1, 4);
127 test(S("srdfq"), 2, 0);
128 test(S("qkdrs"), 2, 1);
129 test(S("ikcrq"), 2, 2);
130 test(S("cdaih"), 2, 3);
131 test(S("dmajb"), 4, 0);
132 test(S("karth"), 4, 1);
133 test(S("lhcdo"), 5, 0);
134 test(S("acbsj"), 6, 0);
135 test(S("pbsjikaole"), 0, 0);
136 test(S("pcbahntsje"), 0, 1);
137 test(S("mprdjbeiak"), 0, 5);
138 test(S("fhepcrntko"), 0, 9);
139 test(S("eqmpaidtls"), 0, 10);
140 test(S("joidhalcmq"), 1, 0);
141 test(S("omigsphflj"), 1, 1);
142 test(S("kocgbphfji"), 1, 4);
143 test(S("onmjekafbi"), 1, 8);
144 test(S("fbslrjiqkm"), 1, 9);
145 test(S("oqmrjahnkg"), 5, 0);
146 test(S("jeidpcmalh"), 5, 1);
147 test(S("schfalibje"), 5, 2);
148 test(S("crliponbqe"), 5, 4);
149 test(S("igdscopqtm"), 5, 5);
150 test(S("qngpdkimlc"), 9, 0);
151 test(S("thdjgafrlb"), 9, 1);
152 test(S("hcjitbfapl"), 10, 0);
153 test(S("mgojkldsqh"), 11, 0);
154 test(S("gfshlcmdjreqipbontak"), 0, 0);
155 test(S("nadkhpfemgclosibtjrq"), 0, 1);
156 test(S("nkodajteqplrbifhmcgs"), 0, 10);
157 test(S("ofdrqmkeblthacpgijsn"), 0, 19);
158 test(S("gbmetiprqdoasckjfhln"), 0, 20);
159 test(S("bdfjqgatlksriohemnpc"), 1, 0);
160 test(S("crnklpmegdqfiashtojb"), 1, 1);
161 test(S("ejqcnahdrkfsmptilgbo"), 1, 9);
162 test(S("jsbtafedocnirgpmkhql"), 1, 18);
163 test(S("prqgnlbaejsmkhdctoif"), 1, 19);
164 test(S("qnmodrtkebhpasifgcjl"), 10, 0);
165 test(S("pejafmnokrqhtisbcdgl"), 10, 1);
166 test(S("cpebqsfmnjdolhkratgi"), 10, 5);
167 test(S("odnqkgijrhabfmcestlp"), 10, 9);
168 test(S("lmofqdhpkibagnrcjste"), 10, 10);
169 test(S("lgjqketopbfahrmnsicd"), 19, 0);
170 test(S("ktsrmnqagdecfhijpobl"), 19, 1);
171 test(S("lsaijeqhtrbgcdmpfkno"), 20, 0);
172 test(S("dplqartnfgejichmoskb"), 21, 0);
173 }
174 #endif
175
176 return true;
177 }
178
main(int,char **)179 int main(int, char**)
180 {
181 test();
182 #if TEST_STD_VER > 17
183 static_assert(test());
184 #endif
185
186 return 0;
187 }
188