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

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.

Replies are listed 'Best First'.
Re^4: Testing Random Code
by Ovid (Cardinal) on Sep 19, 2006 at 12:20 UTC
      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;