-
Notifications
You must be signed in to change notification settings - Fork 5
/
test
executable file
·600 lines (523 loc) · 12.1 KB
/
test
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
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
#!/bin/sh
# testlib taken from https://github.com/lhoursquentin/clash
c_red="$(printf '\033[1;31m')"
c_green="$(printf '\033[1;32m')"
c_bold="$(printf '\033[1m')"
c_reset="$(printf '\033[0m')"
condition()
{
nb_args="$#"
set -- "$@" "$@"
index=0
while [ "$index" -lt "$nb_args" ]; do
if [ "$1" = '--' ]; then
separator_index="$index"
fi
index="$((index + 1))"
shift
done
index=0
while [ "$index" != "$separator_index" ]; do
set -- "$@" "$1"
index="$((index + 1))"
shift
done
shift "$(($# - separator_index))"
"$@" || return # return for set -e
}
assert() {
condition_retval=0
condition "$@" || condition_retval="$?" # for set -e
if [ "$condition_retval" "$assert_operator" 0 ]; then
return
fi
assertion="$*"
shift "$((separator_index + 1))"
{
[ -t 2 ] && color=true || unset color
printf '%sError%s - ' "${color+$c_red}" "${color+$c_reset}"
printf "$@"
echo
} >&2
failed_assertions="$failed_assertions
$assertion
-------------------------------------"
return 1
}
assert_true() {
printf '%s -> true\n' "$*"
assert_operator='='
assert "$@"
}
assert_false() {
printf '%s -> false\n' "$*"
assert_operator='!='
assert "$@"
}
assert_out() {
printf '%s\n' "$*"
out_file="$(mktemp)"
condition "$@" > "$out_file"
shift "$((separator_index + 1))"
output="$(cat "$out_file"; printf x)"
output="${output%x}"
rm "$out_file"
assert_operator='='
assert [ "$output" = "$*" ] -- 'Output differ'
}
end_tests() {
[ -t 1 ] && color=true || unset color
printf '%s================ END ================%s\n' "${color+$c_bold}" "${color+$c_reset}"
if [ -z "$failed_assertions" ]; then
printf '%sPASS%s\n' "${color+$c_green}" "${color+$c_reset}"
else
printf '%s\n%sFAIL%s\n' "$failed_assertions" "${color+$c_red}" "${color+$c_reset}"
return 1
fi
}
# end of testlib
bin=./sed-bin
translator=./par.sed
generated_file=generated.c
generated_init_file=generated-init.c
trap 'rm -f "$bin_generated_file_output" "$previous_generated_init_file" "$expected_output"' EXIT
expected_output="$(mktemp)"
bin_generated_file_output="$(mktemp)"
previous_generated_init_file="$(mktemp)"
# toybox, busybox, bsd sed and gnu sed are the most common targets
sed_implementation='sed' # requiring an IFS split rules out default zsh
# --posix is usually for the gnu version
if "$sed_implementation" --posix '' /dev/null 2> /dev/null; then
sed_implementation="$sed_implementation --posix"
fi
compile_translator() {
# Tranlate the translator with itself
"$translator" < "$translator" > "$generated_file" &&
# So after doing some experiments, it seems that on FreeBSD the inode might
# be the exact same when re-creating the file very quicky, and in that case
# if the timestamp is also the same, meaning we recreate the file in less
# than one second, then make concludes that everything is up-to-date. The
# workaround here to force make to re-compile sed-bin.c is to remove sed-bin
# and sed-bin.o, which would have to be built again anyways.
rm -f sed-bin.o sed-bin &&
make -s &&
cp "$generated_init_file" "$previous_generated_init_file" &&
# Compiled tranlator should be able to translate the translator and yield the
# exact same output
"$bin" < "$translator" > "$bin_generated_file_output" &&
diff "$generated_file" "$bin_generated_file_output" &&
diff "$previous_generated_init_file" "$generated_init_file"
}
equal_output() {
echo --------------------------
printf '%s\n' "$input" | timeout 20 $sed_implementation $n_opt "$sed_code" > "$expected_output" &&
printf '%s\n' "$sed_code" | timeout 20 "$translator" > "$generated_file" &&
rm -f sed-bin.o sed-bin &&
make -s &&
printf '%s\n' "$input" | timeout 20 "$bin" $n_opt > "$bin_generated_file_output" &&
diff "$expected_output" "$bin_generated_file_output"
}
verify() {
sed_code="$1"
input="$2"
n_opt=
assert_true equal_output "$sed_code" -- "input: $input"
n_opt=-n
assert_true equal_output "$n_opt" "$sed_code" -- "input: $input"
echo ==========================
}
# Replace by smaller
verify 's/Hell/Y/' 'Hello World'
verify 's/llo W/y w/' 'Hello World'
verify 's/ World/!/' 'Hello World'
# Replace by bigger
verify 's/Hello/What in the/' 'Hello World'
verify 's/o/ what is wrong with this/' 'Hello World'
verify 's/ld/k! Oh no :(/' 'Hello World'
# Replace by same size
verify 's/Hello/Yolo!/' 'Hello World'
verify 's/o/a/' 'Hello World'
verify 's/ld/k!/' 'Hello World'
# With regex
verify 's/[hH]/Well h/' 'Hello World'
verify 's/. ./a-w/' 'Hello World'
verify 's/o.*/!/' 'Hello World'
verify 's/.*//' 'Hello World'
verify 's/.*/Bye/' 'Hello World'
verify 's/[Hh]ello.* World*//' 'Hello World'
verify 's/Hello World/Bye/' 'Hello World'
verify 's/Hello World//' 'Hello World'
# Fail
verify 's/xz/nope/' 'Hello World'
# Back refs
verify 's/\([^ ]*\)/\1! \1/' 'Hello World'
verify 's/\([^ ]*\) \(.\)/\1! \2 /' 'Hello World'
verify 's/.*r/& & &/' 'Hello World'
verify 's/Hello/\& literal ampersand/' 'Hello World'
verify 's/\(x\)*foo/b\1ar/' 'foo'
verify 's/\(x\)*foo/b\1ar/' 'xfoo'
verify 's/\(x\)*foo/b\1ar/' 'xxfoo'
verify 's/\(x\)*foo/\1/' 'foo'
verify 's/\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)/\1\2\3\4\5\6\7\8\9/' '123456789'
# g opt
verify 's/o/a/g' 'Hello World'
verify 's/o/abocde/g' 'Hello World'
verify 's/o/ yo/g' 'Hello World'
verify 's/damn/no/g' 'Oh damn god damnit'
verify 's/damn/n/g' 'Oh damn god damnit'
verify 's/.*/Bye/g' 'Hello World'
verify 's/\([^ ]*\)/\1! \1/g' 'Hello World'
verify 's/[^a]/foo/g' 'bar'
verify 's/^a/foo/g' 'aaaa'
verify 's/a//g' 'aaaa'
verify 's/a//g' 'abaca'
# p opt
verify 's/foo/bar/p;d' 'padding foo padding'
verify 's/foo/bar/pg;d' 'padding foo padding foo padding'
verify 's/foo/bar/gp;d' 'padding foo padding foo padding'
verify 's/^/hey/p;d' 'foo'
verify 's/^//p;d' 'foo'
# s with \n
verify 'N; s/^foo\nbar/yo\nlo/g' 'foo
bar'
# s empty match check
verify 's/[^ ]*/yo/g' 'Hello world'
verify 's/[^ ]*/yo/g' 'Hello '
verify 's/[^ ]*/yo/' 'foo'
# s insert at beginning and end
verify 's/^/Leading insert/' 'Hello World'
verify 's/$/Trailing insert/' 'Hello World'
verify 's/x/y/3' 'axbxcxdxex
fxgxhxixjx'
verify 's/x/y/3g' 'axbxcxdxex
fxgxhxixjx'
verify 's/x/y/p3g' 'axbxcxdxex
fxgxhxixjx'
verify 's/x/y/3pg' 'axbxcxdxex
fxgxhxixjx'
verify 's/x/y/pg3' 'axbxcxdxex
fxgxhxixjx'
verify 's/x/y/4; t
d' 'axbxcxdxex
fxgxhxixjx'
# sub success should not be set if the nth is never reached
verify 's/x/y/40; t
d' 'axbxcxdxex
fxgxhxixjx'
# check we can parse nth over 9
verify 's/x/y/12' 'xxxxxxxxxxxxxxxxxxxx'
test_file="$(mktemp)"
rm "$test_file"
verify 's/x/y/pg3w '"$test_file"'
r '"$test_file" 'axbxcxdxex
fxgxhxixjx'
rm "$test_file"
verify 's/non-matching/y/pg3w '"$test_file"'
r '"$test_file" 'axbxcxdxex
fxgxhxixjx'
rm "$test_file"
# t
verify ': start
s/^[^[:blank:]]//; t start' 'Hello World'
# Label name not taking the whole line is not supported by FreeBSD.
# Check that we support it anyways.
# t, verify that reading a newline resets the substitution success to false
verify 't end; s/foo//; d; : end' 'foo
bar'
verify 't end
s/foo//; d; : end' 'foo
bar'
verify 's/.*/yo/; n; t end
p; : end
d' 'foo
bar'
verify 's/.*/yo/; N; t end
p; : end
d' 'foo
bar'
verify 's/^/x/; t; s/^/y/' 'foo
bar'
verify 's/^/x/; t ; s/^/y/' 'foo
bar'
verify 's/^/x/; t
s/^/y/' 'foo
bar'
verify 's/non matching pattern/x/; t; s/^/y/' 'foo
bar'
verify 's/non matching pattern/x/; t ; s/^/y/' 'foo
bar'
verify 's/non matching pattern/x/; t
s/^/y/' 'foo
bar'
# b
verify ':start
s/.//; /^[^[:blank:]]/b start' 'Hello World'
verify ':start; s/.//; /^[^[:blank:]]/b start' 'Hello World'
verify 's/^/x/; b; s/^/y/' 'foo
bar'
verify 's/^/x/; b ; s/^/y/' 'foo
bar'
verify 's/^/x/; b
s/^/y/' 'foo
bar'
# H and G
verify 'H;H;G;' 'Hello World'
# p and P
verify 'h; s/ World//; x; s/Hello //; H; s/.*/end/; H; g; p' 'Hello World'
verify 'h; s/ World//; x; s/Hello //; H; s/.*/end/; H; g; P' 'Hello World'
verify P foo
# = equal
verify '=' 'Abc'
verify '=' 'This line
is not a
single line'
# d and D
verify 's/Hello/foo/; t end
d; s/foo/bar/; :end' 'Hello
World
Hello
You'
verify 's/Hello/foo/; t end
D; s/foo/bar/; :end' 'Hello
World
Hello
You'
verify '/^hey/{=;q;}; N; s/bar/hey/; D; s/.*/unreach/; p' 'foo
bar
baz'
verify '/match fail/d; s/foo/bar/' 'foo'
# This one is actually a meaningful use of D (combined with P), kind of an
# unreadable upgrade of `grep -o`
verify 't match
s/[^:]*: \(...\)/\
\1\
/
D
: match
P
D
' 'stuff: foo, other stuff: bar
some stuff: baz
unrelated
final stuff: fun'
# n and N
verify 's/Hello/foo/; n; s/World/bar/; q' 'Hello
World'
verify ': start
s/^.//; N; b start' 'This line
is not a
single line'
# ultimate test, compile the translator and verify that the binary can become
# the translator
assert_true compile_translator -- 'translating the translator with a compiled translator yields the original translation'
verify 'i \
f"oo \
b\jr\
b\\az\
b\nuf\
funk
s/Hello/Bye/' 'Hello
World'
# empty pattern
verify '/foo/s///' 'foo bar
funk'
verify '/foo/s///' 'foo bar
funk'
verify '/foo/{/funk/s//hey/;}' 'foo funk'
verify 's/foo/bar/; s//funk/' 'foo foo foo'
verify '/foo/!s/f/z/' 'foo
bar
foo'
verify '/foo/!s/f/z/' 'foo bar foo'
verify '1!s/f/z/' 'foo
foo'
verify '1!s/f/z/' 'foo bar'
# ranges
verify '1,3s/^/x/' 'foo
bar
funk
fax'
verify '2,3s/^/x/' 'foo
bar
funk
fax'
verify '2,2s/^/x/' 'foo
bar
funk
fax'
verify '2,10s/^/x/' 'foo
bar
funk
fax'
verify '2,1s/^/x/' 'foo
bar
funk
fax'
verify '1,/bar/s/^/x/' 'foo
bar
funk
fax'
verify '/foo/,/funk/s/^/x/' 'foo
bar
funk
fax'
verify '\,foo,,/funk/s,^,x,' 'foo
bar
funk
fax'
verify '/foo/,\,funk,s,^,x,' 'foo
bar
funk
fax'
verify '\,foo,,\,funk,s,^,x,' 'foo
bar
funk
fax'
verify '/foo/,/funk/s/^/x/; 1n' 'foo
bar
funk
fax'
verify '/foo/,/bar/s/^/x/' 'foo
bar
funk
fax'
verify '/foo/,/bar/s/^/x/; 1n' 'foo
bar
funk
fax'
verify '/foo/,/non existing/s/^/x/' 'foo
bar
funk
fax'
verify '/foo/,3s/^/x/' 'foo
bar
funk
fax'
verify '/foo/,3s/^/x/; 1n;' 'foo
bar
funk
fax'
# Busybox v1.30.1 fails this one
verify '2!{1,2s/^/> /;}' 'foo
bar
funk
fax'
verify 'a \
funk\
punk
s/^/x/
a \
yo\
yeah
s/^/y/
n
s/^/z/' 'foo
bar
baz
funk'
verify '$s/^/last line: /' 'foo
bar
baz'
verify '${s/^/last line: /; p;}' 'foo
bar
baz'
verify '$,1s/^/last line: /' 'foo
bar
baz'
verify '2,$s/^/not first: /' 'foo
bar
baz
funk'
verify '/bar/,$s/^/after bar: /' 'foo
bar
baz
funk'
verify '/bar/,${s/^/after bar: /;}' 'foo
bar
baz
funk'
verify '$,/funk/s/^/between last line and funk: /' 'foo
bar
baz
funk'
verify '$,/non-existing/s/^/between last line and some non matching regex: /' 'foo
bar
baz
funk'
verify 'c \
punk\
dunk
s/^/x/' 'foo
bar
baz
funk'
verify 'y/abc/def/' 'abbac'
verify 'y abc def ' 'abbac'
verify 'ya\abcadefa' 'abbac'
verify 'h; y/abc/def/; h' 'abbac'
verify 'y/axb/d\ne/' 'HelloxWorld'
verify 'N; y/a\nb/dxe/' 'Hello
World'
verify 'y///' 'foo'
verify 's/a/b/; y/c/d/; s/e/f/' 'abcdef'
verify 'l' 'foo \\ \ '"$(printf '\a\b\f\r\t\v')"' é '"$(printf '\001\020\300')"' bar'
verify 'r non-existing-file
s/^/x/' 'bar
baz'
test_file="$(mktemp)"
echo foo > "$test_file"
verify 'r '"$test_file"'
s/^/x/' 'bar
baz'
# like a/c/i the filename takes the whole line
verify 'r '"$test_file"'; s/^/x/' 'bar
baz'
# Even if w is not executed, POSIX specifies that files must be created before
# processing
rm "$test_file"
verify '/non matching pattern/w '"$test_file" 'foo'
assert_true [ -f "$test_file" ] -- 'File created without w cmd execution'
verify 's/^/x/; /foo/w '"$test_file"'
s/^/y/; r '"$test_file"'
s/^/z/' 'foo'
rm "$test_file"
verify 's/^$/x/' ''
# tabs as a separator
verify ' /f/ s/foo/bar/; p' 'foo'
# #n
verify '#n' 'foo'
verify '#no output' 'foo'
verify '#n
s/^/x/' 'foo
bar'
verify '#n
n
s/^/x/' 'foo
bar'
verify '#n
q' 'foo
bar'
verify '#n
b' 'foo
bar'
verify '#n
t' 'foo
bar'
# BRE ranges
verify 's/[/]/x/g' 'a/b/c'
verify 's/a/[\n]/g' 'a/b/c'
verify 's/[x/y]/[xy]/g' 'a/b/c'
verify 'N;N;s/\na[\n]b\n/yo/g' '
anb
'
# FreeBSD sed fails this one
verify 'y/[/]/' '[[]]'
verify '/[/]/s/[/]/yo/' '/'
verify 's/hey/[/' 'hey'
# With GNU sed not in POSIX mode [\n] means newline, whereas in POSIX mode
# this means either backslash or n, we'll follow POSIX mode behavior.
verify 's/[\n[:blank:]\n]/foo/g' 'n \ n \ n'
verify 's/\[/]foo/' 'a[b'
verify 's/[[:blank:]/]/foo/g' 'a/b c'
verify 's/[^]/]/x/g' 'a]b/c'
end_tests