xref: /llvm-project/libcxx/test/std/strings/basic.string/string.ops/string_substr/substr.pass.cpp (revision f520c1445f2d9cfb6153e3c125076aa6e3a76fc8)
1 //===----------------------------------------------------------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is dual licensed under the MIT and the University of Illinois Open
6 // Source Licenses. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 // XFAIL: libcpp-no-exceptions
11 // <string>
12 
13 // basic_string substr(size_type pos = 0, size_type n = npos) const;
14 
15 #include <string>
16 #include <stdexcept>
17 #include <algorithm>
18 #include <cassert>
19 
20 #include "min_allocator.h"
21 
22 template <class S>
23 void
24 test(const S& s, typename S::size_type pos, typename S::size_type n)
25 {
26     try
27     {
28         S str = s.substr(pos, n);
29         assert(str.__invariants());
30         assert(pos <= s.size());
31         typename S::size_type rlen = std::min(n, s.size() - pos);
32         assert(str.size() == rlen);
33         assert(S::traits_type::compare(s.data()+pos, str.data(), rlen) == 0);
34     }
35     catch (std::out_of_range&)
36     {
37         assert(pos > s.size());
38     }
39 }
40 
41 int main()
42 {
43     {
44     typedef std::string S;
45     test(S(""), 0, 0);
46     test(S(""), 1, 0);
47     test(S("pniot"), 0, 0);
48     test(S("htaob"), 0, 1);
49     test(S("fodgq"), 0, 2);
50     test(S("hpqia"), 0, 4);
51     test(S("qanej"), 0, 5);
52     test(S("dfkap"), 1, 0);
53     test(S("clbao"), 1, 1);
54     test(S("ihqrf"), 1, 2);
55     test(S("mekdn"), 1, 3);
56     test(S("ngtjf"), 1, 4);
57     test(S("srdfq"), 2, 0);
58     test(S("qkdrs"), 2, 1);
59     test(S("ikcrq"), 2, 2);
60     test(S("cdaih"), 2, 3);
61     test(S("dmajb"), 4, 0);
62     test(S("karth"), 4, 1);
63     test(S("lhcdo"), 5, 0);
64     test(S("acbsj"), 6, 0);
65     test(S("pbsjikaole"), 0, 0);
66     test(S("pcbahntsje"), 0, 1);
67     test(S("mprdjbeiak"), 0, 5);
68     test(S("fhepcrntko"), 0, 9);
69     test(S("eqmpaidtls"), 0, 10);
70     test(S("joidhalcmq"), 1, 0);
71     test(S("omigsphflj"), 1, 1);
72     test(S("kocgbphfji"), 1, 4);
73     test(S("onmjekafbi"), 1, 8);
74     test(S("fbslrjiqkm"), 1, 9);
75     test(S("oqmrjahnkg"), 5, 0);
76     test(S("jeidpcmalh"), 5, 1);
77     test(S("schfalibje"), 5, 2);
78     test(S("crliponbqe"), 5, 4);
79     test(S("igdscopqtm"), 5, 5);
80     test(S("qngpdkimlc"), 9, 0);
81     test(S("thdjgafrlb"), 9, 1);
82     test(S("hcjitbfapl"), 10, 0);
83     test(S("mgojkldsqh"), 11, 0);
84     test(S("gfshlcmdjreqipbontak"), 0, 0);
85     test(S("nadkhpfemgclosibtjrq"), 0, 1);
86     test(S("nkodajteqplrbifhmcgs"), 0, 10);
87     test(S("ofdrqmkeblthacpgijsn"), 0, 19);
88     test(S("gbmetiprqdoasckjfhln"), 0, 20);
89     test(S("bdfjqgatlksriohemnpc"), 1, 0);
90     test(S("crnklpmegdqfiashtojb"), 1, 1);
91     test(S("ejqcnahdrkfsmptilgbo"), 1, 9);
92     test(S("jsbtafedocnirgpmkhql"), 1, 18);
93     test(S("prqgnlbaejsmkhdctoif"), 1, 19);
94     test(S("qnmodrtkebhpasifgcjl"), 10, 0);
95     test(S("pejafmnokrqhtisbcdgl"), 10, 1);
96     test(S("cpebqsfmnjdolhkratgi"), 10, 5);
97     test(S("odnqkgijrhabfmcestlp"), 10, 9);
98     test(S("lmofqdhpkibagnrcjste"), 10, 10);
99     test(S("lgjqketopbfahrmnsicd"), 19, 0);
100     test(S("ktsrmnqagdecfhijpobl"), 19, 1);
101     test(S("lsaijeqhtrbgcdmpfkno"), 20, 0);
102     test(S("dplqartnfgejichmoskb"), 21, 0);
103     }
104 #if __cplusplus >= 201103L
105     {
106     typedef std::basic_string<char, std::char_traits<char>, min_allocator<char>> S;
107     test(S(""), 0, 0);
108     test(S(""), 1, 0);
109     test(S("pniot"), 0, 0);
110     test(S("htaob"), 0, 1);
111     test(S("fodgq"), 0, 2);
112     test(S("hpqia"), 0, 4);
113     test(S("qanej"), 0, 5);
114     test(S("dfkap"), 1, 0);
115     test(S("clbao"), 1, 1);
116     test(S("ihqrf"), 1, 2);
117     test(S("mekdn"), 1, 3);
118     test(S("ngtjf"), 1, 4);
119     test(S("srdfq"), 2, 0);
120     test(S("qkdrs"), 2, 1);
121     test(S("ikcrq"), 2, 2);
122     test(S("cdaih"), 2, 3);
123     test(S("dmajb"), 4, 0);
124     test(S("karth"), 4, 1);
125     test(S("lhcdo"), 5, 0);
126     test(S("acbsj"), 6, 0);
127     test(S("pbsjikaole"), 0, 0);
128     test(S("pcbahntsje"), 0, 1);
129     test(S("mprdjbeiak"), 0, 5);
130     test(S("fhepcrntko"), 0, 9);
131     test(S("eqmpaidtls"), 0, 10);
132     test(S("joidhalcmq"), 1, 0);
133     test(S("omigsphflj"), 1, 1);
134     test(S("kocgbphfji"), 1, 4);
135     test(S("onmjekafbi"), 1, 8);
136     test(S("fbslrjiqkm"), 1, 9);
137     test(S("oqmrjahnkg"), 5, 0);
138     test(S("jeidpcmalh"), 5, 1);
139     test(S("schfalibje"), 5, 2);
140     test(S("crliponbqe"), 5, 4);
141     test(S("igdscopqtm"), 5, 5);
142     test(S("qngpdkimlc"), 9, 0);
143     test(S("thdjgafrlb"), 9, 1);
144     test(S("hcjitbfapl"), 10, 0);
145     test(S("mgojkldsqh"), 11, 0);
146     test(S("gfshlcmdjreqipbontak"), 0, 0);
147     test(S("nadkhpfemgclosibtjrq"), 0, 1);
148     test(S("nkodajteqplrbifhmcgs"), 0, 10);
149     test(S("ofdrqmkeblthacpgijsn"), 0, 19);
150     test(S("gbmetiprqdoasckjfhln"), 0, 20);
151     test(S("bdfjqgatlksriohemnpc"), 1, 0);
152     test(S("crnklpmegdqfiashtojb"), 1, 1);
153     test(S("ejqcnahdrkfsmptilgbo"), 1, 9);
154     test(S("jsbtafedocnirgpmkhql"), 1, 18);
155     test(S("prqgnlbaejsmkhdctoif"), 1, 19);
156     test(S("qnmodrtkebhpasifgcjl"), 10, 0);
157     test(S("pejafmnokrqhtisbcdgl"), 10, 1);
158     test(S("cpebqsfmnjdolhkratgi"), 10, 5);
159     test(S("odnqkgijrhabfmcestlp"), 10, 9);
160     test(S("lmofqdhpkibagnrcjste"), 10, 10);
161     test(S("lgjqketopbfahrmnsicd"), 19, 0);
162     test(S("ktsrmnqagdecfhijpobl"), 19, 1);
163     test(S("lsaijeqhtrbgcdmpfkno"), 20, 0);
164     test(S("dplqartnfgejichmoskb"), 21, 0);
165     }
166 #endif
167 }
168