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