1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175
|
#!/usr/bin/perl
my $file = "tf$$.txt";
$: = Tie::File::_default_recsep();
print "1..71\n";
my $N = 1;
use Tie::File;
print "ok $N\n"; $N++;
my $o = tie @a, 'Tie::File', $file, autochomp => 1, autodefer => 0;
print $o ? "ok $N\n" : "not ok $N\n";
$N++;
# 3-5 create
$a[0] = 'rec0';
check_contents("rec0");
# 6-11 append
$a[1] = 'rec1';
check_contents("rec0", "rec1");
$a[2] = 'rec2';
check_contents("rec0", "rec1", "rec2");
# 12-20 same-length alterations
$a[0] = 'new0';
check_contents("new0", "rec1", "rec2");
$a[1] = 'new1';
check_contents("new0", "new1", "rec2");
$a[2] = 'new2';
check_contents("new0", "new1", "new2");
# 21-35 lengthening alterations
$a[0] = 'long0';
check_contents("long0", "new1", "new2");
$a[1] = 'long1';
check_contents("long0", "long1", "new2");
$a[2] = 'long2';
check_contents("long0", "long1", "long2");
$a[1] = 'longer1';
check_contents("long0", "longer1", "long2");
$a[0] = 'longer0';
check_contents("longer0", "longer1", "long2");
# 36-50 shortening alterations, including truncation
$a[0] = 'short0';
check_contents("short0", "longer1", "long2");
$a[1] = 'short1';
check_contents("short0", "short1", "long2");
$a[2] = 'short2';
check_contents("short0", "short1", "short2");
$a[1] = 'sh1';
check_contents("short0", "sh1", "short2");
$a[0] = 'sh0';
check_contents("sh0", "sh1", "short2");
# (51-56) file with holes
$a[4] = 'rec4';
check_contents("sh0", "sh1", "short2", "", "rec4");
$a[3] = 'rec3';
check_contents("sh0", "sh1", "short2", "rec3", "rec4");
# (57-59) zero out file
@a = ();
check_contents();
# (60-62) insert into the middle of an empty file
$a[3] = "rec3";
check_contents("", "", "", "rec3");
# (63-68) Test the ->autochomp() method
@a = qw(Gold Frankincense Myrrh);
my $ac;
$ac = $o->autochomp();
expect($ac);
# See if that accidentally changed it
$ac = $o->autochomp();
expect($ac);
# Now clear it
$ac = $o->autochomp(0);
expect($ac);
expect(join("-", @a), "Gold$:-Frankincense$:-Myrrh$:");
# Now set it again
$ac = $o->autochomp(1);
expect(!$ac);
expect(join("-", @a), "Gold-Frankincense-Myrrh");
# (69) Does 'splice' work correctly with autochomp?
my @sr;
@sr = splice @a, 0, 2;
expect(join("-", @sr), "Gold-Frankincense");
# (70-71) Didn't you forget that fetch may return an unchomped cached record?
$a1 = $a[0]; # populate cache
$a2 = $a[0];
expect($a1, "Myrrh");
expect($a2, "Myrrh");
# Actually no, you didn't---_fetch might return such a record, but
# the chomping is done by FETCH.
use POSIX 'SEEK_SET';
sub check_contents {
my @c = @_;
my $x = join $:, @c, '';
local *FH = $o->{fh};
seek FH, 0, SEEK_SET;
# my $open = open FH, "< $file";
my $a;
{ local $/; $a = <FH> }
$a = "" unless defined $a;
if ($a eq $x) {
print "ok $N\n";
} else {
ctrlfix($a, $x);
print "not ok $N\n# expected <$x>, got <$a>\n";
}
$N++;
# now check FETCH:
my $good = 1;
my $msg;
for (0.. $#c) {
my $aa = $a[$_];
unless ($aa eq $c[$_]) {
$msg = "expected <$c[$_]>, got <$aa>";
ctrlfix($msg);
$good = 0;
}
}
print $good ? "ok $N\n" : "not ok $N # $msg\n";
$N++;
print $o->_check_integrity($file, $ENV{INTEGRITY})
? "ok $N\n" : "not ok $N\n";
$N++;
}
sub expect {
if (@_ == 1) {
print $_[0] ? "ok $N\n" : "not ok $N\n";
} elsif (@_ == 2) {
my ($a, $x) = @_;
if (! defined($a) && ! defined($x)) { print "ok $N\n" }
elsif ( defined($a) && ! defined($x)) {
ctrlfix(my $msg = "expected UNDEF, got <$a>");
print "not ok $N \# $msg\n";
}
elsif (! defined($a) && defined($x)) {
ctrlfix(my $msg = "expected <$x>, got UNDEF");
print "not ok $N \# $msg\n";
} elsif ($a eq $x) { print "ok $N\n" }
else {
ctrlfix(my $msg = "expected <$x>, got <$a>");
print "not ok $N \# $msg\n";
}
} else {
die "expect() got ", scalar(@_), " args, should have been 1 or 2";
}
$N++;
}
sub ctrlfix {
for (@_) {
s/\n/\\n/g;
s/\r/\\r/g;
}
}
END {
undef $o;
untie @a;
1 while unlink $file;
}
|