1N/A#!/usr/bin/perl
1N/A#
1N/A# Unit tests of _twrite function
1N/A#
1N/A# _twrite($self, $data, $pos, $len)
1N/A#
1N/A# 't' here is for 'tail'. This writes $data at absolute position $pos
1N/A# in the file, overwriting exactly $len of the bytes at that position.
1N/A# Everything else is moved down or up, dependong on whether
1N/A# length($data) > $len or length($data) < $len.
1N/A# $len == 0 is a pure insert; $len == length($data) is a simple overwrite.
1N/A#
1N/A
1N/Amy $file = "tf$$.txt";
1N/A
1N/Aprint "1..181\n";
1N/A
1N/Amy $N = 1;
1N/Ause Tie::File;
1N/Aprint "ok $N\n"; $N++;
1N/A
1N/A$: = Tie::File::_default_recsep();
1N/A
1N/A# (2) Peter Scott sent this one. It fails in 0.51 and works in 0.90
1N/A# <4.3.2.7.2.20020331102819.00b913d0@shell2.webquarry.com>
1N/A#
1N/A# The problem was premature termination in the inner loop
1N/A# because you had $more_data scoped *inside* the block instead of outside.
1N/A# 20020331
1N/Aopen F, "> $file" or die "Couldn't open $file: $!";
1N/Abinmode F;
1N/Afor (1..100) {
1N/A print F "$_ ", 'a'x150, $: ;
1N/A}
1N/Aclose F;
1N/A# The file is now 15292 characters long on Unix, 15392 on Win32
1N/Adie -s $file unless -s $file == 15292 + 100 * length($:);
1N/A
1N/Atie my @lines, 'Tie::File', $file or die $!;
1N/Apush @lines, "1001 ".('a' x 100);
1N/Asplice @lines, 0, 1;
1N/Auntie @lines;
1N/A
1N/Amy $s = -s $file;
1N/Amy $x = 15292 - 152 + 105 + 100*length($:);
1N/Aprint $s == $x
1N/A ? "ok $N\n" : "not ok $N # expected $x, got $s\n";
1N/A$N++;
1N/A
1N/Amy @subtests = qw(x <x x> x><x <x> <x><x x><x> <x><x> <x><x><x> 0);
1N/A
1N/A# (3-73) These were generated by 'gentests.pl' to cover all possible cases
1N/A# (I hope)
1N/A# Legend:
1N/A# x: data is entirely contained within one block
1N/A# x>: data runs from the middle to the end of the block
1N/A# <x: data runs from the start to the middle of the block
1N/A# <x>: data occupies precisely one block
1N/A# x><x: data overlaps one block boundary
1N/A# <x><x: data runs from the start of one block into the middle of the next
1N/A# x><x>: data runs from the middle of one block to the end of the next
1N/A# <x><x>: data occupies two blocks exactly
1N/A# <x><x><x>: data occupies three blocks exactly
1N/A# 0: data is null
1N/A#
1N/A# For each possible alignment of the old and new data, we investigate
1N/A# up to three situations: old data is shorter, old and new data are the
1N/A# same length, and new data is shorter.
1N/A#
1N/A# try($pos, $old, $new) means to run a test where the data starts at
1N/A# position $pos, the old data has length $old,
1N/A# and the new data has length $new.
1N/Atry( 9659, 6635, 6691); # old=x , new=x ; old < new
1N/Atry( 8605, 2394, 2394); # old=x , new=x ; old = new
1N/Atry( 9768, 1361, 664); # old=x , new=x ; old > new
1N/Atry( 9955, 6429, 6429); # old=x> , new=x ; old = new
1N/Atry(10550, 5834, 4123); # old=x> , new=x ; old > new
1N/Atry(14580, 6158, 851); # old=x><x , new=x ; old > new
1N/Atry(13442, 11134, 1572); # old=x><x> , new=x ; old > new
1N/Atry( 8394, 0, 5742); # old=0 , new=x ; old < new
1N/Atry( 8192, 2819, 6738); # old=<x , new=<x ; old < new
1N/Atry( 8192, 514, 514); # old=<x , new=<x ; old = new
1N/Atry( 8192, 2196, 858); # old=<x , new=<x ; old > new
1N/Atry( 8192, 8192, 8192); # old=<x> , new=<x ; old = new
1N/Atry( 8192, 8192, 1290); # old=<x> , new=<x ; old > new
1N/Atry( 8192, 10575, 6644); # old=<x><x , new=<x ; old > new
1N/Atry( 8192, 16384, 5616); # old=<x><x> , new=<x ; old > new
1N/Atry( 8192, 24576, 6253); # old=<x><x><x>, new=<x ; old > new
1N/Atry( 8192, 0, 6870); # old=0 , new=<x ; old < new
1N/Atry( 8478, 6259, 7906); # old=x , new=x> ; old < new
1N/Atry( 9965, 6419, 6419); # old=x> , new=x> ; old = new
1N/Atry(16059, 6102, 325); # old=x><x , new=x> ; old > new
1N/Atry( 9503, 15073, 6881); # old=x><x> , new=x> ; old > new
1N/Atry( 9759, 0, 6625); # old=0 , new=x> ; old < new
1N/Atry( 8525, 2081, 8534); # old=x , new=x><x ; old < new
1N/Atry(15550, 834, 1428); # old=x> , new=x><x ; old < new
1N/Atry(14966, 1668, 3479); # old=x><x , new=x><x ; old < new
1N/Atry(16316, 1605, 1605); # old=x><x , new=x><x ; old = new
1N/Atry(16093, 4074, 993); # old=x><x , new=x><x ; old > new
1N/Atry(14739, 9837, 9837); # old=x><x> , new=x><x ; old = new
1N/Atry(14071, 10505, 7344); # old=x><x> , new=x><x ; old > new
1N/Atry(12602, 0, 8354); # old=0 , new=x><x ; old < new
1N/Atry( 8192, 2767, 8192); # old=<x , new=<x> ; old < new
1N/Atry( 8192, 8192, 8192); # old=<x> , new=<x> ; old = new
1N/Atry( 8192, 14817, 8192); # old=<x><x , new=<x> ; old > new
1N/Atry( 8192, 16384, 8192); # old=<x><x> , new=<x> ; old > new
1N/Atry( 8192, 24576, 8192); # old=<x><x><x>, new=<x> ; old > new
1N/Atry( 8192, 0, 8192); # old=0 , new=<x> ; old < new
1N/Atry( 8192, 6532, 10882); # old=<x , new=<x><x ; old < new
1N/Atry( 8192, 8192, 16044); # old=<x> , new=<x><x ; old < new
1N/Atry( 8192, 9555, 11020); # old=<x><x , new=<x><x ; old < new
1N/Atry( 8192, 9001, 9001); # old=<x><x , new=<x><x ; old = new
1N/Atry( 8192, 11760, 10274); # old=<x><x , new=<x><x ; old > new
1N/Atry( 8192, 16384, 10781); # old=<x><x> , new=<x><x ; old > new
1N/Atry( 8192, 24576, 9284); # old=<x><x><x>, new=<x><x ; old > new
1N/Atry( 8192, 0, 12488); # old=0 , new=<x><x ; old < new
1N/Atry( 8222, 6385, 16354); # old=x , new=x><x> ; old < new
1N/Atry(13500, 2884, 11076); # old=x> , new=x><x> ; old < new
1N/Atry(14069, 4334, 10507); # old=x><x , new=x><x> ; old < new
1N/Atry(14761, 9815, 9815); # old=x><x> , new=x><x> ; old = new
1N/Atry(10469, 0, 14107); # old=0 , new=x><x> ; old < new
1N/Atry( 8192, 4181, 16384); # old=<x , new=<x><x> ; old < new
1N/Atry( 8192, 8192, 16384); # old=<x> , new=<x><x> ; old < new
1N/Atry( 8192, 12087, 16384); # old=<x><x , new=<x><x> ; old < new
1N/Atry( 8192, 16384, 16384); # old=<x><x> , new=<x><x> ; old = new
1N/Atry( 8192, 24576, 16384); # old=<x><x><x>, new=<x><x> ; old > new
1N/Atry( 8192, 0, 16384); # old=0 , new=<x><x> ; old < new
1N/Atry( 8192, 4968, 24576); # old=<x , new=<x><x><x>; old < new
1N/Atry( 8192, 8192, 24576); # old=<x> , new=<x><x><x>; old < new
1N/Atry( 8192, 14163, 24576); # old=<x><x , new=<x><x><x>; old < new
1N/Atry( 8192, 16384, 24576); # old=<x><x> , new=<x><x><x>; old < new
1N/Atry( 8192, 24576, 24576); # old=<x><x><x>, new=<x><x><x>; old = new
1N/Atry( 8192, 0, 24576); # old=0 , new=<x><x><x>; old < new
1N/Atry( 8771, 776, 0); # old=x , new=0 ; old > new
1N/Atry( 8192, 2813, 0); # old=<x , new=0 ; old > new
1N/Atry(13945, 2439, 0); # old=x> , new=0 ; old > new
1N/Atry(14493, 6090, 0); # old=x><x , new=0 ; old > new
1N/Atry( 8192, 8192, 0); # old=<x> , new=0 ; old > new
1N/Atry( 8192, 10030, 0); # old=<x><x , new=0 ; old > new
1N/Atry(14983, 9593, 0); # old=x><x> , new=0 ; old > new
1N/Atry( 8192, 16384, 0); # old=<x><x> , new=0 ; old > new
1N/Atry( 8192, 24576, 0); # old=<x><x><x>, new=0 ; old > new
1N/Atry(10489, 0, 0); # old=0 , new=0 ; old = new
1N/A
1N/A# (74-114)
1N/A# These tests all take place at the start of the file
1N/Atry( 0, 771, 1593); # old=<x , new=<x ; old < new
1N/Atry( 0, 4868, 4868); # old=<x , new=<x ; old = new
1N/Atry( 0, 147, 118); # old=<x , new=<x ; old > new
1N/Atry( 0, 8192, 8192); # old=<x> , new=<x ; old = new
1N/Atry( 0, 8192, 4574); # old=<x> , new=<x ; old > new
1N/Atry( 0, 11891, 1917); # old=<x><x , new=<x ; old > new
1N/Atry( 0, 16384, 5155); # old=<x><x> , new=<x ; old > new
1N/Atry( 0, 24576, 2953); # old=<x><x><x>, new=<x ; old > new
1N/Atry( 0, 0, 1317); # old=0 , new=<x ; old < new
1N/Atry( 0, 5609, 8192); # old=<x , new=<x> ; old < new
1N/Atry( 0, 8192, 8192); # old=<x> , new=<x> ; old = new
1N/Atry( 0, 11083, 8192); # old=<x><x , new=<x> ; old > new
1N/Atry( 0, 16384, 8192); # old=<x><x> , new=<x> ; old > new
1N/Atry( 0, 24576, 8192); # old=<x><x><x>, new=<x> ; old > new
1N/Atry( 0, 0, 8192); # old=0 , new=<x> ; old < new
1N/Atry( 0, 6265, 9991); # old=<x , new=<x><x ; old < new
1N/Atry( 0, 8192, 16119); # old=<x> , new=<x><x ; old < new
1N/Atry( 0, 10218, 11888); # old=<x><x , new=<x><x ; old < new
1N/Atry( 0, 14126, 14126); # old=<x><x , new=<x><x ; old = new
1N/Atry( 0, 12002, 9034); # old=<x><x , new=<x><x ; old > new
1N/Atry( 0, 16384, 13258); # old=<x><x> , new=<x><x ; old > new
1N/Atry( 0, 24576, 14367); # old=<x><x><x>, new=<x><x ; old > new
1N/Atry( 0, 0, 10881); # old=0 , new=<x><x ; old < new
1N/Atry( 0, 6448, 16384); # old=<x , new=<x><x> ; old < new
1N/Atry( 0, 8192, 16384); # old=<x> , new=<x><x> ; old < new
1N/Atry( 0, 15082, 16384); # old=<x><x , new=<x><x> ; old < new
1N/Atry( 0, 16384, 16384); # old=<x><x> , new=<x><x> ; old = new
1N/Atry( 0, 24576, 16384); # old=<x><x><x>, new=<x><x> ; old > new
1N/Atry( 0, 0, 16384); # old=0 , new=<x><x> ; old < new
1N/Atry( 0, 2421, 24576); # old=<x , new=<x><x><x>; old < new
1N/Atry( 0, 8192, 24576); # old=<x> , new=<x><x><x>; old < new
1N/Atry( 0, 11655, 24576); # old=<x><x , new=<x><x><x>; old < new
1N/Atry( 0, 16384, 24576); # old=<x><x> , new=<x><x><x>; old < new
1N/Atry( 0, 24576, 24576); # old=<x><x><x>, new=<x><x><x>; old = new
1N/Atry( 0, 0, 24576); # old=0 , new=<x><x><x>; old < new
1N/Atry( 0, 6530, 0); # old=<x , new=0 ; old > new
1N/Atry( 0, 8192, 0); # old=<x> , new=0 ; old > new
1N/Atry( 0, 14707, 0); # old=<x><x , new=0 ; old > new
1N/Atry( 0, 16384, 0); # old=<x><x> , new=0 ; old > new
1N/Atry( 0, 24576, 0); # old=<x><x><x>, new=0 ; old > new
1N/Atry( 0, 0, 0); # old=0 , new=0 ; old = new
1N/A
1N/A# (115-141)
1N/A# These tests all take place at the end of the file
1N/A$FLEN = 40960; # Force the file to be exactly 40960 bytes long
1N/Atry(32768, 8192, 8192); # old=<x> , new=<x ; old = new
1N/Atry(32768, 8192, 4026); # old=<x> , new=<x ; old > new
1N/Atry(24576, 16384, 1917); # old=<x><x> , new=<x ; old > new
1N/Atry(16384, 24576, 3818); # old=<x><x><x>, new=<x ; old > new
1N/Atry(40960, 0, 2779); # old=0 , new=<x ; old < new
1N/Atry(32768, 8192, 8192); # old=<x> , new=<x> ; old = new
1N/Atry(24576, 16384, 8192); # old=<x><x> , new=<x> ; old > new
1N/Atry(16384, 24576, 8192); # old=<x><x><x>, new=<x> ; old > new
1N/Atry(40960, 0, 8192); # old=0 , new=<x> ; old < new
1N/Atry(32768, 8192, 10724); # old=<x> , new=<x><x ; old < new
1N/Atry(24576, 16384, 12221); # old=<x><x> , new=<x><x ; old > new
1N/Atry(16384, 24576, 15030); # old=<x><x><x>, new=<x><x ; old > new
1N/Atry(40960, 0, 11752); # old=0 , new=<x><x ; old < new
1N/Atry(32768, 8192, 16384); # old=<x> , new=<x><x> ; old < new
1N/Atry(24576, 16384, 16384); # old=<x><x> , new=<x><x> ; old = new
1N/Atry(16384, 24576, 16384); # old=<x><x><x>, new=<x><x> ; old > new
1N/Atry(40960, 0, 16384); # old=0 , new=<x><x> ; old < new
1N/Atry(32768, 8192, 24576); # old=<x> , new=<x><x><x>; old < new
1N/Atry(24576, 16384, 24576); # old=<x><x> , new=<x><x><x>; old < new
1N/Atry(16384, 24576, 24576); # old=<x><x><x>, new=<x><x><x>; old = new
1N/Atry(40960, 0, 24576); # old=0 , new=<x><x><x>; old < new
1N/Atry(35973, 4987, 0); # old=x> , new=0 ; old > new
1N/Atry(32768, 8192, 0); # old=<x> , new=0 ; old > new
1N/Atry(29932, 11028, 0); # old=x><x> , new=0 ; old > new
1N/Atry(24576, 16384, 0); # old=<x><x> , new=0 ; old > new
1N/Atry(16384, 24576, 0); # old=<x><x><x>, new=0 ; old > new
1N/Atry(40960, 0, 0); # old=0 , new=0 ; old = new
1N/A
1N/A# (142-181)
1N/A# These tests all take place at the end of the file
1N/A$FLEN = 42000; # Force the file to be exactly 42000 bytes long
1N/Atry(41275, 725, 4059); # old=x , new=x ; old < new
1N/Atry(41683, 317, 317); # old=x , new=x ; old = new
1N/Atry(41225, 775, 405); # old=x , new=x ; old > new
1N/Atry(35709, 6291, 284); # old=x><x , new=x ; old > new
1N/Atry(42000, 0, 2434); # old=0 , new=x ; old < new
1N/Atry(40960, 1040, 1608); # old=<x , new=<x ; old < new
1N/Atry(40960, 1040, 1040); # old=<x , new=<x ; old = new
1N/Atry(40960, 1040, 378); # old=<x , new=<x ; old > new
1N/Atry(32768, 9232, 5604); # old=<x><x , new=<x ; old > new
1N/Atry(42000, 0, 6637); # old=0 , new=<x ; old < new
1N/Atry(41022, 978, 8130); # old=x , new=x> ; old < new
1N/Atry(39994, 2006, 966); # old=x><x , new=x> ; old > new
1N/Atry(42000, 0, 7152); # old=0 , new=x> ; old < new
1N/Atry(41613, 387, 10601); # old=x , new=x><x ; old < new
1N/Atry(38460, 3540, 3938); # old=x><x , new=x><x ; old < new
1N/Atry(36725, 5275, 5275); # old=x><x , new=x><x ; old = new
1N/Atry(37990, 4010, 3199); # old=x><x , new=x><x ; old > new
1N/Atry(42000, 0, 9189); # old=0 , new=x><x ; old < new
1N/Atry(40960, 1040, 8192); # old=<x , new=<x> ; old < new
1N/Atry(32768, 9232, 8192); # old=<x><x , new=<x> ; old > new
1N/Atry(42000, 0, 8192); # old=0 , new=<x> ; old < new
1N/Atry(40960, 1040, 11778); # old=<x , new=<x><x ; old < new
1N/Atry(32768, 9232, 13792); # old=<x><x , new=<x><x ; old < new
1N/Atry(32768, 9232, 9232); # old=<x><x , new=<x><x ; old = new
1N/Atry(32768, 9232, 8795); # old=<x><x , new=<x><x ; old > new
1N/Atry(42000, 0, 8578); # old=0 , new=<x><x ; old < new
1N/Atry(41531, 469, 15813); # old=x , new=x><x> ; old < new
1N/Atry(39618, 2382, 9534); # old=x><x , new=x><x> ; old < new
1N/Atry(42000, 0, 15344); # old=0 , new=x><x> ; old < new
1N/Atry(40960, 1040, 16384); # old=<x , new=<x><x> ; old < new
1N/Atry(32768, 9232, 16384); # old=<x><x , new=<x><x> ; old < new
1N/Atry(42000, 0, 16384); # old=0 , new=<x><x> ; old < new
1N/Atry(40960, 1040, 24576); # old=<x , new=<x><x><x>; old < new
1N/Atry(32768, 9232, 24576); # old=<x><x , new=<x><x><x>; old < new
1N/Atry(42000, 0, 24576); # old=0 , new=<x><x><x>; old < new
1N/Atry(41500, 500, 0); # old=x , new=0 ; old > new
1N/Atry(40960, 1040, 0); # old=<x , new=0 ; old > new
1N/Atry(35272, 6728, 0); # old=x><x , new=0 ; old > new
1N/Atry(32768, 9232, 0); # old=<x><x , new=0 ; old > new
1N/Atry(42000, 0, 0); # old=0 , new=0 ; old = new
1N/A
1N/Asub try {
1N/A my ($pos, $len, $newlen) = @_;
1N/A open F, "> $file" or die "Couldn't open file $file: $!";
1N/A binmode F;
1N/A
1N/A # The record has exactly 17 characters. This will help ensure that
1N/A # even if _twrite screws up, the data doesn't coincidentally
1N/A # look good because the remainder accidentally lines up.
1N/A my $d = substr("0123456789abcdef$:", -17);
1N/A my $recs = defined($FLEN) ?
1N/A int($FLEN/length($d))+1 : # enough to make up at least $FLEN
1N/A int(8192*5/length($d))+1; # at least 5 blocks' worth
1N/A my $oldfile = $d x $recs;
1N/A my $flen = defined($FLEN) ? $FLEN : $recs * 17;
1N/A substr($oldfile, $FLEN) = "" if defined $FLEN; # truncate
1N/A print F $oldfile;
1N/A close F;
1N/A
1N/A die "wrong length!" unless -s $file == $flen;
1N/A
1N/A my $newdata = "-" x $newlen;
1N/A my $expected = $oldfile;
1N/A substr($expected, $pos, $len) = $newdata;
1N/A
1N/A my $o = tie my @lines, 'Tie::File', $file or die $!;
1N/A $o->_twrite($newdata, $pos, $len);
1N/A undef $o; untie @lines;
1N/A
1N/A open F, "< $file" or die "Couldn't open file $file: $!";
1N/A binmode F;
1N/A my $actual;
1N/A { local $/;
1N/A $actual = <F>;
1N/A }
1N/A close F;
1N/A
1N/A my ($alen, $xlen) = (length $actual, length $expected);
1N/A unless ($alen == $xlen) {
1N/A print "# try(@_) expected file length $xlen, actual $alen!\n";
1N/A }
1N/A print $actual eq $expected ? "ok $N\n" : "not ok $N\n";
1N/A $N++;
1N/A}
1N/A
1N/A
1N/A
1N/Ause POSIX 'SEEK_SET';
1N/Asub check_contents {
1N/A my @c = @_;
1N/A my $x = join $:, @c, '';
1N/A local *FH = $o->{fh};
1N/A seek FH, 0, SEEK_SET;
1N/A# my $open = open FH, "< $file";
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 # now check FETCH:
1N/A my $good = 1;
1N/A my $msg;
1N/A for (0.. $#c) {
1N/A my $aa = $a[$_];
1N/A unless ($aa eq "$c[$_]$:") {
1N/A $msg = "expected <$c[$_]$:>, got <$aa>";
1N/A ctrlfix($msg);
1N/A $good = 0;
1N/A }
1N/A }
1N/A print $good ? "ok $N\n" : "not ok $N # $msg\n";
1N/A $N++;
1N/A
1N/A print $o->_check_integrity($file, $ENV{INTEGRITY})
1N/A ? "ok $N\n" : "not ok $N\n";
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