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