1 | /*
|
---|
2 | * Copyright (c) 2017 Jaroslav Jindrak
|
---|
3 | * All rights reserved.
|
---|
4 | *
|
---|
5 | * Redistribution and use in source and binary forms, with or without
|
---|
6 | * modification, are permitted provided that the following conditions
|
---|
7 | * are met:
|
---|
8 | *
|
---|
9 | * - Redistributions of source code must retain the above copyright
|
---|
10 | * notice, this list of conditions and the following disclaimer.
|
---|
11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
12 | * notice, this list of conditions and the following disclaimer in the
|
---|
13 | * documentation and/or other materials provided with the distribution.
|
---|
14 | * - The name of the author may not be used to endorse or promote products
|
---|
15 | * derived from this software without specific prior written permission.
|
---|
16 | *
|
---|
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
27 | */
|
---|
28 |
|
---|
29 | #include <string>
|
---|
30 | #include <initializer_list>
|
---|
31 | #include <internal/test/tests.hpp>
|
---|
32 | #include <cstdio>
|
---|
33 |
|
---|
34 | namespace std::test
|
---|
35 | {
|
---|
36 | bool string_test::run()
|
---|
37 | {
|
---|
38 | test_construction_and_assignment();
|
---|
39 | test_append();
|
---|
40 | test_insert();
|
---|
41 |
|
---|
42 | return true;
|
---|
43 | }
|
---|
44 |
|
---|
45 | const char* string_test::name()
|
---|
46 | {
|
---|
47 | return "string";
|
---|
48 | }
|
---|
49 |
|
---|
50 | void string_test::test_construction_and_assignment()
|
---|
51 | {
|
---|
52 | const char* check1 = "hello";
|
---|
53 |
|
---|
54 | std::string str1{"hello"};
|
---|
55 | test_eq(
|
---|
56 | "size of string",
|
---|
57 | str1.size(), 5ul
|
---|
58 | );
|
---|
59 | test_eq(
|
---|
60 | "initialization from a cstring literal",
|
---|
61 | str1.begin(), str1.end(),
|
---|
62 | check1, check1 + 5
|
---|
63 | );
|
---|
64 |
|
---|
65 | std::string str2{str1};
|
---|
66 | test_eq(
|
---|
67 | "copy constructor",
|
---|
68 | str1.begin(), str1.end(),
|
---|
69 | str2.begin(), str2.end()
|
---|
70 | );
|
---|
71 |
|
---|
72 | std::string str3{std::move(str1)};
|
---|
73 | test_eq(
|
---|
74 | "move constructor equality",
|
---|
75 | str2.begin(), str2.end(),
|
---|
76 | str3.begin(), str3.end()
|
---|
77 | );
|
---|
78 | test_eq(
|
---|
79 | "move constructor source empty",
|
---|
80 | str1.size(), 0ul
|
---|
81 | );
|
---|
82 |
|
---|
83 | std::string str4{};
|
---|
84 | test_eq(
|
---|
85 | "default constructor empty",
|
---|
86 | str4.size(), 0ul
|
---|
87 | );
|
---|
88 |
|
---|
89 | str4.assign(str3, 2ul, 2ul);
|
---|
90 | test_eq(
|
---|
91 | "assign substring to an empty string",
|
---|
92 | str4.begin(), str4.end(),
|
---|
93 | str3.begin() + 2, str3.begin() + 4
|
---|
94 | );
|
---|
95 |
|
---|
96 | std::string str5{str3.begin() + 2, str3.begin() + 4};
|
---|
97 | test_eq(
|
---|
98 | "constructor from a pair of iterators",
|
---|
99 | str5.begin(), str5.end(),
|
---|
100 | str3.begin() + 2, str3.begin() + 4
|
---|
101 | );
|
---|
102 | }
|
---|
103 |
|
---|
104 | void string_test::test_append()
|
---|
105 | {
|
---|
106 | std::string check{"hello, world"};
|
---|
107 |
|
---|
108 | std::string str1{"hello, "};
|
---|
109 | str1.append("world");
|
---|
110 | test_eq(
|
---|
111 | "append cstring",
|
---|
112 | str1.begin(), str1.end(),
|
---|
113 | check.begin(), check.end()
|
---|
114 | );
|
---|
115 |
|
---|
116 | std::string str2{"hello, "};
|
---|
117 | str2.append(std::string{"world"});
|
---|
118 | test_eq(
|
---|
119 | "append rvalue string",
|
---|
120 | str2.begin(), str2.end(),
|
---|
121 | check.begin(), check.end()
|
---|
122 | );
|
---|
123 |
|
---|
124 | std::string str3{"hello, "};
|
---|
125 | std::string apendee{"world"};
|
---|
126 | str3.append(apendee);
|
---|
127 | test_eq(
|
---|
128 | "append lvalue string",
|
---|
129 | str3.begin(), str3.end(),
|
---|
130 | check.begin(), check.end()
|
---|
131 | );
|
---|
132 |
|
---|
133 | std::string str4{"hello, "};
|
---|
134 | str4.append(apendee.begin(), apendee.end());
|
---|
135 | test_eq(
|
---|
136 | "append iterator range",
|
---|
137 | str4.begin(), str4.end(),
|
---|
138 | check.begin(), check.end()
|
---|
139 | );
|
---|
140 |
|
---|
141 | std::string str5{"hello, "};
|
---|
142 | str5.append({'w', 'o', 'r', 'l', 'd'});
|
---|
143 | test_eq(
|
---|
144 | "append initializer list",
|
---|
145 | str5.begin(), str5.end(),
|
---|
146 | check.begin(), check.end()
|
---|
147 | );
|
---|
148 |
|
---|
149 | std::string str6{"hello, "};
|
---|
150 | str6 += "world";
|
---|
151 | test_eq(
|
---|
152 | "append using +=",
|
---|
153 | str6.begin(), str6.end(),
|
---|
154 | check.begin(), check.end()
|
---|
155 | );
|
---|
156 | }
|
---|
157 |
|
---|
158 | void string_test::test_insert()
|
---|
159 | {
|
---|
160 | std::string check{"hello, world"};
|
---|
161 |
|
---|
162 | std::string str1{", world"};
|
---|
163 | str1.insert(0, "hello");
|
---|
164 | test_eq(
|
---|
165 | "insert at the beggining",
|
---|
166 | str1.begin(), str1.end(),
|
---|
167 | check.begin(), check.end()
|
---|
168 | );
|
---|
169 |
|
---|
170 | std::string str2{"hello,world"};
|
---|
171 | str2.insert(str2.begin() + 6, ' ');
|
---|
172 | test_eq(
|
---|
173 | "insert char in the middle",
|
---|
174 | str2.begin(), str2.end(),
|
---|
175 | check.begin(), check.end()
|
---|
176 | );
|
---|
177 |
|
---|
178 | std::string str3{"heo, world"};
|
---|
179 | str3.insert(str3.begin() + 2, 2ul, 'l');
|
---|
180 | test_eq(
|
---|
181 | "insert n chars",
|
---|
182 | str3.begin(), str3.end(),
|
---|
183 | check.begin(), check.end()
|
---|
184 | );
|
---|
185 |
|
---|
186 | std::string str4{"h, world"};
|
---|
187 | std::string insertee{"ello"};
|
---|
188 | str4.insert(str4.begin() + 1, insertee.begin(),
|
---|
189 | insertee.end());
|
---|
190 | test_eq(
|
---|
191 | "insert iterator range",
|
---|
192 | str4.begin(), str4.end(),
|
---|
193 | check.begin(), check.end()
|
---|
194 | );
|
---|
195 |
|
---|
196 | std::string str5{"hel, world"};
|
---|
197 | std::initializer_list<char> init{'l', 'o'};
|
---|
198 | str5.insert(str5.begin() + 3, init);
|
---|
199 | test_eq(
|
---|
200 | "insert initializer list",
|
---|
201 | str5.begin(), str5.end(),
|
---|
202 | check.begin(), check.end()
|
---|
203 | );
|
---|
204 | }
|
---|
205 | }
|
---|