Skip to content

Instantly share code, notes, and snippets.

@zdk
Created October 26, 2011 03:33
Show Gist options
  • Save zdk/1315350 to your computer and use it in GitHub Desktop.
Save zdk/1315350 to your computer and use it in GitHub Desktop.
FizzBuzz
#FizzBuzz.pm
#!/usr/bin/env perl
package Fizzbuzz;
sub execute {
my $x = shift;
my $m3 = ( $x % 3 ) == 0;
my $m5 = ( $x % 5 ) == 0;
return 'FizzBuzz' if ( $m3 and $m5 );
return 'Fizz' if ( $m3 );
return 'Buzz' if ( $m5 );
return $x;
}
1;
#fizzbuzz_test.t
use Test::More tests => 101;
require Fizzbuzz;
ok(1, "just checking before starting");
my @numbers = (1..100);
foreach my $n (@numbers) {
if ( ($n % 3 == 0) and ($n % 5 == 0) ) {
is( Fizzbuzz::execute($n), 'FizzBuzz', "When it's dividable by 3 and 5, it should give FizzBuzz" );
} elsif ( $n % 3 == 0 ) {
is( Fizzbuzz::execute($n), 'Fizz', "When it's dividable by 3, it should give Fizz" );
} elsif ( $n % 5 == 0 ) {
is( Fizzbuzz::execute($n), 'Buzz', "When it's diviable by 5, it should give Buzz" );
} else {
is( Fizzbuzz::execute($n), $n, "Otherwise give the number back" );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment