From fd0ddb72aba9ed4b231ba3d50de90528b4cbe025 Mon Sep 17 00:00:00 2001 From: Mark Date: Mon, 27 Mar 2023 10:54:36 -0700 Subject: [PATCH] Added tests --- src/main.rs | 166 +++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 164 insertions(+), 2 deletions(-) diff --git a/src/main.rs b/src/main.rs index 4008eae..b7498c1 100644 --- a/src/main.rs +++ b/src/main.rs @@ -7,9 +7,9 @@ use termion::raw::RawTerminal; use termion::{color, style}; pub mod tokens; -mod parser; +pub mod parser; mod promptbuffer; -mod evaluate; +pub mod evaluate; use crate::tokens::Token; use crate::promptbuffer::PromptBuffer; @@ -194,4 +194,166 @@ fn main() -> Result<(), std::io::Error> { write!(stdout, "\r\n")?; return Ok(()); +} + + + + + + +#[cfg(test)] +mod tests { + // Many of these have been borrowed from insect. + use crate::parser; + use crate::evaluate; + use crate::tokens; + + 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(); + let tokens::Token::Number(_, v) = n else {panic!()}; + assert_eq!(v, r); + } + + fn bad_expr(s: &str) { + let s = String::from(s); + match parser::parse(&s) { + Err(_) => { return }, + _ => {} + }; + + panic!() + } + + #[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"); + + bad_expr("123.."); + bad_expr("0.."); + bad_expr(".0."); + bad_expr("."); + bad_expr(". 2"); + bad_expr("..2"); + } + + #[test] + fn big_numbers() { + good_expr(1234567890000000f64, "1234567890000000"); + good_expr(1234567890000000f64, "1234567890000000.0"); + //good_expr(1234567890000000f64, "+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"); + } + + #[test] + fn bad_expressions() { + bad_expr("2^"); + bad_expr("^2"); + bad_expr("5*"); + bad_expr("5/"); + bad_expr("5%"); + bad_expr("%2"); + bad_expr("3 + "); + bad_expr("3 + @"); + bad_expr("3 - "); + } + + #[test] + fn operators() { + good_expr(125f64, "5^3"); + good_expr(125f64, "5 ^ 3"); + good_expr(125f64, "( 5 ) ^ ( 3 )"); + good_expr(125f64, "( ( 5 ) ^ ( 3 ) )"); + good_expr(125f64, "( 5 ^ 3 )"); + //good_expr(125f64, "5^(+3)"); + //good_expr(125f64, "+5^3"); + + good_expr(64f64, "4^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(-81f64, "-3**4"); + //good_expr(-81f64, "-3 ** 4"); + good_expr(-81f64, "-(3^4)"); + + //good_expr(f64, "3 ^ (-1.4)"); + //good_expr(f64, "3 ** (-1.4)"); + + //good_expr(f64, "2^3^4^5"); + good_expr(0.5f64, "2^-1"); + good_expr(0.25f64, "2^-2"); + + + 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(-15f64, "( 5 * (-3) )"); + //good_expr(-15f64, "+5*(-3)"); + + good_expr(2f64, "6/3"); + good_expr(2f64, "6 / 3"); + good_expr(2f64, "( 6 ) / ( 3 )"); + good_expr(2f64, "( ( 6 ) / ( 3 ) )"); + good_expr(2f64, "( 6 / 3 )"); + + good_expr(2f64, "5%3"); + good_expr(2f64, "5 % 3"); + good_expr(2f64, "( 5 ) % ( 3 )"); + good_expr(2f64, "( ( 5 ) % ( 3 ) )"); + good_expr(2f64, "( 5 % 3 )"); + + good_expr(8f64, "5+3"); + good_expr(8f64, "5 + 3"); + good_expr(8f64, "( 5 ) + ( 3 )"); + good_expr(8f64, "( ( 5 ) + ( 3 ) )"); + good_expr(8f64, "( 5 + 3 )"); + + good_expr(2f64, "5-3"); + good_expr(2f64, "5 - 3"); + good_expr(2f64, "( 5 ) - ( 3 )"); + good_expr(2f64, "( ( 5 ) - ( 3 ) )"); + good_expr(2f64, "( 5 - 3 )"); + } } \ No newline at end of file