2023-02-27 17:30:49 -08:00
|
|
|
mod append_lib_tests {
|
2024-02-06 22:39:08 +00:00
|
|
|
use flesh::ast::{eval, lex, SymTable};
|
2024-07-10 13:22:28 -07:00
|
|
|
use flesh::stdlib::static_stdlib;
|
2023-02-27 17:30:49 -08:00
|
|
|
|
|
|
|
|
#[test]
|
2023-03-09 16:03:06 -08:00
|
|
|
fn test_cons_to_empty_list() {
|
|
|
|
|
let document = "(cons () 1)";
|
2023-02-27 22:53:54 -08:00
|
|
|
let result = "(1)";
|
|
|
|
|
let mut syms = SymTable::new();
|
2023-05-26 06:41:18 +00:00
|
|
|
static_stdlib(&mut syms);
|
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]
|
2023-03-09 16:03:06 -08:00
|
|
|
fn test_multi_cons_to_empty_list() {
|
2024-07-10 13:22:28 -07:00
|
|
|
let document = "(cons () 1 \"two\" 3.4)";
|
|
|
|
|
let result = "(1 \"two\" 3.4)";
|
2023-02-28 09:59:33 -08:00
|
|
|
|
|
|
|
|
let mut syms = SymTable::new();
|
2023-05-26 06:41:18 +00:00
|
|
|
static_stdlib(&mut syms);
|
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]
|
2023-03-09 16:03:06 -08:00
|
|
|
fn test_cons_to_full_list() {
|
|
|
|
|
let document = "(cons (1 2) 3)";
|
2023-02-27 17:30:49 -08:00
|
|
|
let result = "(1 2 3)";
|
|
|
|
|
|
2023-02-27 22:53:54 -08:00
|
|
|
let mut syms = SymTable::new();
|
2023-05-26 06:41:18 +00:00
|
|
|
static_stdlib(&mut syms);
|
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]
|
2023-03-09 16:03:06 -08:00
|
|
|
fn test_mono_cons() {
|
|
|
|
|
let document = "(cons)";
|
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();
|
2023-05-26 06:41:18 +00:00
|
|
|
static_stdlib(&mut syms);
|
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]
|
2023-03-09 16:03:06 -08:00
|
|
|
fn test_cons_no_list() {
|
2024-07-10 13:22:28 -07:00
|
|
|
let document = "(cons \"test\" 1 2 3)";
|
|
|
|
|
let result = "(\"test\" 1 2 3)";
|
2023-02-27 17:30:49 -08:00
|
|
|
|
2023-02-27 22:53:54 -08:00
|
|
|
let mut syms = SymTable::new();
|
2023-05-26 06:41:18 +00:00
|
|
|
static_stdlib(&mut syms);
|
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();
|
2023-05-26 06:41:18 +00:00
|
|
|
static_stdlib(&mut syms);
|
2023-03-07 14:29:31 -08:00
|
|
|
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();
|
2023-05-26 06:41:18 +00:00
|
|
|
static_stdlib(&mut syms);
|
2023-03-07 14:29:31 -08:00
|
|
|
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();
|
2023-05-26 06:41:18 +00:00
|
|
|
static_stdlib(&mut syms);
|
2023-03-07 14:29:31 -08:00
|
|
|
assert_eq!(
|
|
|
|
|
*eval(&lex(&document.to_string()).unwrap(), &mut syms)
|
|
|
|
|
.err()
|
|
|
|
|
.unwrap()
|
2023-05-23 22:06:11 +00:00
|
|
|
.0
|
|
|
|
|
.first()
|
|
|
|
|
.unwrap()
|
|
|
|
|
.message,
|
|
|
|
|
"input is empty".to_string(),
|
2023-03-07 14:29:31 -08:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_car_normal() {
|
|
|
|
|
let document = "(car (1 2 3))";
|
|
|
|
|
let result = "1";
|
|
|
|
|
|
|
|
|
|
let mut syms = SymTable::new();
|
2023-05-26 06:41:18 +00:00
|
|
|
static_stdlib(&mut syms);
|
2023-03-07 14:29:31 -08:00
|
|
|
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();
|
2023-05-26 06:41:18 +00:00
|
|
|
static_stdlib(&mut syms);
|
2023-03-07 14:29:31 -08:00
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
|
*eval(&lex(&document.to_string()).unwrap(), &mut syms)
|
|
|
|
|
.err()
|
|
|
|
|
.unwrap()
|
2023-05-23 22:06:11 +00:00
|
|
|
.0
|
|
|
|
|
.first()
|
|
|
|
|
.unwrap()
|
|
|
|
|
.message,
|
|
|
|
|
"input is empty".to_string(),
|
2023-03-07 14:29:31 -08:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_cdr_normal() {
|
|
|
|
|
let document = "(cdr (1 2 3))";
|
|
|
|
|
let result = "3";
|
|
|
|
|
|
|
|
|
|
let mut syms = SymTable::new();
|
2023-05-26 06:41:18 +00:00
|
|
|
static_stdlib(&mut syms);
|
2023-03-07 14:29:31 -08:00
|
|
|
|
|
|
|
|
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() {
|
2024-07-10 13:22:28 -07:00
|
|
|
let document = "(def test \"\" (pop (1 2 3)))";
|
2023-03-08 20:59:22 -08:00
|
|
|
let check1 = "(car test)";
|
|
|
|
|
let result1 = "1";
|
|
|
|
|
let check2 = "(cdr test)";
|
|
|
|
|
let result2 = "(2 3)";
|
|
|
|
|
|
|
|
|
|
let mut syms = SymTable::new();
|
2023-05-26 06:41:18 +00:00
|
|
|
static_stdlib(&mut syms);
|
2024-07-10 13:22:28 -07:00
|
|
|
|
2023-03-08 20:59:22 -08:00
|
|
|
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() {
|
2024-07-10 13:22:28 -07:00
|
|
|
let document = "(def test \"\" (pop (1)))";
|
2023-03-08 20:59:22 -08:00
|
|
|
let check1 = "(car test)";
|
|
|
|
|
let result1 = "1";
|
|
|
|
|
let check2 = "(cdr test)";
|
|
|
|
|
let result2 = "(<nil>)";
|
|
|
|
|
|
|
|
|
|
let mut syms = SymTable::new();
|
2023-05-26 06:41:18 +00:00
|
|
|
static_stdlib(&mut syms);
|
2024-07-10 13:22:28 -07:00
|
|
|
|
2023-03-08 20:59:22 -08:00
|
|
|
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() {
|
2024-07-10 13:22:28 -07:00
|
|
|
let document = "(def test \"\" (dq (1 2 3)))";
|
2023-03-08 20:59:22 -08:00
|
|
|
let check1 = "(car test)";
|
|
|
|
|
let result1 = "3";
|
|
|
|
|
let check2 = "(cdr test)";
|
|
|
|
|
let result2 = "(1 2)";
|
|
|
|
|
|
|
|
|
|
let mut syms = SymTable::new();
|
2023-05-26 06:41:18 +00:00
|
|
|
static_stdlib(&mut syms);
|
2024-07-10 13:22:28 -07:00
|
|
|
|
2023-03-08 20:59:22 -08:00
|
|
|
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() {
|
2024-07-10 13:22:28 -07:00
|
|
|
let document = "(def test \"\" (dq (1)))";
|
2023-03-08 20:59:22 -08:00
|
|
|
let check1 = "(car test)";
|
|
|
|
|
let result1 = "1";
|
|
|
|
|
let check2 = "(cdr test)";
|
|
|
|
|
let result2 = "(<nil>)";
|
|
|
|
|
|
|
|
|
|
let mut syms = SymTable::new();
|
2023-05-26 06:41:18 +00:00
|
|
|
static_stdlib(&mut syms);
|
2024-07-10 13:22:28 -07:00
|
|
|
|
2023-03-08 20:59:22 -08:00
|
|
|
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() {
|
2024-07-10 13:22:28 -07:00
|
|
|
let document = "(reverse (\"test\" 1 2 3))";
|
|
|
|
|
let result = "(3 2 1 \"test\")";
|
2023-03-08 20:59:22 -08:00
|
|
|
|
|
|
|
|
let mut syms = SymTable::new();
|
2023-05-26 06:41:18 +00:00
|
|
|
static_stdlib(&mut syms);
|
2023-03-08 20:59:22 -08:00
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
|
*eval(&lex(&document.to_string()).unwrap(), &mut syms)
|
|
|
|
|
.unwrap()
|
|
|
|
|
.to_string(),
|
|
|
|
|
result.to_string(),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_reverse_mono() {
|
2024-07-10 13:22:28 -07:00
|
|
|
let document = "(reverse (\"test\"))";
|
|
|
|
|
let result = "(\"test\")";
|
2023-03-08 20:59:22 -08:00
|
|
|
|
|
|
|
|
let mut syms = SymTable::new();
|
2023-05-26 06:41:18 +00:00
|
|
|
static_stdlib(&mut syms);
|
2023-03-08 20:59:22 -08:00
|
|
|
|
|
|
|
|
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();
|
2023-05-26 06:41:18 +00:00
|
|
|
static_stdlib(&mut syms);
|
2023-03-08 20:59:22 -08:00
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
|
*eval(&lex(&document.to_string()).unwrap(), &mut syms)
|
|
|
|
|
.unwrap()
|
|
|
|
|
.to_string(),
|
|
|
|
|
result.to_string(),
|
|
|
|
|
);
|
|
|
|
|
}
|
2023-07-30 12:26:02 -07:00
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_islist_t() {
|
|
|
|
|
let document = "(list? ())";
|
|
|
|
|
let result = "true";
|
|
|
|
|
|
|
|
|
|
let mut syms = SymTable::new();
|
|
|
|
|
static_stdlib(&mut syms);
|
|
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
|
*eval(&lex(&document.to_string()).unwrap(), &mut syms)
|
|
|
|
|
.unwrap()
|
|
|
|
|
.to_string(),
|
|
|
|
|
result.to_string(),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_islist_f() {
|
|
|
|
|
let document = "(list? 1223)";
|
|
|
|
|
let result = "false";
|
|
|
|
|
|
|
|
|
|
let mut syms = SymTable::new();
|
|
|
|
|
static_stdlib(&mut syms);
|
|
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
|
*eval(&lex(&document.to_string()).unwrap(), &mut syms)
|
|
|
|
|
.unwrap()
|
|
|
|
|
.to_string(),
|
|
|
|
|
result.to_string(),
|
|
|
|
|
);
|
|
|
|
|
}
|
2023-02-27 17:30:49 -08:00
|
|
|
}
|