Add test, fixes#5

This commit is contained in:
Alexandre Stein 2022-11-22 21:44:55 +01:00
parent a033d3b647
commit 3781029019
1 changed files with 28 additions and 0 deletions

View File

@ -322,3 +322,31 @@ fn main() {
let res = calculation(&symbols, &numbers);
println!("{res}");
}
#[cfg(test)]
mod tests {
use crate::{calculation, parse};
#[test]
fn basics() {
let (s, n) = parse("2+2".to_string());
assert_eq!(calculation(&s, &n), 4.0);
let (s, n) = parse("4-9".to_string());
assert_eq!(calculation(&s, &n), -5.0);
let (s, n) = parse("7*3".to_string());
assert_eq!(calculation(&s, &n), 21.0);
let (s, n) = parse("9/3".to_string());
assert_eq!(calculation(&s, &n), 3.0);
let (s, n) = parse("15*20".to_string());
assert_ne!(calculation(&s, &n), 0.0);
}
#[test]
fn signs_2() {
let (s, n) = parse("2+2*4".to_string());
assert_eq!(calculation(&s, &n), 10.0);
let (s, n) = parse("30/4-9".to_string());
assert_eq!(calculation(&s, &n), -1.5);
}
}