Upgrade to Tie::File 0.92, from mjd.
[p5sagit/p5-mst-13.2.git] / lib / Tie / File / t / 04_splice.t
CommitLineData
b5aed31e 1#!/usr/bin/perl
b3fe5a4c 2
b5aed31e 3#
4# Check SPLICE function's effect on the file
5# (07_rv_splice.t checks its return value)
6#
7# Each call to 'check_contents' actually performs two tests.
8# First, it calls the tied object's own 'check_integrity' method,
9# which makes sure that the contents of the read cache and offset tables
10# accurately reflect the contents of the file.
11# Then, it checks the actual contents of the file against the expected
12# contents.
13
27531ffb 14
b5aed31e 15my $file = "tf$$.txt";
b3fe5a4c 16$: = Tie::File::_default_recsep();
17my $data = "rec0$:rec1$:rec2$:";
bf919750 18print "1..118\n";
7b6b3db1 19
20init_file($data);
b5aed31e 21
22my $N = 1;
23use Tie::File;
24print "ok $N\n"; $N++; # partial credit just for showing up
25
26my $o = tie @a, 'Tie::File', $file;
27print $o ? "ok $N\n" : "not ok $N\n";
28$N++;
29
b3fe5a4c 30$: = $o->{recsep};
b5aed31e 31my $n;
32
33# (3-22) splicing at the beginning
b5aed31e 34splice(@a, 0, 0, "rec4");
b3fe5a4c 35check_contents("rec4$:$data");
b5aed31e 36splice(@a, 0, 1, "rec5"); # same length
b3fe5a4c 37check_contents("rec5$:$data");
b5aed31e 38splice(@a, 0, 1, "record5"); # longer
b3fe5a4c 39check_contents("record5$:$data");
b5aed31e 40
41splice(@a, 0, 1, "r5"); # shorter
b3fe5a4c 42check_contents("r5$:$data");
b5aed31e 43splice(@a, 0, 1); # removal
44check_contents("$data");
45splice(@a, 0, 0); # no-op
46check_contents("$data");
47splice(@a, 0, 0, 'r7', 'rec8'); # insert more than one
b3fe5a4c 48check_contents("r7$:rec8$:$data");
b5aed31e 49splice(@a, 0, 2, 'rec7', 'record8', 'rec9'); # insert more than delete
b3fe5a4c 50check_contents("rec7$:record8$:rec9$:$data");
b5aed31e 51
52splice(@a, 0, 3, 'record9', 'rec10'); # delete more than insert
b3fe5a4c 53check_contents("record9$:rec10$:$data");
b5aed31e 54splice(@a, 0, 2); # delete more than one
55check_contents("$data");
56
57
58# (23-42) splicing in the middle
59splice(@a, 1, 0, "rec4");
b3fe5a4c 60check_contents("rec0$:rec4$:rec1$:rec2$:");
b5aed31e 61splice(@a, 1, 1, "rec5"); # same length
b3fe5a4c 62check_contents("rec0$:rec5$:rec1$:rec2$:");
b5aed31e 63splice(@a, 1, 1, "record5"); # longer
b3fe5a4c 64check_contents("rec0$:record5$:rec1$:rec2$:");
b5aed31e 65
66splice(@a, 1, 1, "r5"); # shorter
b3fe5a4c 67check_contents("rec0$:r5$:rec1$:rec2$:");
b5aed31e 68splice(@a, 1, 1); # removal
69check_contents("$data");
70splice(@a, 1, 0); # no-op
71check_contents("$data");
72splice(@a, 1, 0, 'r7', 'rec8'); # insert more than one
b3fe5a4c 73check_contents("rec0$:r7$:rec8$:rec1$:rec2$:");
b5aed31e 74splice(@a, 1, 2, 'rec7', 'record8', 'rec9'); # insert more than delete
b3fe5a4c 75check_contents("rec0$:rec7$:record8$:rec9$:rec1$:rec2$:");
b5aed31e 76
77splice(@a, 1, 3, 'record9', 'rec10'); # delete more than insert
b3fe5a4c 78check_contents("rec0$:record9$:rec10$:rec1$:rec2$:");
b5aed31e 79splice(@a, 1, 2); # delete more than one
80check_contents("$data");
81
82# (43-62) splicing at the end
83splice(@a, 3, 0, "rec4");
b3fe5a4c 84check_contents("$ {data}rec4$:");
b5aed31e 85splice(@a, 3, 1, "rec5"); # same length
b3fe5a4c 86check_contents("$ {data}rec5$:");
b5aed31e 87splice(@a, 3, 1, "record5"); # longer
b3fe5a4c 88check_contents("$ {data}record5$:");
b5aed31e 89
90splice(@a, 3, 1, "r5"); # shorter
b3fe5a4c 91check_contents("$ {data}r5$:");
b5aed31e 92splice(@a, 3, 1); # removal
93check_contents("$data");
94splice(@a, 3, 0); # no-op
95check_contents("$data");
96splice(@a, 3, 0, 'r7', 'rec8'); # insert more than one
b3fe5a4c 97check_contents("$ {data}r7$:rec8$:");
b5aed31e 98splice(@a, 3, 2, 'rec7', 'record8', 'rec9'); # insert more than delete
b3fe5a4c 99check_contents("$ {data}rec7$:record8$:rec9$:");
b5aed31e 100
101splice(@a, 3, 3, 'record9', 'rec10'); # delete more than insert
b3fe5a4c 102check_contents("$ {data}record9$:rec10$:");
b5aed31e 103splice(@a, 3, 2); # delete more than one
104check_contents("$data");
105
106# (63-82) splicing with negative subscript
107splice(@a, -1, 0, "rec4");
b3fe5a4c 108check_contents("rec0$:rec1$:rec4$:rec2$:");
b5aed31e 109splice(@a, -1, 1, "rec5"); # same length
b3fe5a4c 110check_contents("rec0$:rec1$:rec4$:rec5$:");
b5aed31e 111splice(@a, -1, 1, "record5"); # longer
b3fe5a4c 112check_contents("rec0$:rec1$:rec4$:record5$:");
b5aed31e 113
114splice(@a, -1, 1, "r5"); # shorter
b3fe5a4c 115check_contents("rec0$:rec1$:rec4$:r5$:");
b5aed31e 116splice(@a, -1, 1); # removal
b3fe5a4c 117check_contents("rec0$:rec1$:rec4$:");
b5aed31e 118splice(@a, -1, 0); # no-op
b3fe5a4c 119check_contents("rec0$:rec1$:rec4$:");
b5aed31e 120splice(@a, -1, 0, 'r7', 'rec8'); # insert more than one
b3fe5a4c 121check_contents("rec0$:rec1$:r7$:rec8$:rec4$:");
b5aed31e 122splice(@a, -1, 2, 'rec7', 'record8', 'rec9'); # insert more than delete
b3fe5a4c 123check_contents("rec0$:rec1$:r7$:rec8$:rec7$:record8$:rec9$:");
b5aed31e 124
125splice(@a, -3, 3, 'record9', 'rec10'); # delete more than insert
b3fe5a4c 126check_contents("rec0$:rec1$:r7$:rec8$:record9$:rec10$:");
b5aed31e 127splice(@a, -4, 3); # delete more than one
b3fe5a4c 128check_contents("rec0$:rec1$:rec10$:");
b5aed31e 129
130# (83-84) scrub it all out
131splice(@a, 0, 3);
132check_contents("");
133
134# (85-86) put some back in
135splice(@a, 0, 0, "rec0", "rec1");
b3fe5a4c 136check_contents("rec0$:rec1$:");
b5aed31e 137
138# (87-88) what if we remove too many records?
139splice(@a, 0, 17);
140check_contents("");
141
51efdd02 142# (89-92) In the past, splicing past the end was not correctly detected
143# (1.14)
144splice(@a, 89, 3);
145check_contents("");
146splice(@a, @a, 3);
147check_contents("");
148
149# (93-96) Also we did not emulate splice's freaky behavior when inserting
150# past the end of the array (1.14)
151splice(@a, 89, 0, "I", "like", "pie");
b3fe5a4c 152check_contents("I$:like$:pie$:");
51efdd02 153splice(@a, 89, 0, "pie pie pie");
b3fe5a4c 154check_contents("I$:like$:pie$:pie pie pie$:");
51efdd02 155
156# (97) Splicing with too large a negative number should be fatal
cf8feb78 157# This test ignored because it causes 5.6.1 and 5.7.3 to dump core
158# It also garbles the stack under 5.005_03 (20020401)
51efdd02 159# NOT MY FAULT
cf8feb78 160if ($] > 5.008) {
51efdd02 161 eval { splice(@a, -7, 0) };
162 print $@ =~ /^Modification of non-creatable array value attempted, subscript -7/
163 ? "ok $N\n" : "not ok $N \# \$\@ was '$@'\n";
164} else {
cf8feb78 165 print "ok $N \# skipped (5.6.0 through 5.8 dump core here.)\n";
51efdd02 166}
167$N++;
168
7b6b3db1 169# (98-101) Test default arguments
170splice @a, 0, 0, (0..11);
171splice @a, 4;
b3fe5a4c 172check_contents("0$:1$:2$:3$:");
7b6b3db1 173splice @a;
174check_contents("");
27531ffb 175
176# (102-103) I think there's a bug here---it will fail to clear the EOF flag
177@a = (0..11);
178splice @a, -1, 1000;
179check_contents("0$:1$:2$:3$:4$:5$:6$:7$:8$:9$:10$:");
bf919750 180
27531ffb 181# (104-106) make sure that undefs are treated correctly---they should
182# be converted to empty records, and should not raise any warnings.
183# (Some of these failed in 0.90. The change to _fixrec fixed them.)
184# 20020331
185{
186 my $good = 1; my $warn;
187 # If any of these raise warnings, we have a problem.
188 local $SIG{__WARN__} = sub { $good = 0; $warn = shift(); ctrlfix($warn)};
189 local $^W = 1;
190 @a = (1);
191 splice @a, 1, 0, undef, undef, undef;
192 print $good ? "ok $N\n" : "not ok $N # $warn\n";
193 $N++; $good = 1;
194 print defined($a[2]) ? "ok $N\n" : "not ok $N\n";
195 $N++; $good = 1;
196 my @r = splice @a, 2;
197 print defined($r[0]) ? "ok $N\n" : "not ok $N\n";
198 $N++; $good = 1;
199}
51efdd02 200
bf919750 201# (107-118) splice with negative length was treated wrong
202# 20020402 Reported by Juerd Waalboer
203@a = (0..8) ;
204splice @a, 0, -3;
205check_contents("6$:7$:8$:");
206@a = (0..8) ;
207splice @a, 1, -3;
208check_contents("0$:6$:7$:8$:");
209@a = (0..8) ;
210splice @a, 7, -3;
211check_contents("0$:1$:2$:3$:4$:5$:6$:7$:8$:");
212@a = (0..2) ;
213splice @a, 0, -3;
214check_contents("0$:1$:2$:");
215@a = (0..2) ;
216splice @a, 1, -3;
217check_contents("0$:1$:2$:");
218@a = (0..2) ;
219splice @a, 7, -3;
220check_contents("0$:1$:2$:");
221
b5aed31e 222sub init_file {
223 my $data = shift;
224 open F, "> $file" or die $!;
1768807e 225 binmode F;
b5aed31e 226 print F $data;
227 close F;
228}
229
7b6b3db1 230use POSIX 'SEEK_SET';
b5aed31e 231sub check_contents {
232 my $x = shift;
b5aed31e 233 my $integrity = $o->_check_integrity($file, $ENV{INTEGRITY});
7b6b3db1 234 local *FH = $o->{fh};
235 seek FH, 0, SEEK_SET;
b5aed31e 236 print $integrity ? "ok $N\n" : "not ok $N\n";
237 $N++;
b5aed31e 238 my $a;
239 { local $/; $a = <FH> }
7b6b3db1 240 $a = "" unless defined $a;
241 if ($a eq $x) {
242 print "ok $N\n";
243 } else {
b3fe5a4c 244 ctrlfix($a, $x);
7b6b3db1 245 print "not ok $N\n# expected <$x>, got <$a>\n";
246 }
b5aed31e 247 $N++;
248}
249
b3fe5a4c 250
251sub ctrlfix {
252 for (@_) {
253 s/\n/\\n/g;
254 s/\r/\\r/g;
255 }
256}
257
b5aed31e 258END {
7b6b3db1 259 undef $o;
260 untie @a;
b5aed31e 261 1 while unlink $file;
262}
263