in reply to John Guttag's book - 2nd exercise. My attempt in Perl.
G'day pritesh_ugrankar,
Here's one way to do it:
#!/usr/bin/env perl use strict; use warnings; use constant { INPUT => 0, EXPECT => 1, NO_ODDS => 'no odds found' }; use Test::More; my @tests = ( [[qw{-11 -13 4}], -11], [[qw{-12 -14 4}], NO_ODDS], [[qw{0 0 0}], NO_ODDS], [[qw{1 1 1}], 1], [[qw{1 0 1}], 1], [[qw{0 0 1}], 1], [[qw{2 3 2}], 3], [[qw{2 -3 2}], -3], [[qw{-2 -3 -4}], -3], [[qw{-1 -3 -5}], -1], [[qw{3 5 7}], 7], [[qw{7 5 3}], 7], [[qw{5 7 3}], 7], ); plan tests => scalar @tests; for my $test (@tests) { my ($x, $y, $z) = @{$test->[INPUT]}; my $u = ($x, $y)[$x % 2 ? ($y % 2 ? $x < $y : 0) : 1]; my $v = ($u, $z)[$u % 2 ? ($z % 2 ? $u < $z : 0) : 1]; ok((NO_ODDS, $v)[$v % 2] eq $test->[EXPECT]); }
See Test::More if you're not familiar with that module. It's very useful in situations like this where you want to test a solution against a variety of input data.
All those tests passed:
1..13 ok 1 ... 2 to 12 all 'ok' also ... ok 13
As you can hopefully see, it's very easy to add more test data by simply adding more elements to @tests.
— Ken
|
|---|