Skip to content

Commit

Permalink
Unfudge passing tests for RT #122497, add test for golfed code from s…
Browse files Browse the repository at this point in the history
…aid ticket
  • Loading branch information
usev6 committed Jan 22, 2015
1 parent 8c62c13 commit 317910a
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 48 deletions.
10 changes: 9 additions & 1 deletion S32-hash/exists-adverb.t
Expand Up @@ -2,7 +2,7 @@ use v6;

use Test;

plan 46;
plan 47;

# L<S02/Names and Variables/:exists>

Expand Down Expand Up @@ -98,4 +98,12 @@ sub gen_hash {
is %h.elems, 26, "should not have changed hash";
} #46

# RT #122497
{
BEGIN my %cache = 1 => 0;
sub foo($n) {%cache{$n}:exists}
is foo(42), False,
'no internal error with :exists adverb on non existing key of hash initialized at BEGIN time';
}

# vim: ft=perl6
21 changes: 9 additions & 12 deletions integration/advent2011-day04.t
Expand Up @@ -10,25 +10,22 @@ our %cache;
multi sub trait_mod:<is>(Routine $r, :$Cached!) {
#wrap the routine in a block that..
$r.wrap(-> $arg {
# looks up the argument in the cache
%cache{$arg}:exists
?? %cache{$arg}
# looks up the argument in the cache
%cache{$arg}:exists
?? %cache{$arg}
# ... and calls the original, if it
# is not found in the cache
!! (%cache{$arg} = callwith($arg))
!! (%cache{$arg} = callwith($arg))
}
);
}
# example aplication:

# example aplication:
sub fib($x) is Cached {
$x <= 1 ?? 1 !! fib($x - 1) + fib($x - 2);
}
# only one call for each value from 0 to 10
#?rakudo.jvm 2 skip 'RT #122497'
#?rakudo.parrot 2 skip 'RT #122497'
# only one call for each value from 0 to 10
is fib(10), 89, 'fibinacci output';
is_deeply %cache, {1 => 1, 0 => 1, 2 => 2, 3 => 3,
4 => 5, 5 => 8, 6 => 13, 7 => 21,
8 => 34, 9 => 55, 10 => 89}, 'caching';

4 => 5, 5 => 8, 6 => 13, 7 => 21,
8 => 34, 9 => 55, 10 => 89}, 'caching';
46 changes: 21 additions & 25 deletions integration/advent2012-day04.t
Expand Up @@ -7,10 +7,10 @@ is do { [+] grep * %% 2, (1, 2, *+* ...^ * > 4_000_000) }, 4613732, 'fibonacci';

sub largest-prime-factor($n is copy) {
for 2, 3, *+2 ... * {
while $n %% $_ {
$n div= $_;
return $_ if $_ > $n;
}
while $n %% $_ {
$n div= $_;
return $_ if $_ > $n;
}
}
}

Expand All @@ -20,26 +20,26 @@ is largest-prime-factor(600_851_475_143), 6857, 'largest prime factor';
# Problem 53
is_deeply do {
[1], -> @p { [0, @p Z+ @p, 0] } ... * # generate Pascal's triangle
==> (*[0..100])() # get rows up to n = 100
==> map *.list # flatten rows into a single list
==> grep * > 1_000_000 # filter elements exceeding 1e6
==> elems()
==> (*[0..100])() # get rows up to n = 100
==> map *.list # flatten rows into a single list
==> grep * > 1_000_000 # filter elements exceeding 1e6
==> elems()
}, 4075, "Pascal's triangle";

