substr.pass.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. //===----------------------------------------------------------------------===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. // <string>
  10. // basic_string substr(size_type pos = 0, size_type n = npos) const;
  11. #include <string>
  12. #include <stdexcept>
  13. #include <algorithm>
  14. #include <cassert>
  15. template <class S>
  16. void
  17. test(const S& s, typename S::size_type pos, typename S::size_type n)
  18. {
  19. try
  20. {
  21. S str = s.substr(pos, n);
  22. assert(str.__invariants());
  23. assert(pos <= s.size());
  24. typename S::size_type rlen = std::min(n, s.size() - pos);
  25. assert(str.size() == rlen);
  26. assert(S::traits_type::compare(s.data()+pos, str.data(), rlen) == 0);
  27. }
  28. catch (std::out_of_range&)
  29. {
  30. assert(pos > s.size());
  31. }
  32. }
  33. typedef std::string S;
  34. int main()
  35. {
  36. test(S(""), 0, 0);
  37. test(S(""), 1, 0);
  38. test(S("pniot"), 0, 0);
  39. test(S("htaob"), 0, 1);
  40. test(S("fodgq"), 0, 2);
  41. test(S("hpqia"), 0, 4);
  42. test(S("qanej"), 0, 5);
  43. test(S("dfkap"), 1, 0);
  44. test(S("clbao"), 1, 1);
  45. test(S("ihqrf"), 1, 2);
  46. test(S("mekdn"), 1, 3);
  47. test(S("ngtjf"), 1, 4);
  48. test(S("srdfq"), 2, 0);
  49. test(S("qkdrs"), 2, 1);
  50. test(S("ikcrq"), 2, 2);
  51. test(S("cdaih"), 2, 3);
  52. test(S("dmajb"), 4, 0);
  53. test(S("karth"), 4, 1);
  54. test(S("lhcdo"), 5, 0);
  55. test(S("acbsj"), 6, 0);
  56. test(S("pbsjikaole"), 0, 0);
  57. test(S("pcbahntsje"), 0, 1);
  58. test(S("mprdjbeiak"), 0, 5);
  59. test(S("fhepcrntko"), 0, 9);
  60. test(S("eqmpaidtls"), 0, 10);
  61. test(S("joidhalcmq"), 1, 0);
  62. test(S("omigsphflj"), 1, 1);
  63. test(S("kocgbphfji"), 1, 4);
  64. test(S("onmjekafbi"), 1, 8);
  65. test(S("fbslrjiqkm"), 1, 9);
  66. test(S("oqmrjahnkg"), 5, 0);
  67. test(S("jeidpcmalh"), 5, 1);
  68. test(S("schfalibje"), 5, 2);
  69. test(S("crliponbqe"), 5, 4);
  70. test(S("igdscopqtm"), 5, 5);
  71. test(S("qngpdkimlc"), 9, 0);
  72. test(S("thdjgafrlb"), 9, 1);
  73. test(S("hcjitbfapl"), 10, 0);
  74. test(S("mgojkldsqh"), 11, 0);
  75. test(S("gfshlcmdjreqipbontak"), 0, 0);
  76. test(S("nadkhpfemgclosibtjrq"), 0, 1);
  77. test(S("nkodajteqplrbifhmcgs"), 0, 10);
  78. test(S("ofdrqmkeblthacpgijsn"), 0, 19);
  79. test(S("gbmetiprqdoasckjfhln"), 0, 20);
  80. test(S("bdfjqgatlksriohemnpc"), 1, 0);
  81. test(S("crnklpmegdqfiashtojb"), 1, 1);
  82. test(S("ejqcnahdrkfsmptilgbo"), 1, 9);
  83. test(S("jsbtafedocnirgpmkhql"), 1, 18);
  84. test(S("prqgnlbaejsmkhdctoif"), 1, 19);
  85. test(S("qnmodrtkebhpasifgcjl"), 10, 0);
  86. test(S("pejafmnokrqhtisbcdgl"), 10, 1);
  87. test(S("cpebqsfmnjdolhkratgi"), 10, 5);
  88. test(S("odnqkgijrhabfmcestlp"), 10, 9);
  89. test(S("lmofqdhpkibagnrcjste"), 10, 10);
  90. test(S("lgjqketopbfahrmnsicd"), 19, 0);
  91. test(S("ktsrmnqagdecfhijpobl"), 19, 1);
  92. test(S("lsaijeqhtrbgcdmpfkno"), 20, 0);
  93. test(S("dplqartnfgejichmoskb"), 21, 0);
  94. }