Commit | Line | Data |
a0cb3900 |
1 | #!/usr/bin/perl |
2 | |
484fdf61 |
3 | use lib qw(. ..); |
a0cb3900 |
4 | use Memoize 0.52 qw(memoize unmemoize); |
5 | use Fcntl; |
6b79e901 |
6 | eval {require Memoize::AnyDBM_File}; |
7 | if ($@) { |
8 | print "1..0\n"; |
9 | exit 0; |
10 | } |
11 | |
12 | |
a0cb3900 |
13 | |
14 | print "1..4\n"; |
15 | |
16 | sub i { |
17 | $_[0]; |
18 | } |
19 | |
20 | $ARG = 'Keith Bostic is a pinhead'; |
21 | |
22 | sub c119 { 119 } |
23 | sub c7 { 7 } |
24 | sub c43 { 43 } |
25 | sub c23 { 23 } |
26 | sub c5 { 5 } |
27 | |
28 | sub n { |
29 | $_[0]+1; |
30 | } |
31 | |
2359510d |
32 | $file = "md$$"; |
a0cb3900 |
33 | @files = ($file, "$file.db", "$file.dir", "$file.pag"); |
899dc88a |
34 | 1 while unlink @files; |
a0cb3900 |
35 | |
36 | |
37 | tryout('Memoize::AnyDBM_File', $file, 1); # Test 1..4 |
38 | # tryout('DB_File', $file, 1); # Test 1..4 |
899dc88a |
39 | 1 while unlink $file, "$file.dir", "$file.pag"; |
a0cb3900 |
40 | |
41 | sub tryout { |
42 | my ($tiepack, $file, $testno) = @_; |
43 | |
899dc88a |
44 | tie my %cache => $tiepack, $file, O_RDWR | O_CREAT, 0666 |
45 | or die $!; |
a0cb3900 |
46 | |
47 | memoize 'c5', |
899dc88a |
48 | SCALAR_CACHE => [HASH => \%cache], |
49 | LIST_CACHE => 'FAULT' |
a0cb3900 |
50 | ; |
51 | |
52 | my $t1 = c5($ARG); |
53 | my $t2 = c5($ARG); |
54 | print (($t1 == 5) ? "ok $testno\n" : "not ok $testno\n"); |
55 | $testno++; |
56 | print (($t2 == 5) ? "ok $testno\n" : "not ok $testno\n"); |
57 | unmemoize 'c5'; |
58 | |
59 | # Now something tricky---we'll memoize c23 with the wrong table that |
60 | # has the 5 already cached. |
61 | memoize 'c23', |
899dc88a |
62 | SCALAR_CACHE => ['HASH', \%cache], |
a0cb3900 |
63 | LIST_CACHE => 'FAULT' |
64 | ; |
65 | |
66 | my $t3 = c23($ARG); |
67 | my $t4 = c23($ARG); |
68 | $testno++; |
69 | print (($t3 == 5) ? "ok $testno\n" : "not ok $testno # Result $t3\n"); |
70 | $testno++; |
71 | print (($t4 == 5) ? "ok $testno\n" : "not ok $testno # Result $t4\n"); |
72 | unmemoize 'c23'; |
73 | } |
74 | |
75 | { |
76 | my @present = grep -e, @files; |
77 | if (@present && (@failed = grep { not unlink } @present)) { |
78 | warn "Can't unlink @failed! ($!)"; |
79 | } |
80 | } |