avoid temp file littering in tests
[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 # $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
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$$");
44 1 while unlink $file;
45 tryout('Memoize::Storable', $file, 1);  # Test 1..4
46 1 while unlink $file;
47
48 sub tryout {
49   my ($tiepack, $file, $testno) = @_;
50
51   tie my %cache => $tiepack, $file
52     or die $!;
53
54   memoize 'c5', 
55   SCALAR_CACHE => [HASH => \%cache],
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', 
71   SCALAR_CACHE => [HASH => \%cache],
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