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