3 # Unit tests for abstract cache implementation
5 # Test the following methods:
13 # * rekey(okeys,nkeys)
19 # 20020327 You somehow managed to miss:
20 # * reduce_size_to(bytes)
23 # print "1..0\n"; exit;
26 my ($N, @R, $Q, $ar) = (1);
32 my $h = Tie::File::Cache->new(10000) or die;
36 # (3) Are all the methods there?
39 for my $meth (qw(new is_empty empty lookup remove
40 insert update rekey expire ckeys bytes
41 set_limit adj_limit flush reduce_size_to
42 _produce _produce_lru )) {
43 unless ($h->can($meth)) {
44 print STDERR "# Method '$meth' is missing.\n";
48 print $good ? "ok $N\n" : "not ok $N\n";
52 # (4-5) Straight insert and removal FIFO test
55 $h->insert($_, $ar++);
63 ? "ok $N\n" : "not ok $N \# expected ($iota), got (@R)\n";
67 # (6-7) Remove from empty heap
69 print ! defined $n ? "ok $N\n" : "not ok $N \# expected UNDEF, got $n";
73 # (8-9) Interleaved insert and removal
78 $h->insert($Q, "b$Q");
86 print "@R" eq $iota ? "ok $N\n" : "not ok $N \# expected ($iota), got (@R)\n";
90 # (10) It should be empty now
91 print $h->is_empty ? "ok $N\n" : "not ok $N\n";
94 # (11-12) Insert and delete
97 $h->insert($_, "c$Q");
100 for (2, 4, 6, 8, 10) {
104 push @R, $n while defined ($n = $h->expire);
105 print "@R" eq "c1 c3 c5 c7 c9" ?
106 "ok $N\n" : "not ok $N \# expected (c1 c3 c5 c7 c9), got (@R)\n";
110 # (13-14) Interleaved insert and delete
115 $h->insert($Q, "d$Q");
119 $h->remove($QQ) if $QQ % 2 == 0;
123 push @R, $n while defined ($n = $h->expire);
124 print "@R" eq "d1 d3 d5 d7 d9" ?
125 "ok $N\n" : "not ok $N \# expected (d1 d3 d5 d7 d9), got (@R)\n";
133 $h->insert($_, "e$Q");
134 unless ($h->_check_integrity) {
135 die "Integrity failed after inserting ($_, e$Q)\n";
140 for (2, 4, 6, 8, 10) {
144 push @R, $n while defined ($n = $h->expire);
145 print "@R" eq "e1 e3 e5 e7 e9 e2 e4 e6 e8 e10" ?
147 "not ok $N \# expected (e1 e3 e5 e7 e9 e2 e4 e6 e8 e10), got (@R)\n";
154 $h->insert($_, "f$Q");
158 for (2, 4, 6, 4, 8) {
159 my $r = $h->lookup($_);
160 print $r eq "f$_" ? "ok $N\n" : "not ok $N \# expected f$_, got $r\n";
165 # (23) It shouldn't be empty
166 print ! $h->is_empty ? "ok $N\n" : "not ok $N\n";
169 # (24-25) Lookup should have promoted the looked-up records
171 push @R, $n while defined ($n = $h->expire);
172 print "@R" eq "f1 f3 f5 f7 f9 f10 f2 f6 f4 f8" ?
174 "not ok $N \# expected (f1 f3 f5 f7 f9 f10 f2 f6 f4 f8), got (@R)\n";
178 # (26-29) Typical 'rekey' operation
181 $h->insert($_, "g$Q");
184 $h->rekey([6,7,8,9,10], [8,9,10,11,12]);
185 my %x = qw(1 g1 2 g2 3 g3 4 g4 5 g5
186 8 g6 9 g7 10 g8 11 g9 12 g10);
189 for my $k (keys %x) {
190 my $v = $h->lookup($k);
191 $v = "UNDEF" unless defined $v;
192 unless ($v eq $x{$k}) {
193 print "# looked up $k, got $v, expected $x{$k}\n";
197 print $good ? "ok $N\n" : "not ok $N\n";
204 my $v = $h->lookup($k);
206 print "# looked up $k, got $v, should have been undef\n";
210 print $good ? "ok $N\n" : "not ok $N\n";
216 @R = sort { $a <=> $b } $h->ckeys;
217 print "@R" eq "1 2 3 4 5 8 9 10 11 12" ?
219 "not ok $N \# expected (1 2 3 4 5 8 9 10 11 12) got (@R)\n";
225 $h->update($_, "h$_");
228 for (sort { $a <=> $b } $h->ckeys) {
229 push @R, $h->lookup($_);
231 print "@R" eq "h1 h2 h3 h4 h5 h8 h9 h10 h11 h12" ?
233 "not ok $N \# expected (h1 h2 h3 h4 h5 h8 h9 h10 h11 h12) got (@R)\n";
240 print $B == 23 ? "ok $N\n" : "not ok $N \# expected 23, got $B\n";
243 $h->update('12', "yobgorgle");
245 print $B == 29 ? "ok $N\n" : "not ok $N \# expected 29, got $B\n";
251 print $h->is_empty ? "ok $N\n" : "not ok $N\n";
255 print ! defined $n ? "ok $N\n" : "not ok $N \# expected UNDEF, got $n";
259 # (42) very weak testing of DESTROY
261 # are we still alive?
267 print $h->_check_integrity ? "ok $N\n" : "not ok $N\n";