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 // size_type copy(charT* s, size_type n, size_type pos = 0) const;
13
14 #include <string>
15 #include <stdexcept>
16 #include <algorithm>
17 #include <cassert>
18
19 #include "test_macros.h"
20 #include "min_allocator.h"
21
22 template <class S>
23 void
test(S str,typename S::value_type * s,typename S::size_type n,typename S::size_type pos)24 test(S str, typename S::value_type* s, typename S::size_type n,
25 typename S::size_type pos)
26 {
27 const S& cs = str;
28 if (pos <= cs.size())
29 {
30 typename S::size_type r = cs.copy(s, n, pos);
31 typename S::size_type rlen = std::min(n, cs.size() - pos);
32 assert(r == rlen);
33 for (r = 0; r < rlen; ++r)
34 assert(S::traits_type::eq(cs[pos+r], s[r]));
35 }
36 #ifndef TEST_HAS_NO_EXCEPTIONS
37 else
38 {
39 try
40 {
41 typename S::size_type r = cs.copy(s, n, pos);
42 ((void)r); // Prevent unused warning
43 assert(false);
44 }
45 catch (std::out_of_range&)
46 {
47 assert(pos > str.size());
48 }
49 }
50 #endif
51 }
52
main()53 int main()
54 {
55 {
56 typedef std::string S;
57 char s[50];
58 test(S(""), s, 0, 0);
59 test(S(""), s, 0, 1);
60 test(S(""), s, 1, 0);
61 test(S("abcde"), s, 0, 0);
62 test(S("abcde"), s, 0, 1);
63 test(S("abcde"), s, 0, 2);
64 test(S("abcde"), s, 0, 4);
65 test(S("abcde"), s, 0, 5);
66 test(S("abcde"), s, 0, 6);
67 test(S("abcde"), s, 1, 0);
68 test(S("abcde"), s, 1, 1);
69 test(S("abcde"), s, 1, 2);
70 test(S("abcde"), s, 1, 4);
71 test(S("abcde"), s, 1, 5);
72 test(S("abcde"), s, 2, 0);
73 test(S("abcde"), s, 2, 1);
74 test(S("abcde"), s, 2, 2);
75 test(S("abcde"), s, 2, 4);
76 test(S("abcde"), s, 4, 0);
77 test(S("abcde"), s, 4, 1);
78 test(S("abcde"), s, 4, 2);
79 test(S("abcde"), s, 5, 0);
80 test(S("abcde"), s, 5, 1);
81 test(S("abcde"), s, 6, 0);
82 test(S("abcdefghijklmnopqrst"), s, 0, 0);
83 test(S("abcdefghijklmnopqrst"), s, 0, 1);
84 test(S("abcdefghijklmnopqrst"), s, 0, 2);
85 test(S("abcdefghijklmnopqrst"), s, 0, 10);
86 test(S("abcdefghijklmnopqrst"), s, 0, 19);
87 test(S("abcdefghijklmnopqrst"), s, 0, 20);
88 test(S("abcdefghijklmnopqrst"), s, 0, 21);
89 test(S("abcdefghijklmnopqrst"), s, 1, 0);
90 test(S("abcdefghijklmnopqrst"), s, 1, 1);
91 test(S("abcdefghijklmnopqrst"), s, 1, 2);
92 test(S("abcdefghijklmnopqrst"), s, 1, 9);
93 test(S("abcdefghijklmnopqrst"), s, 1, 18);
94 test(S("abcdefghijklmnopqrst"), s, 1, 19);
95 test(S("abcdefghijklmnopqrst"), s, 1, 20);
96 test(S("abcdefghijklmnopqrst"), s, 2, 0);
97 test(S("abcdefghijklmnopqrst"), s, 2, 1);
98 test(S("abcdefghijklmnopqrst"), s, 2, 2);
99 test(S("abcdefghijklmnopqrst"), s, 2, 9);
100 test(S("abcdefghijklmnopqrst"), s, 2, 17);
101 test(S("abcdefghijklmnopqrst"), s, 2, 18);
102 test(S("abcdefghijklmnopqrst"), s, 2, 19);
103 test(S("abcdefghijklmnopqrst"), s, 10, 0);
104 test(S("abcdefghijklmnopqrst"), s, 10, 1);
105 test(S("abcdefghijklmnopqrst"), s, 10, 2);
106 test(S("abcdefghijklmnopqrst"), s, 10, 5);
107 test(S("abcdefghijklmnopqrst"), s, 10, 9);
108 test(S("abcdefghijklmnopqrst"), s, 10, 10);
109 test(S("abcdefghijklmnopqrst"), s, 10, 11);
110 test(S("abcdefghijklmnopqrst"), s, 19, 0);
111 test(S("abcdefghijklmnopqrst"), s, 19, 1);
112 test(S("abcdefghijklmnopqrst"), s, 19, 2);
113 test(S("abcdefghijklmnopqrst"), s, 20, 0);
114 test(S("abcdefghijklmnopqrst"), s, 20, 1);
115 test(S("abcdefghijklmnopqrst"), s, 21, 0);
116 }
117 #if TEST_STD_VER >= 11
118 {
119 typedef std::basic_string<char, std::char_traits<char>, min_allocator<char>> S;
120 char s[50];
121 test(S(""), s, 0, 0);
122 test(S(""), s, 0, 1);
123 test(S(""), s, 1, 0);
124 test(S("abcde"), s, 0, 0);
125 test(S("abcde"), s, 0, 1);
126 test(S("abcde"), s, 0, 2);
127 test(S("abcde"), s, 0, 4);
128 test(S("abcde"), s, 0, 5);
129 test(S("abcde"), s, 0, 6);
130 test(S("abcde"), s, 1, 0);
131 test(S("abcde"), s, 1, 1);
132 test(S("abcde"), s, 1, 2);
133 test(S("abcde"), s, 1, 4);
134 test(S("abcde"), s, 1, 5);
135 test(S("abcde"), s, 2, 0);
136 test(S("abcde"), s, 2, 1);
137 test(S("abcde"), s, 2, 2);
138 test(S("abcde"), s, 2, 4);
139 test(S("abcde"), s, 4, 0);
140 test(S("abcde"), s, 4, 1);
141 test(S("abcde"), s, 4, 2);
142 test(S("abcde"), s, 5, 0);
143 test(S("abcde"), s, 5, 1);
144 test(S("abcde"), s, 6, 0);
145 test(S("abcdefghijklmnopqrst"), s, 0, 0);
146 test(S("abcdefghijklmnopqrst"), s, 0, 1);
147 test(S("abcdefghijklmnopqrst"), s, 0, 2);
148 test(S("abcdefghijklmnopqrst"), s, 0, 10);
149 test(S("abcdefghijklmnopqrst"), s, 0, 19);
150 test(S("abcdefghijklmnopqrst"), s, 0, 20);
151 test(S("abcdefghijklmnopqrst"), s, 0, 21);
152 test(S("abcdefghijklmnopqrst"), s, 1, 0);
153 test(S("abcdefghijklmnopqrst"), s, 1, 1);
154 test(S("abcdefghijklmnopqrst"), s, 1, 2);
155 test(S("abcdefghijklmnopqrst"), s, 1, 9);
156 test(S("abcdefghijklmnopqrst"), s, 1, 18);
157 test(S("abcdefghijklmnopqrst"), s, 1, 19);
158 test(S("abcdefghijklmnopqrst"), s, 1, 20);
159 test(S("abcdefghijklmnopqrst"), s, 2, 0);
160 test(S("abcdefghijklmnopqrst"), s, 2, 1);
161 test(S("abcdefghijklmnopqrst"), s, 2, 2);
162 test(S("abcdefghijklmnopqrst"), s, 2, 9);
163 test(S("abcdefghijklmnopqrst"), s, 2, 17);
164 test(S("abcdefghijklmnopqrst"), s, 2, 18);
165 test(S("abcdefghijklmnopqrst"), s, 2, 19);
166 test(S("abcdefghijklmnopqrst"), s, 10, 0);
167 test(S("abcdefghijklmnopqrst"), s, 10, 1);
168 test(S("abcdefghijklmnopqrst"), s, 10, 2);
169 test(S("abcdefghijklmnopqrst"), s, 10, 5);
170 test(S("abcdefghijklmnopqrst"), s, 10, 9);
171 test(S("abcdefghijklmnopqrst"), s, 10, 10);
172 test(S("abcdefghijklmnopqrst"), s, 10, 11);
173 test(S("abcdefghijklmnopqrst"), s, 19, 0);
174 test(S("abcdefghijklmnopqrst"), s, 19, 1);
175 test(S("abcdefghijklmnopqrst"), s, 19, 2);
176 test(S("abcdefghijklmnopqrst"), s, 20, 0);
177 test(S("abcdefghijklmnopqrst"), s, 20, 1);
178 test(S("abcdefghijklmnopqrst"), s, 21, 0);
179 }
180 #endif
181 }
182