3 # Check SPLICE function's return value
4 # (04_splice.t checks its effect on the file)
9 $: = Tie::File::_default_recsep();
10 my $data = "rec0$:rec1$:rec2$:";
16 print "ok $N\n"; $N++; # partial credit just for showing up
20 my $o = tie @a, 'Tie::File', $file, autochomp => 0;
21 print $o ? "ok $N\n" : "not ok $N\n";
26 # (3-12) splicing at the beginning
27 @r = splice(@a, 0, 0, "rec4");
29 @r = splice(@a, 0, 1, "rec5"); # same length
31 @r = splice(@a, 0, 1, "record5"); # longer
34 @r = splice(@a, 0, 1, "r5"); # shorter
35 check_result("record5");
36 @r = splice(@a, 0, 1); # removal
38 @r = splice(@a, 0, 0); # no-op
40 @r = splice(@a, 0, 0, 'r7', 'rec8'); # insert more than one
42 @r = splice(@a, 0, 2, 'rec7', 'record8', 'rec9'); # insert more than delete
43 check_result('r7', 'rec8');
45 @r = splice(@a, 0, 3, 'record9', 'rec10'); # delete more than insert
46 check_result('rec7', 'record8', 'rec9');
47 @r = splice(@a, 0, 2); # delete more than one
48 check_result('record9', 'rec10');
51 # (13-22) splicing in the middle
52 @r = splice(@a, 1, 0, "rec4");
54 @r = splice(@a, 1, 1, "rec5"); # same length
56 @r = splice(@a, 1, 1, "record5"); # longer
59 @r = splice(@a, 1, 1, "r5"); # shorter
60 check_result("record5");
61 @r = splice(@a, 1, 1); # removal
63 @r = splice(@a, 1, 0); # no-op
65 @r = splice(@a, 1, 0, 'r7', 'rec8'); # insert more than one
67 @r = splice(@a, 1, 2, 'rec7', 'record8', 'rec9'); # insert more than delete
68 check_result('r7', 'rec8');
70 @r = splice(@a, 1, 3, 'record9', 'rec10'); # delete more than insert
71 check_result('rec7', 'record8', 'rec9');
72 @r = splice(@a, 1, 2); # delete more than one
73 check_result('record9','rec10');
75 # (23-32) splicing at the end
76 @r = splice(@a, 3, 0, "rec4");
78 @r = splice(@a, 3, 1, "rec5"); # same length
80 @r = splice(@a, 3, 1, "record5"); # longer
83 @r = splice(@a, 3, 1, "r5"); # shorter
84 check_result('record5');
85 @r = splice(@a, 3, 1); # removal
87 @r = splice(@a, 3, 0); # no-op
89 @r = splice(@a, 3, 0, 'r7', 'rec8'); # insert more than one
91 @r = splice(@a, 3, 2, 'rec7', 'record8', 'rec9'); # insert more than delete
92 check_result('r7', 'rec8');
94 @r = splice(@a, 3, 3, 'record9', 'rec10'); # delete more than insert
95 check_result('rec7', 'record8', 'rec9');
96 @r = splice(@a, 3, 2); # delete more than one
97 check_result('record9', 'rec10');
99 # (33-42) splicing with negative subscript
100 @r = splice(@a, -1, 0, "rec4");
102 @r = splice(@a, -1, 1, "rec5"); # same length
103 check_result('rec2');
104 @r = splice(@a, -1, 1, "record5"); # longer
105 check_result("rec5");
107 @r = splice(@a, -1, 1, "r5"); # shorter
108 check_result("record5");
109 @r = splice(@a, -1, 1); # removal
111 @r = splice(@a, -1, 0); # no-op
113 @r = splice(@a, -1, 0, 'r7', 'rec8'); # insert more than one
115 @r = splice(@a, -1, 2, 'rec7', 'record8', 'rec9'); # insert more than delete
116 check_result('rec4');
118 @r = splice(@a, -3, 3, 'record9', 'rec10'); # delete more than insert
119 check_result('rec7', 'record8', 'rec9');
120 @r = splice(@a, -4, 3); # delete more than one
121 check_result('r7', 'rec8', 'record9');
123 # (43) scrub it all out
124 @r = splice(@a, 0, 3);
125 check_result('rec0', 'rec1', 'rec10');
127 # (44) put some back in
128 @r = splice(@a, 0, 0, "rec0", "rec1");
131 # (45) what if we remove too many records?
132 @r = splice(@a, 0, 17);
133 check_result('rec0', 'rec1');
135 # (46-48) Now check the scalar context return
136 splice(@a, 0, 0, qw(I like pie));
138 $r = splice(@a, 0, 0);
139 print !defined($r) ? "ok $N\n" : "not ok $N \# return should have been undef, was <$r>\n";
142 $r = splice(@a, 2, 1);
143 print $r eq "pie$:" ? "ok $N\n" : "not ok $N \# return should have been 'pie\\n', was <$r>\n";
146 $r = splice(@a, 0, 2);
147 print $r eq "like$:" ? "ok $N\n" : "not ok $N \# return should have been 'like\\n', was <$r>\n";
150 # (49-50) Test default arguments
151 splice @a, 0, 0, (0..11);
157 # (51-56) splice with negative length was treated wrong
158 # 20020402 Reported by Juerd Waalboer
160 @r = splice @a, 0, -3;
163 @r = splice @a, 1, -3;
166 @r = splice @a, 7, -3;
169 @r = splice @a, 0, -3;
172 @r = splice @a, 1, -3;
175 @r = splice @a, 7, -3;
180 open F, "> $file" or die $!;
186 # actual results are in @r.
187 # expected results are in @_
192 $good = 0 unless @r == @x;
193 for my $i (0 .. $#r) {
194 $good = 0 unless $r[$i] eq $x[$i];
196 print $good ? "ok $N\n" : "not ok $N \# was (@r); should be (@x)\n";
203 1 while unlink $file;