Lines
100 %
Functions
Branches
use super::*;
#[test]
fn test_expr_is_atom() {
assert!(Expr::Nil.is_atom());
assert!(Expr::T.is_atom());
assert!(Expr::Number(Number::Index(42)).is_atom());
assert!(Expr::String("hello".into()).is_atom());
assert!(Expr::Symbol("foo".into()).is_atom());
assert!(Expr::Keyword("bar".into()).is_atom());
assert!(!Expr::List(vec![]).is_atom());
assert!(!Expr::cons(Expr::Nil, Expr::Nil).is_atom());
assert!(Expr::Lambda(LambdaParams::simple(vec![]), Box::new(Expr::Nil)).is_atom());
}
fn entity_kind_wire_codes_round_trip_and_are_distinct() {
let codes: Vec<i64> = EntityKind::ALL
.iter()
.map(|kind| kind.wire_code())
.collect();
assert_eq!(codes, (0..8).collect::<Vec<i64>>());
for kind in EntityKind::ALL {
assert_eq!(EntityKind::from_wire_code(kind.wire_code()), Some(kind));
assert_eq!(EntityKind::from_wire_code(8), None);
assert_eq!(EntityKind::from_wire_code(-1), None);
fn quoted_list_of_nothing_is_nil() {
assert_eq!(Expr::quoted_list(vec![]), Expr::Nil);
assert_eq!(
Expr::quoted_list(vec![Expr::T]),
Expr::Datum(Box::new(Expr::List(vec![Expr::T])))
);
fn quoted_number_string_and_keyword_are_the_atom() {
let number = Expr::Number(Number::Scalar(Fraction::new(1, 2)));
let string = Expr::String("a".into());
let keyword = Expr::Keyword("K".into());
assert_eq!(Expr::quoted(number.clone()), number);
assert_eq!(Expr::quoted(string.clone()), string);
assert_eq!(Expr::quoted(keyword.clone()), keyword);
assert_eq!(Expr::quoted_code(keyword.clone()), keyword);
fn test_expr_as_symbol() {
assert_eq!(Expr::Symbol("foo".into()).as_symbol(), Some("foo"));
assert_eq!(Expr::Number(Number::Index(1)).as_symbol(), None);
fn test_expr_as_list() {
let list = Expr::List(vec![Expr::Symbol("a".into())]);
assert!(list.as_list().is_some());
assert_eq!(list.as_list().unwrap().len(), 1);
assert!(Expr::Symbol("a".into()).as_list().is_none());
assert_eq!(Expr::Nil.as_list(), Some(&[][..]));
fn into_data_makes_inner_quotes_quote_lists() {
let sym = |name: &str| Expr::Symbol(name.to_string());
let datum = Expr::List(vec![sym("A"), Expr::Quote(Box::new(sym("B")))]);
datum.into_data(),
Expr::List(vec![sym("A"), Expr::List(vec![sym("QUOTE"), sym("B")])])
let outer = Expr::Quote(Box::new(sym("B")));
assert_eq!(outer.clone().into_data(), outer);
fn quoted_is_a_datum_normalised_and_quoted_code_is_a_quote_form() {
let items = Expr::List(vec![sym("A"), Expr::Quote(Box::new(sym("B")))]);
Expr::quoted(items.clone()),
Expr::Datum(Box::new(Expr::List(vec![
sym("A"),
Expr::List(vec![sym("QUOTE"), sym("B")])
])))
Expr::quoted_code(items.clone()),
Expr::Quote(Box::new(items))
fn into_code_unwraps_a_datum_and_keeps_any_other_value() {
let list = Expr::List(vec![sym("+"), Expr::Number(Number::Index(1))]);
assert_eq!(Expr::quoted(list.clone()).into_code(), list);
assert_eq!(Expr::quoted(sym("A")).into_code(), sym("A"));
let three = Expr::Number(Number::Index(3));
assert_eq!(three.clone().into_code(), three);
Expr::quoted_code(sym("A")).into_code(),
Expr::quoted_code(sym("A"))
assert_eq!(Expr::Nil.into_code(), Expr::Nil);
fn into_datum_member_drops_the_datum_wrapper_and_listifies_quotes() {
assert_eq!(Expr::quoted(sym("A")).into_datum_member(), sym("A"));
Expr::quoted_code(sym("A")).into_datum_member(),
Expr::List(vec![sym("QUOTE"), sym("A")])
fn quoted_of_a_quote_form_is_the_list_quote_x() {
let quote_a = Expr::quoted_code(sym("A"));
Expr::quoted(quote_a),
Expr::Datum(Box::new(Expr::List(vec![sym("QUOTE"), sym("A")])))
fn into_code_is_the_inverse_of_into_data_at_every_depth() {
let quote = |inner: Expr| Expr::quoted_code(inner);
let code = Expr::List(vec![
sym("F"),
quote(sym("A")),
Expr::List(vec![sym("G"), quote(Expr::List(vec![sym("H"), sym("B")]))]),
Expr::cons(quote(sym("C")), sym("D")),
]);
assert_eq!(Expr::quoted(code.clone()).into_code(), code);
fn into_code_keeps_the_inside_of_a_quote_as_data() {
let inner = Expr::List(vec![sym("G"), Expr::List(vec![sym("QUOTE"), sym("B")])]);
let datum = Expr::quoted(Expr::List(vec![sym("QUOTE"), inner.clone()]));
assert_eq!(datum.into_code(), Expr::quoted_code(inner));
fn any_form_finds_a_form_at_any_depth() {
let deep = Expr::List(vec![
Expr::symbol("A"),
Expr::cons(
Expr::Nil,
Expr::Quote(Box::new(Expr::List(vec![Expr::WasmLocal(
3,
WasmType::I32,
)]))),
),
assert!(deep.any_form(Expr::is_wasm_runtime));
assert!(!Expr::List(vec![Expr::symbol("A"), Expr::Nil]).any_form(Expr::is_wasm_runtime));
fn any_form_reaches_the_body_and_defaults_of_a_lambda() {
let body = Expr::Lambda(
LambdaParams::simple(vec!["X".to_string()]),
Box::new(Expr::WasmGlobal(1, WasmType::Ratio)),
assert!(body.any_form(Expr::is_wasm_runtime));
let params = LambdaParams {
optional: vec![("Y".to_string(), Some(Expr::WasmRuntime(WasmType::I32)))],
..LambdaParams::simple(vec![])
};
let defaulted = Expr::Lambda(params, Box::new(Expr::Nil));
assert!(defaulted.any_form(Expr::is_wasm_runtime));
fn a_structure_rides_a_pair_cell_and_round_trips() {
let ty = WasmType::StructRef(StructId(4));
let elem = PairElement::from_wasm_type(ty).expect("a structure rides a cell");
assert_eq!(elem, PairElement::Struct(StructId(4)));
assert_eq!(elem.as_wasm_type(), ty);
assert_eq!(elem.to_string(), "struct#4");
assert_eq!(ty.to_string(), "struct#4");
elem.widen(PairElement::Struct(StructId(5))),
PairElement::AnyRef
fn a_closure_rides_a_pair_cell_and_round_trips() {
let ty = WasmType::Closure(ClosureSigId(2));
let elem = PairElement::from_wasm_type(ty).expect("a closure rides a cell");
assert_eq!(elem, PairElement::Closure(ClosureSigId(2)));
assert_eq!(elem.to_string(), ty.to_string());
elem.widen(PairElement::Closure(ClosureSigId(3))),
fn lambda_params_and_expr_keep_their_size() {
assert_eq!(std::mem::size_of::<LambdaParams>(), 120);
assert_eq!(std::mem::size_of::<Expr>(), 128);
KeyParam::new("x", None).keyword,
"X",
"a plain key is named by its variable"
fn a_number_keeps_its_kind_and_exact_value() {
let index = Number::Index(2);
let scalar = Number::Scalar(Fraction::new(2, 1));
assert_eq!(index.value(), scalar.value());
assert_ne!(index, scalar);
Number::Scalar(Fraction::new(1, 2)).value(),
Fraction::new(2, 4)