RC3: lib/constant.t #35
[p5sagit/p5-mst-13.2.git] / lib / Memoize / t / tie_gdbm.t
CommitLineData
a0cb3900 1#!/usr/bin/perl
2
484fdf61 3use lib qw(. ..);
a0cb3900 4use Memoize 0.45 qw(memoize unmemoize);
5use Fcntl;
6
7sub i {
8 $_[0];
9}
10
11sub c119 { 119 }
12sub c7 { 7 }
13sub c43 { 43 }
14sub c23 { 23 }
15sub c5 { 5 }
16
17sub n {
18 $_[0]+1;
19}
20
21eval {require GDBM_File};
22if ($@) {
23 print "1..0\n";
24 exit 0;
25}
26
27print "1..4\n";
28
29if (eval {require File::Spec::Functions}) {
30 File::Spec::Functions->import();
31} else {
32 *catfile = sub { join '/', @_ };
33}
34$tmpdir = $ENV{TMP} || $ENV{TMPDIR} || '/tmp';
35$file = catfile($tmpdir, "md$$");
899dc88a 361 while unlink $file, "$file.dir", "$file.pag";
a0cb3900 37tryout('GDBM_File', $file, 1); # Test 1..4
899dc88a 381 while unlink $file, "$file.dir", "$file.pag";
a0cb3900 39
40sub tryout {
899dc88a 41 require GDBM_File;
a0cb3900 42 my ($tiepack, $file, $testno) = @_;
43
899dc88a 44 tie my %cache => $tiepack, $file, O_RDWR | O_CREAT, 0666
45 or die $!;
a0cb3900 46
47 memoize 'c5',
899dc88a 48 SCALAR_CACHE => [HASH => \%cache],
a0cb3900 49 LIST_CACHE => 'FAULT'
50 ;
51
52 my $t1 = c5();
53 my $t2 = c5();
54 print (($t1 == 5) ? "ok $testno\n" : "not ok $testno\n");
55 $testno++;
56 print (($t2 == 5) ? "ok $testno\n" : "not ok $testno\n");
57 unmemoize 'c5';
58
59 # Now something tricky---we'll memoize c23 with the wrong table that
60 # has the 5 already cached.
61 memoize 'c23',
899dc88a 62 SCALAR_CACHE => [HASH => \%cache],
a0cb3900 63 LIST_CACHE => 'FAULT'
64 ;
65
66 my $t3 = c23();
67 my $t4 = c23();
68 $testno++;
69 print (($t3 == 5) ? "ok $testno\n" : "not ok $testno\n");
70 $testno++;
71 print (($t4 == 5) ? "ok $testno\n" : "not ok $testno\n");
72 unmemoize 'c23';
73}
74