1N/A#!/usr/bin/perl
1N/A#
1N/A# Check FETCHSIZE and SETSIZE functions
1N/A# PUSH POP SHIFT UNSHIFT
1N/A#
1N/A
1N/Ause POSIX 'SEEK_SET';
1N/A
1N/Amy $file = "tf$$.txt";
1N/Amy ($o, $n);
1N/A
1N/Aprint "1..16\n";
1N/A
1N/Amy $N = 1;
1N/Ause Tie::File;
1N/Aprint "ok $N\n"; $N++;
1N/A
1N/A# 2-3 FETCHSIZE 0-length file
1N/Aopen F, "> $file" or die $!;
1N/Abinmode F;
1N/Aclose F;
1N/A$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/A
1N/A$n = @a;
1N/Aprint $n == 0 ? "ok $N\n" : "not ok $N # $n, s/b 0\n";
1N/A$N++;
1N/A
1N/A# Reset everything
1N/Aundef $o;
1N/Auntie @a;
1N/A
1N/Amy $data = "rec0$:rec1$:rec2$:";
1N/Aopen F, "> $file" or die $!;
1N/Abinmode F;
1N/Aprint F $data;
1N/Aclose F;
1N/A
1N/A$o = tie @a, 'Tie::File', $file;
1N/Aprint $o ? "ok $N\n" : "not ok $N\n";
1N/A$N++;
1N/A
1N/A# 4-5 FETCHSIZE positive-length file
1N/A$n = @a;
1N/Aprint $n == 3 ? "ok $N\n" : "not ok $N # $n, s/b 0\n";
1N/A$N++;
1N/A
1N/A# STORESIZE
1N/A# (6-7) Make it longer:
1N/Apopulate();
1N/A$#a = 4;
1N/Acheck_contents("$data$:$:");
1N/A
1N/A# (8-9) Make it longer again:
1N/Apopulate();
1N/A$#a = 6;
1N/Acheck_contents("$data$:$:$:$:");
1N/A
1N/A# (10-11) Make it shorter:
1N/Apopulate();
1N/A$#a = 4;
1N/Acheck_contents("$data$:$:");
1N/A
1N/A# (12-13) Make it shorter again:
1N/Apopulate();
1N/A$#a = 2;
1N/Acheck_contents($data);
1N/A
1N/A# (14-15) Get rid of it completely:
1N/Apopulate();
1N/A$#a = -1;
1N/Acheck_contents('');
1N/A
1N/A# (16) 20020324 I have an idea that shortening the array will not
1N/A# expunge a cached record at the end if one is present.
1N/A$o->defer;
1N/A$a[3] = "record";
1N/Amy $r = $a[3];
1N/A$#a = -1;
1N/A$r = $a[3];
1N/Aprint (! defined $r ? "ok $N\n" : "not ok $N \# was <$r>; should be UNDEF\n");
1N/A# Turns out not to be the case---STORESIZE explicitly removes them later
1N/A# 20020326 Well, but happily, this test did fail today.
1N/A
1N/A# In the past, there was a bug in STORESIZE that it didn't correctly
1N/A# remove deleted records from the cache. This wasn't detected
1N/A# because these tests were all done with an empty cache. populate()
1N/A# will ensure that the cache is fully populated.
1N/Asub populate {
1N/A my $z;
1N/A $z = $a[$_] for 0 .. $#a;
1N/A}
1N/A
1N/Asub check_contents {
1N/A my $x = shift;
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($a, $x);
1N/A print "not ok $N\n# expected <$x>, got <$a>\n";
1N/A }
1N/A $N++;
1N/A my $integrity = $o->_check_integrity($file, $ENV{INTEGRITY});
1N/A print $integrity ? "ok $N\n" : "not ok $N \# integrity\n";
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