Added tests

pull/2/head
Mark 2023-03-27 10:54:36 -07:00
parent 1794042514
commit fd0ddb72ab
Signed by: Mark
GPG Key ID: AD62BB059C2AAEE4
1 changed files with 164 additions and 2 deletions

View File

@ -7,9 +7,9 @@ use termion::raw::RawTerminal;
use termion::{color, style}; use termion::{color, style};
pub mod tokens; pub mod tokens;
mod parser; pub mod parser;
mod promptbuffer; mod promptbuffer;
mod evaluate; pub mod evaluate;
use crate::tokens::Token; use crate::tokens::Token;
use crate::promptbuffer::PromptBuffer; use crate::promptbuffer::PromptBuffer;
@ -195,3 +195,165 @@ fn main() -> Result<(), std::io::Error> {
write!(stdout, "\r\n")?; write!(stdout, "\r\n")?;
return Ok(()); 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 )");
}
}