/
memoize.t
135 lines (107 loc) · 3.24 KB
/
memoize.t
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
use v6.c;
use Test;
use Memoize <memoize unmemoize flush_cache>;
plan 20;
my $size = 100;
my $times = 10;
# basic, by string
{
my @seen;
sub a($a) { ++@seen[$a]; $a };
my $original = &a.^name;
isnt memoize('a').^name, $original,
'did the memoization change the sub with default normalizer';
for ^$times {
a($_) for (^$size).pick(*);
}
is-deeply @seen, [1 xx $size],
'was each different value only seen once with default normalizer';
is unmemoize('a').^name, $original,
'did the unmemoization restore the sub with default normalizer';
lives-ok { flush_cache('a') }, 'can we flush the cache ok';
}
# basic, by code object
{
my @seen;
sub a($a) { ++@seen[$a]; $a };
my $original = &a.^name;
isnt memoize(&a).^name, $original,
'did the memoization change the sub with default normalizer';
for ^$times {
a($_) for (^$size).pick(*);
}
is-deeply @seen, [1 xx $size],
'was each different value only seen once with default normalizer';
is unmemoize(&a).^name, $original,
'did the unmemoization restore the sub with default normalizer';
is &a.cache.elems, $size, 'did we get see all the values';
flush_cache(&a);
is &a.cache.elems, 0, 'did we all the values get removed';
}
# specific normalizer, code object
{
my @seen;
my $normalized;
sub a($a) { ++@seen[$a]; $a };
my $original = &a.^name;
isnt memoize(&a, NORMALIZER => -> \c { ++$normalized; c.gist }).^name,
$original,
'did the memoization change the sub with given normalizer';
for ^$times {
a($_) for (^$size).pick(*);
}
is $normalized, $times * $size, "did the normalizer get called";
is-deeply @seen, [1 xx $size],
'was each different value only seen once with given normalizer';
}
# in memory cache, by code object
{
my @seen;
sub a($a) { ++@seen[$a]; $a };
my $original = &a.^name;
isnt memoize(&a, :CACHE<MEMORY>).^name,
$original,
'did the memoization change the sub';
for ^$times {
a($_) for (^$size).pick(*);
}
is-deeply @seen, [1 xx $size],
'was each different value only seen once';
}
# in memory multi cache, by code object
{
my @seen;
sub a($a) { ++@seen[$a]; $a };
my $original = &a.^name;
isnt memoize(&a, :CACHE<MULTI>).^name,
$original,
'did the memoization change the sub';
await do for ^$times { start {
for ^$size {
sleep rand / 100;
a($_)
}
} }
is-deeply [@seen.map: *.Bool], [True xx $size],
'was each different value only seen at least once';
}
# with specific cache, by code object
{
my $flushed;
role Flush { method FLUSH() { ++$flushed } }
my @seen;
sub a($a) { ++@seen[$a]; $a };
my $original = &a.^name;
isnt memoize( &a, :CACHE(my %hash does Flush)).^name,
$original,
'did the memoization change the sub';
for ^$times {
a($_) for (^$size).pick(*);
}
is-deeply @seen, [1 xx $size],
'was each different value only seen once';
is unmemoize(&a).^name, $original,
'did the unmemoization restore the sub with default normalizer';
ok $flushed, 'did we actually call FLUSH';
}
# vim: ft=perl6 expandtab sw=4