| 1 | #!/usr/bin/perl |
| 2 | # -*- mode: perl; perl-indent-level: 2 -*- |
| 3 | |
| 4 | use lib qw(. ..); |
| 5 | use Memoize 0.45 qw(memoize unmemoize); |
| 6 | # $Memoize::Storable::Verbose = 0; |
| 7 | |
| 8 | eval {require Memoize::Storable}; |
| 9 | if ($@) { |
| 10 | print "1..0\n"; |
| 11 | exit 0; |
| 12 | } |
| 13 | |
| 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 | $file = "storable$$"; |
| 37 | 1 while unlink $file; |
| 38 | tryout('Memoize::Storable', $file, 1); # Test 1..4 |
| 39 | 1 while unlink $file; |
| 40 | |
| 41 | sub tryout { |
| 42 | my ($tiepack, $file, $testno) = @_; |
| 43 | |
| 44 | tie my %cache => $tiepack, $file |
| 45 | or die $!; |
| 46 | |
| 47 | memoize 'c5', |
| 48 | SCALAR_CACHE => [HASH => \%cache], |
| 49 | LIST_CACHE => 'FAULT' |
| 50 | ; |
| 51 | |
| 52 | my $t1 = c5(); |
| 53 | my $t2 = c5(); |
| 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 | 1; |
| 59 | 1; |
| 60 | |
| 61 | # Now something tricky---we'll memoize c23 with the wrong table that |
| 62 | # has the 5 already cached. |
| 63 | memoize 'c23', |
| 64 | SCALAR_CACHE => [HASH => \%cache], |
| 65 | LIST_CACHE => 'FAULT' |
| 66 | ; |
| 67 | |
| 68 | my $t3 = c23(); |
| 69 | my $t4 = c23(); |
| 70 | $testno++; |
| 71 | print (($t3 == 5) ? "ok $testno\n" : "not ok $testno\n"); |
| 72 | $testno++; |
| 73 | print (($t4 == 5) ? "ok $testno\n" : "not ok $testno\n"); |
| 74 | unmemoize 'c23'; |
| 75 | } |
| 76 | |