From ec76e7b082e86bd546632a331874e4b89eff21c6 Mon Sep 17 00:00:00 2001 From: NerdyPepper Date: Thu, 28 Mar 2019 22:37:43 +0530 Subject: add unit tests --- src/main.rs | 32 +++++++++++++++++++++++++++++--- 1 file changed, 29 insertions(+), 3 deletions(-) (limited to 'src') diff --git a/src/main.rs b/src/main.rs index 37b8d04..4cde172 100644 --- a/src/main.rs +++ b/src/main.rs @@ -25,7 +25,7 @@ fn main() { let evaled = eval_math_expression(&expr[..]); match evaled { Ok(ans) => println!("{}", ans), - Err(e) => handler(e), + Err(e) => eprintln!("{}", handler(e)), }; } else { let config_builder = Builder::new(); @@ -47,7 +47,7 @@ fn main() { let evaled = eval_math_expression(&line[..]); match evaled { Ok(ans) => println!("{}", ans), - Err(e) => handler(e), + Err(e) => println!("{}", handler(e)), }; }, Err(ReadlineError::Interrupted) => { @@ -96,6 +96,32 @@ fn eval_math_expression(input: &str) -> Result { let lexed = lexer(&input[..])?; let postfixed = to_postfix(lexed)?; let evaled = eval_postfix(postfixed)?; - Ok(evaled) + Ok(format!("{:.*}", 5, evaled).parse::().unwrap()) } + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn basic_ops() { + let evaled = eval_math_expression("6*2 + 3 + 12 -3").unwrap(); + assert_eq!(24., evaled); + } + #[test] + fn trignometric_fns() { + let evaled = eval_math_expression("sin(30) + tan(45").unwrap(); + assert_eq!(1.5, evaled); + } + #[test] + fn brackets() { + let evaled = eval_math_expression("(((1 + 2 + 3) ^ 2 ) - 4)").unwrap(); + assert_eq!(32., evaled); + } + #[test] + fn floating_ops() { + let evaled = eval_math_expression("1.2816 + 1 + 1.2816/1.2").unwrap(); + assert_eq!(3.3496, evaled); + } +} -- cgit v1.2.3