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