Last active
August 29, 2015 14:10
-
-
Save olix0r/858d41f06f3bd7a945de to your computer and use it in GitHub Desktop.
babby's breakfast program
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// Food, food, food. I love food. | |
pub mod food { | |
use std::rand::{Rand, Rng}; | |
/// All food has calories | |
pub trait Food { | |
fn calories(&self) -> u64; | |
} | |
/// There are some things you can eat for breakfast. | |
pub enum Breakfast { | |
Eggs, | |
Granola, | |
None | |
} | |
/// Sometimes you don't know what you want for breakfast... | |
impl Rand for Breakfast { | |
fn rand<R: Rng>(rng: &mut R) -> Breakfast { | |
match rng.gen_range(0u, 3u) { | |
0u => Eggs, | |
1u => Granola, | |
_ => None | |
} | |
} | |
} | |
/// Breakfast is food, too. | |
impl Food for Breakfast { | |
fn calories(&self) -> u64 { | |
match *self { | |
Eggs => 160, | |
Granola => 140, | |
None => 0 | |
} | |
} | |
} | |
#[cfg(test)] | |
mod test { | |
use super::{Breakfast, Granola, Eggs, None, Food}; | |
use std::rand; | |
use std::rand::Rng; | |
#[test] | |
fn breakfast_calories() { | |
assert!(Eggs.calories() > Granola.calories()); | |
assert!(Eggs.calories() > None.calories()); | |
assert!(Granola.calories() > None.calories()); | |
} | |
#[test] | |
fn random_breakfast() { | |
let mut rng = rand::task_rng(); | |
let breakfast: Breakfast = rng.gen(); | |
breakfast.calories(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment