3 # Unit tests of _upcopy function
5 # _upcopy($self, $source, $dest, $len)
7 # Take a block of data of leength $len at $source and copy it
8 # to $dest, which must be <= $source but which need not be <= $source - $len
9 # (That is, this will only copy a block to a position earlier in the file,
10 # but the source and destination regions may overlap.)
13 my $file = "tf$$.txt";
19 print "ok $N\n"; $N++;
21 $: = Tie::File::_default_recsep();
23 my @subtests = qw(x <x x> x><x <x> <x><x x><x> <x><x> <x><x><x> 0);
25 $FLEN = 40970; # 2410 records of 17 chars each
27 # (2-7) Trivial non-moves at start of file
35 # (8-13) Trivial non-moves in middle of file
43 # (14) Trivial non-move at end of file
46 # (15-17) Trivial non-move of tail of file
49 try($FLEN, $FLEN, undef);
51 # (18-24) Moves to start of file
60 # (25-31) Moves in middle of file
69 # (32-43) Moves from end of file
71 try($FLEN-10, 10000, 10);
72 try($FLEN-100, 10000, 100);
73 try($FLEN-1000, 200, 1000);
74 try($FLEN-10000, 200, 10000);
75 try($FLEN-20000, 200, 20000);
76 try($FLEN, 10000, undef);
77 try($FLEN-10, 10000, undef);
78 try($FLEN-100, 10000, undef);
79 try($FLEN-1000, 200, undef);
80 try($FLEN-10000, 200, undef);
81 try($FLEN-20000, 200, undef);
85 # (44-55) Moves from end of file when file ends on a block boundary
87 try($FLEN-10, 10000, 10);
88 try($FLEN-100, 10000, 100);
89 try($FLEN-1000, 200, 1000);
90 try($FLEN-10000, 200, 10000);
91 try($FLEN-20000, 200, 20000);
92 try($FLEN, 10000, undef);
93 try($FLEN-10, 10000, undef);
94 try($FLEN-100, 10000, undef);
95 try($FLEN-1000, 200, undef);
96 try($FLEN-10000, 200, undef);
97 try($FLEN-20000, 200, undef);
100 my ($src, $dst, $len) = @_;
101 open F, "> $file" or die "Couldn't open file $file: $!";
104 # The record has exactly 17 characters. This will help ensure that
105 # even if _upcopy screws up, the data doesn't coincidentally
106 # look good because the remainder accidentally lines up.
107 my $d = substr("0123456789abcdef$:", -17);
108 my $recs = defined($FLEN) ?
109 int($FLEN/length($d))+1 : # enough to make up at least $FLEN
110 int(8192*5/length($d))+1; # at least 5 blocks' worth
111 my $oldfile = $d x $recs;
112 my $flen = defined($FLEN) ? $FLEN : $recs * 17;
113 substr($oldfile, $FLEN) = "" if defined $FLEN; # truncate
117 die "wrong length!" unless -s $file == $flen;
119 # If len is specified, use that. If it's undef,
120 # then behave *as if* we had specified the whole rest of the file
121 my $expected = $oldfile;
123 substr($expected, $dst, $len) = substr($expected, $src, $len);
125 substr($expected, $dst) = substr($expected, $src);
128 my $o = tie my @lines, 'Tie::File', $file or die $!;
129 local $SIG{ALRM} = sub { die "Alarm clock" };
130 my $a_retval = eval { alarm(5) unless $^P; $o->_upcopy($src, $dst, $len) };
132 undef $o; untie @lines; alarm(0);
134 if ($err =~ /^Alarm clock/) {
136 print "not ok $N\n"; $N++;
144 open F, "< $file" or die "Couldn't open file $file: $!";
152 my ($alen, $xlen) = (length $actual, length $expected);
153 unless ($alen == $xlen) {
154 print "# try(@_) expected file length $xlen, actual $alen!\n";
156 print $actual eq $expected ? "ok $N\n" : "not ok $N\n";
162 use POSIX 'SEEK_SET';
165 my $x = join $:, @c, '';
166 local *FH = $o->{fh};
167 seek FH, 0, SEEK_SET;
168 # my $open = open FH, "< $file";
170 { local $/; $a = <FH> }
171 $a = "" unless defined $a;
176 print "not ok $N\n# expected <$x>, got <$a>\n";
185 unless ($aa eq "$c[$_]$:") {
186 $msg = "expected <$c[$_]$:>, got <$aa>";
191 print $good ? "ok $N\n" : "not ok $N # $msg\n";
194 print $o->_check_integrity($file, $ENV{INTEGRITY})
195 ? "ok $N\n" : "not ok $N\n";
209 1 while unlink $file;