Improved tests

pull/2/head
Mark 2023-04-07 09:42:50 -07:00
parent dd9633d822
commit 77157925fe
Signed by: Mark
GPG Key ID: AD62BB059C2AAEE4
2 changed files with 117 additions and 97 deletions

View File

@ -12,10 +12,7 @@ Roadmap for fixes and features.
## General ## General
- CLI Options: version, help, evaluate - CLI Options: version, help, evaluate
- Compile to WASM, publish a webapp - Compile to WASM, publish a webapp
- Better tests - Trigonometry & function tests
- Direct expression printing
- Better comparison
- Trigonometry
- Manpage - Manpage

View File

@ -62,14 +62,12 @@ fn draw_greeter(stdout: &mut RawTerminal<std::io::Stdout>) -> Result<(), std::io
return Ok(()); return Ok(());
} }
fn main() -> Result<(), std::io::Error> { fn main() -> Result<(), std::io::Error> {
let mut stdout = stdout().into_raw_mode().unwrap(); let mut stdout = stdout().into_raw_mode().unwrap();
draw_greeter(&mut stdout)?; draw_greeter(&mut stdout)?;
//let size = termion::terminal_size().unwrap(); //let size = termion::terminal_size().unwrap();
//write!(stdout, "{:?}", size).unwrap(); //write!(stdout, "{:?}", size).unwrap();
let mut pb: PromptBuffer = PromptBuffer::new(64); let mut pb: PromptBuffer = PromptBuffer::new(64);
@ -158,7 +156,7 @@ fn main() -> Result<(), std::io::Error> {
Key::Right => { pb.cursor_right(); }, Key::Right => { pb.cursor_right(); },
Key::Up => { pb.hist_up(); }, Key::Up => { pb.hist_up(); },
Key::Down => { pb.hist_down(); }, Key::Down => { pb.hist_down(); },
Key::Ctrl('d') | Key::Ctrl('d') |
Key::Ctrl('c') => { break 'outer; }, Key::Ctrl('c') => { break 'outer; },
_ => {} _ => {}
@ -173,17 +171,6 @@ fn main() -> Result<(), std::io::Error> {
return Ok(()); return Ok(());
} }
// Tests to add:
// 3!+1
// 3!3
// 1e2
// 1e-2
// 1e0
// 1e1.2
// 1e(2)
// e2e
// 2 2e2
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
// Many of these have been borrowed from insect. // Many of these have been borrowed from insect.
@ -192,22 +179,28 @@ mod tests {
use crate::tokens; use crate::tokens;
use crate::quantity::Quantity; use crate::quantity::Quantity;
fn good_expr(r: f64, s: &str) { fn eval_to_str(s: &str) -> Result<String, ()> {
let s = String::from(s); let g = match parser::parse(&String::from(s)) {
let g = parser::parse(&s).unwrap(); Ok(x) => x,
let g = evaluate::evaluate(g).unwrap(); Err(_) => return Err(())
let n = g.eval().unwrap(); };
let tokens::Token::Number(v) = n else {panic!()}; //let out_str = g.print();
// TODO: better comparison return match evaluate::evaluate(g) {
let r = Quantity::new_float(r); Ok(x) => Ok(x.print()),
let v = Quantity::Float{v: v.to_float()}; Err(_) => Err(())
assert_eq!(v, r); };
}
fn good_expr(r: &str, s: &str) {
let out = eval_to_str(s).unwrap();
assert_eq!(r, out);
} }
fn bad_expr(s: &str) { fn bad_expr(s: &str) {
let s = String::from(s); let out = eval_to_str(s);
match parser::parse(&s) {
match out {
Err(_) => { return }, Err(_) => { return },
_ => {} _ => {}
}; };
@ -217,25 +210,23 @@ mod tests {
#[test] #[test]
fn basic_numbers() { fn basic_numbers() {
/* good_expr("1", "1");
good_expr(1f64, "1"); good_expr("1", "1.0");
good_expr(1f64, "1.0"); good_expr("1", "1.0000");
good_expr(1f64, "1.0000"); good_expr("1", "+1.0");
//good_expr(1f64, "+1.0"); good_expr("1", "+1");
//good_expr(1f64, "+1"); good_expr("3.5", "3.5");
good_expr(3.5f64, "3.5"); good_expr("3.5", "3.50");
good_expr(3.5f64, "3.50"); good_expr("3.5", "+3.50");
//good_expr(3.5f64, "+3.50"); good_expr("0.2", "0.2");
good_expr(0.2f64, "0.2"); good_expr("0.2", "+0.2 ");
//good_expr(0.2f64, "+0.2 "); good_expr("0.2", ".2");
good_expr(0.2f64, ".2"); good_expr("0.2", "+.2");
//good_expr(0.2f64, "+.2"); good_expr("-0.61", "-0.61");
good_expr(-0.61f64, "-0.61"); good_expr("-0.61", "-.61");
good_expr(-0.61f64, "-.61"); good_expr("-0.61", "- .61");
good_expr(-0.61f64, "- .61"); good_expr("0.05", ".05");
good_expr(0.05f64, ".05"); good_expr("-123.45", "-123.45");
good_expr(-123.45f64, "-123.45");
*/
bad_expr("123.."); bad_expr("123..");
bad_expr("0.."); bad_expr("0..");
@ -247,19 +238,23 @@ mod tests {
#[test] #[test]
fn big_numbers() { fn big_numbers() {
good_expr(1234567890000000f64, "1234567890000000"); good_expr("1.2346e15", "1234567890000000");
good_expr(1234567890000000f64, "1234567890000000.0"); good_expr("1.2346e15", "1234567890000000.0");
//good_expr(1234567890000000f64, "+1234567890000000.0"); good_expr("1.2346e15", "+1234567890000000.0");
} }
#[test] #[test]
fn negatives() { fn signs() {
good_expr(-5f64, "-5"); good_expr( "5", "+++++5");
good_expr(5f64, "--5"); good_expr( "5", "++++5");
good_expr(-5f64, "---5"); good_expr( "5", "+++5");
good_expr(5f64, "----5"); good_expr( "5", "++5");
good_expr(-5f64, "-----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] #[test]
@ -279,53 +274,81 @@ mod tests {
#[test] #[test]
fn implicit_multiply() { fn implicit_multiply() {
good_expr(15f64, "5(3)"); good_expr("15", "5(3)");
good_expr(15f64, "(5)3"); good_expr("15", "(5)3");
good_expr(15f64, "(5)(3)"); good_expr("15", "(5)(3)");
bad_expr("5 2"); 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] #[test]
fn operators() { fn operators() {
//good_expr(125f64, "5^(+3)"); good_expr("125", "5^(+3)");
//good_expr(125f64, "+5^3"); good_expr("125", "+5^3");
//good_expr(f64, "3 ^ (-1.4)"); good_expr("0.2148", "3 ^ (-1.4)");
//good_expr(f64, "3 ** (-1.4)");
// Should parse as ((2^3)^4)^5
//good_expr(f64, "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("15", "5*3");
good_expr(15f64, "5 * 3 "); good_expr("15", "5 * 3 ");
good_expr(15f64, "( 5 ) * ( 3 )"); good_expr("15", "( 5 ) * ( 3 )");
good_expr(15f64, "( 5 ) ( 3 )"); good_expr("15", "( 5 ) ( 3 )");
good_expr(15f64, "( ( 5 ) * ( 3 ) )"); good_expr("15", "( ( 5 ) * ( 3 ) )");
good_expr(15f64, "( 5 * 3 )"); good_expr("15", "((5)*(3");
//good_expr(15f64, "5(+3)"); good_expr("15", "( 5 * 3 )");
//good_expr(15f64, "+5*3"); good_expr("15", "5(+3)");
good_expr("15", "+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("-15", "5*(-3)");
good_expr(2f64, "5%3"); good_expr("-15", "5 * (-3)");
good_expr(8f64, "5+3"); good_expr("-15", "( 5 ) * ( -3 )");
good_expr(64f64, "4^3"); good_expr("-15", "( ( 5 ) * (-( 3 )) )");
good_expr(64f64, "4 ^ 3"); good_expr("-15", "( 5 * (-3) )");
good_expr(64f64, "4**3"); good_expr("-15", "+5*(-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(2f64, "rt 4"); good_expr("2", "6/3");
good_expr(2f64, "sqrt 4"); good_expr("2", "5%3");
good_expr(6f64, "2 rt 9"); 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!");
} }
} }