add mod and exp functions, tests, and also some snippets for common
patterns in the readme
This commit is contained in:
parent
7fd47c1812
commit
0c5d14ed8e
4 changed files with 323 additions and 13 deletions
|
|
@ -145,4 +145,146 @@ mod math_lib_tests {
|
|||
result.to_string(),
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_ii_exp() {
|
||||
let document = "(exp 7 20)";
|
||||
let result = 7i128.pow(20);
|
||||
|
||||
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_if_exp() {
|
||||
let document = "(exp 1 10.2)";
|
||||
let result = f64::powf(1f64, 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_fi_exp() {
|
||||
let document = "(exp 1.2 5)";
|
||||
let result = f64::powf(1.2, 5f64);
|
||||
|
||||
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(),
|
||||
format!("{:.5}", result),
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_ff_exp() {
|
||||
let document = "(exp 1.3 1.5)";
|
||||
let result = f64::powf(1.3, 1.5);
|
||||
|
||||
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_ii_mod() {
|
||||
let document = "(def test '' (mod 7 3))";
|
||||
let check1 = "(car test)";
|
||||
let result1 = "2";
|
||||
let check2 = "(cdr test)";
|
||||
let result2 = "1";
|
||||
|
||||
let mut syms = SymTable::new();
|
||||
static_stdlib(&mut syms).unwrap();
|
||||
dynamic_stdlib(&mut syms).unwrap();
|
||||
let _ = *eval(&lex(&document.to_string()).unwrap(), &mut syms).unwrap();
|
||||
assert_eq!(
|
||||
*eval(&lex(&check1.to_string()).unwrap(), &mut syms)
|
||||
.unwrap()
|
||||
.to_string(),
|
||||
result1.to_string(),
|
||||
);
|
||||
assert_eq!(
|
||||
*eval(&lex(&check2.to_string()).unwrap(), &mut syms)
|
||||
.unwrap()
|
||||
.to_string(),
|
||||
result2.to_string(),
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_if_mod() {
|
||||
let document = "(def test '' (mod 7 3.3))";
|
||||
let check1 = "(car test)";
|
||||
let result1 = "2";
|
||||
|
||||
let mut syms = SymTable::new();
|
||||
static_stdlib(&mut syms).unwrap();
|
||||
dynamic_stdlib(&mut syms).unwrap();
|
||||
let _ = *eval(&lex(&document.to_string()).unwrap(), &mut syms).unwrap();
|
||||
assert_eq!(
|
||||
*eval(&lex(&check1.to_string()).unwrap(), &mut syms)
|
||||
.unwrap()
|
||||
.to_string(),
|
||||
result1.to_string(),
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_fi_mod() {
|
||||
let document = "(def test '' (mod 7.2 2))";
|
||||
let check1 = "(car test)";
|
||||
let result1 = "3"; let mut syms = SymTable::new();
|
||||
static_stdlib(&mut syms).unwrap();
|
||||
dynamic_stdlib(&mut syms).unwrap();
|
||||
let _ = *eval(&lex(&document.to_string()).unwrap(), &mut syms).unwrap();
|
||||
assert_eq!(
|
||||
*eval(&lex(&check1.to_string()).unwrap(), &mut syms)
|
||||
.unwrap()
|
||||
.to_string(),
|
||||
result1.to_string(),
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_ff_mod() {
|
||||
let document = "(def test '' (mod 7.2 3.3))";
|
||||
let check1 = "(car test)";
|
||||
let result1 = "2";
|
||||
|
||||
let mut syms = SymTable::new();
|
||||
static_stdlib(&mut syms).unwrap();
|
||||
dynamic_stdlib(&mut syms).unwrap();
|
||||
let _ = *eval(&lex(&document.to_string()).unwrap(), &mut syms).unwrap();
|
||||
assert_eq!(
|
||||
*eval(&lex(&check1.to_string()).unwrap(), &mut syms)
|
||||
.unwrap()
|
||||
.to_string(),
|
||||
result1.to_string(),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue