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