2023-02-27 17:30:49 -08:00
|
|
|
mod append_lib_tests {
|
2023-03-05 22:18:49 -08:00
|
|
|
use relish::ast::{eval, lex, SymTable};
|
2023-03-01 11:38:02 -08:00
|
|
|
use relish::stdlib::{dynamic_stdlib, static_stdlib};
|
2023-02-27 17:30:49 -08:00
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_append_to_empty_list() {
|
2023-02-27 22:53:54 -08:00
|
|
|
let document = "(append () 1)";
|
|
|
|
|
let result = "(1)";
|
2023-02-27 17:30:49 -08:00
|
|
|
|
2023-02-27 22:53:54 -08:00
|
|
|
let mut syms = SymTable::new();
|
|
|
|
|
static_stdlib(&mut syms).unwrap();
|
2023-03-01 11:14:42 -08:00
|
|
|
dynamic_stdlib(&mut syms).unwrap();
|
2023-03-05 22:18:49 -08:00
|
|
|
assert_eq!(
|
2023-03-05 22:21:18 -08:00
|
|
|
*eval(&lex(&document.to_string()).unwrap(), &mut syms)
|
|
|
|
|
.unwrap()
|
|
|
|
|
.to_string(),
|
2023-03-05 22:18:49 -08:00
|
|
|
result.to_string(),
|
|
|
|
|
);
|
2023-02-27 17:30:49 -08:00
|
|
|
}
|
|
|
|
|
|
2023-02-28 09:59:33 -08:00
|
|
|
#[test]
|
|
|
|
|
fn test_multi_append_to_empty_list() {
|
|
|
|
|
let document = "(append () 1 'two' 3.4)";
|
|
|
|
|
let result = "(1 'two' 3.4)";
|
|
|
|
|
|
|
|
|
|
let mut syms = SymTable::new();
|
|
|
|
|
static_stdlib(&mut syms).unwrap();
|
2023-03-01 11:14:42 -08:00
|
|
|
dynamic_stdlib(&mut syms).unwrap();
|
2023-02-28 09:59:33 -08:00
|
|
|
|
2023-03-05 22:18:49 -08:00
|
|
|
assert_eq!(
|
2023-03-05 22:21:18 -08:00
|
|
|
*eval(&lex(&document.to_string()).unwrap(), &mut syms)
|
|
|
|
|
.unwrap()
|
|
|
|
|
.to_string(),
|
2023-03-05 22:18:49 -08:00
|
|
|
result.to_string(),
|
|
|
|
|
);
|
2023-02-28 09:59:33 -08:00
|
|
|
}
|
|
|
|
|
|
2023-02-27 17:30:49 -08:00
|
|
|
#[test]
|
|
|
|
|
fn test_append_to_full_list() {
|
|
|
|
|
let document = "(append (1 2) 3)";
|
|
|
|
|
let result = "(1 2 3)";
|
|
|
|
|
|
2023-02-27 22:53:54 -08:00
|
|
|
let mut syms = SymTable::new();
|
|
|
|
|
static_stdlib(&mut syms).unwrap();
|
2023-03-01 11:14:42 -08:00
|
|
|
dynamic_stdlib(&mut syms).unwrap();
|
2023-02-27 17:30:49 -08:00
|
|
|
|
2023-03-05 22:18:49 -08:00
|
|
|
assert_eq!(
|
2023-03-05 22:21:18 -08:00
|
|
|
*eval(&lex(&document.to_string()).unwrap(), &mut syms)
|
|
|
|
|
.unwrap()
|
|
|
|
|
.to_string(),
|
2023-03-05 22:18:49 -08:00
|
|
|
result.to_string(),
|
|
|
|
|
);
|
2023-02-27 17:30:49 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_mono_append() {
|
|
|
|
|
let document = "(append)";
|
2023-02-27 22:53:54 -08:00
|
|
|
let result = "(<nil>)";
|
2023-02-27 17:30:49 -08:00
|
|
|
|
2023-02-27 22:53:54 -08:00
|
|
|
let mut syms = SymTable::new();
|
|
|
|
|
static_stdlib(&mut syms).unwrap();
|
2023-03-01 11:14:42 -08:00
|
|
|
dynamic_stdlib(&mut syms).unwrap();
|
2023-02-27 17:30:49 -08:00
|
|
|
|
2023-03-05 22:18:49 -08:00
|
|
|
assert_eq!(
|
2023-03-05 22:21:18 -08:00
|
|
|
*eval(&lex(&document.to_string()).unwrap(), &mut syms)
|
|
|
|
|
.unwrap()
|
|
|
|
|
.to_string(),
|
2023-03-05 22:18:49 -08:00
|
|
|
result.to_string(),
|
|
|
|
|
);
|
2023-02-27 17:30:49 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_append_no_list() {
|
|
|
|
|
let document = "(append 'test' 1 2 3)";
|
|
|
|
|
let result = "('test' 1 2 3)";
|
|
|
|
|
|
2023-02-27 22:53:54 -08:00
|
|
|
let mut syms = SymTable::new();
|
|
|
|
|
static_stdlib(&mut syms).unwrap();
|
2023-03-01 11:14:42 -08:00
|
|
|
dynamic_stdlib(&mut syms).unwrap();
|
2023-02-27 17:30:49 -08:00
|
|
|
|
2023-03-05 22:18:49 -08:00
|
|
|
assert_eq!(
|
2023-03-05 22:21:18 -08:00
|
|
|
*eval(&lex(&document.to_string()).unwrap(), &mut syms)
|
|
|
|
|
.unwrap()
|
|
|
|
|
.to_string(),
|
2023-03-05 22:18:49 -08:00
|
|
|
result.to_string(),
|
|
|
|
|
);
|
2023-02-27 17:30:49 -08:00
|
|
|
}
|
2023-03-07 14:29:31 -08:00
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_len_empty() {
|
|
|
|
|
let document = "(len ())";
|
|
|
|
|
let result = "0";
|
|
|
|
|
|
|
|
|
|
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_len_normal() {
|
|
|
|
|
let document = "(len (1 2 3))";
|
|
|
|
|
let result = "3";
|
|
|
|
|
|
|
|
|
|
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_car_empty() {
|
|
|
|
|
let document = "(car ())";
|
|
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
.err()
|
|
|
|
|
.unwrap()
|
|
|
|
|
.to_string(),
|
|
|
|
|
"error in call to car: argument is empty".to_string(),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_car_normal() {
|
|
|
|
|
let document = "(car (1 2 3))";
|
|
|
|
|
let result = "1";
|
|
|
|
|
|
|
|
|
|
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_cdr_empty() {
|
|
|
|
|
let document = "(cdr ())";
|
|
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
.err()
|
|
|
|
|
.unwrap()
|
|
|
|
|
.to_string(),
|
|
|
|
|
"error in call to cdr: argument is empty".to_string(),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_cdr_normal() {
|
|
|
|
|
let document = "(cdr (1 2 3))";
|
|
|
|
|
let result = "3";
|
|
|
|
|
|
|
|
|
|
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(),
|
|
|
|
|
);
|
|
|
|
|
}
|
2023-03-08 20:59:22 -08:00
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_pop() {
|
|
|
|
|
let document = "(def test '' (pop (1 2 3)))";
|
|
|
|
|
let check1 = "(car test)";
|
|
|
|
|
let result1 = "1";
|
|
|
|
|
let check2 = "(cdr test)";
|
|
|
|
|
let result2 = "(2 3)";
|
|
|
|
|
|
|
|
|
|
let mut syms = SymTable::new();
|
|
|
|
|
static_stdlib(&mut syms).unwrap();
|
|
|
|
|
dynamic_stdlib(&mut syms).unwrap();
|
|
|
|
|
eval(&lex(&document.to_string()).unwrap(), &mut syms).unwrap();
|
|
|
|
|
let ch1 = lex(&check1.to_string()).unwrap();
|
|
|
|
|
let ch2 = lex(&check2.to_string()).unwrap();
|
|
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
|
*eval(&ch1, &mut syms).unwrap().to_string(),
|
|
|
|
|
result1.to_string(),
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
|
*eval(&ch2, &mut syms).unwrap().to_string(),
|
|
|
|
|
result2.to_string(),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_pop_mono() {
|
|
|
|
|
let document = "(def test '' (pop (1)))";
|
|
|
|
|
let check1 = "(car test)";
|
|
|
|
|
let result1 = "1";
|
|
|
|
|
let check2 = "(cdr test)";
|
|
|
|
|
let result2 = "(<nil>)";
|
|
|
|
|
|
|
|
|
|
let mut syms = SymTable::new();
|
|
|
|
|
static_stdlib(&mut syms).unwrap();
|
|
|
|
|
dynamic_stdlib(&mut syms).unwrap();
|
|
|
|
|
eval(&lex(&document.to_string()).unwrap(), &mut syms).unwrap();
|
|
|
|
|
let ch1 = lex(&check1.to_string()).unwrap();
|
|
|
|
|
let ch2 = lex(&check2.to_string()).unwrap();
|
|
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
|
*eval(&ch1, &mut syms).unwrap().to_string(),
|
|
|
|
|
result1.to_string(),
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
|
*eval(&ch2, &mut syms).unwrap().to_string(),
|
|
|
|
|
result2.to_string(),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_dq() {
|
|
|
|
|
let document = "(def test '' (dq (1 2 3)))";
|
|
|
|
|
let check1 = "(car test)";
|
|
|
|
|
let result1 = "3";
|
|
|
|
|
let check2 = "(cdr test)";
|
|
|
|
|
let result2 = "(1 2)";
|
|
|
|
|
|
|
|
|
|
let mut syms = SymTable::new();
|
|
|
|
|
static_stdlib(&mut syms).unwrap();
|
|
|
|
|
dynamic_stdlib(&mut syms).unwrap();
|
|
|
|
|
eval(&lex(&document.to_string()).unwrap(), &mut syms).unwrap();
|
|
|
|
|
let ch1 = lex(&check1.to_string()).unwrap();
|
|
|
|
|
let ch2 = lex(&check2.to_string()).unwrap();
|
|
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
|
*eval(&ch1, &mut syms).unwrap().to_string(),
|
|
|
|
|
result1.to_string(),
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
|
*eval(&ch2, &mut syms).unwrap().to_string(),
|
|
|
|
|
result2.to_string(),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_dq_mono() {
|
|
|
|
|
let document = "(def test '' (dq (1)))";
|
|
|
|
|
let check1 = "(car test)";
|
|
|
|
|
let result1 = "1";
|
|
|
|
|
let check2 = "(cdr test)";
|
|
|
|
|
let result2 = "(<nil>)";
|
|
|
|
|
|
|
|
|
|
let mut syms = SymTable::new();
|
|
|
|
|
static_stdlib(&mut syms).unwrap();
|
|
|
|
|
dynamic_stdlib(&mut syms).unwrap();
|
|
|
|
|
eval(&lex(&document.to_string()).unwrap(), &mut syms).unwrap();
|
|
|
|
|
let ch1 = lex(&check1.to_string()).unwrap();
|
|
|
|
|
let ch2 = lex(&check2.to_string()).unwrap();
|
|
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
|
*eval(&ch1, &mut syms).unwrap().to_string(),
|
|
|
|
|
result1.to_string(),
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
|
*eval(&ch2, &mut syms).unwrap().to_string(),
|
|
|
|
|
result2.to_string(),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_reverse() {
|
|
|
|
|
let document = "(reverse ('test' 1 2 3))";
|
|
|
|
|
let result = "(3 2 1 'test')";
|
|
|
|
|
|
|
|
|
|
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_reverse_mono() {
|
|
|
|
|
let document = "(reverse ('test'))";
|
|
|
|
|
let result = "('test')";
|
|
|
|
|
|
|
|
|
|
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_reverse_nil() {
|
|
|
|
|
let document = "(reverse ())";
|
|
|
|
|
let result = "(<nil>)";
|
|
|
|
|
|
|
|
|
|
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(),
|
|
|
|
|
);
|
|
|
|
|
}
|
2023-02-27 17:30:49 -08:00
|
|
|
}
|