Created
November 14, 2016 14:23
-
-
Save zoffixznet/30068ed81798732f0118840ae06ac43f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use MONKEY-TYPING; | |
use nqp; | |
augment class Junction { | |
multi method defined(Junction:D:) { | |
nqp::stmts( | |
(my int $elems = nqp::elems($!storage)), | |
(my int $i = 0), | |
nqp::if( | |
nqp::iseq_s($!type,'any'), | |
nqp::stmts( | |
nqp::while( | |
(nqp::islt_i($i,$elems) | |
&& nqp::if(nqp::isconcrete(nqp::atpos($!storage,$i)),0,1)), | |
($i = nqp::add_i($i,1)) | |
), | |
nqp::p6bool(nqp::islt_i($i,$elems)) | |
), | |
nqp::if( | |
nqp::iseq_s($!type,'all'), | |
nqp::stmts( | |
nqp::while( | |
(nqp::islt_i($i,$elems) | |
&& nqp::isconcrete(nqp::atpos($!storage,$i))), | |
($i = nqp::add_i($i,1)) | |
), | |
nqp::p6bool(nqp::iseq_i($i,$elems)) | |
), | |
nqp::if( | |
nqp::iseq_s($!type,'none'), | |
nqp::stmts( | |
nqp::while( | |
(nqp::islt_i($i,$elems) | |
&& nqp::if(nqp::isconcrete(nqp::atpos($!storage,$i)),0,1)), | |
($i = nqp::add_i($i,1)) | |
), | |
nqp::p6bool(nqp::iseq_i($i,$elems)) | |
), | |
nqp::stmts( # $!type eq 'one' | |
(my int $seen = 0), | |
($i = nqp::sub_i($i,1)), # increment in condition | |
nqp::while( | |
(nqp::islt_i(($i = nqp::add_i($i,1)),$elems) | |
&& nqp::isle_i($seen,1)), | |
nqp::if( | |
nqp::isconcrete(nqp::atpos($!storage,$i)), | |
($seen = nqp::add_i($seen,1)) | |
) | |
), | |
nqp::p6bool(nqp::iseq_i($seen,1)) | |
) | |
) | |
) | |
) | |
) | |
} | |
} | |
use Test; | |
plan 1; | |
# RT#130099 | |
subtest 'defined with Junctions autothreads' => { | |
plan 2; | |
subtest 'defined() as a sub' => { | |
plan 10; | |
is-deeply defined(any Str, 42), Bool::True, 'any() -> True'; | |
is-deeply defined(any Str, Int), Bool::False, 'any() -> False'; | |
is-deeply defined(none Str, Int), Bool::True, 'none() -> True'; | |
is-deeply defined(none Str, 42), Bool::False, 'none() -> False'; | |
is-deeply defined(all "x", 42), Bool::True, 'all() -> True'; | |
is-deeply defined(all Str, Int), Bool::False, 'all() -> False (1)'; | |
is-deeply defined(all Str, 42), Bool::False, 'all() -> False (1)'; | |
is-deeply defined(one Str, 42), Bool::True, 'one() -> True'; | |
is-deeply defined(one Str, Int), Bool::False, 'one() -> False (1)'; | |
is-deeply defined(one "x", 42), Bool::False, 'one() -> False (2)'; | |
} | |
subtest '.defined() as a method' => { | |
plan 10; | |
is-deeply any( Str, 42).defined, Bool::True, 'any() -> True'; | |
is-deeply any( Str, Int).defined, Bool::False, 'any() -> False'; | |
is-deeply none(Str, Int).defined, Bool::True, 'none() -> True'; | |
is-deeply none(Str, 42).defined, Bool::False, 'none() -> False'; | |
is-deeply all( "x", 42).defined, Bool::True, 'all() -> True'; | |
is-deeply all( Str, Int).defined, Bool::False, 'all() -> False (1)'; | |
is-deeply all( Str, 42).defined, Bool::False, 'all() -> False (1)'; | |
is-deeply one( Str, 42).defined, Bool::True, 'one() -> True'; | |
is-deeply one( Str, Int).defined, Bool::False, 'one() -> False (1)'; | |
is-deeply one( "x", 42).defined, Bool::False, 'one() -> False (2)'; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment