diff options
author | NerdyPepper <[email protected]> | 2019-03-28 17:07:43 +0000 |
---|---|---|
committer | NerdyPepper <[email protected]> | 2019-03-28 17:07:43 +0000 |
commit | ec76e7b082e86bd546632a331874e4b89eff21c6 (patch) | |
tree | 811007bd6057c82c794e57e967d45683caf1f8ba | |
parent | bd20d7b591f9bfedf01403550c74a32268dc9984 (diff) |
add unit tests
-rw-r--r-- | src/main.rs | 32 |
1 files changed, 29 insertions, 3 deletions
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() { | |||
25 | let evaled = eval_math_expression(&expr[..]); | 25 | let evaled = eval_math_expression(&expr[..]); |
26 | match evaled { | 26 | match evaled { |
27 | Ok(ans) => println!("{}", ans), | 27 | Ok(ans) => println!("{}", ans), |
28 | Err(e) => handler(e), | 28 | Err(e) => eprintln!("{}", handler(e)), |
29 | }; | 29 | }; |
30 | } else { | 30 | } else { |
31 | let config_builder = Builder::new(); | 31 | let config_builder = Builder::new(); |
@@ -47,7 +47,7 @@ fn main() { | |||
47 | let evaled = eval_math_expression(&line[..]); | 47 | let evaled = eval_math_expression(&line[..]); |
48 | match evaled { | 48 | match evaled { |
49 | Ok(ans) => println!("{}", ans), | 49 | Ok(ans) => println!("{}", ans), |
50 | Err(e) => handler(e), | 50 | Err(e) => println!("{}", handler(e)), |
51 | }; | 51 | }; |
52 | }, | 52 | }, |
53 | Err(ReadlineError::Interrupted) => { | 53 | Err(ReadlineError::Interrupted) => { |
@@ -96,6 +96,32 @@ fn eval_math_expression(input: &str) -> Result<f64, CalcError> { | |||
96 | let lexed = lexer(&input[..])?; | 96 | let lexed = lexer(&input[..])?; |
97 | let postfixed = to_postfix(lexed)?; | 97 | let postfixed = to_postfix(lexed)?; |
98 | let evaled = eval_postfix(postfixed)?; | 98 | let evaled = eval_postfix(postfixed)?; |
99 | Ok(evaled) | 99 | Ok(format!("{:.*}", 5, evaled).parse::<f64>().unwrap()) |
100 | } | 100 | } |
101 | 101 | ||
102 | |||
103 | #[cfg(test)] | ||
104 | mod tests { | ||
105 | use super::*; | ||
106 | |||
107 | #[test] | ||
108 | fn basic_ops() { | ||
109 | let evaled = eval_math_expression("6*2 + 3 + 12 -3").unwrap(); | ||
110 | assert_eq!(24., evaled); | ||
111 | } | ||
112 | #[test] | ||
113 | fn trignometric_fns() { | ||
114 | let evaled = eval_math_expression("sin(30) + tan(45").unwrap(); | ||
115 | assert_eq!(1.5, evaled); | ||
116 | } | ||
117 | #[test] | ||
118 | fn brackets() { | ||
119 | let evaled = eval_math_expression("(((1 + 2 + 3) ^ 2 ) - 4)").unwrap(); | ||
120 | assert_eq!(32., evaled); | ||
121 | } | ||
122 | #[test] | ||
123 | fn floating_ops() { | ||
124 | let evaled = eval_math_expression("1.2816 + 1 + 1.2816/1.2").unwrap(); | ||
125 | assert_eq!(3.3496, evaled); | ||
126 | } | ||
127 | } | ||