1N/A#!/usr/bin/perl
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/Ause POSIX 'SEEK_SET';
1N/A
1N/Amy $file = "tf$$.txt";
1N/Amy $data = "rec0blahrec1blahrec2blah";
1N/A
1N/Aprint "1..101\n";
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/Ainit_file($data);
1N/A
1N/Amy $o = tie @a, 'Tie::File', $file, recsep => 'blah';
1N/Aprint $o ? "ok $N\n" : "not ok $N\n";
1N/A$N++;
1N/A
1N/Amy $n;
1N/A
1N/A# (3-22) splicing at the beginning
1N/Asplice(@a, 0, 0, "rec4");
1N/Acheck_contents("rec4blah$data");
1N/Asplice(@a, 0, 1, "rec5"); # same length
1N/Acheck_contents("rec5blah$data");
1N/Asplice(@a, 0, 1, "record5"); # longer
1N/Acheck_contents("record5blah$data");
1N/A
1N/Asplice(@a, 0, 1, "r5"); # shorter
1N/Acheck_contents("r5blah$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("r7blahrec8blah$data");
1N/Asplice(@a, 0, 2, 'rec7', 'record8', 'rec9'); # insert more than delete
1N/Acheck_contents("rec7blahrecord8blahrec9blah$data");
1N/A
1N/Asplice(@a, 0, 3, 'record9', 'rec10'); # delete more than insert
1N/Acheck_contents("record9blahrec10blah$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("rec0blahrec4blahrec1blahrec2blah");
1N/Asplice(@a, 1, 1, "rec5"); # same length
1N/Acheck_contents("rec0blahrec5blahrec1blahrec2blah");
1N/Asplice(@a, 1, 1, "record5"); # longer
1N/Acheck_contents("rec0blahrecord5blahrec1blahrec2blah");
1N/A
1N/Asplice(@a, 1, 1, "r5"); # shorter
1N/Acheck_contents("rec0blahr5blahrec1blahrec2blah");
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("rec0blahr7blahrec8blahrec1blahrec2blah");
1N/Asplice(@a, 1, 2, 'rec7', 'record8', 'rec9'); # insert more than delete
1N/Acheck_contents("rec0blahrec7blahrecord8blahrec9blahrec1blahrec2blah");
1N/A
1N/Asplice(@a, 1, 3, 'record9', 'rec10'); # delete more than insert
1N/Acheck_contents("rec0blahrecord9blahrec10blahrec1blahrec2blah");
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}rec4blah");
1N/Asplice(@a, 3, 1, "rec5"); # same length
1N/Acheck_contents("$ {data}rec5blah");
1N/Asplice(@a, 3, 1, "record5"); # longer
1N/Acheck_contents("$ {data}record5blah");
1N/A
1N/Asplice(@a, 3, 1, "r5"); # shorter
1N/Acheck_contents("$ {data}r5blah");
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}r7blahrec8blah");
1N/Asplice(@a, 3, 2, 'rec7', 'record8', 'rec9'); # insert more than delete
1N/Acheck_contents("$ {data}rec7blahrecord8blahrec9blah");
1N/A
1N/Asplice(@a, 3, 3, 'record9', 'rec10'); # delete more than insert
1N/Acheck_contents("$ {data}record9blahrec10blah");
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("rec0blahrec1blahrec4blahrec2blah");
1N/Asplice(@a, -1, 1, "rec5"); # same length
1N/Acheck_contents("rec0blahrec1blahrec4blahrec5blah");
1N/Asplice(@a, -1, 1, "record5"); # longer
1N/Acheck_contents("rec0blahrec1blahrec4blahrecord5blah");
1N/A
1N/Asplice(@a, -1, 1, "r5"); # shorter
1N/Acheck_contents("rec0blahrec1blahrec4blahr5blah");
1N/Asplice(@a, -1, 1); # removal
1N/Acheck_contents("rec0blahrec1blahrec4blah");
1N/Asplice(@a, -1, 0); # no-op
1N/Acheck_contents("rec0blahrec1blahrec4blah");
1N/Asplice(@a, -1, 0, 'r7', 'rec8'); # insert more than one
1N/Acheck_contents("rec0blahrec1blahr7blahrec8blahrec4blah");
1N/Asplice(@a, -1, 2, 'rec7', 'record8', 'rec9'); # insert more than delete
1N/Acheck_contents("rec0blahrec1blahr7blahrec8blahrec7blahrecord8blahrec9blah");
1N/A
1N/Asplice(@a, -3, 3, 'record9', 'rec10'); # delete more than insert
1N/Acheck_contents("rec0blahrec1blahr7blahrec8blahrecord9blahrec10blah");
1N/Asplice(@a, -4, 3); # delete more than one
1N/Acheck_contents("rec0blahrec1blahrec10blah");
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("rec0blahrec1blah");
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# (0.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("Iblahlikeblahpieblah");
1N/Asplice(@a, 89, 0, "pie pie pie");
1N/Acheck_contents("Iblahlikeblahpieblahpie pie pieblah");
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("0blah1blah2blah3blah");
1N/Asplice @a;
1N/Acheck_contents("");
1N/A
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/Asub check_contents {
1N/A my $x = shift;
1N/A my $integrity = $o->_check_integrity($file, $ENV{INTEGRITY});
1N/A print $integrity ? "ok $N\n" : "not ok $N\n";
1N/A $N++;
1N/A local *FH = $o->{fh};
1N/A seek FH, 0, SEEK_SET;
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(my $msg = "# expected <$x>, got <$a>");
1N/A print "not ok $N\n$msg\n";
1N/A }
1N/A $N++;
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