]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - test/std/strings/basic.string/string.modifiers/string_append/string_size_size.pass.cpp
Vendor import of libc++ release_38 branch r257836:
[FreeBSD/FreeBSD.git] / test / std / strings / basic.string / string.modifiers / string_append / string_size_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 // basic_string<charT,traits,Allocator>&
14 //   append(const basic_string<charT,traits>& str, size_type pos, size_type n = npos);
15 //  the "= npos" was added for C++14
16
17 #include <string>
18 #include <stdexcept>
19 #include <cassert>
20
21 #include "min_allocator.h"
22
23 template <class S>
24 void
25 test(S s, S str, typename S::size_type pos, typename S::size_type n, S expected)
26 {
27     try
28     {
29         s.append(str, pos, n);
30         assert(s.__invariants());
31         assert(pos <= str.size());
32         assert(s == expected);
33     }
34     catch (std::out_of_range&)
35     {
36         assert(pos > str.size());
37     }
38 }
39
40 template <class S>
41 void
42 test_npos(S s, S str, typename S::size_type pos, S expected)
43 {
44     try
45     {
46         s.append(str, pos);
47         assert(s.__invariants());
48         assert(pos <= str.size());
49         assert(s == expected);
50     }
51     catch (std::out_of_range&)
52     {
53         assert(pos > str.size());
54     }
55 }
56
57 int main()
58 {
59     {
60     typedef std::string S;
61     test(S(), S(), 0, 0, S());
62     test(S(), S(), 1, 0, S());
63     test(S(), S("12345"), 0, 3, S("123"));
64     test(S(), S("12345"), 1, 4, S("2345"));
65     test(S(), S("12345"), 3, 15, S("45"));
66     test(S(), S("12345"), 5, 15, S(""));
67     test(S(), S("12345"), 6, 15, S("not happening"));
68     test(S(), S("12345678901234567890"), 0, 0, S());
69     test(S(), S("12345678901234567890"), 1, 1, S("2"));
70     test(S(), S("12345678901234567890"), 2, 3, S("345"));
71     test(S(), S("12345678901234567890"), 12, 13, S("34567890"));
72     test(S(), S("12345678901234567890"), 21, 13, S("not happening"));
73
74     test(S("12345"), S(), 0, 0, S("12345"));
75     test(S("12345"), S("12345"), 2, 2, S("1234534"));
76     test(S("12345"), S("1234567890"), 0, 100, S("123451234567890"));
77
78     test(S("12345678901234567890"), S(), 0, 0, S("12345678901234567890"));
79     test(S("12345678901234567890"), S("12345"), 1, 3, S("12345678901234567890234"));
80     test(S("12345678901234567890"), S("12345678901234567890"), 5, 10,
81          S("123456789012345678906789012345"));
82     }
83 #if TEST_STD_VER >= 11
84     {
85     typedef std::basic_string<char, std::char_traits<char>, min_allocator<char>> S;
86     test(S(), S(), 0, 0, S());
87     test(S(), S(), 1, 0, S());
88     test(S(), S("12345"), 0, 3, S("123"));
89     test(S(), S("12345"), 1, 4, S("2345"));
90     test(S(), S("12345"), 3, 15, S("45"));
91     test(S(), S("12345"), 5, 15, S(""));
92     test(S(), S("12345"), 6, 15, S("not happening"));
93     test(S(), S("12345678901234567890"), 0, 0, S());
94     test(S(), S("12345678901234567890"), 1, 1, S("2"));
95     test(S(), S("12345678901234567890"), 2, 3, S("345"));
96     test(S(), S("12345678901234567890"), 12, 13, S("34567890"));
97     test(S(), S("12345678901234567890"), 21, 13, S("not happening"));
98
99     test(S("12345"), S(), 0, 0, S("12345"));
100     test(S("12345"), S("12345"), 2, 2, S("1234534"));
101     test(S("12345"), S("1234567890"), 0, 100, S("123451234567890"));
102
103     test(S("12345678901234567890"), S(), 0, 0, S("12345678901234567890"));
104     test(S("12345678901234567890"), S("12345"), 1, 3, S("12345678901234567890234"));
105     test(S("12345678901234567890"), S("12345678901234567890"), 5, 10,
106          S("123456789012345678906789012345"));
107     }
108 #endif
109     {
110     typedef std::string S;
111     test_npos(S(), S(), 0, S());
112     test_npos(S(), S(), 1, S());
113     test_npos(S(), S("12345"), 0, S("12345"));
114     test_npos(S(), S("12345"), 1, S("2345"));
115     test_npos(S(), S("12345"), 3, S("45"));
116     test_npos(S(), S("12345"), 5, S(""));
117     test_npos(S(), S("12345"), 6, S("not happening"));
118     }
119 }