8920dc9a8826aa8cf7b47a550105297f740f84a0
[p5sagit/p5-mst-13.2.git] / lib / Memoize / t / tie_ndbm.t
1 #!/usr/bin/perl
2
3 BEGIN {
4     chdir 't' if -d 't';
5     @INC = '../lib';
6 }
7 #use lib qw(. ..);
8 use Memoize 0.45 qw(memoize unmemoize);
9 use Fcntl;
10 # use Memoize::NDBM_File;
11 # $Memoize::NDBM_File::Verbose = 0;
12
13 sub i {
14   $_[0];
15 }
16
17 sub c119 { 119 }
18 sub c7 { 7 }
19 sub c43 { 43 }
20 sub c23 { 23 }
21 sub c5 { 5 }
22
23 sub n {
24   $_[0]+1;
25 }
26
27 eval {require Memoize::NDBM_File};
28 if ($@) {
29   print "1..0\n";
30   exit 0;
31 }
32
33 print "1..4\n";
34
35
36 if (eval {require File::Spec::Functions}) {
37  File::Spec::Functions->import();
38 } else {
39   *catfile = sub { join '/', @_ };
40 }
41 $tmpdir = $ENV{TMP} || $ENV{TMPDIR} ||  '/tmp';  
42 $file = catfile($tmpdir, "md$$");
43 1 while unlink $file, "$file.dir", "$file.pag";
44 tryout('Memoize::NDBM_File', $file, 1);  # Test 1..4
45 1 while unlink $file, "$file.dir", "$file.pag";
46
47 sub tryout {
48   my ($tiepack, $file, $testno) = @_;
49
50
51   tie my %cache => $tiepack, $file, O_RDWR | O_CREAT, 0666
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   
66   # Now something tricky---we'll memoize c23 with the wrong table that
67   # has the 5 already cached.
68   memoize 'c23', 
69   SCALAR_CACHE => [HASH => \%cache],
70   LIST_CACHE => 'FAULT'
71     ;
72   
73   my $t3 = c23();
74   my $t4 = c23();
75   $testno++;
76   print (($t3 == 5) ? "ok $testno\n" : "not ok $testno\n");
77   $testno++;
78   print (($t4 == 5) ? "ok $testno\n" : "not ok $testno\n");
79   unmemoize 'c23';
80 }
81