in reply to Testing Random Code

I prefer concise, easily read test cases:
use Test::More qw(no_plan); my @items = map {pin()} 1..1000; my @bad = grep { ! /\A\d{4}\z/ } @items; is_deeply(\@bad,[],"All pins are 4 digits");
BTW - your test case would allow newlines after the digits.

Replies are listed 'Best First'.
Re^2: Testing Random Code
by Ovid (Cardinal) on Sep 19, 2006 at 11:19 UTC

    Oops. You're right. Fixed now.

    Note that your test doesn't catch the main bug I'm dealing with, namely, that all four digits are used.

    Cheers,
    Ovid

    New address of my CGI Course.

      Aye, I was running late for work and indulging myself with a little time on perlmonks.

      Your approach to testing for digit usage seems ok to me, as the more condensed ways of doing it might be harder for a maintainer to follow. I would probably do this:

      my %unused = map {$_=>1} (0..9); for my $item (@items) { last unless %unused; delete $unused{$_} for split '', $item; } if (%unused) { fail(sprintf "Unused digits: %s", join(',',keys %unused)); } else { ok(1,"All digits used"); }
      My only issue with the original test was that with more tests being added you need to accumulate more stats as you go, which adds complexity to the test script. I prefer to keep tests in neat little compartments when possible.