# Problem 9
my @triplet-prods = gather {
sub triplets(\N) {
for 1..Int((1 - sqrt(0.5)) * N) -> \a {
my \u = N * (N - 2 * a);
my \v = 2 * (N - a);
for 1..Int((1 - sqrt(0.5)) * N) -> \a {
my \u = N * (N - 2 * a);
my \v = 2 * (N - a);

# check if b = u/v is an integer
# if so, we've found a triplet
if u %% v {
my \b = u div v;
my \c = N - a - b;
take $(a, b, c);
}
if u %% v {
my \b = u div v;
my \c = N - a - b;
take $(a, b, c);
}
}
}

Expand Down Expand Up @@ -70,11 +70,11 @@ BEGIN my %cache = 1 => 0;
multi factors($n where %cache{$n}:exists) { %cache{$n} }
multi factors($n) {
for 2, 3, *+2 ...^ * > sqrt($n) {
if $n %% $_ {
my $r = $n;
$r div= $_ while $r %% $_;
return %cache{$n} = 1 + factors($r);
}
if $n %% $_ {
my $r = $n;
$r div= $_ while $r %% $_;
return %cache{$n} = 1 + factors($r);
}
}
return %cache{$n} = 1;
}
Expand All @@ -84,8 +84,6 @@ constant $N = 3; # 4 in advent post - very expensive
my $i = 0;
my $result;

#?rakudo.jvm skip 'RT #122497'
#?rakudo.parrot skip 'RT #122497'
{
for 2..* {
$i = factors($_) == $N ?? $i + 1 !! 0;
Expand All @@ -104,8 +102,6 @@ my $result;

is +(2..100 X=> 2..100).classify({ .key ** .value }), 9183, 'distinct term count';

#?rakudo.jvm skip 'RT #122497'
#?rakudo.parrot skip 'RT #122497'
{
constant A = 100;
constant B = 100;
Expand Down
18 changes: 8 additions & 10 deletions integration/advent2012-day21.t
Expand Up @@ -8,19 +8,19 @@ use lib 't/spec/packages';
use Test::Util;

our $basic = q:to"END";
sub collatz-sequence(Int $start) {
sub collatz-sequence(Int $start) {
$start, { when * %% 2 { $_ / 2 }; when * !%% 2 { 3 * $_ + 1 }; } ... 1;
}
sub MAIN(Int $min, Int $max) {
say [max] ($min..$max).map({ +collatz-sequence($_) });
say [max] ($min..$max).map({ +collatz-sequence($_) });
}
END
is_run $basic, {out => '', err => /^'Usage:' .*? '<min> <max>'/}, 'main usage';

our $bench1 = q:to"END";
sub collatz-length(Int $start) {
sub collatz-length(Int $start) {
+($start, { when * %% 2 { $_ / 2 }; when * !%% 2 { 3 * $_ + 1 }; } ... 1);
}
END
Expand All @@ -29,14 +29,14 @@ our $bench2 = q:to"END";
sub collatz-length($start) {
given $start {
when 1 { 1 }
when * !%% 2 { 1 + collatz-length(3 * $_ + 1) }
when * %% 2 { 1 + collatz-length($_ / 2) }
when * !%% 2 { 1 + collatz-length(3 * $_ + 1) }
when * %% 2 { 1 + collatz-length($_ / 2) }
}
}
END

our $bench3 = q:to"END";
sub collatz-length(Int $start) {
sub collatz-length(Int $start) {
+($start, { $_ %% 2 ?? $_ div 2 !! 3 * $_ + 1 } ... 1);
}
END
Expand Down Expand Up @@ -94,7 +94,7 @@ END
my @numbers = 1..200, 10000..10200;
sub collatz-length(Int $start) {
sub collatz-length(Int $start) {
+($start, { when * %% 2 { $_ / 2 }; when * !%% 2 { 3 * $_ + 1 }; } ... 1);
}
my $expected-output = @numbers.map( -> $n {"$n: " ~ collatz-length($n)}).join("\n") ~ "\n";
Expand All @@ -114,8 +114,6 @@ is run-harness($bench2, 'recursive-ternary-hand-cached'), $expected-output, 'rec
is run-harness($bench3, 'sequence-ternary'), $expected-output, 'sequence-ternary';
is run-harness($bench4, 'loop'), $expected-output, 'loop';
is run-harness($bench5, 'recursive-ternary'), $expected-output, 'recursive-ternary';
#?rakudo.jvm skip 'RT #122497'
#?rakudo.parrot skip 'RT #122497'
is run-harness($bench6, 'hand-memoization'), $expected-output, 'hand-memoization';
is run-harness($bench7, 'gerdr'), $expected-output, 'gerdr';
is run-harness($bench8, 'kaz'), $expected-output, 'kaz';
Expand Down

0 comments on commit 317910a

Please sign in to comment.