in reply to Re: Testing Random Code
in thread Testing Random Code

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.

Replies are listed 'Best First'.
Re^3: Testing Random Code
by imp (Priest) on Sep 19, 2006 at 11:52 UTC
    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.
        Use strings instead of arrays and hashes:
        my $counterexamples = join "\n", map( pin(), 1 .. 10000 ), ''; my $digits = "\0" x 10; substr $digits, $_, 1, $_ for $counterexamples =~ /\d/g; ( $digits ^= '0123456789' ) =~ tr/\0//d; $counterexamples =~ s/^\d{4}\n//gm;