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;
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 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
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
main(int,char **)50 int main(int, char**)
51 {
52 {
53 typedef std::string S;
54 test(S(""), 0, 0);
55 test(S(""), 1, 0);
56 test(S("pniot"), 0, 0);
57 test(S("htaob"), 0, 1);
58 test(S("fodgq"), 0, 2);
59 test(S("hpqia"), 0, 4);
60 test(S("qanej"), 0, 5);
61 test(S("dfkap"), 1, 0);
62 test(S("clbao"), 1, 1);
63 test(S("ihqrf"), 1, 2);
64 test(S("mekdn"), 1, 3);
65 test(S("ngtjf"), 1, 4);
66 test(S("srdfq"), 2, 0);
67 test(S("qkdrs"), 2, 1);
68 test(S("ikcrq"), 2, 2);
69 test(S("cdaih"), 2, 3);
70 test(S("dmajb"), 4, 0);
71 test(S("karth"), 4, 1);
72 test(S("lhcdo"), 5, 0);
73 test(S("acbsj"), 6, 0);
74 test(S("pbsjikaole"), 0, 0);
75 test(S("pcbahntsje"), 0, 1);
76 test(S("mprdjbeiak"), 0, 5);
77 test(S("fhepcrntko"), 0, 9);
78 test(S("eqmpaidtls"), 0, 10);
79 test(S("joidhalcmq"), 1, 0);
80 test(S("omigsphflj"), 1, 1);
81 test(S("kocgbphfji"), 1, 4);
82 test(S("onmjekafbi"), 1, 8);
83 test(S("fbslrjiqkm"), 1, 9);
84 test(S("oqmrjahnkg"), 5, 0);
85 test(S("jeidpcmalh"), 5, 1);
86 test(S("schfalibje"), 5, 2);
87 test(S("crliponbqe"), 5, 4);
88 test(S("igdscopqtm"), 5, 5);
89 test(S("qngpdkimlc"), 9, 0);
90 test(S("thdjgafrlb"), 9, 1);
91 test(S("hcjitbfapl"), 10, 0);
92 test(S("mgojkldsqh"), 11, 0);
93 test(S("gfshlcmdjreqipbontak"), 0, 0);
94 test(S("nadkhpfemgclosibtjrq"), 0, 1);
95 test(S("nkodajteqplrbifhmcgs"), 0, 10);
96 test(S("ofdrqmkeblthacpgijsn"), 0, 19);
97 test(S("gbmetiprqdoasckjfhln"), 0, 20);
98 test(S("bdfjqgatlksriohemnpc"), 1, 0);
99 test(S("crnklpmegdqfiashtojb"), 1, 1);
100 test(S("ejqcnahdrkfsmptilgbo"), 1, 9);
101 test(S("jsbtafedocnirgpmkhql"), 1, 18);
102 test(S("prqgnlbaejsmkhdctoif"), 1, 19);
103 test(S("qnmodrtkebhpasifgcjl"), 10, 0);
104 test(S("pejafmnokrqhtisbcdgl"), 10, 1);
105 test(S("cpebqsfmnjdolhkratgi"), 10, 5);
106 test(S("odnqkgijrhabfmcestlp"), 10, 9);
107 test(S("lmofqdhpkibagnrcjste"), 10, 10);
108 test(S("lgjqketopbfahrmnsicd"), 19, 0);
109 test(S("ktsrmnqagdecfhijpobl"), 19, 1);
110 test(S("lsaijeqhtrbgcdmpfkno"), 20, 0);
111 test(S("dplqartnfgejichmoskb"), 21, 0);
112 }
113 #if TEST_STD_VER >= 11
114 {
115 typedef std::basic_string<char, std::char_traits<char>, min_allocator<char>> S;
116 test(S(""), 0, 0);
117 test(S(""), 1, 0);
118 test(S("pniot"), 0, 0);
119 test(S("htaob"), 0, 1);
120 test(S("fodgq"), 0, 2);
121 test(S("hpqia"), 0, 4);
122 test(S("qanej"), 0, 5);
123 test(S("dfkap"), 1, 0);
124 test(S("clbao"), 1, 1);
125 test(S("ihqrf"), 1, 2);
126 test(S("mekdn"), 1, 3);
127 test(S("ngtjf"), 1, 4);
128 test(S("srdfq"), 2, 0);
129 test(S("qkdrs"), 2, 1);
130 test(S("ikcrq"), 2, 2);
131 test(S("cdaih"), 2, 3);
132 test(S("dmajb"), 4, 0);
133 test(S("karth"), 4, 1);
134 test(S("lhcdo"), 5, 0);
135 test(S("acbsj"), 6, 0);
136 test(S("pbsjikaole"), 0, 0);
137 test(S("pcbahntsje"), 0, 1);
138 test(S("mprdjbeiak"), 0, 5);
139 test(S("fhepcrntko"), 0, 9);
140 test(S("eqmpaidtls"), 0, 10);
141 test(S("joidhalcmq"), 1, 0);
142 test(S("omigsphflj"), 1, 1);
143 test(S("kocgbphfji"), 1, 4);
144 test(S("onmjekafbi"), 1, 8);
145 test(S("fbslrjiqkm"), 1, 9);
146 test(S("oqmrjahnkg"), 5, 0);
147 test(S("jeidpcmalh"), 5, 1);
148 test(S("schfalibje"), 5, 2);
149 test(S("crliponbqe"), 5, 4);
150 test(S("igdscopqtm"), 5, 5);
151 test(S("qngpdkimlc"), 9, 0);
152 test(S("thdjgafrlb"), 9, 1);
153 test(S("hcjitbfapl"), 10, 0);
154 test(S("mgojkldsqh"), 11, 0);
155 test(S("gfshlcmdjreqipbontak"), 0, 0);
156 test(S("nadkhpfemgclosibtjrq"), 0, 1);
157 test(S("nkodajteqplrbifhmcgs"), 0, 10);
158 test(S("ofdrqmkeblthacpgijsn"), 0, 19);
159 test(S("gbmetiprqdoasckjfhln"), 0, 20);
160 test(S("bdfjqgatlksriohemnpc"), 1, 0);
161 test(S("crnklpmegdqfiashtojb"), 1, 1);
162 test(S("ejqcnahdrkfsmptilgbo"), 1, 9);
163 test(S("jsbtafedocnirgpmkhql"), 1, 18);
164 test(S("prqgnlbaejsmkhdctoif"), 1, 19);
165 test(S("qnmodrtkebhpasifgcjl"), 10, 0);
166 test(S("pejafmnokrqhtisbcdgl"), 10, 1);
167 test(S("cpebqsfmnjdolhkratgi"), 10, 5);
168 test(S("odnqkgijrhabfmcestlp"), 10, 9);
169 test(S("lmofqdhpkibagnrcjste"), 10, 10);
170 test(S("lgjqketopbfahrmnsicd"), 19, 0);
171 test(S("ktsrmnqagdecfhijpobl"), 19, 1);
172 test(S("lsaijeqhtrbgcdmpfkno"), 20, 0);
173 test(S("dplqartnfgejichmoskb"), 21, 0);
174 }
175 #endif
176
177 return 0;
178 }
179