1N/A#!/usr/bin/perl
1N/A
1N/A$| = 1;
1N/Amy $file = "tf$$.txt";
1N/A1 while unlink $file;
1N/A
1N/Aprint "1..75\n";
1N/A
1N/Amy $N = 1;
1N/Ause Tie::File;
1N/Aprint "ok $N\n"; $N++;
1N/A
1N/Amy $o = tie @a, 'Tie::File', $file, autochomp => 0, autodefer => 0;
1N/Aprint $o ? "ok $N\n" : "not ok $N\n";
1N/A$N++;
1N/A
1N/A$: = $o->{recsep};
1N/A
1N/A# 3-5 create
1N/A$a[0] = 'rec0';
1N/Acheck_contents("rec0");
1N/A
1N/A# 6-11 append
1N/A$a[1] = 'rec1';
1N/Acheck_contents("rec0", "rec1");
1N/A$a[2] = 'rec2';
1N/Acheck_contents("rec0", "rec1", "rec2");
1N/A
1N/A# 12-20 same-length alterations
1N/A$a[0] = 'new0';
1N/Acheck_contents("new0", "rec1", "rec2");
1N/A$a[1] = 'new1';
1N/Acheck_contents("new0", "new1", "rec2");
1N/A$a[2] = 'new2';
1N/Acheck_contents("new0", "new1", "new2");
1N/A
1N/A# 21-35 lengthening alterations
1N/A$a[0] = 'long0';
1N/Acheck_contents("long0", "new1", "new2");
1N/A$a[1] = 'long1';
1N/Acheck_contents("long0", "long1", "new2");
1N/A$a[2] = 'long2';
1N/Acheck_contents("long0", "long1", "long2");
1N/A$a[1] = 'longer1';
1N/Acheck_contents("long0", "longer1", "long2");
1N/A$a[0] = 'longer0';
1N/Acheck_contents("longer0", "longer1", "long2");
1N/A
1N/A# 36-50 shortening alterations, including truncation
1N/A$a[0] = 'short0';
1N/Acheck_contents("short0", "longer1", "long2");
1N/A$a[1] = 'short1';
1N/Acheck_contents("short0", "short1", "long2");
1N/A$a[2] = 'short2';
1N/Acheck_contents("short0", "short1", "short2");
1N/A$a[1] = 'sh1';
1N/Acheck_contents("short0", "sh1", "short2");
1N/A$a[0] = 'sh0';
1N/Acheck_contents("sh0", "sh1", "short2");
1N/A
1N/A# (51-56) file with holes
1N/A$a[4] = 'rec4';
1N/Acheck_contents("sh0", "sh1", "short2", "", "rec4");
1N/A$a[3] = 'rec3';
1N/Acheck_contents("sh0", "sh1", "short2", "rec3", "rec4");
1N/A
1N/A# (57-59) zero out file
1N/A@a = ();
1N/Acheck_contents();
1N/A
1N/A# (60-62) insert into the middle of an empty file
1N/A$a[3] = "rec3";
1N/Acheck_contents("", "", "", "rec3");
1N/A
1N/A# (63-68) 20020326 You thought there would be a bug in STORE where if
1N/A# a cached record was false, STORE wouldn't see it at all. But you
1N/A# forgot that records always come back from the cache with the record
1N/A# separator attached, so they are unlikely to be false. The only
1N/A# really weird case is when the cached record is empty and the record
1N/A# separator is "0". Test that in 09_gen_rs.t.
1N/A$a[1] = "0";
1N/Acheck_contents("", "0", "", "rec3");
1N/A$a[1] = "whoops";
1N/Acheck_contents("", "whoops", "", "rec3");
1N/A
1N/A# (69-72) 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 $a[0] = undef;
1N/A print $good ? "ok $N\n" : "not ok $N # $warn\n";
1N/A $N++; $good = 1;
1N/A print defined($a[0]) ? "ok $N\n" : "not ok $N\n";
1N/A $N++; $good = 1;
1N/A $a[3] = '3';
1N/A print defined($a[1]) ? "ok $N\n" : "not ok $N\n";
1N/A $N++; $good = 1;
1N/A undef $a[3];
1N/A print $good ? "ok $N\n" : "not ok $N # $warn\n";
1N/A $N++; $good = 1;
1N/A}
1N/A
1N/A# (73-75) What if the user has tampered with $\ ?
1N/A{ { local $\ = "stop messing with the funny variables!";
1N/A @a = (0..2);
1N/A }
1N/A check_contents(0..2);
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