RC3: lib/constant.t #35
[p5sagit/p5-mst-13.2.git] / lib / Memoize / t / tie.t
CommitLineData
a0cb3900 1#!/usr/bin/perl
2
484fdf61 3use lib qw(. ..);
a0cb3900 4use Memoize 0.52 qw(memoize unmemoize);
5use Fcntl;
6b79e901 6eval {require Memoize::AnyDBM_File};
7if ($@) {
8 print "1..0\n";
9 exit 0;
10}
11
12
a0cb3900 13
14print "1..4\n";
15
16sub i {
17 $_[0];
18}
19
20$ARG = 'Keith Bostic is a pinhead';
21
22sub c119 { 119 }
23sub c7 { 7 }
24sub c43 { 43 }
25sub c23 { 23 }
26sub c5 { 5 }
27
28sub n {
29 $_[0]+1;
30}
31
a0cb3900 32if (eval {require File::Spec::Functions}) {
6b79e901 33 File::Spec::Functions->import('tmpdir', 'catfile');
dfefebdc 34 $tmpdir = tmpdir();
a0cb3900 35} else {
36 *catfile = sub { join '/', @_ };
dfefebdc 37 $tmpdir = $ENV{TMP} || $ENV{TMPDIR} || '/tmp';
a0cb3900 38}
39$file = catfile($tmpdir, "md$$");
40@files = ($file, "$file.db", "$file.dir", "$file.pag");
899dc88a 411 while unlink @files;
a0cb3900 42
43
44tryout('Memoize::AnyDBM_File', $file, 1); # Test 1..4
45# tryout('DB_File', $file, 1); # Test 1..4
899dc88a 461 while unlink $file, "$file.dir", "$file.pag";
a0cb3900 47
48sub tryout {
49 my ($tiepack, $file, $testno) = @_;
50
899dc88a 51 tie my %cache => $tiepack, $file, O_RDWR | O_CREAT, 0666
52 or die $!;
a0cb3900 53
54 memoize 'c5',
899dc88a 55 SCALAR_CACHE => [HASH => \%cache],
56 LIST_CACHE => 'FAULT'
a0cb3900 57 ;
58
59 my $t1 = c5($ARG);
60 my $t2 = c5($ARG);
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($ARG);
74 my $t4 = c23($ARG);
75 $testno++;
76 print (($t3 == 5) ? "ok $testno\n" : "not ok $testno # Result $t3\n");
77 $testno++;
78 print (($t4 == 5) ? "ok $testno\n" : "not ok $testno # Result $t4\n");
79 unmemoize 'c23';
80}
81
82{
83 my @present = grep -e, @files;
84 if (@present && (@failed = grep { not unlink } @present)) {
85 warn "Can't unlink @failed! ($!)";
86 }
87}