Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1 | //===----------------------------------------------------------------------===// |
| 2 | // |
Howard Hinnant | f5256e1 | 2010-05-11 21:36:01 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 4 | // |
Howard Hinnant | b64f8b0 | 2010-11-16 22:09:02 +0000 | [diff] [blame] | 5 | // This file is dual licensed under the MIT and the University of Illinois Open |
| 6 | // Source Licenses. See LICENSE.TXT for details. |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
| 10 | // <string> |
| 11 | |
| 12 | // size_type max_size() const; |
| 13 | |
Eric Fiselier | 1c3b15d | 2014-11-14 03:16:12 +0000 | [diff] [blame] | 14 | // NOTE: asan and msan will fail for one of two reasons |
| 15 | // 1. If allocator_may_return_null=0 then they will fail because the allocation |
| 16 | // returns null. |
| 17 | // 2. If allocator_may_return_null=1 then they will fail because the allocation |
| 18 | // is too large to succeed. |
Eric Fiselier | 07a4bec | 2015-03-10 20:46:04 +0000 | [diff] [blame] | 19 | // UNSUPPORTED: sanitizer-new-delete |
Eric Fiselier | 1c3b15d | 2014-11-14 03:16:12 +0000 | [diff] [blame] | 20 | |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 21 | #include <string> |
| 22 | #include <cassert> |
| 23 | |
Marshall Clow | 061d0cc | 2013-11-26 20:58:02 +0000 | [diff] [blame] | 24 | #include "min_allocator.h" |
Howard Hinnant | 9dcdcde | 2013-06-28 16:59:19 +0000 | [diff] [blame] | 25 | |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 26 | template <class S> |
| 27 | void |
Marshall Clow | ecc8d7b | 2013-11-06 14:24:38 +0000 | [diff] [blame] | 28 | test1(const S& s) |
| 29 | { |
| 30 | S s2(s); |
| 31 | const size_t sz = s2.max_size() - 1; |
| 32 | try { s2.resize(sz, 'x'); } |
| 33 | catch ( const std::bad_alloc & ) { return ; } |
| 34 | assert ( s2.size() == sz ); |
| 35 | } |
| 36 | |
| 37 | template <class S> |
| 38 | void |
| 39 | test2(const S& s) |
| 40 | { |
| 41 | S s2(s); |
| 42 | const size_t sz = s2.max_size(); |
| 43 | try { s2.resize(sz, 'x'); } |
| 44 | catch ( const std::bad_alloc & ) { return ; } |
| 45 | assert ( s.size() == sz ); |
| 46 | } |
| 47 | |
| 48 | template <class S> |
| 49 | void |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 50 | test(const S& s) |
| 51 | { |
| 52 | assert(s.max_size() >= s.size()); |
Marshall Clow | ecc8d7b | 2013-11-06 14:24:38 +0000 | [diff] [blame] | 53 | test1(s); |
| 54 | test2(s); |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | int main() |
| 58 | { |
Howard Hinnant | 9dcdcde | 2013-06-28 16:59:19 +0000 | [diff] [blame] | 59 | { |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 60 | typedef std::string S; |
| 61 | test(S()); |
| 62 | test(S("123")); |
| 63 | test(S("12345678901234567890123456789012345678901234567890")); |
Howard Hinnant | 9dcdcde | 2013-06-28 16:59:19 +0000 | [diff] [blame] | 64 | } |
| 65 | #if __cplusplus >= 201103L |
| 66 | { |
| 67 | typedef std::basic_string<char, std::char_traits<char>, min_allocator<char>> S; |
| 68 | test(S()); |
| 69 | test(S("123")); |
| 70 | test(S("12345678901234567890123456789012345678901234567890")); |
| 71 | } |
| 72 | #endif |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 73 | } |