1N/A#!/usr/bin/perl
1N/A
1N/A#
1N/A# Check SPLICE function's effect on the file
1N/A# (07_rv_splice.t checks its return value)
1N/A#
1N/A# Each call to 'check_contents' actually performs two tests.
1N/A# First, it calls the tied object's own 'check_integrity' method,
1N/A# which makes sure that the contents of the read cache and offset tables
1N/A# accurately reflect the contents of the file.
1N/A# Then, it checks the actual contents of the file against the expected
1N/A# contents.
1N/A
1N/A
1N/A$| = 1;
1N/Amy $file = "tf$$.txt";
1N/A$: = Tie::File::_default_recsep();
1N/Amy $data = "rec0$:rec1$:rec2$:";
1N/Aprint "1..118\n";
1N/A
1N/Ainit_file($data);
1N/A
1N/Amy $N = 1;
1N/Ause Tie::File;
1N/Aprint "ok $N\n"; $N++; # partial credit just for showing up
1N/A
1N/Amy $o = tie @a, 'Tie::File', $file;
1N/Aprint $o ? "ok $N\n" : "not ok $N\n";
1N/A$N++;
1N/A
1N/A$: = $o->{recsep};
1N/Amy $n;
1N/A
1N/A# (3-22) splicing at the beginning
1N/Asplice(@a, 0, 0, "rec4");
1N/Acheck_contents("rec4$:$data");
1N/Asplice(@a, 0, 1, "rec5"); # same length
1N/Acheck_contents("rec5$:$data");
1N/Asplice(@a, 0, 1, "record5"); # longer
1N/Acheck_contents("record5$:$data");
1N/A
1N/Asplice(@a, 0, 1, "r5"); # shorter
1N/Acheck_contents("r5$:$data");
1N/Asplice(@a, 0, 1); # removal
1N/Acheck_contents("$data");
1N/Asplice(@a, 0, 0); # no-op
1N/Acheck_contents("$data");
1N/Asplice(@a, 0, 0, 'r7', 'rec8'); # insert more than one
1N/Acheck_contents("r7$:rec8$:$data");
1N/Asplice(@a, 0, 2, 'rec7', 'record8', 'rec9'); # insert more than delete
1N/Acheck_contents("rec7$:record8$:rec9$:$data");
1N/A
1N/Asplice(@a, 0, 3, 'record9', 'rec10'); # delete more than insert
1N/Acheck_contents("record9$:rec10$:$data");
1N/Asplice(@a, 0, 2); # delete more than one
1N/Acheck_contents("$data");
1N/A
1N/A
1N/A# (23-42) splicing in the middle
1N/Asplice(@a, 1, 0, "rec4");
1N/Acheck_contents("rec0$:rec4$:rec1$:rec2$:");
1N/Asplice(@a, 1, 1, "rec5"); # same length
1N/Acheck_contents("rec0$:rec5$:rec1$:rec2$:");
1N/Asplice(@a, 1, 1, "record5"); # longer
1N/Acheck_contents("rec0$:record5$:rec1$:rec2$:");
1N/A
1N/Asplice(@a, 1, 1, "r5"); # shorter
1N/Acheck_contents("rec0$:r5$:rec1$:rec2$:");
1N/Asplice(@a, 1, 1); # removal
1N/Acheck_contents("$data");
1N/Asplice(@a, 1, 0); # no-op
1N/Acheck_contents("$data");
1N/Asplice(@a, 1, 0, 'r7', 'rec8'); # insert more than one
1N/Acheck_contents("rec0$:r7$:rec8$:rec1$:rec2$:");
1N/Asplice(@a, 1, 2, 'rec7', 'record8', 'rec9'); # insert more than delete
1N/Acheck_contents("rec0$:rec7$:record8$:rec9$:rec1$:rec2$:");
1N/A
1N/Asplice(@a, 1, 3, 'record9', 'rec10'); # delete more than insert
1N/Acheck_contents("rec0$:record9$:rec10$:rec1$:rec2$:");
1N/Asplice(@a, 1, 2); # delete more than one
1N/Acheck_contents("$data");
1N/A
1N/A# (43-62) splicing at the end
1N/Asplice(@a, 3, 0, "rec4");
1N/Acheck_contents("$ {data}rec4$:");
1N/Asplice(@a, 3, 1, "rec5"); # same length
1N/Acheck_contents("$ {data}rec5$:");
1N/Asplice(@a, 3, 1, "record5"); # longer
1N/Acheck_contents("$ {data}record5$:");
1N/A
1N/Asplice(@a, 3, 1, "r5"); # shorter
1N/Acheck_contents("$ {data}r5$:");
1N/Asplice(@a, 3, 1); # removal
1N/Acheck_contents("$data");
1N/Asplice(@a, 3, 0); # no-op
1N/Acheck_contents("$data");
1N/Asplice(@a, 3, 0, 'r7', 'rec8'); # insert more than one
1N/Acheck_contents("$ {data}r7$:rec8$:");
1N/Asplice(@a, 3, 2, 'rec7', 'record8', 'rec9'); # insert more than delete
1N/Acheck_contents("$ {data}rec7$:record8$:rec9$:");
1N/A
1N/Asplice(@a, 3, 3, 'record9', 'rec10'); # delete more than insert
1N/Acheck_contents("$ {data}record9$:rec10$:");
1N/Asplice(@a, 3, 2); # delete more than one
1N/Acheck_contents("$data");
1N/A
1N/A# (63-82) splicing with negative subscript
1N/Asplice(@a, -1, 0, "rec4");
1N/Acheck_contents("rec0$:rec1$:rec4$:rec2$:");
1N/Asplice(@a, -1, 1, "rec5"); # same length
1N/Acheck_contents("rec0$:rec1$:rec4$:rec5$:");
1N/Asplice(@a, -1, 1, "record5"); # longer
1N/Acheck_contents("rec0$:rec1$:rec4$:record5$:");
1N/A
1N/Asplice(@a, -1, 1, "r5"); # shorter
1N/Acheck_contents("rec0$:rec1$:rec4$:r5$:");
1N/Asplice(@a, -1, 1); # removal
1N/Acheck_contents("rec0$:rec1$:rec4$:");
1N/Asplice(@a, -1, 0); # no-op
1N/Acheck_contents("rec0$:rec1$:rec4$:");
1N/Asplice(@a, -1, 0, 'r7', 'rec8'); # insert more than one
1N/Acheck_contents("rec0$:rec1$:r7$:rec8$:rec4$:");
1N/Asplice(@a, -1, 2, 'rec7', 'record8', 'rec9'); # insert more than delete
1N/Acheck_contents("rec0$:rec1$:r7$:rec8$:rec7$:record8$:rec9$:");
1N/A
1N/Asplice(@a, -3, 3, 'record9', 'rec10'); # delete more than insert
1N/Acheck_contents("rec0$:rec1$:r7$:rec8$:record9$:rec10$:");
1N/Asplice(@a, -4, 3); # delete more than one
1N/Acheck_contents("rec0$:rec1$:rec10$:");
1N/A
1N/A# (83-84) scrub it all out
1N/Asplice(@a, 0, 3);
1N/Acheck_contents("");
1N/A
1N/A# (85-86) put some back in
1N/Asplice(@a, 0, 0, "rec0", "rec1");
1N/Acheck_contents("rec0$:rec1$:");
1N/A
1N/A# (87-88) what if we remove too many records?
1N/Asplice(@a, 0, 17);
1N/Acheck_contents("");
1N/A
1N/A# (89-92) In the past, splicing past the end was not correctly detected
1N/A# (1.14)
1N/Asplice(@a, 89, 3);
1N/Acheck_contents("");
1N/Asplice(@a, @a, 3);
1N/Acheck_contents("");
1N/A
1N/A# (93-96) Also we did not emulate splice's freaky behavior when inserting
1N/A# past the end of the array (1.14)
1N/Asplice(@a, 89, 0, "I", "like", "pie");
1N/Acheck_contents("I$:like$:pie$:");
1N/Asplice(@a, 89, 0, "pie pie pie");
1N/Acheck_contents("I$:like$:pie$:pie pie pie$:");
1N/A
1N/A# (97) Splicing with too large a negative number should be fatal
1N/A# This test ignored because it causes 5.6.1 and 5.7.3 to dump core
1N/A# It also garbles the stack under 5.005_03 (20020401)
1N/A# NOT MY FAULT
1N/Aif ($] > 5.007003) {
1N/A eval { splice(@a, -7, 0) };
1N/A print $@ =~ /^Modification of non-creatable array value attempted, subscript -7/
1N/A ? "ok $N\n" : "not ok $N \# \$\@ was '$@'\n";
1N/A} else {
1N/A print "ok $N \# skipped (versions through 5.7.3 dump core here.)\n";
1N/A}
1N/A$N++;
1N/A
1N/A# (98-101) Test default arguments
1N/Asplice @a, 0, 0, (0..11);
1N/Asplice @a, 4;
1N/Acheck_contents("0$:1$:2$:3$:");
1N/Asplice @a;
1N/Acheck_contents("");
1N/A
1N/A# (102-103) I think there's a bug here---it will fail to clear the EOF flag
1N/A@a = (0..11);
1N/Asplice @a, -1, 1000;
1N/Acheck_contents("0$:1$:2$:3$:4$:5$:6$:7$:8$:9$:10$:");
1N/A
1N/A# (104-106) make sure that undefs are treated correctly---they should
1N/A# be converted to empty records, and should not raise any warnings.
1N/A# (Some of these failed in 0.90. The change to _fixrec fixed them.)
1N/A# 20020331
1N/A{
1N/A my $good = 1; my $warn;
1N/A # If any of these raise warnings, we have a problem.
1N/A local $SIG{__WARN__} = sub { $good = 0; $warn = shift(); ctrlfix($warn)};
1N/A local $^W = 1;
1N/A @a = (1);
1N/A splice @a, 1, 0, undef, undef, undef;
1N/A print $good ? "ok $N\n" : "not ok $N # $warn\n";
1N/A $N++; $good = 1;
1N/A print defined($a[2]) ? "ok $N\n" : "not ok $N\n";
1N/A $N++; $good = 1;
1N/A my @r = splice @a, 2;
1N/A print defined($r[0]) ? "ok $N\n" : "not ok $N\n";
1N/A $N++; $good = 1;
1N/A}
1N/A
1N/A# (107-118) splice with negative length was treated wrong
1N/A# 20020402 Reported by Juerd Waalboer
1N/A@a = (0..8) ;
1N/Asplice @a, 0, -3;
1N/Acheck_contents("6$:7$:8$:");
1N/A@a = (0..8) ;
1N/Asplice @a, 1, -3;
1N/Acheck_contents("0$:6$:7$:8$:");
1N/A@a = (0..8) ;
1N/Asplice @a, 7, -3;
1N/Acheck_contents("0$:1$:2$:3$:4$:5$:6$:7$:8$:");
1N/A@a = (0..2) ;
1N/Asplice @a, 0, -3;
1N/Acheck_contents("0$:1$:2$:");
1N/A@a = (0..2) ;
1N/Asplice @a, 1, -3;
1N/Acheck_contents("0$:1$:2$:");
1N/A@a = (0..2) ;
1N/Asplice @a, 7, -3;
1N/Acheck_contents("0$:1$:2$:");
1N/A
1N/Asub init_file {
1N/A my $data = shift;
1N/A open F, "> $file" or die $!;
1N/A binmode F;
1N/A print F $data;
1N/A close F;
1N/A}
1N/A
1N/Ause POSIX 'SEEK_SET';
1N/Asub check_contents {
1N/A my $x = shift;
1N/A my $integrity = $o->_check_integrity($file, $ENV{INTEGRITY});
1N/A local *FH = $o->{fh};
1N/A seek FH, 0, SEEK_SET;
1N/A print $integrity ? "ok $N\n" : "not ok $N\n";
1N/A $N++;
1N/A my $a;
1N/A { local $/; $a = <FH> }
1N/A $a = "" unless defined $a;
1N/A if ($a eq $x) {
1N/A print "ok $N\n";
1N/A } else {
1N/A ctrlfix($a, $x);
1N/A print "not ok $N\n# expected <$x>, got <$a>\n";
1N/A }
1N/A $N++;
1N/A}
1N/A
1N/A
1N/Asub ctrlfix {
1N/A for (@_) {
1N/A s/\n/\\n/g;
1N/A s/\r/\\r/g;
1N/A }
1N/A}
1N/A
1N/AEND {
1N/A undef $o;
1N/A untie @a;
1N/A 1 while unlink $file;
1N/A}
1N/A