From 77157925fe1b5e9fbf8a55104cf8490c8ed1a6e2 Mon Sep 17 00:00:00 2001 From: Mark Date: Fri, 7 Apr 2023 09:42:50 -0700 Subject: [PATCH] Improved tests --- TODO.md | 5 +- src/main.rs | 209 +++++++++++++++++++++++++++++----------------------- 2 files changed, 117 insertions(+), 97 deletions(-) diff --git a/TODO.md b/TODO.md index ceedf49..128cc1a 100644 --- a/TODO.md +++ b/TODO.md @@ -12,10 +12,7 @@ Roadmap for fixes and features. ## General - CLI Options: version, help, evaluate - Compile to WASM, publish a webapp - - Better tests - - Direct expression printing - - Better comparison - - Trigonometry + - Trigonometry & function tests - Manpage diff --git a/src/main.rs b/src/main.rs index 9029a2c..a8b89b2 100644 --- a/src/main.rs +++ b/src/main.rs @@ -62,14 +62,12 @@ fn draw_greeter(stdout: &mut RawTerminal) -> Result<(), std::io return Ok(()); } - - fn main() -> Result<(), std::io::Error> { let mut stdout = stdout().into_raw_mode().unwrap(); draw_greeter(&mut stdout)?; - //let size = termion::terminal_size().unwrap(); + //let size = termion::terminal_size().unwrap(); //write!(stdout, "{:?}", size).unwrap(); let mut pb: PromptBuffer = PromptBuffer::new(64); @@ -158,7 +156,7 @@ fn main() -> Result<(), std::io::Error> { Key::Right => { pb.cursor_right(); }, Key::Up => { pb.hist_up(); }, Key::Down => { pb.hist_down(); }, - + Key::Ctrl('d') | Key::Ctrl('c') => { break 'outer; }, _ => {} @@ -173,17 +171,6 @@ fn main() -> Result<(), std::io::Error> { return Ok(()); } -// Tests to add: -// 3!+1 -// 3!3 -// 1e2 -// 1e-2 -// 1e0 -// 1e1.2 -// 1e(2) -// e2e -// 2 2e2 - #[cfg(test)] mod tests { // Many of these have been borrowed from insect. @@ -192,22 +179,28 @@ mod tests { use crate::tokens; use crate::quantity::Quantity; - fn good_expr(r: f64, s: &str) { - let s = String::from(s); - let g = parser::parse(&s).unwrap(); - let g = evaluate::evaluate(g).unwrap(); - let n = g.eval().unwrap(); - let tokens::Token::Number(v) = n else {panic!()}; + fn eval_to_str(s: &str) -> Result { + let g = match parser::parse(&String::from(s)) { + Ok(x) => x, + Err(_) => return Err(()) + }; + //let out_str = g.print(); - // TODO: better comparison - let r = Quantity::new_float(r); - let v = Quantity::Float{v: v.to_float()}; - assert_eq!(v, r); + return match evaluate::evaluate(g) { + Ok(x) => Ok(x.print()), + Err(_) => Err(()) + }; + } + + fn good_expr(r: &str, s: &str) { + let out = eval_to_str(s).unwrap(); + assert_eq!(r, out); } fn bad_expr(s: &str) { - let s = String::from(s); - match parser::parse(&s) { + let out = eval_to_str(s); + + match out { Err(_) => { return }, _ => {} }; @@ -217,25 +210,23 @@ mod tests { #[test] fn basic_numbers() { - /* - good_expr(1f64, "1"); - good_expr(1f64, "1.0"); - good_expr(1f64, "1.0000"); - //good_expr(1f64, "+1.0"); - //good_expr(1f64, "+1"); - good_expr(3.5f64, "3.5"); - good_expr(3.5f64, "3.50"); - //good_expr(3.5f64, "+3.50"); - good_expr(0.2f64, "0.2"); - //good_expr(0.2f64, "+0.2 "); - good_expr(0.2f64, ".2"); - //good_expr(0.2f64, "+.2"); - good_expr(-0.61f64, "-0.61"); - good_expr(-0.61f64, "-.61"); - good_expr(-0.61f64, "- .61"); - good_expr(0.05f64, ".05"); - good_expr(-123.45f64, "-123.45"); - */ + good_expr("1", "1"); + good_expr("1", "1.0"); + good_expr("1", "1.0000"); + good_expr("1", "+1.0"); + good_expr("1", "+1"); + good_expr("3.5", "3.5"); + good_expr("3.5", "3.50"); + good_expr("3.5", "+3.50"); + good_expr("0.2", "0.2"); + good_expr("0.2", "+0.2 "); + good_expr("0.2", ".2"); + good_expr("0.2", "+.2"); + good_expr("-0.61", "-0.61"); + good_expr("-0.61", "-.61"); + good_expr("-0.61", "- .61"); + good_expr("0.05", ".05"); + good_expr("-123.45", "-123.45"); bad_expr("123.."); bad_expr("0.."); @@ -247,19 +238,23 @@ mod tests { #[test] fn big_numbers() { - good_expr(1234567890000000f64, "1234567890000000"); - good_expr(1234567890000000f64, "1234567890000000.0"); - //good_expr(1234567890000000f64, "+1234567890000000.0"); + good_expr("1.2346e15", "1234567890000000"); + good_expr("1.2346e15", "1234567890000000.0"); + good_expr("1.2346e15", "+1234567890000000.0"); } - #[test] - fn negatives() { - good_expr(-5f64, "-5"); - good_expr(5f64, "--5"); - good_expr(-5f64, "---5"); - good_expr(5f64, "----5"); - good_expr(-5f64, "-----5"); + fn signs() { + good_expr( "5", "+++++5"); + good_expr( "5", "++++5"); + good_expr( "5", "+++5"); + good_expr( "5", "++5"); + good_expr( "5", "+5"); + good_expr("-5", "-5"); + good_expr( "5", "--5"); + good_expr("-5", "---5"); + good_expr( "5", "----5"); + good_expr("-5", "-----5"); } #[test] @@ -279,53 +274,81 @@ mod tests { #[test] fn implicit_multiply() { - good_expr(15f64, "5(3)"); - good_expr(15f64, "(5)3"); - good_expr(15f64, "(5)(3)"); + good_expr("15", "5(3)"); + good_expr("15", "(5)3"); + good_expr("15", "(5)(3)"); bad_expr("5 2"); } + + #[test] + fn scientific() { + good_expr("100", "1e2"); + good_expr("0.01", "1e-2"); + good_expr("1", "1e0"); + + // In these expressions, `e` is euler's number + // under implicit multiplication + good_expr("5.4366", "1e(2)"); + good_expr("14.778", "e2e"); + + bad_expr("2 2e2"); + bad_expr("1e1.2"); + } + + #[test] fn operators() { - //good_expr(125f64, "5^(+3)"); - //good_expr(125f64, "+5^3"); - //good_expr(f64, "3 ^ (-1.4)"); - //good_expr(f64, "3 ** (-1.4)"); - - //good_expr(f64, "2^3^4^5"); + good_expr("125", "5^(+3)"); + good_expr("125", "+5^3"); + good_expr("0.2148", "3 ^ (-1.4)"); + + // Should parse as ((2^3)^4)^5 + good_expr("1.1529e18", "2^3^4^5"); + + // Should parse as 1/(2pi) + good_expr("0.15915", "1/2pi"); + // Should parse as (1/2)*pi + good_expr("1.5708", "1/2*pi"); - good_expr(15f64, "5*3"); - good_expr(15f64, "5 * 3 "); - good_expr(15f64, "( 5 ) * ( 3 )"); - good_expr(15f64, "( 5 ) ( 3 )"); - good_expr(15f64, "( ( 5 ) * ( 3 ) )"); - good_expr(15f64, "( 5 * 3 )"); - //good_expr(15f64, "5(+3)"); - //good_expr(15f64, "+5*3"); - - good_expr(-15f64, "5*(-3)"); - good_expr(-15f64, "5 * (-3)"); - good_expr(-15f64, "( 5 ) * ( -3 )"); - good_expr(-15f64, "( ( 5 ) * (-( 3 )) )"); - good_expr(-15f64, "( 5 * (-3) )"); - //good_expr(-15f64, "+5*(-3)"); + good_expr("15", "5*3"); + good_expr("15", "5 * 3 "); + good_expr("15", "( 5 ) * ( 3 )"); + good_expr("15", "( 5 ) ( 3 )"); + good_expr("15", "( ( 5 ) * ( 3 ) )"); + good_expr("15", "((5)*(3"); + good_expr("15", "( 5 * 3 )"); + good_expr("15", "5(+3)"); + good_expr("15", "+5*3"); - good_expr(2f64, "6/3"); - good_expr(2f64, "5%3"); - good_expr(8f64, "5+3"); - good_expr(64f64, "4^3"); - good_expr(64f64, "4 ^ 3"); - good_expr(64f64, "4**3"); - good_expr(-81f64, "-3^4"); - good_expr(-81f64, "-(3^4)"); - good_expr(0.5f64, "2^-1"); - good_expr(0.25f64, "2^-2"); + good_expr("-15", "5*(-3)"); + good_expr("-15", "5 * (-3)"); + good_expr("-15", "( 5 ) * ( -3 )"); + good_expr("-15", "( ( 5 ) * (-( 3 )) )"); + good_expr("-15", "( 5 * (-3) )"); + good_expr("-15", "+5*(-3)"); - good_expr(2f64, "rt 4"); - good_expr(2f64, "sqrt 4"); - good_expr(6f64, "2 rt 9"); + good_expr("2", "6/3"); + good_expr("2", "5%3"); + good_expr("8", "5+3"); + good_expr("64", "4^3"); + good_expr("64", "4 ^ 3"); + good_expr("64", "4**3"); + good_expr("-81", "-3^4"); + good_expr("-81", "-(3^4)"); + good_expr("0.5", "2^-1"); + good_expr("0.25", "2^-2"); + + good_expr("2", "rt 4"); + good_expr("2", "sqrt 4"); + good_expr("6", "2 rt 9"); + + good_expr("7", "3!+1"); + good_expr("18", "3!3"); + bad_expr("3.1!"); + bad_expr("pi!"); } } \ No newline at end of file