Home | History | Annotate | Download | only in string.capacity
      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 // size_type max_size() const;
     13 
     14 #include <string>
     15 #include <cassert>
     16 
     17 #include "min_allocator.h"
     18 
     19 template <class S>
     20 void
     21 test1(const S& s)
     22 {
     23     S s2(s);
     24     const size_t sz = s2.max_size() - 1;
     25     try { s2.resize(sz, 'x'); }
     26     catch ( const std::bad_alloc & ) { return ; }
     27     assert ( s2.size() ==  sz );
     28 }
     29 
     30 template <class S>
     31 void
     32 test2(const S& s)
     33 {
     34     S s2(s);
     35     const size_t sz = s2.max_size();
     36     try { s2.resize(sz, 'x'); }
     37     catch ( const std::bad_alloc & ) { return ; }
     38     assert ( s.size() ==  sz );
     39 }
     40 
     41 template <class S>
     42 void
     43 test3(const S& s)
     44 {
     45     S s2(s);
     46     const size_t sz = s2.max_size() + 1;
     47     try { s2.resize(sz, 'x'); }
     48     catch ( const std::length_error & ) { return ; }
     49     assert ( false );
     50 }
     51 
     52 template <class S>
     53 void
     54 test(const S& s)
     55 {
     56     assert(s.max_size() >= s.size());
     57     test1(s);
     58     test2(s);
     59     test3(s);
     60 }
     61 
     62 int main()
     63 {
     64     {
     65     typedef std::string S;
     66     test(S());
     67     test(S("123"));
     68     test(S("12345678901234567890123456789012345678901234567890"));
     69     }
     70 #if __cplusplus >= 201103L
     71     {
     72     typedef std::basic_string<char, std::char_traits<char>, min_allocator<char>> S;
     73     test(S());
     74     test(S("123"));
     75     test(S("12345678901234567890123456789012345678901234567890"));
     76     }
     77 #endif
     78 }
     79