blob: 14fe209fd59a3cc176d8601a889582dae54e5843 [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);
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, S expected)
23{
24 try
25 {
26 s.resize(n);
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, S());
42 test(S(), 1, S(1, '\0'));
43 test(S(), 10, S(10, '\0'));
44 test(S(), 100, S(100, '\0'));
45 test(S("12345"), 0, S());
46 test(S("12345"), 2, S("12"));
47 test(S("12345"), 5, S("12345"));
48 test(S("12345"), 15, S("12345\0\0\0\0\0\0\0\0\0\0", 15));
49 test(S("12345678901234567890123456789012345678901234567890"), 0, S());
50 test(S("12345678901234567890123456789012345678901234567890"), 10,
51 S("1234567890"));
52 test(S("12345678901234567890123456789012345678901234567890"), 50,
53 S("12345678901234567890123456789012345678901234567890"));
54 test(S("12345678901234567890123456789012345678901234567890"), 60,
55 S("12345678901234567890123456789012345678901234567890\0\0\0\0\0\0\0\0\0\0", 60));
56 test(S(), S::npos, 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, S());
62 test(S(), 1, S(1, '\0'));
63 test(S(), 10, S(10, '\0'));
64 test(S(), 100, S(100, '\0'));
65 test(S("12345"), 0, S());
66 test(S("12345"), 2, S("12"));
67 test(S("12345"), 5, S("12345"));
68 test(S("12345"), 15, S("12345\0\0\0\0\0\0\0\0\0\0", 15));
69 test(S("12345678901234567890123456789012345678901234567890"), 0, S());
70 test(S("12345678901234567890123456789012345678901234567890"), 10,
71 S("1234567890"));
72 test(S("12345678901234567890123456789012345678901234567890"), 50,
73 S("12345678901234567890123456789012345678901234567890"));
74 test(S("12345678901234567890123456789012345678901234567890"), 60,
75 S("12345678901234567890123456789012345678901234567890\0\0\0\0\0\0\0\0\0\0", 60));
76 test(S(), S::npos, S("not going to happen"));
77 }
78#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000079}