-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathzig-string-tests.zig
303 lines (237 loc) · 8.95 KB
/
zig-string-tests.zig
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
303
const std = @import("std");
const expect = std.testing.expect;
const expectEqual = std.testing.expectEqual;
const expectEqualStrings = std.testing.expectEqualStrings;
const zig_string = @import("./zig-string.zig");
const String = zig_string.String;
test "Basic Usage" {
// Create your String
var myString = String.init(std.testing.allocator);
defer myString.deinit();
// Use functions provided
try myString.concat("🔥 Hello!");
_ = myString.pop();
try myString.concat(", World 🔥");
// Success!
try expect(myString.cmp("🔥 Hello, World 🔥"));
}
test "String Tests" {
// This is how we create the String
var myStr = String.init(std.testing.allocator);
defer myStr.deinit();
// allocate & capacity
try myStr.allocate(16);
try expectEqual(myStr.capacity(), 16);
try expectEqual(myStr.size, 0);
// truncate
try myStr.truncate();
try expectEqual(myStr.capacity(), myStr.size);
try expectEqual(myStr.capacity(), 0);
// concat
try myStr.concat("A");
try myStr.concat("\u{5360}");
try myStr.concat("💯");
try myStr.concat("Hello🔥");
try expectEqual(myStr.size, 17);
// pop & length
try expectEqual(myStr.len(), 9);
try expectEqualStrings(myStr.pop().?, "🔥");
try expectEqual(myStr.len(), 8);
try expectEqualStrings(myStr.pop().?, "o");
try expectEqual(myStr.len(), 7);
// str & cmp
try expect(myStr.cmp("A\u{5360}💯Hell"));
try expect(myStr.cmp(myStr.str()));
// charAt
try expectEqualStrings(myStr.charAt(2).?, "💯");
try expectEqualStrings(myStr.charAt(1).?, "\u{5360}");
try expectEqualStrings(myStr.charAt(0).?, "A");
// insert
try myStr.insert("🔥", 1);
try expectEqualStrings(myStr.charAt(1).?, "🔥");
try expect(myStr.cmp("A🔥\u{5360}💯Hell"));
// find
try expectEqual(myStr.find("🔥").?, 1);
try expectEqual(myStr.find("💯").?, 3);
try expectEqual(myStr.find("Hell").?, 4);
// remove & removeRange
try myStr.removeRange(0, 3);
try expect(myStr.cmp("💯Hell"));
try myStr.remove(myStr.len() - 1);
try expect(myStr.cmp("💯Hel"));
const whitelist = [_]u8{ ' ', '\t', '\n', '\r' };
// trimStart
try myStr.insert(" ", 0);
myStr.trimStart(whitelist[0..]);
try expect(myStr.cmp("💯Hel"));
// trimEnd
_ = try myStr.concat("lo💯\n ");
myStr.trimEnd(whitelist[0..]);
try expect(myStr.cmp("💯Hello💯"));
// clone
var testStr = try myStr.clone();
defer testStr.deinit();
try expect(testStr.cmp(myStr.str()));
// reverse
myStr.reverse();
try expect(myStr.cmp("💯olleH💯"));
myStr.reverse();
try expect(myStr.cmp("💯Hello💯"));
// repeat
try myStr.repeat(2);
try expect(myStr.cmp("💯Hello💯💯Hello💯💯Hello💯"));
// isEmpty
try expect(!myStr.isEmpty());
// split
try expectEqualStrings(myStr.split("💯", 0).?, "");
try expectEqualStrings(myStr.split("💯", 1).?, "Hello");
try expectEqualStrings(myStr.split("💯", 2).?, "");
try expectEqualStrings(myStr.split("💯", 3).?, "Hello");
try expectEqualStrings(myStr.split("💯", 5).?, "Hello");
try expectEqualStrings(myStr.split("💯", 6).?, "");
var splitStr = String.init(std.testing.allocator);
defer splitStr.deinit();
try splitStr.concat("variable='value'");
try expectEqualStrings(splitStr.split("=", 0).?, "variable");
try expectEqualStrings(splitStr.split("=", 1).?, "'value'");
// splitAll
var splitAllStr = try String.init_with_contents(std.testing.allocator, "THIS IS A TEST");
defer splitAllStr.deinit();
const splitAllSlices = try splitAllStr.splitAll(" ");
try expectEqual(splitAllSlices.len, 5);
try expectEqualStrings(splitAllSlices[0], "THIS");
try expectEqualStrings(splitAllSlices[1], "IS");
try expectEqualStrings(splitAllSlices[2], "A");
try expectEqualStrings(splitAllSlices[3], "");
try expectEqualStrings(splitAllSlices[4], "TEST");
// splitToString
var newSplit = try splitStr.splitToString("=", 0);
try expect(newSplit != null);
defer newSplit.?.deinit();
try expectEqualStrings(newSplit.?.str(), "variable");
// splitAllToStrings
var splitAllStrings = try splitAllStr.splitAllToStrings(" ");
defer for (splitAllStrings) |*str| {
str.deinit();
};
try expectEqual(splitAllStrings.len, 5);
try expectEqualStrings(splitAllStrings[0].str(), "THIS");
try expectEqualStrings(splitAllStrings[1].str(), "IS");
try expectEqualStrings(splitAllStrings[2].str(), "A");
try expectEqualStrings(splitAllStrings[3].str(), "");
try expectEqualStrings(splitAllStrings[4].str(), "TEST");
// lines
const lineSlice = "Line0\r\nLine1\nLine2";
var lineStr = try String.init_with_contents(std.testing.allocator, lineSlice);
defer lineStr.deinit();
var linesSlice = try lineStr.lines();
defer for (linesSlice) |*str| {
str.deinit();
};
try expectEqual(linesSlice.len, 3);
try expect(linesSlice[0].cmp("Line0"));
try expect(linesSlice[1].cmp("Line1"));
try expect(linesSlice[2].cmp("Line2"));
// toLowercase & toUppercase
myStr.toUppercase();
try expect(myStr.cmp("💯HELLO💯💯HELLO💯💯HELLO💯"));
myStr.toLowercase();
try expect(myStr.cmp("💯hello💯💯hello💯💯hello💯"));
// substr
var subStr = try myStr.substr(0, 7);
defer subStr.deinit();
try expect(subStr.cmp("💯hello💯"));
// clear
myStr.clear();
try expectEqual(myStr.len(), 0);
try expectEqual(myStr.size, 0);
// writer
const writer = myStr.writer();
const length = try writer.write("This is a Test!");
try expectEqual(length, 15);
// owned
const mySlice = try myStr.toOwned();
try expectEqualStrings(mySlice.?, "This is a Test!");
std.testing.allocator.free(mySlice.?);
// StringIterator
var i: usize = 0;
var iter = myStr.iterator();
while (iter.next()) |ch| {
if (i == 0) {
try expectEqualStrings("T", ch);
}
i += 1;
}
try expectEqual(i, myStr.len());
// setStr
const contents = "setStr Test!";
try myStr.setStr(contents);
try expect(myStr.cmp(contents));
// non ascii supports in windows
// try expectEqual(std.os.windows.kernel32.GetConsoleOutputCP(), 65001);
}
test "init with contents" {
const initial_contents = "String with initial contents!";
// This is how we create the String with contents at the start
var myStr = try String.init_with_contents(std.testing.allocator, initial_contents);
defer myStr.deinit();
try expectEqualStrings(myStr.str(), initial_contents);
}
test "startsWith Tests" {
var myString = String.init(std.testing.allocator);
defer myString.deinit();
try myString.concat("bananas");
try expect(myString.startsWith("bana"));
try expect(!myString.startsWith("abc"));
}
test "endsWith Tests" {
var myString = String.init(std.testing.allocator);
defer myString.deinit();
try myString.concat("asbananas");
try expect(myString.endsWith("nas"));
try expect(!myString.endsWith("abc"));
try myString.truncate();
try myString.concat("💯hello💯💯hello💯💯hello💯");
std.debug.print("", .{});
try expect(myString.endsWith("hello💯"));
}
test "replace Tests" {
// Create your String
var myString = String.init(std.testing.allocator);
defer myString.deinit();
try myString.concat("hi,how are you");
var result = try myString.replace("hi,", "");
try expect(result);
try expectEqualStrings(myString.str(), "how are you");
result = try myString.replace("abc", " ");
try expect(!result);
myString.clear();
try myString.concat("💯hello💯💯hello💯💯hello💯");
_ = try myString.replace("hello", "hi");
try expectEqualStrings(myString.str(), "💯hi💯💯hi💯💯hi💯");
}
test "rfind Tests" {
var myString = try String.init_with_contents(std.testing.allocator, "💯hi💯💯hi💯💯hi💯");
defer myString.deinit();
try expectEqual(myString.rfind("hi"), 9);
}
test "toCapitalized Tests" {
var myString = try String.init_with_contents(std.testing.allocator, "love and be loved");
defer myString.deinit();
myString.toCapitalized();
try expectEqualStrings(myString.str(), "Love And Be Loved");
}
test "includes Tests" {
var myString = try String.init_with_contents(std.testing.allocator, "love and be loved");
defer myString.deinit();
var needle = try String.init_with_contents(std.testing.allocator, "be");
defer needle.deinit();
try expect(myString.includesLiteral("and"));
try expect(myString.includesString(needle));
try needle.concat("t");
try expect(myString.includesLiteral("tiger") == false);
try expect(myString.includesString(needle) == false);
needle.clear();
try expect(myString.includesLiteral("") == false);
try expect(myString.includesString(needle) == false);
}