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_sdbm.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;
6b79e901 10# use Memoize::SDBM_File;
a0cb3900 11# $Memoize::GDBM_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
18ac4634 27eval {require Memoize::SDBM_File};
a0cb3900 28if ($@) {
29 print "1..0\n";
30 exit 0;
31}
32
33print "1..4\n";
34
f817a191 35my $tmpdir;
36my $file;
a0cb3900 37if (eval {require File::Spec::Functions}) {
6b79e901 38 File::Spec::Functions->import('tmpdir', 'catfile');
39 $tmpdir = tmpdir();
a0cb3900 40} else {
6b79e901 41 *catfile = sub { join '/', @_ };
dfefebdc 42 $tmpdir = $ENV{TMP} || $ENV{TMPDIR} || '/tmp';
a0cb3900 43}
a0cb3900 44$file = catfile($tmpdir, "md$$");
899dc88a 451 while unlink $file, "$file.dir", "$file.pag";
f817a191 46if ($^O eq 'VMS') {
47 1 while unlink "${file}.sdbm_dir";
48}
899dc88a 49tryout('Memoize::SDBM_File', $file, 1); # Test 1..4
501 while unlink $file, "$file.dir", "$file.pag";
f817a191 51if ($^O eq 'VMS') {
52 1 while unlink "${file}.sdbm_dir";
53}
a0cb3900 54
55sub tryout {
56 my ($tiepack, $file, $testno) = @_;
57
899dc88a 58 tie my %cache => $tiepack, $file, O_RDWR | O_CREAT, 0666
59 or die $!;
a0cb3900 60
61 memoize 'c5',
899dc88a 62 SCALAR_CACHE => [HASH => \%cache],
a0cb3900 63 LIST_CACHE => 'FAULT'
64 ;
65
66 my $t1 = c5();
67 my $t2 = c5();
68 print (($t1 == 5) ? "ok $testno\n" : "not ok $testno\n");
69 $testno++;
70 print (($t2 == 5) ? "ok $testno\n" : "not ok $testno\n");
71 unmemoize 'c5';
72
73 # Now something tricky---we'll memoize c23 with the wrong table that
74 # has the 5 already cached.
75 memoize 'c23',
899dc88a 76 SCALAR_CACHE => [HASH => \%cache],
a0cb3900 77 LIST_CACHE => 'FAULT'
78 ;
79
80 my $t3 = c23();
81 my $t4 = c23();
82 $testno++;
83 print (($t3 == 5) ? "ok $testno\n" : "not ok $testno\n");
84 $testno++;
85 print (($t4 == 5) ? "ok $testno\n" : "not ok $testno\n");
86 unmemoize 'c23';
87}
88