blob: e4ff556d9e3f48144dca4fa09c800e3b27738c4f [file] [log] [blame]
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001//===----------------------------------------------------------------------===//
2//
Howard Hinnantf5256e12010-05-11 21:36:01 +00003// The LLVM Compiler Infrastructure
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004//
Howard Hinnantb64f8b02010-11-16 22:09:02 +00005// This file is dual licensed under the MIT and the University of Illinois Open
6// Source Licenses. See LICENSE.TXT for details.
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00007//
8//===----------------------------------------------------------------------===//
9
10// <string>
11
12// size_type max_size() const;
13
Eric Fiselier1c3b15d2014-11-14 03:16:12 +000014// 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 Fiselier07a4bec2015-03-10 20:46:04 +000019// UNSUPPORTED: sanitizer-new-delete
Eric Fiselier1c3b15d2014-11-14 03:16:12 +000020
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000021#include <string>
22#include <cassert>
23
Marshall Clow061d0cc2013-11-26 20:58:02 +000024#include "min_allocator.h"
Howard Hinnant9dcdcde2013-06-28 16:59:19 +000025
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000026template <class S>
27void
Marshall Clowecc8d7b2013-11-06 14:24:38 +000028test1(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
37template <class S>
38void
39test2(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
48template <class S>
49void
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000050test(const S& s)
51{
52 assert(s.max_size() >= s.size());
Marshall Clowecc8d7b2013-11-06 14:24:38 +000053 test1(s);
54 test2(s);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000055}
56
57int main()
58{
Howard Hinnant9dcdcde2013-06-28 16:59:19 +000059 {
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000060 typedef std::string S;
61 test(S());
62 test(S("123"));
63 test(S("12345678901234567890123456789012345678901234567890"));
Howard Hinnant9dcdcde2013-06-28 16:59:19 +000064 }
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 Hinnantbc8d3f92010-05-11 19:42:16 +000073}