-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuilder_test.v
More file actions
440 lines (411 loc) · 10.3 KB
/
Copy pathbuilder_test.v
File metadata and controls
440 lines (411 loc) · 10.3 KB
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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
import strings
type MyInt = int
const maxn = 100000
fn test_sb() {
mut sb := strings.new_builder(100)
sb.write_string('hi')
sb.write_string('!')
sb.write_string('hello')
assert sb.len == 8
sb_end := sb.str()
assert sb_end == 'hi!hello'
assert sb.len == 0
///
sb = strings.new_builder(10)
sb.write_string('a')
sb.write_string('b')
assert sb.len == 2
assert sb.str() == 'ab'
// Test interpolation optimization
sb = strings.new_builder(10)
x := 10
y := MyInt(20)
sb.writeln('x = ${x} y = ${y}')
res := sb.str()
assert res[res.len - 1] == `\n`
println('"${res}"')
assert res.trim_space() == 'x = 10 y = 20'
sb = strings.new_builder(10)
sb.write_string('x = ${x} y = ${y}')
assert sb.str() == 'x = 10 y = 20'
//$if !windows {
sb = strings.new_builder(10)
sb.write_string('123456')
last_2 := sb.cut_last(2)
assert last_2 == '56'
final_sb := sb.str()
assert final_sb == '1234'
//}
sb.clear()
assert sb.str() == ''
}
fn test_big_sb() {
mut sb := strings.new_builder(100)
mut sb2 := strings.new_builder(10000)
for i in 0 .. maxn {
sb.writeln(i.str())
sb2.write_string('+')
}
s := sb.str()
lines := s.split_into_lines()
assert lines.len == maxn
assert lines[0] == '0'
assert lines[1] == '1'
assert lines[777] == '777'
assert lines[98765] == '98765'
println(sb2.len)
assert sb2.len == maxn
}
fn test_byte_write() {
mut sb := strings.new_builder(100)
temp_str := 'byte testing'
mut count := 0
for word in temp_str {
sb.write_u8(word)
count++
assert count == sb.len
}
sb_final := sb.str()
assert sb_final == temp_str
}
fn test_strings_builder_reuse() {
mut sb := strings.new_builder(256)
sb.write_string('world')
assert sb.str() == 'world'
sb.write_string('hello')
assert sb.str() == 'hello'
}
fn test_cut_to() {
mut sb := strings.new_builder(16)
sb.write_string('hello')
assert sb.cut_to(3) == 'lo'
assert sb.len == 3
assert sb.cut_to(3) == ''
assert sb.len == 3
assert sb.cut_to(0) == 'hel'
assert sb.cut_to(32) == ''
assert sb.len == 0
}
fn test_write_rune() {
mut sb := strings.new_builder(10)
sb.write_rune(`h`)
sb.write_rune(`e`)
sb.write_rune(`l`)
sb.write_rune(`l`)
sb.write_rune(`o`)
x := sb.str()
assert x == 'hello'
}
fn test_write_runes() {
mut sb := strings.new_builder(20)
sb.write_runes([`h`, `e`, `l`, `l`, `o`])
sb.write_rune(` `)
sb.write_runes([`w`, `o`, `r`, `l`, `d`])
x := sb.str()
assert x == 'hello world'
}
fn test_ensure_cap() {
mut sb := strings.new_builder(0)
assert sb.cap == 0
sb.ensure_cap(10)
assert sb.cap >= 10
old_cap := sb.cap
sb.ensure_cap(10)
assert sb.cap == old_cap
sb.ensure_cap(15)
assert sb.cap >= 15
old_cap2 := sb.cap
sb.ensure_cap(10)
assert sb.cap == old_cap2
sb.ensure_cap(-1)
assert sb.cap == old_cap2
}
fn test_drain_builder() {
mut sb := strings.new_builder(0)
mut target_sb := strings.new_builder(0)
assert sb.cap == 0
assert target_sb.cap == 0
sb.write_string('abc')
assert sb.len == 3
target_sb.drain_builder(mut sb, 0)
assert sb.len == 0
assert target_sb.len == 3
assert target_sb.str() == 'abc'
}
@[manualfree]
fn sb_i64_str(n i64) string {
mut sb := strings.new_builder(24)
defer {
unsafe { sb.free() }
}
sb.write_decimal(n)
return sb.str()
}
fn test_write_decimal() {
assert sb_i64_str(0) == '0'
assert sb_i64_str(1) == '1'
assert sb_i64_str(-1) == '-1'
assert sb_i64_str(1001) == '1001'
assert sb_i64_str(-1001) == '-1001'
assert sb_i64_str(1234567890) == '1234567890'
assert sb_i64_str(-1234567890) == '-1234567890'
assert sb_i64_str(9223372036854775807) == '9223372036854775807'
assert sb_i64_str(-9223372036854775807) == '-9223372036854775807'
assert sb_i64_str(min_i64) == '-9223372036854775808'
// runtime `min_i64` (parsed, not the constant), whose negation overflows i64:
assert sb_i64_str('-9223372036854775808'.i64()) == '-9223372036854775808'
}
@[manualfree]
fn sb_u64_str(n u64) string {
mut sb := strings.new_builder(24)
defer {
unsafe { sb.free() }
}
sb.write_u_decimal(n)
return sb.str()
}
fn test_write_u_decimal() {
assert sb_u64_str(0) == '0'
assert sb_u64_str(1) == '1'
assert sb_u64_str(1001) == '1001'
assert sb_u64_str(1234567890) == '1234567890'
assert sb_u64_str(u64(9223372036854775807)) == '9223372036854775807'
// values above max_i64, which write_decimal(i64) cannot represent:
assert sb_u64_str(u64(9223372036854775807) + 1) == '9223372036854775808'
assert sb_u64_str(max_u64) == '18446744073709551615'
}
fn test_grow_len() {
mut sb := strings.new_builder(10)
assert sb.len == 0
assert sb.cap == 10
sb.write_string('0123456789')
assert sb.len == 10
unsafe { sb.grow_len(-5) }
assert sb.len == 10
assert sb.cap == 10
unsafe { sb.grow_len(10) }
assert sb.len == 20
assert sb.cap == 20
sb.ensure_cap(35)
assert sb.len == 20
assert sb.cap >= 35
cap_after_ensure := sb.cap
unsafe { sb.grow_len(5) }
assert sb.len == 25
assert sb.cap == cap_after_ensure
}
fn test_write_repeated_rune() {
mut sb := strings.new_builder(20)
sb.write_repeated_rune(`h`, 5)
sb.write_repeated_rune(`w`, 5)
sb.write_repeated_rune(`√`, 5)
sb.write_rune(` `)
x := sb.str()
assert x == 'hhhhhwwwww√√√√√ '
}
struct IndentTest {
param strings.IndentParam
input string
output string
}
// vfmt off
const indent_test_data = [
IndentTest{
input: 'User1 {
name: "John"
settings: {
theme: "dark"
language: "en"
}
}'
output: 'User1 {
name: "John"
settings: {
theme: "dark"
language: "en"
}
}'
},
IndentTest{
input: 'User2{name:"John" settings:{theme:"dark" language:"en" hobbies:["reading","sports"]}}'
output: 'User2{
name:"John" settings:{
theme:"dark" language:"en" hobbies:["reading","sports"]
}
}'
},
IndentTest{
input: 'message {text: "Hello {world}!" count: 5 nested: {data: "Test {inner}"}}'
output: 'message {
text: "Hello {world}!" count: 5 nested: {
data: "Test {inner}"
}
}'
},
IndentTest{
input: 'if x > 0 {println("Positive") for i in 0..x {println(i) if i % 2 == 0 {println("even")}}} else {println("Not positive")}'
output: 'if x > 0 {
println("Positive") for i in 0..x {
println(i) if i % 2 == 0 {
println("even")
}
}
} else {
println("Not positive")
}'
},
IndentTest{
input: 'config{database:{host:"localhost" port:5432} server:{port:8080 routes:{api:"/api" static:"/static"}} log:{level:"info"}}'
output: 'config{
database:{
host:"localhost" port:5432
} server:{
port:8080 routes:{
api:"/api" static:"/static"
}
} log:{
level:"info"
}
}'
},
IndentTest{
input: "MyS{
a: 0
b: 0
son: Son{
k: ''
m: ''
}
}
"
output: "MyS{
a: 0
b: 0
son: Son{
k: ''
m: ''
}
}
"
},
IndentTest{
input: 'config {message: "He said: \\"Hello World!\\"" code: "if (x) { return \\"value\\"; }"}'
output: 'config {
message: "He said: \\"Hello World!\\"" code: "if (x) { return \\"value\\"; }"
}'
},
IndentTest {
input : 'data {text: "String with {curly braces} and \\"quotes\\"" nested: {value: "Inner \\"quoted\\" string"}}'
output : 'data {
text: "String with {curly braces} and \\"quotes\\"" nested: {
value: "Inner \\"quoted\\" string"
}
}'
},
IndentTest {
input : 'escape {backslash: "Path: C:\\\\Users\\\\Test" newline: "Line1\\nLine2" tab: "Column1\\tColumn2"}'
output : 'escape {
backslash: "Path: C:\\\\Users\\\\Test" newline: "Line1\\nLine2" tab: "Column1\\tColumn2"
}'
},
IndentTest {
input : 'nested {outer: "Outer \\"quote\\" with {braces}" inner: {value: "Inner string with \\\\backslash and \\"quotes\\""}}'
output : 'nested {
outer: "Outer \\"quote\\" with {braces}" inner: {
value: "Inner string with \\\\backslash and \\"quotes\\""
}
}'
},
IndentTest {
input : 'complex {str1: "\\"Escaped quotes\\"" str2: "Backslash: \\\\" str3: "Mixed: \\"quoted\\" and \\\\backslash" regex: "Pattern: \\\\d+\\\\.\\\\d+"}'
output : 'complex {
str1: "\\"Escaped quotes\\"" str2: "Backslash: \\\\" str3: "Mixed: \\"quoted\\" and \\\\backslash" regex: "Pattern: \\\\d+\\\\.\\\\d+"
}'
},
IndentTest {
input : 'json {data: "{\\"name\\": \\"John\\", \\"age\\": 30, \\"city\\": \\"New York\\"}"}'
output : 'json {
data: "{\\"name\\": \\"John\\", \\"age\\": 30, \\"city\\": \\"New York\\"}"
}'
},
IndentTest {
input : 'code {function: "fn test() { if (x) { return \\\"value\\\"; } }" error: "Error: \\\"File not found\\\" at line 10"}'
output : 'code {
function: "fn test() { if (x) { return \\\"value\\\"; } }" error: "Error: \\\"File not found\\\" at line 10"
}'
},
IndentTest {
input : 'multiline {message: "Line 1\\nLine 2\\nLine 3 with {braces}\\nLine 4 with \\\"quotes\\\""}'
output : 'multiline {
message: "Line 1\\nLine 2\\nLine 3 with {braces}\\nLine 4 with \\\"quotes\\\""
}'
},
IndentTest {
input : 'extreme {str: "\\\\\\\\\\\\\\"Quadruple escaped\\\\\\\\\\\\\\""}'
output : 'extreme {
str: "\\\\\\\\\\\\\\"Quadruple escaped\\\\\\\\\\\\\\""
}'
},
IndentTest{
param: strings.IndentParam {
block_start: `[`
block_end: `]`
}
input: 'message [text: "Hello {world}!" count: 5 nested: [data: "Test {inner}"]]'
output: 'message [
text: "Hello {world}!" count: 5 nested: [
data: "Test {inner}"
]
]'
},
IndentTest{
param: strings.IndentParam {
block_start: `[`
block_end: `]`
indent_char: `#`
}
input: 'message [text: "Hello {world}!" count: 5 nested: [data: "Test {inner}"]]'
output: 'message [
####text: "Hello {world}!" count: 5 nested: [
########data: "Test {inner}"
####]
]'
},
IndentTest{
param: strings.IndentParam {
block_start: `[`
block_end: `]`
indent_char: `\t`
indent_count: 2
}
input: 'message [text: "Hello {world}!" count: 5 nested: [data: "Test {inner}"]]'
output: 'message [
\t\ttext: "Hello {world}!" count: 5 nested: [
\t\t\t\tdata: "Test {inner}"
\t\t]
]'
},
IndentTest{
param: strings.IndentParam {
block_start: `[`
block_end: `]`
indent_char: `#`
indent_count: 1
starting_level: 2
}
input: 'message [text: "Hello {world}!" count: 5 nested: [data: "Test {inner}"]]'
output: '##message [
###text: "Hello {world}!" count: 5 nested: [
####data: "Test {inner}"
###]
##]'
},
]
// vfmt on
fn test_indent() {
for t in indent_test_data {
mut sb := strings.new_builder(256)
sb.indent(t.input, t.param)
assert sb.str() == t.output
}
}