]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - test/std/strings/basic.string/string.capacity/resize_size.pass.cpp
Vendor import of libc++ trunk r256633:
[FreeBSD/FreeBSD.git] / test / std / strings / basic.string / string.capacity / resize_size.pass.cpp
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 // XFAIL: libcpp-no-exceptions
11 // <string>
12
13 // void resize(size_type n);
14
15 #include <string>
16 #include <stdexcept>
17 #include <cassert>
18
19 #include "min_allocator.h"
20
21 template <class S>
22 void
23 test(S s, typename S::size_type n, S expected)
24 {
25     try
26     {
27         s.resize(n);
28         assert(s.__invariants());
29         assert(n <= s.max_size());
30         assert(s == expected);
31     }
32     catch (std::length_error&)
33     {
34         assert(n > s.max_size());
35     }
36 }
37
38 int main()
39 {
40     {
41     typedef std::string S;
42     test(S(), 0, S());
43     test(S(), 1, S(1, '\0'));
44     test(S(), 10, S(10, '\0'));
45     test(S(), 100, S(100, '\0'));
46     test(S("12345"), 0, S());
47     test(S("12345"), 2, S("12"));
48     test(S("12345"), 5, S("12345"));
49     test(S("12345"), 15, S("12345\0\0\0\0\0\0\0\0\0\0", 15));
50     test(S("12345678901234567890123456789012345678901234567890"), 0, S());
51     test(S("12345678901234567890123456789012345678901234567890"), 10,
52          S("1234567890"));
53     test(S("12345678901234567890123456789012345678901234567890"), 50,
54          S("12345678901234567890123456789012345678901234567890"));
55     test(S("12345678901234567890123456789012345678901234567890"), 60,
56          S("12345678901234567890123456789012345678901234567890\0\0\0\0\0\0\0\0\0\0", 60));
57     test(S(), S::npos, S("not going to happen"));
58     }
59 #if __cplusplus >= 201103L
60     {
61     typedef std::basic_string<char, std::char_traits<char>, min_allocator<char>> S;
62     test(S(), 0, S());
63     test(S(), 1, S(1, '\0'));
64     test(S(), 10, S(10, '\0'));
65     test(S(), 100, S(100, '\0'));
66     test(S("12345"), 0, S());
67     test(S("12345"), 2, S("12"));
68     test(S("12345"), 5, S("12345"));
69     test(S("12345"), 15, S("12345\0\0\0\0\0\0\0\0\0\0", 15));
70     test(S("12345678901234567890123456789012345678901234567890"), 0, S());
71     test(S("12345678901234567890123456789012345678901234567890"), 10,
72          S("1234567890"));
73     test(S("12345678901234567890123456789012345678901234567890"), 50,
74          S("12345678901234567890123456789012345678901234567890"));
75     test(S("12345678901234567890123456789012345678901234567890"), 60,
76          S("12345678901234567890123456789012345678901234567890\0\0\0\0\0\0\0\0\0\0", 60));
77     test(S(), S::npos, S("not going to happen"));
78     }
79 #endif
80 }