Skip to content

Instantly share code, notes, and snippets.

@steveklabnik
Created December 23, 2012 06:32
Show Gist options
  • Save steveklabnik/4362308 to your computer and use it in GitHub Desktop.
Save steveklabnik/4362308 to your computer and use it in GitHub Desktop.
extern mod std;
fn is_three(num: int) -> bool {
num % 3 == 0
}
#[test]
fn test_is_three_with_not_three() {
if is_three(1) {
fail ~"One is not three";
}
}
#[test]
fn test_is_three_with_three() {
if !is_three(3) {
fail ~"Three should be three";
}
}
fn is_five(num: int) -> bool {
num % 5 == 0
}
#[test]
fn test_is_five_with_not_five() {
if is_five(1) {
fail ~"One is not five";
}
}
#[test]
fn test_is_five_with_five() {
if !is_five(5) {
fail ~"Five should be five";
}
}
fn is_fifteen(num: int) -> bool {
num % 15 == 0
}
#[test]
fn test_is_fifteen_with_not_fifteen() {
if is_fifteen(1) {
fail ~"One is not fifteen";
}
}
#[test]
fn test_is_fifteen_with_fifteen() {
if !is_fifteen(15) {
fail ~"Fifteen should be fifteen";
}
}
fn main() {
for int::range(0, 100) |num| {
io::println(
if is_fifteen(num) { ~"FizzBuzz" }
else if is_three(num) { ~"Fizz" }
else if is_five(num) { ~"Buzz" }
else { int::str(num) }
);
}
}
extern mod std;
fn is_three(num: int) -> bool {
num % 3 == 0
}
#[test]
fn test_is_three_with_not_three() {
if is_three(1) {
fail ~"One is not three";
}
}
#[test]
fn test_is_three_with_three() {
if !is_three(3) {
fail ~"Three should be three";
}
}
fn is_five(num: int) -> bool {
num % 5 == 0
}
#[test]
fn test_is_five_with_not_five() {
if is_five(1) {
fail ~"One is not five";
}
}
#[test]
fn test_is_five_with_five() {
if !is_five(5) {
fail ~"Five should be five";
}
}
fn is_fifteen(num: int) -> bool {
num % 15 == 0
}
#[test]
fn test_is_fifteen_with_not_fifteen() {
if is_fifteen(1) {
fail ~"One is not fifteen";
}
}
#[test]
fn test_is_fifteen_with_fifteen() {
if !is_fifteen(15) {
fail ~"Fifteen should be fifteen";
}
}
fn main() {
for int::range(0, 100) |num| {
let answer =
if is_fifteen(num){
~"FizzBuzz"
}
else if is_three(num) {
~"Fizz"
}
else if is_five(num) {
~"Buzz"
}
else {
int::str(num)
};
io::println(answer)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment