Commit | Line | Data |
a0cb3900 |
1 | #!/usr/bin/perl |
2 | # -*- mode: perl; perl-indent-level: 2 -*- |
3 | |
484fdf61 |
4 | use lib qw(. ..); |
a0cb3900 |
5 | use Memoize 0.45 qw(memoize unmemoize); |
a0cb3900 |
6 | # $Memoize::Storable::Verbose = 0; |
7 | |
06916929 |
8 | eval {require Memoize::Storable}; |
899dc88a |
9 | if ($@) { |
10 | print "1..0\n"; |
11 | exit 0; |
12 | } |
13 | |
a0cb3900 |
14 | sub i { |
15 | $_[0]; |
16 | } |
17 | |
18 | sub c119 { 119 } |
19 | sub c7 { 7 } |
20 | sub c43 { 43 } |
21 | sub c23 { 23 } |
22 | sub c5 { 5 } |
23 | |
24 | sub n { |
25 | $_[0]+1; |
26 | } |
27 | |
28 | eval {require Storable}; |
29 | if ($@) { |
30 | print "1..0\n"; |
31 | exit 0; |
32 | } |
33 | |
34 | print "1..4\n"; |
35 | |
36 | |
37 | if (eval {require File::Spec::Functions}) { |
38 | File::Spec::Functions->import(); |
39 | } else { |
40 | *catfile = sub { join '/', @_ }; |
41 | } |
42 | $tmpdir = $ENV{TMP} || $ENV{TMPDIR} || '/tmp'; |
43 | $file = catfile($tmpdir, "storable$$"); |
899dc88a |
44 | 1 while unlink $file; |
a0cb3900 |
45 | tryout('Memoize::Storable', $file, 1); # Test 1..4 |
899dc88a |
46 | 1 while unlink $file; |
a0cb3900 |
47 | |
48 | sub tryout { |
49 | my ($tiepack, $file, $testno) = @_; |
50 | |
899dc88a |
51 | tie my %cache => $tiepack, $file |
52 | or die $!; |
a0cb3900 |
53 | |
54 | memoize 'c5', |
899dc88a |
55 | SCALAR_CACHE => [HASH => \%cache], |
a0cb3900 |
56 | LIST_CACHE => 'FAULT' |
57 | ; |
58 | |
59 | my $t1 = c5(); |
60 | my $t2 = c5(); |
61 | print (($t1 == 5) ? "ok $testno\n" : "not ok $testno\n"); |
62 | $testno++; |
63 | print (($t2 == 5) ? "ok $testno\n" : "not ok $testno\n"); |
64 | unmemoize 'c5'; |
65 | 1; |
66 | 1; |
67 | |
68 | # Now something tricky---we'll memoize c23 with the wrong table that |
69 | # has the 5 already cached. |
70 | memoize 'c23', |
899dc88a |
71 | SCALAR_CACHE => [HASH => \%cache], |
a0cb3900 |
72 | LIST_CACHE => 'FAULT' |
73 | ; |
74 | |
75 | my $t3 = c23(); |
76 | my $t4 = c23(); |
77 | $testno++; |
78 | print (($t3 == 5) ? "ok $testno\n" : "not ok $testno\n"); |
79 | $testno++; |
80 | print (($t4 == 5) ? "ok $testno\n" : "not ok $testno\n"); |
81 | unmemoize 'c23'; |
82 | } |
83 | |