Array-Find

 view release on metacpan or  search on metacpan

t/01-basics.t  view on Meta::CPAN

               arrays=>[
                   [qw/a/], [qw/b a/], [qw/a c a/],
               ]},
    result => [qw/a a a a/],
);
test_find(
    name   => 'multi items',
    args   => {items=>[qw/a b/],
               array=>[qw/b a c a/]},
    result => [qw/a a b/],
);
test_find(
    name   => 'multi arrays + multi items',
    args   => {items=>[qw/a b/],
               arrays=>[
                   [qw/a/], [qw/b a/], [qw/a c a/],
               ]},
    result => [qw/a a a a b/],
);

test_find(
    name   => 'handling undef in array',
    args   => {item=>"", array=>["", "a", undef]},
    result => [""],
);
test_find(
    name   => 'handling undef in item',
    args   => {items=>[undef], array=>["", "a", undef]},
    result => [undef],
);

done_testing();

sub test_find {
    my %args = @_;
    my $name = $args{name};
    my $find_args = $args{args};

    subtest $name => sub {
        my $res = find_in_array(%$find_args);
        if ($args{result}) {
            is_deeply($res, $args{result}, "result") or diag(explain($res));
        }
        if ($args{result_shuffled}) {
            die "Can't test shuffle if result < 2 items" if @$res < 2;
            # repeat so statistically guaranteed to succeed
            my $num_repeat = int(20/@$res);
            $num_repeat    = 5 if $num_repeat < 5;
            my $seen_shuffled;
          R:
            for (1..$num_repeat) {
                my $res2 = find_in_array(%$find_args);
                for (0..@$res2-1) {
                    if ($res->[$_] ne $res2->[$_]) {
                        $seen_shuffled++;
                        last R;
                    }
                }
            }
            ok($seen_shuffled, "result is shuffled") or
                diag("not seeing result shuffled after $num_repeat iterations");
        }
    };
}



( run in 1.768 second using v1.01-cache-2.11-cpan-96521ef73a4 )