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
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
|
#!./perl
=head1 NAME
s2p.t - test suite for s2p/psed
=head1 NOTES
The general idea is to
(a) run psed with a sed script and input data to obtain some output
(b) run s2p with a sed script creating a Perl program and then run the
Perl program with the input data, again producing output
Both final outputs should be identical to the expected output.
A $testcase{<name>} contains entries (after the comment ### <name> ###):
- script: the sed script
- input: the key of the input data, stored in $input{<input>}
- expect: the expected output
- datfil: an additional file [ <path>, <data> ] (if required)
Temporary files are created in the working directory (embedding $$
in the name), and removed after the test.
Except for bin2dec (which indeed converts binary to decimal) none of the
sed scripts is doing something useful.
Author: Wolfgang Laun.
=cut
BEGIN {
chdir 't' if -d 't';
@INC = ( '../lib' );
}
use File::Copy;
use File::Spec;
require './test.pl';
# BRE extensions
$ENV{PSEDEXTBRE} = '<>wW';
our %input = (
bins => <<'[TheEnd]',
0
111
1000
10001
[TheEnd]
text => <<'[TheEnd]',
line 1
line 2
line 3
line 4
line 5
line 6
line 7
line 8
[TheEnd]
adr1 => <<'[TheEnd]',
#no autoprint
# This script should be run on itself
/^#__DATA__$/,${
/^#A$/p
s/^# *[0-9]* *//
/^#\*$/p
/^#\.$/p
/^#\(..\)\(..\)\2\1*$/p
/^#[abc]\{1,\}[def]\{1,\}$/p
}
#__DATA__
#A
#*
#.
#abxyxy
#abxyxyab
#abxyxyabab
#ad
#abcdef
[TheEnd]
);
our %testcase = (
### bin2dec ###
'bin2dec' => {
script => <<'[TheEnd]',
# binary -> decimal
s/^[ ]*\([01]\{1,\}\)[ ]*/\1/
t go
i\
is not a binary number
d
# expand binary to Xs
: go
s/^0*//
s/^1/X/
: expand
s/^\(X\{1,\}\)0/\1\1/
s/^\(X\{1,\}\)1/\1\1X/
t expand
# count Xs in decimal
: count
s/^X/1/
s/0X/1/
s/1X/2/
s/2X/3/
s/3X/4/
s/4X/5/
s/5X/6/
s/6X/7/
s/7X/8/
s/8X/9/
s/9X/X0/
t count
s/^$/0/
[TheEnd]
input => 'bins',
expect => <<'[TheEnd]',
0
7
8
17
[TheEnd]
},
### = ###
'=' => {
script => <<'[TheEnd]',
1=
$=
[TheEnd]
input => 'text',
expect => <<'[TheEnd]',
1
line 1
line 2
line 3
line 4
line 5
line 6
line 7
8
line 8
[TheEnd]
},
### D ###
'D' => {
script => <<'[TheEnd]',
#no autoprint
/1/{
N
N
N
D
}
p
/2/D
=
p
[TheEnd]
input => 'text',
expect => <<'[TheEnd]',
line 2
line 3
line 4
line 3
line 4
4
line 3
line 4
line 5
5
line 5
line 6
6
line 6
line 7
7
line 7
line 8
8
line 8
[TheEnd]
},
### H ###
'H' => {
script => <<'[TheEnd]',
#no autoprint
1,$H
$g
$=
$p
[TheEnd]
input => 'text',
expect => <<'[TheEnd]',
8
line 1
line 2
line 3
line 4
line 5
line 6
line 7
line 8
[TheEnd]
},
### N ###
'N' => {
script => <<'[TheEnd]',
3a\
added line
4a\
added line
5a\
added line
3,5N
=
d
[TheEnd]
input => 'text',
expect => <<'[TheEnd]',
1
2
added line
4
added line
6
7
8
[TheEnd]
},
### P ###
'P' => {
script => <<'[TheEnd]',
1N
2N
3N
4=
4P
4,$d
[TheEnd]
input => 'text',
expect => <<'[TheEnd]',
4
line 1
[TheEnd]
},
### a ###
'a' => {
script => <<'[TheEnd]',
1a\
added line 1.1\
added line 1.2
3a\
added line 3.1
3a\
added line 3.2
[TheEnd]
input => 'text',
expect => <<'[TheEnd]',
line 1
added line 1.1
added line 1.2
line 2
line 3
added line 3.1
added line 3.2
line 4
line 5
line 6
line 7
line 8
[TheEnd]
},
### b ###
'b' => {
script => <<'[TheEnd]',
#no autoprint
2 b eos
4 b eos
p
: eos
[TheEnd]
input => 'text',
expect => <<'[TheEnd]',
line 1
line 3
line 5
line 6
line 7
line 8
[TheEnd]
},
### block ###
'block' => {
script => "#no autoprint\n1,3{\n=\np\n}",
input => 'text',
expect => <<'[TheEnd]',
1
line 1
2
line 2
3
line 3
[TheEnd]
},
### c ###
'c' => {
script => <<'[TheEnd]',
2=
2,4c\
change 2,4 line 1\
change 2,4 line 2
2=
3,5c\
change 3,5 line 1\
change 3,5 line 2
3=
[TheEnd]
input => 'text',
expect => <<'[TheEnd]',
line 1
2
change 2,4 line 1
change 2,4 line 2
line 5
line 6
line 7
line 8
[TheEnd]
},
### c1 ###
'c1' => {
script => <<'[TheEnd]',
1c\
replaces line 1
2,3c\
replaces lines 2-3
/5/,/6/c\
replaces lines 3-4
8,10c\
replaces lines 6-10
[TheEnd]
input => 'text',
expect => <<'[TheEnd]',
replaces line 1
replaces lines 2-3
line 4
replaces lines 3-4
line 7
[TheEnd]
},
### c2 ###
'c2' => {
script => <<'[TheEnd]',
3!c\
replace all except line 3
[TheEnd]
input => 'text',
expect => <<'[TheEnd]',
replace all except line 3
replace all except line 3
line 3
replace all except line 3
replace all except line 3
replace all except line 3
replace all except line 3
replace all except line 3
[TheEnd]
},
### c3 ###
'c3' => {
script => <<'[TheEnd]',
1,4!c\
replace all except 1-4
/5/,/8/!c\
replace all except 5-8
[TheEnd]
input => 'text',
expect => <<'[TheEnd]',
replace all except 5-8
replace all except 5-8
replace all except 5-8
replace all except 5-8
replace all except 1-4
replace all except 1-4
replace all except 1-4
replace all except 1-4
[TheEnd]
},
### d ###
'd' => {
script => <<'[TheEnd]',
# d delete pattern space, start next cycle
2,4 d
5 d
[TheEnd]
input => 'text',
expect => <<'[TheEnd]',
line 1
line 6
line 7
line 8
[TheEnd]
},
### gh ###
'gh' => {
script => <<'[TheEnd]',
1h
2g
3h
4g
5q
[TheEnd]
input => 'text',
expect => <<'[TheEnd]',
line 1
line 1
line 3
line 3
line 5
[TheEnd]
},
### i ###
'i' => {
script => <<'[TheEnd]',
1i\
inserted line 1.1\
inserted line 1.2
3i\
inserted line 3.1
3i\
inserted line 3.2
[TheEnd]
input => 'text',
expect => <<'[TheEnd]',
inserted line 1.1
inserted line 1.2
line 1
line 2
inserted line 3.1
inserted line 3.2
line 3
line 4
line 5
line 6
line 7
line 8
[TheEnd]
},
### n ###
'n' => {
script => <<'[TheEnd]',
3a\
added line
4a\
added line
5a\
added line
3,5n
=
d
[TheEnd]
input => 'text',
expect => <<'[TheEnd]',
1
2
line 3
added line
4
line 5
added line
6
7
8
[TheEnd]
},
### o ###
'o' => {
script => <<'[TheEnd]',
/abc/,/def/ s//XXX/
// i\
cheers
[TheEnd]
input => 'text',
expect => <<'[TheEnd]',
line 1
line 2
line 3
line 4
line 5
line 6
line 7
line 8
[TheEnd]
},
### q ###
'q' => {
script => <<'[TheEnd]',
2a\
append to line 2
3a\
append to line 3 - should not appear in output
3q
[TheEnd]
input => 'text',
expect => <<'[TheEnd]',
line 1
line 2
append to line 2
line 3
[TheEnd]
},
### r ###
'r' => {
datfil => [ 'r.txt', "r.txt line 1\nr.txt line 2\nr.txt line 3\n" ],
script => <<'[TheEnd]',
2r%r.txt%
4r %r.txt%
[TheEnd]
input => 'text',
expect => <<'[TheEnd]',
line 1
line 2
r.txt line 1
r.txt line 2
r.txt line 3
line 3
line 4
r.txt line 1
r.txt line 2
r.txt line 3
line 5
line 6
line 7
line 8
[TheEnd]
},
### s ###
's' => {
script => <<'[TheEnd]',
# enclose any `(a)'.. `(c)' in `-'
s/([a-z])/-\1-/g
s/\([abc]\)/-\1-/g
[TheEnd]
input => 'text',
expect => <<'[TheEnd]',
line 1
line 2
line 3
line 4
line 5
line 6
line 7
line 8
[TheEnd]
},
### s1 ###
's1' => {
script => <<'[TheEnd]',
s/\w/@1/
s/\y/@2/
s/\n/@3/
# this is literal { }
s/a{3}/@4/
# proper repetition
s/a\{3\}/a rep 3/
[TheEnd]
input => 'text',
expect => <<'[TheEnd]',
@1ine 1
@1ine 2
@1ine 3
@1ine 4
@1ine 5
@1ine 6
@1ine 7
@1ine 8
[TheEnd]
},
### t ###
't' => {
script => join( "\n",
'#no autoprint', 's/./X/p', 's/foo/bar/p', 't bye', '=', 'p', ':bye' ),
input => 'text',
expect => <<'[TheEnd]',
Xine 1
Xine 2
Xine 3
Xine 4
Xine 5
Xine 6
Xine 7
Xine 8
[TheEnd]
},
### w ###
'w' => {
datfil => [ 'w.txt', '' ],
script => <<'[TheEnd]',
w %w.txt%
[TheEnd]
input => 'text',
expect => <<'[TheEnd]',
line 1
line 2
line 3
line 4
line 5
line 6
line 7
line 8
[TheEnd]
},
### x ###
'x' => {
script => <<'[TheEnd]',
1h
1d
2x
2,$G
[TheEnd]
input => 'text',
expect => <<'[TheEnd]',
line 1
line 2
line 3
line 2
line 4
line 2
line 5
line 2
line 6
line 2
line 7
line 2
line 8
line 2
[TheEnd]
},
### y ###
'y' => {
script => <<'[TheEnd]',
y/abcdefghijklmnopqrstuvwxyz/ABCDEFGHIJKLMNOPQRSTUVWXYZ/
y/|/\
/
[TheEnd]
input => 'text',
expect => <<'[TheEnd]',
LINE 1
LINE 2
LINE 3
LINE 4
LINE 5
LINE 6
LINE 7
LINE 8
[TheEnd]
},
### cnt ###
'cnt' => {
script => <<'[TheEnd]',
#no autoprint
# delete line, append NL to hold space
s/.*//
H
$!b
# last line only: get hold
g
s/./X/g
t count
: count
s/^X/1/
s/0X/1/
s/1X/2/
s/2X/3/
s/3X/4/
s/4X/5/
s/5X/6/
s/6X/7/
s/7X/8/
s/8X/9/
s/9X/X0/
t count
p
[TheEnd]
input => 'text',
expect => <<'[TheEnd]',
8
[TheEnd]
},
### adr1 ###
'adr1' => {
script => <<'[TheEnd]',
#no autoprint
# This script should be run on itself
/^#__DATA__$/,${
/^#A$/p
s/^# *[0-9]* *//
/^#\*$/p
/^#\.$/p
/^#\(..\)\(..\)\2\1*$/p
/^#[abc]\{1,\}[def]\{1,\}$/p
}
#__DATA__
#A
#*
#.
#abxyxy
#abxyxyab
#abxyxyabab
#ad
#abcdef
[TheEnd]
input => 'adr1',
expect => <<'[TheEnd]',
#A
[TheEnd]
},
);
my @aux = ();
my $ntc = 2 * keys %testcase;
plan( $ntc );
# temporary file names
my $script = "s2pt$$.sed";
my $stdin = "s2pt$$.in";
my $plsed = "s2pt$$.pl";
# various command lines for
my $s2p = File::Spec->catfile( File::Spec->updir(), 'x2p', 's2p' );
my $psed = File::Spec->catfile( File::Spec->curdir(), 'psed' );
if ($^O eq 'VMS') {
# default in the .com extension if it's not already there
$s2p = VMS::Filespec::vmsify($s2p);
$psed = VMS::Filespec::vmsify($psed);
# Converting file specs from Unix format to VMS with the extended
# character set active can result in a trailing '.' added for null
# extensions. This must be removed if the intent is to default the
# extension.
$s2p =~ s/\.$//;
$psed =~ s/\.$//;
$s2p = VMS::Filespec::rmsexpand($s2p, '.com');
$psed = VMS::Filespec::rmsexpand($psed, '.com');
}
my $sedcmd = [ $psed, '-f', $script, $stdin ];
my $s2pcmd = [ $s2p, '-f', $script ];
my $plcmd = [ $plsed, $stdin ];
# psed: we create a local copy as linking may not work on some systems.
copy( $s2p, $psed );
push( @aux, $psed );
# process all testcases
#
my $indat = '';
for my $tc ( sort keys %testcase ){
my( $psedres, $s2pres );
# 1st test: run psed
# prepare the script
open( SED, ">$script" ) || goto FAIL_BOTH;
my $script = $testcase{$tc}{script};
# additional files for r, w: patch script, inserting temporary names
if( exists( $testcase{$tc}{datfil} ) ){
my( $datnam, $datdat ) = @{$testcase{$tc}{datfil}};
my $datfil = "s2pt$$" . $datnam;
push( @aux, $datfil );
open( DAT, ">$datfil" ) || goto FAIL_BOTH;
print DAT $datdat;
close( DAT );
$script =~ s/\%$datnam\%/$datfil/eg;
}
print SED $script;
close( SED ) || goto FAIL_BOTH;
# prepare input
#
if( $indat ne $testcase{$tc}{input} ){
$indat = $testcase{$tc}{input};
open( IN, ">$stdin" ) || goto FAIL_BOTH;
print IN $input{$indat};
close( IN ) || goto FAIL_BOTH;
}
# on VMS, runperl eats blank lines to work around
# spurious newlines in pipes
$testcase{$tc}{expect} =~ s/\n\n/\n/ if $^O eq 'VMS';
# run and compare
#
$psedres = runperl( args => $sedcmd );
is( $psedres, $testcase{$tc}{expect}, "psed $tc" );
# 2nd test: run s2p
# translate the sed script to a Perl program
my $perlprog = runperl( args => $s2pcmd );
open( PP, ">$plsed" ) || goto FAIL_S2P;
print PP $perlprog;
close( PP ) || goto FAIL_S2P;
# execute generated Perl program, compare
$s2pres = runperl( args => $plcmd );
is( $s2pres, $testcase{$tc}{expect}, "s2p $tc" );
next;
FAIL_BOTH:
fail( "psed $tc" );
FAIL_S2P:
fail( "s2p $tc" );
}
END {
for my $f ( $script, $stdin, $plsed, @aux ){
1 while unlink( $f ); # hats off to VMS...
}
}
|