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 keys 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) Straight insert and removal FIFO test
55 $h->insert($_, $ar++);
63 ? "ok $N\n" : "not ok $N \# expected ($iota), got (@R)\n";
66 # (5) Remove from empty heap
68 print ! defined $n ? "ok $N\n" : "not ok $N \# expected UNDEF, got $n";
71 # (6) Interleaved insert and removal
76 $h->insert($Q, "b$Q");
84 print "@R" eq $iota ? "ok $N\n" : "not ok $N \# expected ($iota), got (@R)\n";
87 # (7) It should be empty now
88 print $h->is_empty ? "ok $N\n" : "not ok $N\n";
91 # (8) Insert and delete
94 $h->insert($_, "c$Q");
97 for (2, 4, 6, 8, 10) {
101 push @R, $n while defined ($n = $h->expire);
102 print "@R" eq "c1 c3 c5 c7 c9" ?
103 "ok $N\n" : "not ok $N \# expected (c1 c3 c5 c7 c9), got (@R)\n";
106 # (9) Interleaved insert and delete
111 $h->insert($Q, "d$Q");
115 $h->remove($QQ) if $QQ % 2 == 0;
119 push @R, $n while defined ($n = $h->expire);
120 print "@R" eq "d1 d3 d5 d7 d9" ?
121 "ok $N\n" : "not ok $N \# expected (d1 d3 d5 d7 d9), got (@R)\n";
128 $h->insert($_, "e$Q");
129 unless ($h->_check_integrity) {
130 die "Integrity failed after inserting ($_, e$Q)\n";
135 for (2, 4, 6, 8, 10) {
139 push @R, $n while defined ($n = $h->expire);
140 print "@R" eq "e1 e3 e5 e7 e9 e2 e4 e6 e8 e10" ?
142 "not ok $N \# expected (e1 e3 e5 e7 e9 e2 e4 e6 e8 e10), got (@R)\n";
148 $h->insert($_, "f$Q");
152 for (2, 4, 6, 4, 8) {
153 my $r = $h->lookup($_);
154 print $r eq "f$_" ? "ok $N\n" : "not ok $N \# expected f$_, got $r\n";
158 # (16) It shouldn't be empty
159 print ! $h->is_empty ? "ok $N\n" : "not ok $N\n";
162 # (17) Lookup should have promoted the looked-up records
164 push @R, $n while defined ($n = $h->expire);
165 print "@R" eq "f1 f3 f5 f7 f9 f10 f2 f6 f4 f8" ?
167 "not ok $N \# expected (f1 f3 f5 f7 f9 f10 f2 f6 f4 f8), got (@R)\n";
170 # (18-19) Typical 'rekey' operation
173 $h->insert($_, "g$Q");
176 $h->rekey([6,7,8,9,10], [8,9,10,11,12]);
177 my %x = qw(1 g1 2 g2 3 g3 4 g4 5 g5
178 8 g6 9 g7 10 g8 11 g9 12 g10);
181 for my $k (keys %x) {
182 my $v = $h->lookup($k);
183 $v = "UNDEF" unless defined $v;
184 unless ($v eq $x{$k}) {
185 print "# looked up $k, got $v, expected $x{$k}\n";
189 print $good ? "ok $N\n" : "not ok $N\n";
195 my $v = $h->lookup($k);
197 print "# looked up $k, got $v, should have been undef\n";
201 print $good ? "ok $N\n" : "not ok $N\n";
206 @R = sort { $a <=> $b } $h->keys;
207 print "@R" eq "1 2 3 4 5 8 9 10 11 12" ?
209 "not ok $N \# expected (1 2 3 4 5 8 9 10 11 12) got (@R)\n";
214 $h->update($_, "h$_");
217 for (sort { $a <=> $b } $h->keys) {
218 push @R, $h->lookup($_);
220 print "@R" eq "h1 h2 h3 h4 h5 h8 h9 h10 h11 h12" ?
222 "not ok $N \# expected (h1 h2 h3 h4 h5 h8 h9 h10 h11 h12) got (@R)\n";
228 print $B == 23 ? "ok $N\n" : "not ok $N \# expected 23, got $B\n";
230 $h->update('12', "yobgorgle");
232 print $B == 29 ? "ok $N\n" : "not ok $N \# expected 29, got $B\n";
237 print $h->is_empty ? "ok $N\n" : "not ok $N\n";
240 print ! defined $n ? "ok $N\n" : "not ok $N \# expected UNDEF, got $n";
243 # (26) very weak testing of DESTROY
245 # are we still alive?