Home | History | Annotate | Download | only in string_assign
      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 // template <class T>
     13 //    basic_string& assign(const T& t, size_type pos, size_type n=npos); // C++17
     14 
     15 #include <string>
     16 #include <stdexcept>
     17 #include <cassert>
     18 
     19 #include "test_macros.h"
     20 #include "min_allocator.h"
     21 
     22 template <class S, class SV>
     23 void
     24 test(S s, SV sv, typename S::size_type pos, typename S::size_type n, S expected)
     25 {
     26     if (pos <= sv.size())
     27     {
     28         s.assign(sv, pos, n);
     29         LIBCPP_ASSERT(s.__invariants());
     30         assert(s == expected);
     31     }
     32 #ifndef TEST_HAS_NO_EXCEPTIONS
     33     else
     34     {
     35         try
     36         {
     37             s.assign(sv, pos, n);
     38             assert(false);
     39         }
     40         catch (std::out_of_range&)
     41         {
     42             assert(pos > sv.size());
     43         }
     44     }
     45 #endif
     46 }
     47 
     48 template <class S, class SV>
     49 void
     50 test_npos(S s, SV sv, typename S::size_type pos, S expected)
     51 {
     52     if (pos <= sv.size())
     53     {
     54         s.assign(sv, pos);
     55         LIBCPP_ASSERT(s.__invariants());
     56         assert(s == expected);
     57     }
     58 #ifndef TEST_HAS_NO_EXCEPTIONS
     59     else
     60     {
     61         try
     62         {
     63             s.assign(sv, pos);
     64             assert(false);
     65         }
     66         catch (std::out_of_range&)
     67         {
     68             assert(pos > sv.size());
     69         }
     70     }
     71 #endif
     72 }
     73 
     74 int main()
     75 {
     76     {
     77     typedef std::string S;
     78     typedef std::string_view SV;
     79     test(S(), SV(), 0, 0, S());
     80     test(S(), SV(), 1, 0, S());
     81     test(S(), SV("12345"), 0, 3, S("123"));
     82     test(S(), SV("12345"), 1, 4, S("2345"));
     83     test(S(), SV("12345"), 3, 15, S("45"));
     84     test(S(), SV("12345"), 5, 15, S(""));
     85     test(S(), SV("12345"), 6, 15, S("not happening"));
     86     test(S(), SV("12345678901234567890"), 0, 0, S());
     87     test(S(), SV("12345678901234567890"), 1, 1, S("2"));
     88     test(S(), SV("12345678901234567890"), 2, 3, S("345"));
     89     test(S(), SV("12345678901234567890"), 12, 13, S("34567890"));
     90     test(S(), SV("12345678901234567890"), 21, 13, S("not happening"));
     91 
     92     test(S("12345"), SV(), 0, 0, S());
     93     test(S("12345"), SV("12345"), 2, 2, S("34"));
     94     test(S("12345"), SV("1234567890"), 0, 100, S("1234567890"));
     95 
     96     test(S("12345678901234567890"), SV(), 0, 0, S());
     97     test(S("12345678901234567890"), SV("12345"), 1, 3, S("234"));
     98     test(S("12345678901234567890"), SV("12345678901234567890"), 5, 10,
     99          S("6789012345"));
    100     }
    101 #if TEST_STD_VER >= 11
    102     {
    103     typedef std::basic_string     <char, std::char_traits<char>, min_allocator<char>> S;
    104     typedef std::basic_string_view<char, std::char_traits<char> > SV;
    105     test(S(), SV(), 0, 0, S());
    106     test(S(), SV(), 1, 0, S());
    107     test(S(), SV("12345"), 0, 3, S("123"));
    108     test(S(), SV("12345"), 1, 4, S("2345"));
    109     test(S(), SV("12345"), 3, 15, S("45"));
    110     test(S(), SV("12345"), 5, 15, S(""));
    111     test(S(), SV("12345"), 6, 15, S("not happening"));
    112     test(S(), SV("12345678901234567890"), 0, 0, S());
    113     test(S(), SV("12345678901234567890"), 1, 1, S("2"));
    114     test(S(), SV("12345678901234567890"), 2, 3, S("345"));
    115     test(S(), SV("12345678901234567890"), 12, 13, S("34567890"));
    116     test(S(), SV("12345678901234567890"), 21, 13, S("not happening"));
    117 
    118     test(S("12345"), SV(), 0, 0, S());
    119     test(S("12345"), SV("12345"), 2, 2, S("34"));
    120     test(S("12345"), SV("1234567890"), 0, 100, S("1234567890"));
    121 
    122     test(S("12345678901234567890"), SV(), 0, 0, S());
    123     test(S("12345678901234567890"), SV("12345"), 1, 3, S("234"));
    124     test(S("12345678901234567890"), SV("12345678901234567890"), 5, 10,
    125          S("6789012345"));
    126     }
    127 #endif
    128     {
    129     typedef std::string S;
    130     typedef std::string_view SV;
    131     test_npos(S(), SV(), 0, S());
    132     test_npos(S(), SV(), 1, S());
    133     test_npos(S(), SV("12345"), 0, S("12345"));
    134     test_npos(S(), SV("12345"), 1, S("2345"));
    135     test_npos(S(), SV("12345"), 3, S("45"));
    136     test_npos(S(), SV("12345"), 5, S(""));
    137     test_npos(S(), SV("12345"), 6, S("not happening"));
    138     }
    139 
    140     {
    141     std::string s = "ABCD";
    142     std::string_view sv = "EFGH";
    143     char arr[] = "IJKL";
    144 
    145     s.assign("CDEF", 0);    // calls assign(const char *, len)
    146     assert(s == "");
    147     s.clear();
    148 
    149     s.assign("QRST", 0, std::string::npos); // calls assign(string("QRST", pos, len)
    150     assert(s == "QRST");
    151     s.clear();
    152 
    153     s.assign(sv, 0);  // calls assign(T, pos, npos)
    154     assert(s == sv);
    155     s.clear();
    156 
    157     s.assign(sv, 0, std::string::npos);   // calls assign(T, pos, npos)
    158     assert(s == sv);
    159     s.clear();
    160 
    161     s.assign(arr, 0);     // calls assign(const char *, len)
    162     assert(s == "");
    163     s.clear();
    164 
    165     s.assign(arr, 0, std::string::npos);    // calls assign(string("IJKL"), pos, npos)
    166     assert(s == "IJKL");
    167     s.clear();
    168 
    169     s.assign(arr, 0);     // calls assign(const char *, len)
    170     assert(s == "");
    171     s.clear();
    172     }
    173 
    174     {
    175     std::string s = "ABCD";
    176     std::string_view sv = s;
    177     s.assign(sv);
    178     assert(s == "ABCD");
    179 
    180     sv = s;
    181     s.assign(sv, 0, std::string::npos);
    182     assert(s == "ABCD");
    183     }
    184 
    185     {
    186     std::string s = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    187     std::string_view sv = s;
    188     s.assign(sv);
    189     assert(s == "ABCDEFGHIJKLMNOPQRSTUVWXYZ");
    190 
    191     sv = s;
    192     s.assign(sv, 0, std::string::npos);
    193     assert(s == "ABCDEFGHIJKLMNOPQRSTUVWXYZ");
    194     }
    195 }
    196