diff options
Diffstat (limited to 'gnu/usr.bin/perl/cpan/Memoize/t/normalize.t')
-rwxr-xr-x | gnu/usr.bin/perl/cpan/Memoize/t/normalize.t | 53 |
1 files changed, 31 insertions, 22 deletions
diff --git a/gnu/usr.bin/perl/cpan/Memoize/t/normalize.t b/gnu/usr.bin/perl/cpan/Memoize/t/normalize.t index a920ff4b307..8b9f90f2b7e 100755 --- a/gnu/usr.bin/perl/cpan/Memoize/t/normalize.t +++ b/gnu/usr.bin/perl/cpan/Memoize/t/normalize.t @@ -1,10 +1,6 @@ -#!/usr/bin/perl - -use lib '..'; +use strict; use warnings; use Memoize; - -print "1..7\n"; - +use Test::More tests => 11; sub n_null { '' } @@ -24,34 +20,47 @@ my $a_normal = memoize('a1', INSTALL => undef); my $a_nomemo = memoize('a2', INSTALL => undef, NORMALIZER => 'n_diff'); my $a_allmemo = memoize('a3', INSTALL => undef, NORMALIZER => 'n_null'); +my @ARGS; @ARGS = (1, 2, 3, 2, 1); -@res = map { &$a_normal($_) } @ARGS; -print ((("@res" eq "1-1 2-2 3-3 2-2 1-1") ? '' : 'not '), "ok 1\n"); - -@res = map { &$a_nomemo($_) } @ARGS; -print ((("@res" eq "1-1 2-2 3-3 2-4 1-5") ? '' : 'not '), "ok 2\n"); +is_deeply [map $a_normal->($_), @ARGS], [qw(1-1 2-2 3-3 2-2 1-1)], 'no normalizer'; +is_deeply [map $a_nomemo->($_), @ARGS], [qw(1-1 2-2 3-3 2-4 1-5)], 'n_diff'; +is_deeply [map $a_allmemo->($_), @ARGS], [qw(1-1 1-1 1-1 1-1 1-1)], 'n_null'; -@res = map { &$a_allmemo($_) } @ARGS; -print ((("@res" eq "1-1 1-1 1-1 1-1 1-1") ? '' : 'not '), "ok 3\n"); - - - # Test fully-qualified name and installation +my $COUNT; $COUNT = 0; sub parity { $COUNT++; $_[0] % 2 } sub parnorm { $_[0] % 2 } memoize('parity', NORMALIZER => 'main::parnorm'); -@res = map { &parity($_) } @ARGS; -print ((("@res" eq "1 0 1 0 1") ? '' : 'not '), "ok 4\n"); -print (( ($COUNT == 2) ? '' : 'not '), "ok 5\n"); +is_deeply [map parity($_), @ARGS], [qw(1 0 1 0 1)], 'parity normalizer'; +is $COUNT, 2, '... with the expected number of calls'; # Test normalization with reference to normalizer function $COUNT = 0; sub par2 { $COUNT++; $_[0] % 2 } memoize('par2', NORMALIZER => \&parnorm); -@res = map { &par2($_) } @ARGS; -print ((("@res" eq "1 0 1 0 1") ? '' : 'not '), "ok 6\n"); -print (( ($COUNT == 2) ? '' : 'not '), "ok 7\n"); +is_deeply [map par2($_), @ARGS], [qw(1 0 1 0 1)], '... also installable by coderef'; +is $COUNT, 2, '... still with the expected number of calls'; + +$COUNT = 0; +sub count_uninitialized { $COUNT += join('', @_) =~ /\AUse of uninitialized value / } +my $war1 = memoize(sub {1}, NORMALIZER => sub {undef}); +{ local $SIG{__WARN__} = \&count_uninitialized; $war1->() } +is $COUNT, 0, 'no warning when normalizer returns undef'; +# Context propagated correctly to normalizer? +sub n { + my $which = wantarray ? 'list' : 'scalar'; + local $Test::Builder::Level = $Test::Builder::Level + 2; + is $_[0], $which, "$which context propagates properly"; +} +sub f { 1 } +memoize('f', NORMALIZER => 'n'); +my $s = f 'scalar'; +my @a = f 'list'; +sub args { scalar @_ } +sub null_args { join chr(28), splice @_ } +memoize('args', NORMALIZER => 'null_args'); +ok args(1), 'original @_ is protected from normalizer'; |