libcxx

libcxx mirror with random patches
git clone https://git.neptards.moe/neptards/libcxx.git
Log | Files | Refs

substr.pass.cpp (7406B)


      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(const basic_string<charT,traits,Allocator>& str,
     13 //              size_type pos, size_type n,
     14 //              const Allocator& a = Allocator());
     15 //
     16 // basic_string(const basic_string<charT,traits,Allocator>& str,
     17 //              size_type pos,
     18 //              const Allocator& a = Allocator());
     19 
     20 #include <string>
     21 #include <stdexcept>
     22 #include <algorithm>
     23 #include <vector>
     24 #include <scoped_allocator>
     25 #include <cassert>
     26 
     27 #include "test_macros.h"
     28 #include "test_allocator.h"
     29 #include "min_allocator.h"
     30 
     31 template <class S>
     32 void
     33 test(S str, unsigned pos)
     34 {
     35     typedef typename S::traits_type T;
     36     typedef typename S::allocator_type A;
     37 
     38     if (pos <= str.size())
     39     {
     40         S s2(str, pos);
     41         LIBCPP_ASSERT(s2.__invariants());
     42         typename S::size_type rlen = str.size() - pos;
     43         assert(s2.size() == rlen);
     44         assert(T::compare(s2.data(), str.data() + pos, rlen) == 0);
     45         assert(s2.get_allocator() == A());
     46         assert(s2.capacity() >= s2.size());
     47     }
     48 #ifndef TEST_HAS_NO_EXCEPTIONS
     49     else
     50     {
     51         try
     52         {
     53             S s2(str, pos);
     54             assert(false);
     55         }
     56         catch (std::out_of_range&)
     57         {
     58             assert(pos > str.size());
     59         }
     60     }
     61 #endif
     62 }
     63 
     64 template <class S>
     65 void
     66 test(S str, unsigned pos, unsigned n)
     67 {
     68     typedef typename S::traits_type T;
     69     typedef typename S::allocator_type A;
     70     if (pos <= str.size())
     71     {
     72         S s2(str, pos, n);
     73         LIBCPP_ASSERT(s2.__invariants());
     74         typename S::size_type rlen = std::min<typename S::size_type>(str.size() - pos, n);
     75         assert(s2.size() == rlen);
     76         assert(T::compare(s2.data(), str.data() + pos, rlen) == 0);
     77         assert(s2.get_allocator() == A());
     78         assert(s2.capacity() >= s2.size());
     79     }
     80 #ifndef TEST_HAS_NO_EXCEPTIONS
     81     else
     82     {
     83         try
     84         {
     85             S s2(str, pos, n);
     86             assert(false);
     87         }
     88         catch (std::out_of_range&)
     89         {
     90             assert(pos > str.size());
     91         }
     92     }
     93 #endif
     94 }
     95 
     96 template <class S>
     97 void
     98 test(S str, unsigned pos, unsigned n, const typename S::allocator_type& a)
     99 {
    100     typedef typename S::traits_type T;
    101 
    102     if (pos <= str.size())
    103     {
    104         S s2(str, pos, n, a);
    105         LIBCPP_ASSERT(s2.__invariants());
    106         typename S::size_type rlen = std::min<typename S::size_type>(str.size() - pos, n);
    107         assert(s2.size() == rlen);
    108         assert(T::compare(s2.data(), str.data() + pos, rlen) == 0);
    109         assert(s2.get_allocator() == a);
    110         assert(s2.capacity() >= s2.size());
    111     }
    112 #ifndef TEST_HAS_NO_EXCEPTIONS
    113     else
    114     {
    115         try
    116         {
    117             S s2(str, pos, n, a);
    118             assert(false);
    119         }
    120         catch (std::out_of_range&)
    121         {
    122             assert(pos > str.size());
    123         }
    124     }
    125 #endif
    126 }
    127 
    128 #if TEST_STD_VER >= 11
    129 #ifndef TEST_HAS_NO_EXCEPTIONS
    130 void test2583()
    131 {   // LWG #2583
    132     typedef std::basic_string<char, std::char_traits<char>, test_allocator<char> > StringA;
    133     std::vector<StringA, std::scoped_allocator_adaptor<test_allocator<StringA>>> vs;
    134     StringA s{"1234"};
    135     vs.emplace_back(s, 2);
    136 
    137     try { vs.emplace_back(s, 5); }
    138     catch (const std::out_of_range&) { return; }
    139     assert(false);
    140 }
    141 #endif
    142 #endif
    143 
    144 int main()
    145 {
    146     {
    147     typedef test_allocator<char> A;
    148     typedef std::basic_string<char, std::char_traits<char>, A> S;
    149 
    150     test(S(A(3)), 0);
    151     test(S(A(3)), 1);
    152     test(S("1", A(5)), 0);
    153     test(S("1", A(5)), 1);
    154     test(S("1", A(5)), 2);
    155     test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A(7)), 0);
    156     test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A(7)), 5);
    157     test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A(7)), 50);
    158     test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A(7)), 500);
    159 
    160     test(S(A(3)), 0, 0);
    161     test(S(A(3)), 0, 1);
    162     test(S(A(3)), 1, 0);
    163     test(S(A(3)), 1, 1);
    164     test(S(A(3)), 1, 2);
    165     test(S("1", A(5)), 0, 0);
    166     test(S("1", A(5)), 0, 1);
    167     test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A(7)), 50, 0);
    168     test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A(7)), 50, 1);
    169     test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A(7)), 50, 10);
    170     test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A(7)), 50, 100);
    171 
    172     test(S(A(3)), 0, 0, A(4));
    173     test(S(A(3)), 0, 1, A(4));
    174     test(S(A(3)), 1, 0, A(4));
    175     test(S(A(3)), 1, 1, A(4));
    176     test(S(A(3)), 1, 2, A(4));
    177     test(S("1", A(5)), 0, 0, A(6));
    178     test(S("1", A(5)), 0, 1, A(6));
    179     test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A(7)), 50, 0, A(8));
    180     test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A(7)), 50, 1, A(8));
    181     test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A(7)), 50, 10, A(8));
    182     test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A(7)), 50, 100, A(8));
    183     }
    184 #if TEST_STD_VER >= 11
    185     {
    186     typedef min_allocator<char> A;
    187     typedef std::basic_string<char, std::char_traits<char>, A> S;
    188 
    189     test(S(A()), 0);
    190     test(S(A()), 1);
    191     test(S("1", A()), 0);
    192     test(S("1", A()), 1);
    193     test(S("1", A()), 2);
    194     test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A()), 0);
    195     test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A()), 5);
    196     test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A()), 50);
    197     test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A()), 500);
    198 
    199     test(S(A()), 0, 0);
    200     test(S(A()), 0, 1);
    201     test(S(A()), 1, 0);
    202     test(S(A()), 1, 1);
    203     test(S(A()), 1, 2);
    204     test(S("1", A()), 0, 0);
    205     test(S("1", A()), 0, 1);
    206     test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A()), 50, 0);
    207     test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A()), 50, 1);
    208     test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A()), 50, 10);
    209     test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A()), 50, 100);
    210 
    211     test(S(A()), 0, 0, A());
    212     test(S(A()), 0, 1, A());
    213     test(S(A()), 1, 0, A());
    214     test(S(A()), 1, 1, A());
    215     test(S(A()), 1, 2, A());
    216     test(S("1", A()), 0, 0, A());
    217     test(S("1", A()), 0, 1, A());
    218     test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A()), 50, 0, A());
    219     test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A()), 50, 1, A());
    220     test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A()), 50, 10, A());
    221     test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A()), 50, 100, A());
    222     }
    223 
    224 #ifndef TEST_HAS_NO_EXCEPTIONS
    225     test2583();
    226 #endif
    227 #endif
    228 }