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_ndbm.t
CommitLineData
a0cb3900 1#!/usr/bin/perl
2
5317c87c 3BEGIN {
4 chdir 't' if -d 't';
5 @INC = '../lib';
6}
7#use lib qw(. ..);
a0cb3900 8use Memoize 0.45 qw(memoize unmemoize);
9use Fcntl;
10# use Memoize::NDBM_File;
11# $Memoize::NDBM_File::Verbose = 0;
12
13sub i {
14 $_[0];
15}
16
17sub c119 { 119 }
18sub c7 { 7 }
19sub c43 { 43 }
20sub c23 { 23 }
21sub c5 { 5 }
22
23sub n {
24 $_[0]+1;
25}
26
27eval {require Memoize::NDBM_File};
28if ($@) {
29 print "1..0\n";
30 exit 0;
31}
32
33print "1..4\n";
34
35
36if (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$$");
94c0dff6 431 while unlink $file, "$file.dir", "$file.pag", "$file.db";
a0cb3900 44tryout('Memoize::NDBM_File', $file, 1); # Test 1..4
94c0dff6 451 while unlink $file, "$file.dir", "$file.pag", "$file.db";
a0cb3900 46
47sub tryout {
48 my ($tiepack, $file, $testno) = @_;
49
50
899dc88a 51 tie my %cache => $tiepack, $file, O_RDWR | O_CREAT, 0666
52 or die $!;
53
a0cb3900 54 memoize 'c5',
899dc88a 55 SCALAR_CACHE => [HASH => \%cache],
a0cb3900 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',
899dc88a 69 SCALAR_CACHE => [HASH => \%cache],
a0cb3900 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