From 4e2fe9eb3be93caa7b10c454e5f431f9e3370abb Mon Sep 17 00:00:00 2001 From: PaddiM8 Date: Thu, 11 Jun 2020 22:10:23 +0200 Subject: [PATCH] Added test_fn and test_sum_fn for the interpreter. --- kalk/src/interpreter.rs | 49 ++++++++++++++++++++++++++++++++++------ kalk/src/test_helpers.rs | 13 +++++++++++ 2 files changed, 55 insertions(+), 7 deletions(-) diff --git a/kalk/src/interpreter.rs b/kalk/src/interpreter.rs index 8a2c6ea..672fa54 100644 --- a/kalk/src/interpreter.rs +++ b/kalk/src/interpreter.rs @@ -246,6 +246,7 @@ mod tests { use super::*; use crate::lexer::TokenKind::*; use crate::test_helpers::*; + use test_case::test_case; const PRECISION: u32 = 53; @@ -299,19 +300,19 @@ mod tests { let deg = Stmt::Expr(Box::new(Expr::Unit(literal("1"), Deg))); assert_eq!(interpret(rad).unwrap().unwrap(), 1); - assert_eq!( - interpret(deg).unwrap().unwrap(), - Float::with_val(10, 0.017456) + assert!( + (interpret(deg).unwrap().unwrap() - Float::with_val(PRECISION, 0.017456)).abs() + < Float::with_val(PRECISION, 0.0001) ); } #[test] fn test_var() { - let stmt = Stmt::Expr(Box::new(Expr::Var(String::from("x")))); + let stmt = Stmt::Expr(var("x")); // Prepare by inserting a variable declaration in the symbol table. let mut symbol_table = SymbolTable::new(); - symbol_table.insert("x", Stmt::VarDecl(String::from("x"), literal("1"))); + symbol_table.insert("x", var_decl("x", literal("1"))); let mut context = Context::new(&mut symbol_table, &Unit::Radians, PRECISION); assert_eq!(context.interpret(vec![stmt]).unwrap().unwrap(), 1); @@ -319,7 +320,7 @@ mod tests { #[test] fn test_undefined_var() { - let stmt = Stmt::Expr(Box::new(Expr::Var(String::from("x")))); + let stmt = Stmt::Expr(var("x")); assert_eq!( interpret(stmt), @@ -329,7 +330,7 @@ mod tests { #[test] fn test_var_decl() { - let stmt = Stmt::VarDecl(String::from("x"), literal("1")); + let stmt = var_decl("x", literal("1")); let mut symbol_table = SymbolTable::new(); Context::new(&mut symbol_table, &Unit::Radians, PRECISION) .interpret(vec![stmt]) @@ -337,4 +338,38 @@ mod tests { assert!(symbol_table.contains_var("x")); } + + #[test] + fn test_fn() { + let stmt = Stmt::Expr(fn_call("f", vec![*literal("1")])); + + // Prepare by inserting a variable declaration in the symbol table. + let mut symbol_table = SymbolTable::new(); + symbol_table.insert( + "f()", + fn_decl( + "f", + vec![String::from("x")], + binary(var("x"), TokenKind::Plus, literal("2")), + ), + ); + + let mut context = Context::new(&mut symbol_table, &Unit::Radians, PRECISION); + assert_eq!(context.interpret(vec![stmt]).unwrap().unwrap(), 3); + } + + #[test_case("1", "2", 9f64)] + #[test_case("1.2", "2.3", 9f64)] + fn test_sum_fn(start: &str, to: &str, result: f64) { + let stmt = Stmt::Expr(fn_call( + "sum", + vec![ + *literal(start), + *literal(to), + *binary(var("n"), TokenKind::Plus, literal("3")), + ], + )); + + assert_eq!(interpret(stmt).unwrap().unwrap(), result); + } } diff --git a/kalk/src/test_helpers.rs b/kalk/src/test_helpers.rs index 66fa4e2..89d8d9f 100644 --- a/kalk/src/test_helpers.rs +++ b/kalk/src/test_helpers.rs @@ -1,5 +1,6 @@ #![allow(dead_code)] use crate::ast::Expr; +use crate::ast::Stmt; use crate::lexer::Token; use crate::lexer::TokenKind; @@ -19,6 +20,10 @@ pub fn var(identifier: &str) -> Box { Box::new(Expr::Var(identifier.into())) } +pub fn fn_call(identifier: &str, arguments: Vec) -> Box { + Box::new(Expr::FnCall(identifier.into(), arguments)) +} + pub fn binary(left: Box, op: TokenKind, right: Box) -> Box { Box::new(Expr::Binary(left, op, right)) } @@ -30,3 +35,11 @@ pub fn unary(op: TokenKind, expr: Box) -> Box { pub fn group(expr: Box) -> Box { Box::new(Expr::Group(expr)) } + +pub fn var_decl(identifier: &str, value: Box) -> Stmt { + Stmt::VarDecl(identifier.into(), value) +} + +pub fn fn_decl(identifier: &str, parameters: Vec, value: Box) -> Stmt { + Stmt::FnDecl(identifier.into(), parameters, value) +}