These are my unit tests (in Rust)
#[test]
fn test_plain_win() {
let mut cash = 100;
play(&mut cash, &"32 PLAIN 32".to_owned());
assert_eq!(cash, 975);
}
#[test]
fn test_plain_lose() {
let mut cash = 100;
play(&mut cash, &"32 PLAIN 15".to_owned());
assert_eq!(cash, 75);
}
#[test]
fn test_even_win() {
let mut cash = 100;
play(&mut cash, &"32 EVEN".to_owned());
assert_eq!(cash, 125);
}
#[test]
fn test_even_lose() {
let mut cash = 100;
play(&mut cash, &"15 EVEN".to_owned());
assert_eq!(cash, 75);
play(&mut cash, &"0 EVEN".to_owned());
assert_eq!(cash, 56);
}
#[test]
fn test_odd_win() {
let mut cash = 100;
play(&mut cash, &"31 ODD".to_owned());
assert_eq!(cash, 125);
play(&mut cash, &"0 ODD".to_owned());
assert_eq!(cash, 157);
}
#[test]
fn test_odd_lose() {
let mut cash = 100;
play(&mut cash, &"32 ODD".to_owned());
assert_eq!(cash, 75);
}
#[test]
fn test_0_cash() {
let mut cash = 0;
play(&mut cash, &"32 ODD".to_owned());
assert_eq!(cash, 0);
play(&mut cash, &"15 ODD".to_owned());
assert_eq!(cash, 0);
play(&mut cash, &"32 EVEN".to_owned());
assert_eq!(cash, 0);
play(&mut cash, &"15 EVEN".to_owned());
assert_eq!(cash, 0);
play(&mut cash, &"32 PLAIN 15".to_owned());
assert_eq!(cash, 0);
play(&mut cash, &"32 PLAIN 32".to_owned());
assert_eq!(cash, 0);
}
#[test]
fn test_1_cash() {
let mut cash = 1;
play(&mut cash, &"15 ODD".to_owned());
assert_eq!(cash, 2);
play(&mut cash, &"32 ODD".to_owned());
assert_eq!(cash, 1);
play(&mut cash, &"32 EVEN".to_owned());
assert_eq!(cash, 2);
play(&mut cash, &"15 EVEN".to_owned());
assert_eq!(cash, 1);
play(&mut cash, &"32 PLAIN 32".to_owned());
assert_eq!(cash, 36);
play(&mut cash, &"32 PLAIN 15".to_owned());
assert_eq!(cash, 27);
}
#[test]
fn test_all_even() {
for ball in 0..37 {
let mut cash = 100;
play(&mut cash, &format!("{} EVEN", ball));
if ball % 2 == 1 || ball == 0 {
assert!(cash == 75, "unexpected lose: {} EVEN", ball);
} else {
assert!(cash == 125, "unexpected win: {} EVEN", ball);
}
}
}
#[test]
fn test_all_odd() {
for ball in 0..37 {
let mut cash = 100;
play(&mut cash, &format!("{} ODD", ball));
if ball % 2 == 1 || ball == 0 {
assert!(cash == 125, "unexpected win: {} ODD", ball);
} else {
assert!(cash == 75, "unexpected lose: {} ODD", ball);
}
}
}
#[test]
fn test_all_plain() {
for target in 0..37 {
for ball in 0..37 {
let mut cash = 100;
play(&mut cash, &format!("{} PLAIN {}", ball, target));
if ball == target {
assert!(cash == 975, "unexpected win: {} PLAIN {}", ball, target);
} else {
assert!(cash == 75, "unexpected lose: {} PLAIN {}", ball, target);
}
}
}
}
#[test]
fn test_lose_for_0_to_1_000_000() {
for bank in 0..1_000_001 {
let mut cash = bank;
play(&mut cash, &"32 ODD".to_owned());
assert!(cash == (bank - ((bank as f64)/4.).ceil() as usize), "failed for bank={}", bank);
}
}
#[test]
fn test_win_for_0_to_1_000_000() {
for bank in 0..1_000_001 {
let mut cash = bank;
play(&mut cash, &"32 EVEN".to_owned());
assert!(cash == (bank + ((bank as f64)/4.).ceil() as usize), "failed for bank={}", bank);
}
}
And they all pass.