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