for beter make distclean (was Re: [PATCH] Re: [ID 20020305.026] Not OK: perl v5.7...
[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 BEGIN {
5     chdir 't' if -d 't';
6     @INC = '../lib';
7 }
8 use Memoize 0.45 qw(memoize unmemoize);
9 # $Memoize::Storable::Verbose = 0;
10
11 eval {require Memoize::Storable};
12 if ($@) {
13   print "1..0\n";
14   exit 0;
15 }
16
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$$");
47 1 while unlink $file;
48 tryout('Memoize::Storable', $file, 1);  # Test 1..4
49 1 while unlink $file;
50
51 sub tryout {
52   my ($tiepack, $file, $testno) = @_;
53
54   tie my %cache => $tiepack, $file
55     or die $!;
56
57   memoize 'c5', 
58   SCALAR_CACHE => [HASH => \%cache],
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', 
74   SCALAR_CACHE => [HASH => \%cache],
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