]> 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++ trunk r290819:
[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 // <string>
11
12 // basic_string<charT,traits,Allocator>&
13 //   append(const basic_string<charT,traits>& str, size_type pos, size_type n = npos);
14 //  the "= npos" was added for C++14
15
16 #include <string>
17 #include <stdexcept>
18 #include <cassert>
19
20 #include "test_macros.h"
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     if (pos <= str.size())
28     {
29         s.append(str, pos, n);
30         LIBCPP_ASSERT(s.__invariants());
31         assert(s == expected);
32     }
33 #ifndef TEST_HAS_NO_EXCEPTIONS
34     else
35     {
36         try
37         {
38             s.append(str, pos, n);
39             assert(false);
40         }
41         catch (std::out_of_range&)
42         {
43             assert(pos > str.size());
44         }
45     }
46 #endif
47 }
48
49 template <class S>
50 void
51 test_npos(S s, S str, typename S::size_type pos, S expected)
52 {
53     if (pos <= str.size())
54     {
55         s.append(str, pos);
56         LIBCPP_ASSERT(s.__invariants());
57         assert(s == expected);
58     }
59 #ifndef TEST_HAS_NO_EXCEPTIONS
60     else
61     {
62         try
63         {
64             s.append(str, pos);
65             assert(false);
66         }
67         catch (std::out_of_range&)
68         {
69             assert(pos > str.size());
70         }
71     }
72 #endif
73 }
74
75 int main()
76 {
77     {
78     typedef std::string S;
79     test(S(), S(), 0, 0, S());
80     test(S(), S(), 1, 0, S());
81     test(S(), S("12345"), 0, 3, S("123"));
82     test(S(), S("12345"), 1, 4, S("2345"));
83     test(S(), S("12345"), 3, 15, S("45"));
84     test(S(), S("12345"), 5, 15, S(""));
85     test(S(), S("12345"), 6, 15, S("not happening"));
86     test(S(), S("12345678901234567890"), 0, 0, S());
87     test(S(), S("12345678901234567890"), 1, 1, S("2"));
88     test(S(), S("12345678901234567890"), 2, 3, S("345"));
89     test(S(), S("12345678901234567890"), 12, 13, S("34567890"));
90     test(S(), S("12345678901234567890"), 21, 13, S("not happening"));
91
92     test(S("12345"), S(), 0, 0, S("12345"));
93     test(S("12345"), S("12345"), 2, 2, S("1234534"));
94     test(S("12345"), S("1234567890"), 0, 100, S("123451234567890"));
95
96     test(S("12345678901234567890"), S(), 0, 0, S("12345678901234567890"));
97     test(S("12345678901234567890"), S("12345"), 1, 3, S("12345678901234567890234"));
98     test(S("12345678901234567890"), S("12345678901234567890"), 5, 10,
99          S("123456789012345678906789012345"));
100     }
101 #if TEST_STD_VER >= 11
102     {
103     typedef std::basic_string<char, std::char_traits<char>, min_allocator<char>> S;
104     test(S(), S(), 0, 0, S());
105     test(S(), S(), 1, 0, S());
106     test(S(), S("12345"), 0, 3, S("123"));
107     test(S(), S("12345"), 1, 4, S("2345"));
108     test(S(), S("12345"), 3, 15, S("45"));
109     test(S(), S("12345"), 5, 15, S(""));
110     test(S(), S("12345"), 6, 15, S("not happening"));
111     test(S(), S("12345678901234567890"), 0, 0, S());
112     test(S(), S("12345678901234567890"), 1, 1, S("2"));
113     test(S(), S("12345678901234567890"), 2, 3, S("345"));
114     test(S(), S("12345678901234567890"), 12, 13, S("34567890"));
115     test(S(), S("12345678901234567890"), 21, 13, S("not happening"));
116
117     test(S("12345"), S(), 0, 0, S("12345"));
118     test(S("12345"), S("12345"), 2, 2, S("1234534"));
119     test(S("12345"), S("1234567890"), 0, 100, S("123451234567890"));
120
121     test(S("12345678901234567890"), S(), 0, 0, S("12345678901234567890"));
122     test(S("12345678901234567890"), S("12345"), 1, 3, S("12345678901234567890234"));
123     test(S("12345678901234567890"), S("12345678901234567890"), 5, 10,
124          S("123456789012345678906789012345"));
125     }
126 #endif
127     {
128     typedef std::string S;
129     test_npos(S(), S(), 0, S());
130     test_npos(S(), S(), 1, S());
131     test_npos(S(), S("12345"), 0, S("12345"));
132     test_npos(S(), S("12345"), 1, S("2345"));
133     test_npos(S(), S("12345"), 3, S("45"));
134     test_npos(S(), S("12345"), 5, S(""));
135     test_npos(S(), S("12345"), 6, S("not happening"));
136     }
137 }