Home | History | Annotate | Download | only in string_insert
      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 // XFAIL: libcpp-no-exceptions
     11 // <string>
     12 
     13 // basic_string<charT,traits,Allocator>&
     14 //   insert(size_type pos1, const basic_string& str);
     15 
     16 #include <string>
     17 #include <stdexcept>
     18 #include <cassert>
     19 
     20 #include "test_macros.h"
     21 #include "min_allocator.h"
     22 
     23 template <class S>
     24 void
     25 test(S s, typename S::size_type pos, S str, S expected)
     26 {
     27     typename S::size_type old_size = s.size();
     28     S s0 = s;
     29     try
     30     {
     31         s.insert(pos, str);
     32         LIBCPP_ASSERT(s.__invariants());
     33         assert(pos <= old_size);
     34         assert(s == expected);
     35     }
     36     catch (std::out_of_range&)
     37     {
     38         assert(pos > old_size);
     39         assert(s == s0);
     40     }
     41 }
     42 
     43 int main()
     44 {
     45     {
     46     typedef std::string S;
     47     test(S(""), 0, S(""), S(""));
     48     test(S(""), 0, S("12345"), S("12345"));
     49     test(S(""), 0, S("1234567890"), S("1234567890"));
     50     test(S(""), 0, S("12345678901234567890"), S("12345678901234567890"));
     51     test(S(""), 1, S(""), S("can't happen"));
     52     test(S(""), 1, S("12345"), S("can't happen"));
     53     test(S(""), 1, S("1234567890"), S("can't happen"));
     54     test(S(""), 1, S("12345678901234567890"), S("can't happen"));
     55     test(S("abcde"), 0, S(""), S("abcde"));
     56     test(S("abcde"), 0, S("12345"), S("12345abcde"));
     57     test(S("abcde"), 0, S("1234567890"), S("1234567890abcde"));
     58     test(S("abcde"), 0, S("12345678901234567890"), S("12345678901234567890abcde"));
     59     test(S("abcde"), 1, S(""), S("abcde"));
     60     test(S("abcde"), 1, S("12345"), S("a12345bcde"));
     61     test(S("abcde"), 1, S("1234567890"), S("a1234567890bcde"));
     62     test(S("abcde"), 1, S("12345678901234567890"), S("a12345678901234567890bcde"));
     63     test(S("abcde"), 2, S(""), S("abcde"));
     64     test(S("abcde"), 2, S("12345"), S("ab12345cde"));
     65     test(S("abcde"), 2, S("1234567890"), S("ab1234567890cde"));
     66     test(S("abcde"), 2, S("12345678901234567890"), S("ab12345678901234567890cde"));
     67     test(S("abcde"), 4, S(""), S("abcde"));
     68     test(S("abcde"), 4, S("12345"), S("abcd12345e"));
     69     test(S("abcde"), 4, S("1234567890"), S("abcd1234567890e"));
     70     test(S("abcde"), 4, S("12345678901234567890"), S("abcd12345678901234567890e"));
     71     test(S("abcde"), 5, S(""), S("abcde"));
     72     test(S("abcde"), 5, S("12345"), S("abcde12345"));
     73     test(S("abcde"), 5, S("1234567890"), S("abcde1234567890"));
     74     test(S("abcde"), 5, S("12345678901234567890"), S("abcde12345678901234567890"));
     75     test(S("abcde"), 6, S(""), S("can't happen"));
     76     test(S("abcde"), 6, S("12345"), S("can't happen"));
     77     test(S("abcde"), 6, S("1234567890"), S("can't happen"));
     78     test(S("abcde"), 6, S("12345678901234567890"), S("can't happen"));
     79     test(S("abcdefghij"), 0, S(""), S("abcdefghij"));
     80     test(S("abcdefghij"), 0, S("12345"), S("12345abcdefghij"));
     81     test(S("abcdefghij"), 0, S("1234567890"), S("1234567890abcdefghij"));
     82     test(S("abcdefghij"), 0, S("12345678901234567890"), S("12345678901234567890abcdefghij"));
     83     test(S("abcdefghij"), 1, S(""), S("abcdefghij"));
     84     test(S("abcdefghij"), 1, S("12345"), S("a12345bcdefghij"));
     85     test(S("abcdefghij"), 1, S("1234567890"), S("a1234567890bcdefghij"));
     86     test(S("abcdefghij"), 1, S("12345678901234567890"), S("a12345678901234567890bcdefghij"));
     87     test(S("abcdefghij"), 5, S(""), S("abcdefghij"));
     88     test(S("abcdefghij"), 5, S("12345"), S("abcde12345fghij"));
     89     test(S("abcdefghij"), 5, S("1234567890"), S("abcde1234567890fghij"));
     90     test(S("abcdefghij"), 5, S("12345678901234567890"), S("abcde12345678901234567890fghij"));
     91     test(S("abcdefghij"), 9, S(""), S("abcdefghij"));
     92     test(S("abcdefghij"), 9, S("12345"), S("abcdefghi12345j"));
     93     test(S("abcdefghij"), 9, S("1234567890"), S("abcdefghi1234567890j"));
     94     test(S("abcdefghij"), 9, S("12345678901234567890"), S("abcdefghi12345678901234567890j"));
     95     test(S("abcdefghij"), 10, S(""), S("abcdefghij"));
     96     test(S("abcdefghij"), 10, S("12345"), S("abcdefghij12345"));
     97     test(S("abcdefghij"), 10, S("1234567890"), S("abcdefghij1234567890"));
     98     test(S("abcdefghij"), 10, S("12345678901234567890"), S("abcdefghij12345678901234567890"));
     99     test(S("abcdefghij"), 11, S(""), S("can't happen"));
    100     test(S("abcdefghij"), 11, S("12345"), S("can't happen"));
    101     test(S("abcdefghij"), 11, S("1234567890"), S("can't happen"));
    102     test(S("abcdefghij"), 11, S("12345678901234567890"), S("can't happen"));
    103     test(S("abcdefghijklmnopqrst"), 0, S(""), S("abcdefghijklmnopqrst"));
    104     test(S("abcdefghijklmnopqrst"), 0, S("12345"), S("12345abcdefghijklmnopqrst"));
    105     test(S("abcdefghijklmnopqrst"), 0, S("1234567890"), S("1234567890abcdefghijklmnopqrst"));
    106     test(S("abcdefghijklmnopqrst"), 0, S("12345678901234567890"), S("12345678901234567890abcdefghijklmnopqrst"));
    107     test(S("abcdefghijklmnopqrst"), 1, S(""), S("abcdefghijklmnopqrst"));
    108     test(S("abcdefghijklmnopqrst"), 1, S("12345"), S("a12345bcdefghijklmnopqrst"));
    109     test(S("abcdefghijklmnopqrst"), 1, S("1234567890"), S("a1234567890bcdefghijklmnopqrst"));
    110     test(S("abcdefghijklmnopqrst"), 1, S("12345678901234567890"), S("a12345678901234567890bcdefghijklmnopqrst"));
    111     test(S("abcdefghijklmnopqrst"), 10, S(""), S("abcdefghijklmnopqrst"));
    112     test(S("abcdefghijklmnopqrst"), 10, S("12345"), S("abcdefghij12345klmnopqrst"));
    113     test(S("abcdefghijklmnopqrst"), 10, S("1234567890"), S("abcdefghij1234567890klmnopqrst"));
    114     test(S("abcdefghijklmnopqrst"), 10, S("12345678901234567890"), S("abcdefghij12345678901234567890klmnopqrst"));
    115     test(S("abcdefghijklmnopqrst"), 19, S(""), S("abcdefghijklmnopqrst"));
    116     test(S("abcdefghijklmnopqrst"), 19, S("12345"), S("abcdefghijklmnopqrs12345t"));
    117     test(S("abcdefghijklmnopqrst"), 19, S("1234567890"), S("abcdefghijklmnopqrs1234567890t"));
    118     test(S("abcdefghijklmnopqrst"), 19, S("12345678901234567890"), S("abcdefghijklmnopqrs12345678901234567890t"));
    119     test(S("abcdefghijklmnopqrst"), 20, S(""), S("abcdefghijklmnopqrst"));
    120     test(S("abcdefghijklmnopqrst"), 20, S("12345"), S("abcdefghijklmnopqrst12345"));
    121     test(S("abcdefghijklmnopqrst"), 20, S("1234567890"), S("abcdefghijklmnopqrst1234567890"));
    122     test(S("abcdefghijklmnopqrst"), 20, S("12345678901234567890"), S("abcdefghijklmnopqrst12345678901234567890"));
    123     test(S("abcdefghijklmnopqrst"), 21, S(""), S("can't happen"));
    124     test(S("abcdefghijklmnopqrst"), 21, S("12345"), S("can't happen"));
    125     test(S("abcdefghijklmnopqrst"), 21, S("1234567890"), S("can't happen"));
    126     test(S("abcdefghijklmnopqrst"), 21, S("12345678901234567890"), S("can't happen"));
    127     }
    128 #if TEST_STD_VER >= 11
    129     {
    130     typedef std::basic_string<char, std::char_traits<char>, min_allocator<char>> S;
    131     test(S(""), 0, S(""), S(""));
    132     test(S(""), 0, S("12345"), S("12345"));
    133     test(S(""), 0, S("1234567890"), S("1234567890"));
    134     test(S(""), 0, S("12345678901234567890"), S("12345678901234567890"));
    135     test(S(""), 1, S(""), S("can't happen"));
    136     test(S(""), 1, S("12345"), S("can't happen"));
    137     test(S(""), 1, S("1234567890"), S("can't happen"));
    138     test(S(""), 1, S("12345678901234567890"), S("can't happen"));
    139     test(S("abcde"), 0, S(""), S("abcde"));
    140     test(S("abcde"), 0, S("12345"), S("12345abcde"));
    141     test(S("abcde"), 0, S("1234567890"), S("1234567890abcde"));
    142     test(S("abcde"), 0, S("12345678901234567890"), S("12345678901234567890abcde"));
    143     test(S("abcde"), 1, S(""), S("abcde"));
    144     test(S("abcde"), 1, S("12345"), S("a12345bcde"));
    145     test(S("abcde"), 1, S("1234567890"), S("a1234567890bcde"));
    146     test(S("abcde"), 1, S("12345678901234567890"), S("a12345678901234567890bcde"));
    147     test(S("abcde"), 2, S(""), S("abcde"));
    148     test(S("abcde"), 2, S("12345"), S("ab12345cde"));
    149     test(S("abcde"), 2, S("1234567890"), S("ab1234567890cde"));
    150     test(S("abcde"), 2, S("12345678901234567890"), S("ab12345678901234567890cde"));
    151     test(S("abcde"), 4, S(""), S("abcde"));
    152     test(S("abcde"), 4, S("12345"), S("abcd12345e"));
    153     test(S("abcde"), 4, S("1234567890"), S("abcd1234567890e"));
    154     test(S("abcde"), 4, S("12345678901234567890"), S("abcd12345678901234567890e"));
    155     test(S("abcde"), 5, S(""), S("abcde"));
    156     test(S("abcde"), 5, S("12345"), S("abcde12345"));
    157     test(S("abcde"), 5, S("1234567890"), S("abcde1234567890"));
    158     test(S("abcde"), 5, S("12345678901234567890"), S("abcde12345678901234567890"));
    159     test(S("abcde"), 6, S(""), S("can't happen"));
    160     test(S("abcde"), 6, S("12345"), S("can't happen"));
    161     test(S("abcde"), 6, S("1234567890"), S("can't happen"));
    162     test(S("abcde"), 6, S("12345678901234567890"), S("can't happen"));
    163     test(S("abcdefghij"), 0, S(""), S("abcdefghij"));
    164     test(S("abcdefghij"), 0, S("12345"), S("12345abcdefghij"));
    165     test(S("abcdefghij"), 0, S("1234567890"), S("1234567890abcdefghij"));
    166     test(S("abcdefghij"), 0, S("12345678901234567890"), S("12345678901234567890abcdefghij"));
    167     test(S("abcdefghij"), 1, S(""), S("abcdefghij"));
    168     test(S("abcdefghij"), 1, S("12345"), S("a12345bcdefghij"));
    169     test(S("abcdefghij"), 1, S("1234567890"), S("a1234567890bcdefghij"));
    170     test(S("abcdefghij"), 1, S("12345678901234567890"), S("a12345678901234567890bcdefghij"));
    171     test(S("abcdefghij"), 5, S(""), S("abcdefghij"));
    172     test(S("abcdefghij"), 5, S("12345"), S("abcde12345fghij"));
    173     test(S("abcdefghij"), 5, S("1234567890"), S("abcde1234567890fghij"));
    174     test(S("abcdefghij"), 5, S("12345678901234567890"), S("abcde12345678901234567890fghij"));
    175     test(S("abcdefghij"), 9, S(""), S("abcdefghij"));
    176     test(S("abcdefghij"), 9, S("12345"), S("abcdefghi12345j"));
    177     test(S("abcdefghij"), 9, S("1234567890"), S("abcdefghi1234567890j"));
    178     test(S("abcdefghij"), 9, S("12345678901234567890"), S("abcdefghi12345678901234567890j"));
    179     test(S("abcdefghij"), 10, S(""), S("abcdefghij"));
    180     test(S("abcdefghij"), 10, S("12345"), S("abcdefghij12345"));
    181     test(S("abcdefghij"), 10, S("1234567890"), S("abcdefghij1234567890"));
    182     test(S("abcdefghij"), 10, S("12345678901234567890"), S("abcdefghij12345678901234567890"));
    183     test(S("abcdefghij"), 11, S(""), S("can't happen"));
    184     test(S("abcdefghij"), 11, S("12345"), S("can't happen"));
    185     test(S("abcdefghij"), 11, S("1234567890"), S("can't happen"));
    186     test(S("abcdefghij"), 11, S("12345678901234567890"), S("can't happen"));
    187     test(S("abcdefghijklmnopqrst"), 0, S(""), S("abcdefghijklmnopqrst"));
    188     test(S("abcdefghijklmnopqrst"), 0, S("12345"), S("12345abcdefghijklmnopqrst"));
    189     test(S("abcdefghijklmnopqrst"), 0, S("1234567890"), S("1234567890abcdefghijklmnopqrst"));
    190     test(S("abcdefghijklmnopqrst"), 0, S("12345678901234567890"), S("12345678901234567890abcdefghijklmnopqrst"));
    191     test(S("abcdefghijklmnopqrst"), 1, S(""), S("abcdefghijklmnopqrst"));
    192     test(S("abcdefghijklmnopqrst"), 1, S("12345"), S("a12345bcdefghijklmnopqrst"));
    193     test(S("abcdefghijklmnopqrst"), 1, S("1234567890"), S("a1234567890bcdefghijklmnopqrst"));
    194     test(S("abcdefghijklmnopqrst"), 1, S("12345678901234567890"), S("a12345678901234567890bcdefghijklmnopqrst"));
    195     test(S("abcdefghijklmnopqrst"), 10, S(""), S("abcdefghijklmnopqrst"));
    196     test(S("abcdefghijklmnopqrst"), 10, S("12345"), S("abcdefghij12345klmnopqrst"));
    197     test(S("abcdefghijklmnopqrst"), 10, S("1234567890"), S("abcdefghij1234567890klmnopqrst"));
    198     test(S("abcdefghijklmnopqrst"), 10, S("12345678901234567890"), S("abcdefghij12345678901234567890klmnopqrst"));
    199     test(S("abcdefghijklmnopqrst"), 19, S(""), S("abcdefghijklmnopqrst"));
    200     test(S("abcdefghijklmnopqrst"), 19, S("12345"), S("abcdefghijklmnopqrs12345t"));
    201     test(S("abcdefghijklmnopqrst"), 19, S("1234567890"), S("abcdefghijklmnopqrs1234567890t"));
    202     test(S("abcdefghijklmnopqrst"), 19, S("12345678901234567890"), S("abcdefghijklmnopqrs12345678901234567890t"));
    203     test(S("abcdefghijklmnopqrst"), 20, S(""), S("abcdefghijklmnopqrst"));
    204     test(S("abcdefghijklmnopqrst"), 20, S("12345"), S("abcdefghijklmnopqrst12345"));
    205     test(S("abcdefghijklmnopqrst"), 20, S("1234567890"), S("abcdefghijklmnopqrst1234567890"));
    206     test(S("abcdefghijklmnopqrst"), 20, S("12345678901234567890"), S("abcdefghijklmnopqrst12345678901234567890"));
    207     test(S("abcdefghijklmnopqrst"), 21, S(""), S("can't happen"));
    208     test(S("abcdefghijklmnopqrst"), 21, S("12345"), S("can't happen"));
    209     test(S("abcdefghijklmnopqrst"), 21, S("1234567890"), S("can't happen"));
    210     test(S("abcdefghijklmnopqrst"), 21, S("12345678901234567890"), S("can't happen"));
    211     }
    212 #endif
    213 }
    214