extern crate nom;
extern crate rand;
use std::str::FromStr;
mod error;
pub use error::DiceFormatError;
mod dice;
pub use dice::DiceRoll;
mod parse;
pub fn roll(count: usize, dice: u8, bonus: i32) -> i32 {
DiceRoll { count, dice, bonus }.roll()
}
pub fn roll_from_str(cmd: &str) -> Result<i32, DiceFormatError> {
Ok(DiceRoll::from_str(cmd)?.roll())
}
#[test]
fn test_roll() {
for _ in 0..1000 {
let r = roll(1, 6, 0);
assert!(r >= 1);
assert!(r <= 6);
}
for _ in 0..1000 {
let r = roll(2, 6, 0);
assert!(r >= 1);
assert!(r <= 12);
}
for _ in 0..1000 {
let r = roll(1, 6, 6);
assert!(r >= 6);
assert!(r <= 12);
}
}
#[test]
fn test_roll_from_str() {
for _ in 0..1000 {
let r = roll_from_str("1d6").unwrap();
assert!(r >= 1);
assert!(r <= 6);
}
for _ in 0..1000 {
let r = roll_from_str("2d6").unwrap();
assert!(r >= 1);
assert!(r <= 12);
}
for _ in 0..1000 {
let r = roll_from_str("1d6+6").unwrap();
assert!(r >= 6);
assert!(r <= 12);
}
}