-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_std_string_implementations.d
302 lines (247 loc) · 7.17 KB
/
test_std_string_implementations.d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
///////////////////////////////////////////////////////////////////////////////
// Import implementations
///////////////////////////////////////////////////////////////////////////////
import std_string_class;
import std_string_struct;
extern(C++) {
// Returns a statically allocated string by ptr.
std_string* getStringPtr();
// Returns a dynamicaly allocated string by ptr.
std_string* getStringPtr(const(char*) ptr,size_t size);
// Returns a statically allocated string by reference.
ref std_string getStringRef();
// Returns a statically allocated string by copy.
std_string getStringCopy(); // Can't work with class implementation.
// Returns the size of the string
pragma(mangle, "_Z13getStringSizeRKSs") size_t getStringSize(ref const std_string str);
}
///////////////////////////////////////////////////////////////////////////////
// Most of the tests below will allocate a string on the C++ side via the
// make function below.
// Since we don't deallocate, this program leaks memory.
///////////////////////////////////////////////////////////////////////////////
version(use_structs) {
ref std_string make(string str) {
return *getStringPtr(str.ptr, str.length);
}
}
version(use_classes) {
std_string make(string str) {
return cast(std_string)(getStringPtr(str.ptr, str.length));
}
}
import std.stdio;
void test_string_iterator() {
auto s = make("ab");
assert(s.begin[0] == 'a');
assert(s.end[-1] == 'b');
assert(s.end[0] == '\0');
assert(s.cbegin[0] == 'a');
assert(s.cend[-1] == 'b');
assert(s.cend[0] == '\0');
}
void test_string_const_iterator() {
const s = make("ab");
assert(s.begin[0] == 'a');
assert(s.end[-1] == 'b');
assert(s.end[0] == '\0');
assert(s.cbegin[0] == 'a');
assert(s.cend[-1] == 'b');
assert(s.cend[0] == '\0');
}
void test_string_capacity() {
const c = make("ab");
assert(c.size == 2);
assert(c.length == 2);
assert(c.max_size > 0);
assert(c.capacity >= 2);
assert(!c.empty());
//
auto s = make("ab");
s.clear();
assert(s.empty());
s.reserve(5);
assert(s.capacity() >= 5);
s.shrink_to_fit();
assert(s.empty());
s.resize(1);
assert(s.size == 1);
s.resize(2, 'a');
assert(s.size == 2);
}
void test_string_element_access() {
const c = make("ab");
assert(c[0] == 'a');
assert(c.at(1) == 'b');
assert(c.back() == 'b');
assert(c.front() == 'a');
auto s = make("ab");
assert(c[0] == 'a');
assert(s.at(1) == 'b');
assert(s.back() == 'b');
assert(s.front() == 'a');
s[0] = 'c';
assert(s[0] == 'c');
}
void test_string_append() {
const constant = make("abc");
auto s = make("");
s.append(constant);
assert(s.asArray == "abc");
s.clear;
s.append(constant, 1, 2);
assert(s.asArray == "bc");
s.clear;
s.append("a");
assert(s.asArray == "a");
s.clear;
s.append("ab", 1);
assert(s.asArray == "a");
s.clear;
s.append(2, 'a');
assert(s.asArray == "aa");
}
void test_string_assign() {
const constant = make("abc");
auto s = make("");
s.assign(constant);
assert(s.asArray == "abc");
s.assign(constant, 1, 2);
assert(s.asArray == "bc");
s.assign("a");
assert(s.asArray == "a");
s.assign("ab", 1);
assert(s.asArray == "a");
s.assign(2, 'a');
assert(s.asArray == "aa");
}
void test_string_insert() {
const abc = make("abc");
const def = make("def");
auto s = make("abc");
s.insert(1, def);
assert(s.asArray == "adefbc");
s.assign(abc);
s.insert(1, def, 1, 1);
assert(s.asArray == "aebc");
s.assign(abc);
s.insert(1, "def");
assert(s.asArray == "adefbc");
s.assign(abc);
s.insert(1, "def", 2);
assert(s.asArray == "adebc");
s.assign(abc);
s.insert(1, 2, 'f');
assert(s.asArray == "affbc");
}
void test_string_erase() {
auto s = make("abc");
s.erase(1, 1);
assert(s.asArray == "ac");
}
void test_string_push_back() {
auto s = make("");
assert(s.size() == 0);
s.push_back('_');
assert(s.size() == 1);
s.pop_back();
assert(s.size() == 0);
// s += '_';
// assert(s.size() == 1);
// assert(s.asArray == "_");
}
void test_string_copy() {
const s = make("hello");
char[2] buffer;
s.copy(buffer.ptr, 2);
assert(buffer == "he");
s.copy(buffer.ptr, 2, 1);
assert(buffer == "el");
s.copy(buffer.ptr, 2, 0);
assert(buffer == "he");
}
void test_string_find() {
const s = make("hello");
assert(s.find('e') == 1);
assert(s.find("el") == 1);
assert(s.find(s) == 0);
assert(s.find('_') == std_string.npos);
assert(s.rfind('e') == 1);
assert(s.rfind("el") == 1);
assert(s.rfind(s) == 0);
assert(s.rfind('_') == std_string.npos);
assert(s.find_first_of('e') == 1);
assert(s.find_first_of("el") == 1);
assert(s.find_first_of(s) == 0);
assert(s.find_first_of('_') == std_string.npos);
assert(s.find_last_of('e') == 1);
assert(s.find_last_of("el") == 3);
assert(s.find_last_of(s) == 4);
assert(s.find_last_of('_') == std_string.npos);
assert(s.find_first_not_of('e') == 0);
assert(s.find_first_not_of("el") == 0);
assert(s.find_first_not_of(s) == std_string.npos);
assert(s.find_first_not_of('_') == 0);
assert(s.find_last_not_of('e') == 4);
assert(s.find_last_not_of("el") == 4);
assert(s.find_last_not_of(s) == std_string.npos);
assert(s.find_last_not_of('_') == 4);
}
///////////////////////////////////////////////////////////////////////////////
// Test below works with classes by hacking the type system.
///////////////////////////////////////////////////////////////////////////////
void test_string_pass_by_const_ref() {
const s = make("hello");
version(use_structs) {
assert(getStringSize(s) == 5);
}
version(use_classes) {
assert(getStringSize(*s.c_ptr) == 5);
assert(getStringSize(s.c_ref) == 5);
}
}
///////////////////////////////////////////////////////////////////////////////
// Tests below currently don't work with classes because of layout issues.
///////////////////////////////////////////////////////////////////////////////
// substr return by value, this code cannot work out of the box with class implementation.
void test_string_substr() {
version(use_structs) {
const s = make("hello");
assert(s.substr().asArray == s.asArray);
assert(s.substr(1, 2).asArray == "el");
}
}
void test_string_copy_ctor() {
version(use_structs) {
auto string_from_d = std_string("string_from_d\0overun");
assert(string_from_d.asArray == "string_from_d");
auto string_copy_from_d = std_string(string_from_d);
assert(string_copy_from_d.asArray == string_from_d.asArray);
string_copy_from_d.push_back('_');
assert(string_copy_from_d.asArray != string_from_d.asArray);
assert(string_copy_from_d.data !is string_from_d.data);
}
}
import std.stdio;
void main() {
test_string_copy_ctor();
test_string_iterator();
test_string_const_iterator();
test_string_capacity();
test_string_element_access();
test_string_append();
test_string_assign();
test_string_insert();
test_string_erase();
test_string_push_back();
test_string_copy();
test_string_find();
test_string_substr();
test_string_pass_by_const_ref();
version(use_structs) {
writeln(getStringPtr().asArray());
writeln(getStringRef().asArray());
// substr return by value, this code cannot work out of the box with class implementation.
writeln(getStringCopy().asArray());
}
}