blob: f293df971f21aa2aad360e3ad0e1dc9a7a9e5471 [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// void resize(size_type n, charT c);
13
14#include <string>
15#include <stdexcept>
16#include <cassert>
17
Marshall Clow061d0cc2013-11-26 20:58:02 +000018#include "min_allocator.h"
Howard Hinnant9dcdcde2013-06-28 16:59:19 +000019
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000020template <class S>
21void
22test(S s, typename S::size_type n, typename S::value_type c, S expected)
23{
24 try
25 {
26 s.resize(n, c);
Dan Albert1d4a1ed2016-05-25 22:36:09 -070027 assert(s.__invariants());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000028 assert(n <= s.max_size());
29 assert(s == expected);
30 }
31 catch (std::length_error&)
32 {
33 assert(n > s.max_size());
34 }
35}
36
37int main()
38{
Howard Hinnant9dcdcde2013-06-28 16:59:19 +000039 {
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000040 typedef std::string S;
41 test(S(), 0, 'a', S());
42 test(S(), 1, 'a', S("a"));
43 test(S(), 10, 'a', S(10, 'a'));
44 test(S(), 100, 'a', S(100, 'a'));
45 test(S("12345"), 0, 'a', S());
46 test(S("12345"), 2, 'a', S("12"));
47 test(S("12345"), 5, 'a', S("12345"));
48 test(S("12345"), 15, 'a', S("12345aaaaaaaaaa"));
49 test(S("12345678901234567890123456789012345678901234567890"), 0, 'a', S());
50 test(S("12345678901234567890123456789012345678901234567890"), 10, 'a',
51 S("1234567890"));
52 test(S("12345678901234567890123456789012345678901234567890"), 50, 'a',
53 S("12345678901234567890123456789012345678901234567890"));
54 test(S("12345678901234567890123456789012345678901234567890"), 60, 'a',
55 S("12345678901234567890123456789012345678901234567890aaaaaaaaaa"));
56 test(S(), S::npos, 'a', S("not going to happen"));
Howard Hinnant9dcdcde2013-06-28 16:59:19 +000057 }
Dan Albert1d4a1ed2016-05-25 22:36:09 -070058#if __cplusplus >= 201103L
Howard Hinnant9dcdcde2013-06-28 16:59:19 +000059 {
60 typedef std::basic_string<char, std::char_traits<char>, min_allocator<char>> S;
61 test(S(), 0, 'a', S());
62 test(S(), 1, 'a', S("a"));
63 test(S(), 10, 'a', S(10, 'a'));
64 test(S(), 100, 'a', S(100, 'a'));
65 test(S("12345"), 0, 'a', S());
66 test(S("12345"), 2, 'a', S("12"));
67 test(S("12345"), 5, 'a', S("12345"));
68 test(S("12345"), 15, 'a', S("12345aaaaaaaaaa"));
69 test(S("12345678901234567890123456789012345678901234567890"), 0, 'a', S());
70 test(S("12345678901234567890123456789012345678901234567890"), 10, 'a',
71 S("1234567890"));
72 test(S("12345678901234567890123456789012345678901234567890"), 50, 'a',
73 S("12345678901234567890123456789012345678901234567890"));
74 test(S("12345678901234567890123456789012345678901234567890"), 60, 'a',
75 S("12345678901234567890123456789012345678901234567890aaaaaaaaaa"));
76 test(S(), S::npos, 'a', S("not going to happen"));
77 }
78#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000079}