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 19template <class S> 20void 21test1(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 30template <class S> 31void 32test2(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 41template <class S> 42void 43test3(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 52template <class S> 53void 54test(const S& s) 55{ 56 assert(s.max_size() >= s.size()); 57 test1(s); 58 test2(s); 59 test3(s); 60} 61 62int 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