From: Vincent Pit Date: Fri, 8 Aug 2008 19:54:50 +0000 (+0200) Subject: Put grep/map tests into their own test file X-Git-Tag: v0.03~6 X-Git-Url: http://git.vpit.fr/?a=commitdiff_plain;h=002535b0100121f92a04638a58e2d34add019275;p=perl%2Fmodules%2FSub-Nary.git Put grep/map tests into their own test file --- diff --git a/MANIFEST b/MANIFEST index 37e7c29..922c7e3 100644 --- a/MANIFEST +++ b/MANIFEST @@ -16,6 +16,7 @@ t/21-list.t t/22-call.t t/23-branch.t t/24-ops.t +t/25-grepmap.t t/90-boilerplate.t t/91-pod.t t/92-pod-coverage.t diff --git a/t/20-return.t b/t/20-return.t index ccfd9e9..e08fdba 100644 --- a/t/20-return.t +++ b/t/20-return.t @@ -3,7 +3,7 @@ use strict; use warnings; -use Test::More tests => 79; +use Test::More tests => 63; use Sub::Nary; @@ -11,8 +11,6 @@ my $sn = Sub::Nary->new(); my ($x, $y, @a, %h); -my $exp_2 = { 1 => 0.5, 2 => 0.5 }; - my @tests = ( [ sub { return }, 0 ], [ sub { return () }, 0 ], @@ -88,30 +86,6 @@ my @tests = ( [ sub { return eval { do { eval { @a } } } }, 'list' ], [ sub { eval 'return 1, 2' }, 'list' ], - - [ sub { grep { return 2, 4 } 5 .. 10 }, 2 ], - [ sub { grep { $_ > 1 } do { return 2, 4; 5 .. 10 } }, 2 ], - [ sub { grep { return 2, 4 } () }, 0 ], - [ sub { grep { return $_ ? 2 : (3, 4) } 7 .. 8 }, $exp_2 ], - [ sub { grep { return 2 if $_; 3 } 7 .. 8 }, - { 1 => 0.75, list => 0.25 } ], - [ sub { grep { $_ > 1 } do { return $x ? 7 : (8, 9) } }, $exp_2 ], - [ sub { grep { return $_ ? 2 : (3, 4) } do { return 3 .. 5 if $x; } }, - { 3 => 0.5, 1 => 0.25, 2 => 0.25 } ], - [ sub { grep { return $_ ? 2 : (3, 4) } do { return 3 .. 5 if $x; () } }, - { 3 => 0.5, 0 => 0.5 } ], - - [ sub { map { return 2, 4 } 5 .. 10 }, 2 ], - [ sub { map { $_ + 1 } do { return 2, 4; 5 .. 10 } }, 2 ], - [ sub { map { return 2, 4 } () }, 0 ], - [ sub { map { return $_ ? 2 : (3, 4) } 7 .. 8 }, $exp_2 ], - [ sub { map { return 2 if $_; 3 } 7 .. 8 }, - { 1 => 0.75, 2 => 0.25 } ], - [ sub { map { $_ > 1 } do { return $x ? 7 : (8, 9) } }, $exp_2 ], - [ sub { map { return $_ ? 2 : (3, 4) } do { return 3 .. 5 if $x; } }, - { 3 => 0.5, 1 => 0.25, 2 => 0.25 } ], - [ sub { map { return $_ ? 2 : (3, 4) } do { return 3 .. 5 if $x; () } }, - { 3 => 0.5, 0 => 0.5 } ], ); my $i = 1; diff --git a/t/24-ops.t b/t/24-ops.t index b636cf8..a69cd0a 100644 --- a/t/24-ops.t +++ b/t/24-ops.t @@ -3,7 +3,7 @@ use strict; use warnings; -use Test::More tests => 49; +use Test::More tests => 34; use Sub::Nary; @@ -11,58 +11,10 @@ my $sn = Sub::Nary->new(); my ($x, %h); -sub zeroorone { - return (rand() < 0.1) ? () : 1; -} - -sub oneortwo { - if (rand() < 0.1) { - return 3 - } else { - 4, 5 - } -} - -sub onetwothree { - my $r = rand(); - if ($r < 0.1) { - return 3 - } elsif ($r < 0.9) { - return 4, 5 - } - return 4, do { 5, 6 }; -} - -# { 1 => 0.5, 2 => 0.5 } * 0.5 + { 2 => 0.25, 3 => 0.5, 4 => 0.25 } * 0.5 -my $exp_22 = { 1 => 0.5 * 0.5, 2 => (0.5 + 0.25) * 0.5, 3 => 0.5 * 0.5, 4 => 0.25 * 0.5 }; - -# { 1 => 0.5, 2 => 0.25, 3 => 0.25 } * 0.5 + { 2 => 0.25, 3 => 0.25, 4 => 0.3125, 5 => 0.125, 6 => 0.0625 } * 0.5 -my $exp_32 = { 1 => 0.5/2, 2 => (0.25+0.25)/2, 3 => (0.25+0.25)/2, 4 => (0.3125)/2, 5 => (0.125)/2, 6 => (0.0625)/2 }; - -my $b3 = 0.5 ** 3; -my $exp_23 = { 3 => $b3, 4 => 3 * $b3, 5 => 3 * $b3, 6 => $b3 }; - my @tests = ( [ sub { delete $h{foo} }, 1 ], [ sub { delete @h{qw/foo bar baz/} }, 3 ], - [ sub { grep { 1 } 1 .. 10 }, 'list' ], - [ sub { grep { 1 } @_ }, 'list' ], - - [ sub { map { $_ } 1 .. 3 }, 3 ], - [ sub { map { () } @_ }, 0 ], - [ sub { map { @_ } () }, 0 ], - [ sub { map { @_ } 1, 2 }, 'list' ], - [ sub { map { $_ } oneortwo() }, { 1 => 0.5, 2 => 0.5 } ], - [ sub { map { $_ ? 7 : (8, 9) } 1 .. 3 }, $exp_23 ], - [ sub { map oneortwo, 1 .. 3 }, $exp_23 ], - [ sub { map oneortwo, @_ }, 'list' ], - [ sub { map zeroorone, @_ }, { 0 => 0.5, list => 0.5 } ], - [ sub { map { $_ ? () : 12 } do { $x ? 7 : () } }, { 0 => 0.75, 1 => 0.25 } ], - [ sub { map zeroorone, do { $x ? 7 : () } }, { 0 => 0.75, 1 => 0.25 } ], - [ sub { map oneortwo, oneortwo }, $exp_22 ], - [ sub { map onetwothree, oneortwo }, $exp_32 ], - [ sub { return <$x> }, 'list' ], [ sub { -f $0, -r $0 }, 2 ], diff --git a/t/25-grepmap.t b/t/25-grepmap.t new file mode 100644 index 0000000..06a4ddf --- /dev/null +++ b/t/25-grepmap.t @@ -0,0 +1,96 @@ +#!perl -T + +use strict; +use warnings; + +use Test::More tests => 31; + +use Sub::Nary; + +my $sn = Sub::Nary->new(); + +my ($x, $y, @a, %h); + +sub zeroorone { + return (rand() < 0.1) ? () : 1; +} + +sub oneortwo { + if (rand() < 0.1) { + return 3 + } else { + 4, 5 + } +} + +sub onetwothree { + my $r = rand(); + if ($r < 0.1) { + return 3 + } elsif ($r < 0.9) { + return 4, 5 + } + return 4, do { 5, 6 }; +} + +my $exp_2 = { 1 => 0.5, 2 => 0.5 }; + +# { 1 => 0.5, 2 => 0.5 } * 0.5 + { 2 => 0.25, 3 => 0.5, 4 => 0.25 } * 0.5 +my $exp_22 = { 1 => 0.5 * 0.5, 2 => (0.5 + 0.25) * 0.5, 3 => 0.5 * 0.5, 4 => 0.25 * 0.5 }; + +# { 1 => 0.5, 2 => 0.25, 3 => 0.25 } * 0.5 + { 2 => 0.25, 3 => 0.25, 4 => 0.3125, 5 => 0.125, 6 => 0.0625 } * 0.5 +my $exp_32 = { 1 => 0.5/2, 2 => (0.25+0.25)/2, 3 => (0.25+0.25)/2, 4 => (0.3125)/2, 5 => (0.125)/2, 6 => (0.0625)/2 }; + +my $b3 = 0.5 ** 3; +my $exp_23 = { 3 => $b3, 4 => 3 * $b3, 5 => 3 * $b3, 6 => $b3 }; + +my @tests = ( + [ sub { grep { return 2, 4 } 5 .. 10 }, 2 ], + [ sub { grep { $_ > 1 } do { return 2, 4; 5 .. 10 } }, 2 ], + [ sub { grep { return 2, 4 } () }, 0 ], + [ sub { grep { return $_ ? 2 : (3, 4) } 7 .. 8 }, $exp_2 ], + [ sub { grep { return 2 if $_; 3 } 7 .. 8 }, + { 1 => 0.75, list => 0.25 } ], + [ sub { grep { $_ > 1 } do { return $x ? 7 : (8, 9) } }, $exp_2 ], + [ sub { grep { return $_ ? 2 : (3, 4) } do { return 3 .. 5 if $x; } }, + { 3 => 0.5, 1 => 0.25, 2 => 0.25 } ], + [ sub { grep { return $_ ? 2 : (3, 4) } do { return 3 .. 5 if $x; () } }, + { 3 => 0.5, 0 => 0.5 } ], + + [ sub { map { return 2, 4 } 5 .. 10 }, 2 ], + [ sub { map { $_ + 1 } do { return 2, 4; 5 .. 10 } }, 2 ], + [ sub { map { return 2, 4 } () }, 0 ], + [ sub { map { return $_ ? 2 : (3, 4) } 7 .. 8 }, $exp_2 ], + [ sub { map { return 2 if $_; 3 } 7 .. 8 }, + { 1 => 0.75, 2 => 0.25 } ], + [ sub { map { $_ > 1 } do { return $x ? 7 : (8, 9) } }, $exp_2 ], + [ sub { map { return $_ ? 2 : (3, 4) } do { return 3 .. 5 if $x; } }, + { 3 => 0.5, 1 => 0.25, 2 => 0.25 } ], + [ sub { map { return $_ ? 2 : (3, 4) } do { return 3 .. 5 if $x; () } }, + { 3 => 0.5, 0 => 0.5 } ], + + [ sub { grep { 1 } 1 .. 10 }, 'list' ], + [ sub { grep { 1 } @_ }, 'list' ], + + [ sub { map { $_ } 1 .. 3 }, 3 ], + [ sub { map { () } @_ }, 0 ], + [ sub { map { @_ } () }, 0 ], + [ sub { map { @_ } 1, 2 }, 'list' ], + [ sub { map { $_ } oneortwo() }, { 1 => 0.5, 2 => 0.5 } ], + [ sub { map { $_ ? 7 : (8, 9) } 1 .. 3 }, $exp_23 ], + [ sub { map oneortwo, 1 .. 3 }, $exp_23 ], + [ sub { map oneortwo, @_ }, 'list' ], + [ sub { map zeroorone, @_ }, { 0 => 0.5, list => 0.5 } ], + [ sub { map { $_ ? () : 12 } do { $x ? 7 : () } }, { 0 => 0.75, 1 => 0.25 } ], + [ sub { map zeroorone, do { $x ? 7 : () } }, { 0 => 0.75, 1 => 0.25 } ], + [ sub { map oneortwo, oneortwo }, $exp_22 ], + [ sub { map onetwothree, oneortwo }, $exp_32 ], +); + +my $i = 1; +for (@tests) { + my $r = $sn->nary($_->[0]); + my $exp = ref $_->[1] ? $_->[1] : { $_->[1] => 1 }; + is_deeply($r, $exp, 'grep/map test ' . $i); + ++$i; +}