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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
|
#!/usr/bin/perl
#
# Check interactions of deferred writing
# with miscellaneous methods like DELETE, EXISTS,
# FETCHSIZE, STORESIZE, CLEAR, EXTEND
#
use POSIX 'SEEK_SET';
my $file = "tf$$.txt";
$: = Tie::File::_default_recsep();
my $data = "rec0$:rec1$:rec2$:";
my ($o, $n);
print "1..53\n";
my $N = 1;
use Tie::File;
print "ok $N\n"; $N++;
open F, '>', $file or die $!;
binmode F;
print F $data;
close F;
$o = tie @a, 'Tie::File', $file;
print $o ? "ok $N\n" : "not ok $N\n";
$N++;
# (3-6) EXISTS
if ($] >= 5.006) {
eval << 'TESTS';
$o->defer;
expect(not exists $a[4]);
$a[4] = "rec4";
expect(exists $a[4]);
check_contents($data); # nothing written yet
$o->discard;
TESTS
} else {
for (3..6) {
print "ok $_ \# skipped (no exists for arrays)\n";
$N++;
}
}
# (7-10) FETCHSIZE
$o->defer;
expect($#a, 2);
$a[4] = "rec4";
expect($#a, 4);
check_contents($data); # nothing written yet
$o->discard;
# (11-21) STORESIZE
$o->defer;
$#a = 4;
check_contents($data); # nothing written yet
expect($#a, 4);
$o->flush;
expect($#a, 4);
check_contents("$data$:$:"); # two extra empty records
$o->defer;
$a[4] = "rec4";
$#a = 2;
expect($a[4], undef);
check_contents($data); # written data was unwritten
$o->flush;
check_contents($data); # nothing left to write
# (22-28) CLEAR
$o->defer;
$a[9] = "rec9";
check_contents($data); # nothing written yet
@a = ();
check_contents(""); # this happens right away
expect($a[9], undef);
$o->flush;
check_contents(""); # nothing left to write
# (29-34) EXTEND
# Actually it's not real clear what these tests are for
# since EXTEND has no defined semantics
$o->defer;
@a = (0..3);
check_contents(""); # nothing happened yet
expect($a[3], "3");
expect($a[4], undef);
$o->flush;
check_contents("0$:1$:2$:3$:"); # file now 4 records long
# (35-53) DELETE
if ($] >= 5.006) {
eval << 'TESTS';
my $del;
$o->defer;
$del = delete $a[2];
check_contents("0$:1$:2$:3$:"); # nothing happened yet
expect($a[2], "");
expect($del, "2");
$del = delete $a[3]; # shortens file!
check_contents("0$:1$:2$:"); # deferred writes NOT flushed
expect($a[3], undef);
expect($a[2], "");
expect($del, "3");
$a[2] = "cookies";
$del = delete $a[2]; # shortens file!
expect($a[2], undef);
expect($del, 'cookies');
check_contents("0$:1$:");
$a[0] = "crackers";
$del = delete $a[0]; # file unchanged
expect($a[0], "");
expect($del, 'crackers');
check_contents("0$:1$:"); # no change yet
$o->flush;
check_contents("$:1$:"); # record 0 is NOT 'cookies';
TESTS
} else {
for (35..53) {
print "ok $_ \# skipped (no delete for arrays)\n";
$N++;
}
}
################################################################
sub check_caches {
my ($xcache, $xdefer) = @_;
# my $integrity = $o->_check_integrity($file, $ENV{INTEGRITY});
# print $integrity ? "ok $N\n" : "not ok $N\n";
# $N++;
my $good = 1;
$good &&= hash_equal($o->{cache}, $xcache, "true cache", "expected cache");
$good &&= hash_equal($o->{deferred}, $xdefer, "true defer", "expected defer");
print $good ? "ok $N\n" : "not ok $N\n";
$N++;
}
sub hash_equal {
my ($a, $b, $ha, $hb) = @_;
$ha = 'first hash' unless defined $ha;
$hb = 'second hash' unless defined $hb;
my $good = 1;
my %b_seen;
for my $k (keys %$a) {
if (! exists $b->{$k}) {
print ctrlfix("# Key $k is in $ha but not $hb"), "\n";
$good = 0;
} elsif ($b->{$k} ne $a->{$k}) {
print ctrlfix("# Key $k is <$a->{$k}> in $ha but <$b->{$k}> in $hb"), "\n";
$b_seen{$k} = 1;
$good = 0;
} else {
$b_seen{$k} = 1;
}
}
for my $k (keys %$b) {
unless ($b_seen{$k}) {
print ctrlfix("# Key $k is in $hb but not $ha"), "\n";
$good = 0;
}
}
$good;
}
sub check_contents {
my $x = shift;
my $integrity = $o->_check_integrity($file, $ENV{INTEGRITY});
print $integrity ? "ok $N\n" : "not ok $N\n";
$N++;
local *FH = $o->{fh};
seek FH, 0, SEEK_SET;
my $a;
{ local $/; $a = <FH> }
$a = "" unless defined $a;
if ($a eq $x) {
print "ok $N\n";
} else {
my $msg = ctrlfix("# expected <$x>, got <$a>");
print "not ok $N\n$msg\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 {
local $_ = shift;
s/\n/\\n/g;
s/\r/\\r/g;
$_;
}
END {
undef $o;
untie @a;
1 while unlink $file;
}
|