1#!/usr/bin/perl
2# -*- mode: perl; perl-indent-level: 2 -*-
3
4use lib qw(. ..);
5use Memoize 0.45 qw(memoize unmemoize);
6# $Memoize::Storable::Verbose = 0;
7
8eval {require Memoize::Storable};
9if ($@) {
10  print "1..0\n";
11  exit 0;
12}
13
14sub i {
15  $_[0];
16}
17
18sub c119 { 119 }
19sub c7 { 7 }
20sub c43 { 43 }
21sub c23 { 23 }
22sub c5 { 5 }
23
24sub n {
25  $_[0]+1;
26}
27
28eval {require Storable};
29if ($@) {
30  print "1..0\n";
31  exit 0;
32}
33
34print "1..4\n";
35
36
37if (eval {require File::Spec::Functions}) {
38 File::Spec::Functions->import();
39} else {
40  *catfile = sub { join '/', @_ };
41}
42$tmpdir = $ENV{TMP} || $ENV{TMPDIR} ||  '/tmp';  
43$file = catfile($tmpdir, "storable$$");
441 while unlink $file;
45tryout('Memoize::Storable', $file, 1);  # Test 1..4
461 while unlink $file;
47
48sub tryout {
49  my ($tiepack, $file, $testno) = @_;
50
51  tie my %cache => $tiepack, $file
52    or die $!;
53
54  memoize 'c5', 
55  SCALAR_CACHE => [HASH => \%cache],
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  1;
66  1;
67
68  # Now something tricky---we'll memoize c23 with the wrong table that
69  # has the 5 already cached.
70  memoize 'c23', 
71  SCALAR_CACHE => [HASH => \%cache],
72  LIST_CACHE => 'FAULT'
73    ;
74  
75  my $t3 = c23();
76  my $t4 = c23();
77  $testno++;
78  print (($t3 == 5) ? "ok $testno\n" : "not ok $testno\n");
79  $testno++;
80  print (($t4 == 5) ? "ok $testno\n" : "not ok $testno\n");
81  unmemoize 'c23';
82}
83
84