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