VMS specific cleanup and strictness for tie_sdbm.t
[p5sagit/p5-mst-13.2.git] / lib / Memoize / t / tie.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.52 qw(memoize unmemoize);
9use Fcntl;
6b79e901 10eval {require Memoize::AnyDBM_File};
11if ($@) {
12 print "1..0\n";
13 exit 0;
14}
15
16
a0cb3900 17
18print "1..4\n";
19
20sub i {
21 $_[0];
22}
23
24$ARG = 'Keith Bostic is a pinhead';
25
26sub c119 { 119 }
27sub c7 { 7 }
28sub c43 { 43 }
29sub c23 { 23 }
30sub c5 { 5 }
31
32sub n {
33 $_[0]+1;
34}
35
a0cb3900 36if (eval {require File::Spec::Functions}) {
6b79e901 37 File::Spec::Functions->import('tmpdir', 'catfile');
dfefebdc 38 $tmpdir = tmpdir();
a0cb3900 39} else {
40 *catfile = sub { join '/', @_ };
dfefebdc 41 $tmpdir = $ENV{TMP} || $ENV{TMPDIR} || '/tmp';
a0cb3900 42}
43$file = catfile($tmpdir, "md$$");
44@files = ($file, "$file.db", "$file.dir", "$file.pag");
899dc88a 451 while unlink @files;
a0cb3900 46
47
48tryout('Memoize::AnyDBM_File', $file, 1); # Test 1..4
49# tryout('DB_File', $file, 1); # Test 1..4
899dc88a 501 while unlink $file, "$file.dir", "$file.pag";
a0cb3900 51
52sub tryout {
53 my ($tiepack, $file, $testno) = @_;
54
899dc88a 55 tie my %cache => $tiepack, $file, O_RDWR | O_CREAT, 0666
56 or die $!;
a0cb3900 57
58 memoize 'c5',
899dc88a 59 SCALAR_CACHE => [HASH => \%cache],
60 LIST_CACHE => 'FAULT'
a0cb3900 61 ;
62
63 my $t1 = c5($ARG);
64 my $t2 = c5($ARG);
65 print (($t1 == 5) ? "ok $testno\n" : "not ok $testno\n");
66 $testno++;
67 print (($t2 == 5) ? "ok $testno\n" : "not ok $testno\n");
68 unmemoize 'c5';
69
70 # Now something tricky---we'll memoize c23 with the wrong table that
71 # has the 5 already cached.
72 memoize 'c23',
899dc88a 73 SCALAR_CACHE => ['HASH', \%cache],
a0cb3900 74 LIST_CACHE => 'FAULT'
75 ;
76
77 my $t3 = c23($ARG);
78 my $t4 = c23($ARG);
79 $testno++;
80 print (($t3 == 5) ? "ok $testno\n" : "not ok $testno # Result $t3\n");
81 $testno++;
82 print (($t4 == 5) ? "ok $testno\n" : "not ok $testno # Result $t4\n");
83 unmemoize 'c23';
84}
85
86{
87 my @present = grep -e, @files;
88 if (@present && (@failed = grep { not unlink } @present)) {
89 warn "Can't unlink @failed! ($!)";
90 }
91}