1#!/usr/bin/perl 2 3use strict; 4use warnings; 5 6# 7# Tests for various caching errors 8# 9 10my $file = "tf20-$$.txt"; 11$: = Tie::File::_default_recsep(); 12my $data = join $:, "record0" .. "record9", ""; 13my $V = $ENV{INTEGRITY}; # Verbose integrity checking? 14 15print "1..111\n"; 16 17my $N = 1; 18use Tie::File; 19print "ok $N\n"; $N++; 20 21open F, '>', $file or die $!; 22binmode F; 23print F $data; 24close F; 25 26# Limit cache size to 30 bytes 27my $MAX = 30; 28# -- that's enough space for 3 records, but not 4, on both \n and \r\n systems 29my @a; 30my $o = tie @a, 'Tie::File', $file, memory => $MAX, autodefer => 0; 31print $o ? "ok $N\n" : "not ok $N\n"; 32$N++; 33 34# (3-5) Let's see if data was properly expired from the cache 35my @z = @a; # force cache to contain all ten records 36# It should now contain only the *last* three records, 7, 8, and 9 37{ 38 my $x = "7 8 9"; 39 my $a = join " ", sort $o->{cache}->ckeys; 40 if ($a eq $x) { print "ok $N\n" } 41 else { print "not ok $N # cache keys were <$a>; expected <$x>\n" } 42 $N++; 43} 44check(); 45 46# Here we redo *all* the splice tests, with populate() 47# calls before each one, to make sure that splice() does not botch the cache. 48 49# (6-25) splicing at the beginning 50splice(@a, 0, 0, "rec4"); 51check(); 52splice(@a, 0, 1, "rec5"); # same length 53check(); 54splice(@a, 0, 1, "record5"); # longer 55check(); 56splice(@a, 0, 1, "r5"); # shorter 57check(); 58splice(@a, 0, 1); # removal 59check(); 60splice(@a, 0, 0); # no-op 61check(); 62 63splice(@a, 0, 0, 'r7', 'rec8'); # insert more than one 64check(); 65splice(@a, 0, 2, 'rec7', 'record8', 'rec9'); # insert more than delete 66check(); 67splice(@a, 0, 3, 'record9', 'rec10'); # delete more than insert 68check(); 69splice(@a, 0, 2); # delete more than one 70check(); 71 72 73# (26-45) splicing in the middle 74splice(@a, 1, 0, "rec4"); 75check(); 76splice(@a, 1, 1, "rec5"); # same length 77check(); 78splice(@a, 1, 1, "record5"); # longer 79check(); 80splice(@a, 1, 1, "r5"); # shorter 81check(); 82splice(@a, 1, 1); # removal 83check(); 84splice(@a, 1, 0); # no-op 85check(); 86 87splice(@a, 1, 0, 'r7', 'rec8'); # insert more than one 88check(); 89splice(@a, 1, 2, 'rec7', 'record8', 'rec9'); # insert more than delete 90check(); 91splice(@a, 1, 3, 'record9', 'rec10'); # delete more than insert 92check(); 93splice(@a, 1, 2); # delete more than one 94check(); 95 96# (46-65) splicing at the end 97splice(@a, 3, 0, "rec4"); 98check(); 99splice(@a, 3, 1, "rec5"); # same length 100check(); 101splice(@a, 3, 1, "record5"); # longer 102check(); 103splice(@a, 3, 1, "r5"); # shorter 104check(); 105splice(@a, 3, 1); # removal 106check(); 107splice(@a, 3, 0); # no-op 108check(); 109 110splice(@a, 3, 0, 'r7', 'rec8'); # insert more than one 111check(); 112splice(@a, 3, 2, 'rec7', 'record8', 'rec9'); # insert more than delete 113check(); 114splice(@a, 3, 3, 'record9', 'rec10'); # delete more than insert 115check(); 116splice(@a, 3, 2); # delete more than one 117check(); 118 119# (66-85) splicing with negative subscript 120splice(@a, -1, 0, "rec4"); 121check(); 122splice(@a, -1, 1, "rec5"); # same length 123check(); 124splice(@a, -1, 1, "record5"); # longer 125check(); 126splice(@a, -1, 1, "r5"); # shorter 127check(); 128splice(@a, -1, 1); # removal 129check(); 130splice(@a, -1, 0); # no-op 131check(); 132 133splice(@a, -1, 0, 'r7', 'rec8'); # insert more than one 134check(); 135splice(@a, -1, 2, 'rec7', 'record8', 'rec9'); # insert more than delete 136check(); 137splice(@a, -3, 3, 'record9', 'rec10'); # delete more than insert 138check(); 139splice(@a, -4, 3); # delete more than one 140check(); 141 142# (86-87) scrub it all out 143splice(@a, 0, 3); 144check(); 145 146# (88-89) put some back in 147splice(@a, 0, 0, "rec0", "rec1"); 148check(); 149 150# (90-91) what if we remove too many records? 151splice(@a, 0, 17); 152check(); 153 154# (92-95) In the past, splicing past the end was not correctly detected 155# (1.14) 156splice(@a, 89, 3); 157check(); 158splice(@a, @a, 3); 159check(); 160 161# (96-99) Also we did not emulate splice's freaky behavior when inserting 162# past the end of the array (1.14) 163splice(@a, 89, 0, "I", "like", "pie"); 164check(); 165splice(@a, 89, 0, "pie pie pie"); 166check(); 167 168# (100-105) Test default arguments 169splice @a, 0, 0, (0..11); 170check(); 171splice @a, 4; 172check(); 173splice @a; 174check(); 175 176# (106-111) One last set of tests. I don't know what state the cache 177# is in now. But if I read any three records, those three records are 178# what should be in the cache, and nothing else. 179@a = "record0" .. "record9"; 180check(); # In 0.18 #107 fails here--STORE was not flushing the cache when 181 # replacing an old cached record with a longer one 182for (5, 6, 1) { my $z = $a[$_] } 183{ 184 my $x = "5 6 1"; 185 my $a = join " ", $o->{cache}->_produce_lru; 186 if ($a eq $x) { print "ok $N\n" } 187 else { print "not ok $N # LRU was <$a>; expected <$x>\n" } 188 $N++; 189 $x = "1 5 6"; 190 $a = join " ", sort $o->{cache}->ckeys; 191 if ($a eq $x) { print "ok $N\n" } 192 else { print "not ok $N # cache keys were <$a>; expected <$x>\n" } 193 $N++; 194} 195check(); 196 197 198sub init_file { 199 my $data = shift; 200 open F, '>', $file or die $!; 201 binmode F; 202 print F $data; 203 close F; 204} 205 206sub check { 207 my $integrity = $o->_check_integrity($file, $ENV{INTEGRITY}); 208 print $integrity ? "ok $N\n" : "not ok $N\n"; 209 $N++; 210 211 my $b = $o->{cache}->bytes; 212 print $b <= $MAX 213 ? "ok $N\n" 214 : "not ok $N # $b bytes cached, should be <= $MAX\n"; 215 $N++; 216} 217 218 219sub ctrlfix { 220 for (@_) { 221 s/\n/\\n/g; 222 s/\r/\\r/g; 223 } 224} 225 226END { 227 undef $o; 228 untie @a; 229 1 while unlink $file; 230} 231 232 233 234