85 lines
2.2 KiB
Rust
85 lines
2.2 KiB
Rust
|
|
mod math_lib_tests {
|
||
|
|
use relish::ast::{eval, lex, SymTable};
|
||
|
|
use relish::stdlib::{dynamic_stdlib, static_stdlib};
|
||
|
|
|
||
|
|
#[test]
|
||
|
|
fn test_add_chain() {
|
||
|
|
let document = "(add 1 2 3 4)";
|
||
|
|
let result = "10";
|
||
|
|
|
||
|
|
let mut syms = SymTable::new();
|
||
|
|
static_stdlib(&mut syms).unwrap();
|
||
|
|
dynamic_stdlib(&mut syms).unwrap();
|
||
|
|
assert_eq!(
|
||
|
|
*eval(&lex(&document.to_string()).unwrap(), &mut syms)
|
||
|
|
.unwrap()
|
||
|
|
.to_string(),
|
||
|
|
result.to_string(),
|
||
|
|
);
|
||
|
|
}
|
||
|
|
|
||
|
|
#[test]
|
||
|
|
fn test_add_chain_mixed() {
|
||
|
|
let document = "(add 1 2.2 3 4)";
|
||
|
|
let result = "10.2";
|
||
|
|
|
||
|
|
let mut syms = SymTable::new();
|
||
|
|
static_stdlib(&mut syms).unwrap();
|
||
|
|
dynamic_stdlib(&mut syms).unwrap();
|
||
|
|
assert_eq!(
|
||
|
|
*eval(&lex(&document.to_string()).unwrap(), &mut syms)
|
||
|
|
.unwrap()
|
||
|
|
.to_string(),
|
||
|
|
result.to_string(),
|
||
|
|
);
|
||
|
|
}
|
||
|
|
|
||
|
|
#[test]
|
||
|
|
fn test_mul_chain() {
|
||
|
|
let document = "(mul 1 2 3 4)";
|
||
|
|
let result = "24";
|
||
|
|
|
||
|
|
let mut syms = SymTable::new();
|
||
|
|
static_stdlib(&mut syms).unwrap();
|
||
|
|
dynamic_stdlib(&mut syms).unwrap();
|
||
|
|
assert_eq!(
|
||
|
|
*eval(&lex(&document.to_string()).unwrap(), &mut syms)
|
||
|
|
.unwrap()
|
||
|
|
.to_string(),
|
||
|
|
result.to_string(),
|
||
|
|
);
|
||
|
|
}
|
||
|
|
|
||
|
|
#[test]
|
||
|
|
fn test_sub_chain() {
|
||
|
|
let document = "(sub 1 2.2 3 4)";
|
||
|
|
let result = "-8.2";
|
||
|
|
|
||
|
|
let mut syms = SymTable::new();
|
||
|
|
static_stdlib(&mut syms).unwrap();
|
||
|
|
dynamic_stdlib(&mut syms).unwrap();
|
||
|
|
assert_eq!(
|
||
|
|
*eval(&lex(&document.to_string()).unwrap(), &mut syms)
|
||
|
|
.unwrap()
|
||
|
|
.to_string(),
|
||
|
|
result.to_string(),
|
||
|
|
);
|
||
|
|
}
|
||
|
|
|
||
|
|
#[test]
|
||
|
|
fn test_div() {
|
||
|
|
let document = "(div 10 5)";
|
||
|
|
let result = "2";
|
||
|
|
|
||
|
|
let mut syms = SymTable::new();
|
||
|
|
static_stdlib(&mut syms).unwrap();
|
||
|
|
dynamic_stdlib(&mut syms).unwrap();
|
||
|
|
assert_eq!(
|
||
|
|
*eval(&lex(&document.to_string()).unwrap(), &mut syms)
|
||
|
|
.unwrap()
|
||
|
|
.to_string(),
|
||
|
|
result.to_string(),
|
||
|
|
);
|
||
|
|
}
|
||
|
|
}
|