substr.pass.cpp 5.2 KB

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