Added test_fn and test_sum_fn for the interpreter.

This commit is contained in:
PaddiM8 2020-06-11 22:10:23 +02:00
parent b3e07b710e
commit 4e2fe9eb3b
2 changed files with 55 additions and 7 deletions

View File

@ -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);
}
}

View File

@ -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<Expr> {
Box::new(Expr::Var(identifier.into()))
}
pub fn fn_call(identifier: &str, arguments: Vec<Expr>) -> Box<Expr> {
Box::new(Expr::FnCall(identifier.into(), arguments))
}
pub fn binary(left: Box<Expr>, op: TokenKind, right: Box<Expr>) -> Box<Expr> {
Box::new(Expr::Binary(left, op, right))
}
@ -30,3 +35,11 @@ pub fn unary(op: TokenKind, expr: Box<Expr>) -> Box<Expr> {
pub fn group(expr: Box<Expr>) -> Box<Expr> {
Box::new(Expr::Group(expr))
}
pub fn var_decl(identifier: &str, value: Box<Expr>) -> Stmt {
Stmt::VarDecl(identifier.into(), value)
}
pub fn fn_decl(identifier: &str, parameters: Vec<String>, value: Box<Expr>) -> Stmt {
Stmt::FnDecl(identifier.into(), parameters, value)
}