blob: a73f70cd4b89e73b8c89cdae61603bc60734b283 [file] [log] [blame]
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001//===----------------------------------------------------------------------===//
2//
Howard Hinnantf5256e12010-05-11 21:36:01 +00003// The LLVM Compiler Infrastructure
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004//
Howard Hinnantb64f8b02010-11-16 22:09:02 +00005// This file is dual licensed under the MIT and the University of Illinois Open
6// Source Licenses. See LICENSE.TXT for details.
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00007//
8//===----------------------------------------------------------------------===//
9
10// <string>
11
12// basic_string substr(size_type pos = 0, size_type n = npos) const;
13
14#include <string>
15#include <stdexcept>
16#include <algorithm>
17#include <cassert>
18
Marshall Clow061d0cc2013-11-26 20:58:02 +000019#include "min_allocator.h"
Howard Hinnant9dcdcde2013-06-28 16:59:19 +000020
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000021template <class S>
22void
23test(const S& s, typename S::size_type pos, typename S::size_type n)
24{
25 try
26 {
27 S str = s.substr(pos, n);
Dan Albert1d4a1ed2016-05-25 22:36:09 -070028 assert(str.__invariants());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000029 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 catch (std::out_of_range&)
35 {
36 assert(pos > s.size());
37 }
38}
39
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000040int main()
41{
Howard Hinnant9dcdcde2013-06-28 16:59:19 +000042 {
43 typedef std::string S;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000044 test(S(""), 0, 0);
45 test(S(""), 1, 0);
46 test(S("pniot"), 0, 0);
47 test(S("htaob"), 0, 1);
48 test(S("fodgq"), 0, 2);
49 test(S("hpqia"), 0, 4);
50 test(S("qanej"), 0, 5);
51 test(S("dfkap"), 1, 0);
52 test(S("clbao"), 1, 1);
53 test(S("ihqrf"), 1, 2);
54 test(S("mekdn"), 1, 3);
55 test(S("ngtjf"), 1, 4);
56 test(S("srdfq"), 2, 0);
57 test(S("qkdrs"), 2, 1);
58 test(S("ikcrq"), 2, 2);
59 test(S("cdaih"), 2, 3);
60 test(S("dmajb"), 4, 0);
61 test(S("karth"), 4, 1);
62 test(S("lhcdo"), 5, 0);
63 test(S("acbsj"), 6, 0);
64 test(S("pbsjikaole"), 0, 0);
65 test(S("pcbahntsje"), 0, 1);
66 test(S("mprdjbeiak"), 0, 5);
67 test(S("fhepcrntko"), 0, 9);
68 test(S("eqmpaidtls"), 0, 10);
69 test(S("joidhalcmq"), 1, 0);
70 test(S("omigsphflj"), 1, 1);
71 test(S("kocgbphfji"), 1, 4);
72 test(S("onmjekafbi"), 1, 8);
73 test(S("fbslrjiqkm"), 1, 9);
74 test(S("oqmrjahnkg"), 5, 0);
75 test(S("jeidpcmalh"), 5, 1);
76 test(S("schfalibje"), 5, 2);
77 test(S("crliponbqe"), 5, 4);
78 test(S("igdscopqtm"), 5, 5);
79 test(S("qngpdkimlc"), 9, 0);
80 test(S("thdjgafrlb"), 9, 1);
81 test(S("hcjitbfapl"), 10, 0);
82 test(S("mgojkldsqh"), 11, 0);
83 test(S("gfshlcmdjreqipbontak"), 0, 0);
84 test(S("nadkhpfemgclosibtjrq"), 0, 1);
85 test(S("nkodajteqplrbifhmcgs"), 0, 10);
86 test(S("ofdrqmkeblthacpgijsn"), 0, 19);
87 test(S("gbmetiprqdoasckjfhln"), 0, 20);
88 test(S("bdfjqgatlksriohemnpc"), 1, 0);
89 test(S("crnklpmegdqfiashtojb"), 1, 1);
90 test(S("ejqcnahdrkfsmptilgbo"), 1, 9);
91 test(S("jsbtafedocnirgpmkhql"), 1, 18);
92 test(S("prqgnlbaejsmkhdctoif"), 1, 19);
93 test(S("qnmodrtkebhpasifgcjl"), 10, 0);
94 test(S("pejafmnokrqhtisbcdgl"), 10, 1);
95 test(S("cpebqsfmnjdolhkratgi"), 10, 5);
96 test(S("odnqkgijrhabfmcestlp"), 10, 9);
97 test(S("lmofqdhpkibagnrcjste"), 10, 10);
98 test(S("lgjqketopbfahrmnsicd"), 19, 0);
99 test(S("ktsrmnqagdecfhijpobl"), 19, 1);
100 test(S("lsaijeqhtrbgcdmpfkno"), 20, 0);
101 test(S("dplqartnfgejichmoskb"), 21, 0);
Howard Hinnant9dcdcde2013-06-28 16:59:19 +0000102 }
Dan Albert1d4a1ed2016-05-25 22:36:09 -0700103#if __cplusplus >= 201103L
Howard Hinnant9dcdcde2013-06-28 16:59:19 +0000104 {
105 typedef std::basic_string<char, std::char_traits<char>, min_allocator<char>> S;
106 test(S(""), 0, 0);
107 test(S(""), 1, 0);
108 test(S("pniot"), 0, 0);
109 test(S("htaob"), 0, 1);
110 test(S("fodgq"), 0, 2);
111 test(S("hpqia"), 0, 4);
112 test(S("qanej"), 0, 5);
113 test(S("dfkap"), 1, 0);
114 test(S("clbao"), 1, 1);
115 test(S("ihqrf"), 1, 2);
116 test(S("mekdn"), 1, 3);
117 test(S("ngtjf"), 1, 4);
118 test(S("srdfq"), 2, 0);
119 test(S("qkdrs"), 2, 1);
120 test(S("ikcrq"), 2, 2);
121 test(S("cdaih"), 2, 3);
122 test(S("dmajb"), 4, 0);
123 test(S("karth"), 4, 1);
124 test(S("lhcdo"), 5, 0);
125 test(S("acbsj"), 6, 0);
126 test(S("pbsjikaole"), 0, 0);
127 test(S("pcbahntsje"), 0, 1);
128 test(S("mprdjbeiak"), 0, 5);
129 test(S("fhepcrntko"), 0, 9);
130 test(S("eqmpaidtls"), 0, 10);
131 test(S("joidhalcmq"), 1, 0);
132 test(S("omigsphflj"), 1, 1);
133 test(S("kocgbphfji"), 1, 4);
134 test(S("onmjekafbi"), 1, 8);
135 test(S("fbslrjiqkm"), 1, 9);
136 test(S("oqmrjahnkg"), 5, 0);
137 test(S("jeidpcmalh"), 5, 1);
138 test(S("schfalibje"), 5, 2);
139 test(S("crliponbqe"), 5, 4);
140 test(S("igdscopqtm"), 5, 5);
141 test(S("qngpdkimlc"), 9, 0);
142 test(S("thdjgafrlb"), 9, 1);
143 test(S("hcjitbfapl"), 10, 0);
144 test(S("mgojkldsqh"), 11, 0);
145 test(S("gfshlcmdjreqipbontak"), 0, 0);
146 test(S("nadkhpfemgclosibtjrq"), 0, 1);
147 test(S("nkodajteqplrbifhmcgs"), 0, 10);
148 test(S("ofdrqmkeblthacpgijsn"), 0, 19);
149 test(S("gbmetiprqdoasckjfhln"), 0, 20);
150 test(S("bdfjqgatlksriohemnpc"), 1, 0);
151 test(S("crnklpmegdqfiashtojb"), 1, 1);
152 test(S("ejqcnahdrkfsmptilgbo"), 1, 9);
153 test(S("jsbtafedocnirgpmkhql"), 1, 18);
154 test(S("prqgnlbaejsmkhdctoif"), 1, 19);
155 test(S("qnmodrtkebhpasifgcjl"), 10, 0);
156 test(S("pejafmnokrqhtisbcdgl"), 10, 1);
157 test(S("cpebqsfmnjdolhkratgi"), 10, 5);
158 test(S("odnqkgijrhabfmcestlp"), 10, 9);
159 test(S("lmofqdhpkibagnrcjste"), 10, 10);
160 test(S("lgjqketopbfahrmnsicd"), 19, 0);
161 test(S("ktsrmnqagdecfhijpobl"), 19, 1);
162 test(S("lsaijeqhtrbgcdmpfkno"), 20, 0);
163 test(S("dplqartnfgejichmoskb"), 21, 0);
164 }
165#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000166}