use std::collections::HashSet;
use flow_abi::description::{CallTypes, Term};
use flow_abi::wire::Wire as _;
use flow_abi::{
DIALECT, Json, ResultType, Signature, call_bindings, call_views, crossable, schema,
};
use flow_abi_testing::catalog::catalog;
use flow_types::Type;
const DEPTH: usize = 20_000;
fn nested(depth: usize, inner: Type) -> Type {
(0..depth).fold(inner, |ty, _| Type::list(ty))
}
#[test]
fn a_type_twenty_thousand_deep_clones_compares_hashes_substitutes_and_drops_on_a_small_stack() {
flow_testing::on_a_small_stack(|| {
let deep = nested(DEPTH, Type::Int);
let copy = deep.clone();
assert!(copy == deep, "a clone is equal to its type");
assert!(
nested(DEPTH, Type::Text) != deep,
"types differing at the bottom are unequal"
);
let seen: HashSet<Type> = [deep.clone(), copy].into_iter().collect();
assert_eq!(seen.len(), 1, "equal types hash alike");
let substituted = nested(DEPTH, Type::Param(0)).subst(&[Type::Int]);
assert!(
substituted == deep,
"the parameter is replaced at the bottom"
);
assert!(
crossable(&catalog(), &deep).is_ok(),
"a list of integers crosses"
);
});
}
#[test]
fn a_result_type_twenty_thousand_deep_is_described_read_named_and_compared_on_a_small_stack() {
flow_testing::on_a_small_stack(|| {
let deep = nested(DEPTH, Type::Int);
let catalog = catalog();
let described = ResultType::from_catalog(&catalog, &deep).expect("the type is described");
assert!(
described.result() == &deep,
"the description reads back to the type"
);
let read = ResultType::read(described.bytes()).expect("the bytes read");
assert!(read == described, "the bytes read to the same description");
assert!(
catalog.same(&deep, read.catalog(), read.result()),
"the type read is the same type"
);
let name = catalog.name(&deep);
assert_eq!(
name,
format!("{}Int{}", "List<".repeat(DEPTH), ">".repeat(DEPTH)),
"each list is named around its item"
);
});
}
#[test]
fn a_generic_call_bound_twenty_thousand_deep_carries_its_types_on_a_small_stack() {
flow_testing::on_a_small_stack(|| {
let deep = nested(DEPTH, Type::Int);
let signature = Signature {
generic: vec!["T".into()],
params: vec![("items".into(), Type::list(Type::Param(0)))],
result: Type::Text,
};
let views = call_views(&catalog(), &signature, std::slice::from_ref(&deep))
.expect("the views are written");
let read = CallTypes::decode(&views.bytes()).expect("the views read");
assert!(read == views, "the views read back to themselves");
let (_, bindings) = call_bindings(&read).expect("the views are valid");
assert!(
bindings == [("T".to_owned(), deep)],
"the views bind the deep type"
);
});
}
fn nested_term(depth: usize, inner: Term) -> Term {
(0..depth).fold(inner, |term, _| Term::List(Box::new(term)))
}
#[test]
fn a_term_twenty_thousand_deep_clones_compares_hashes_and_drops_on_a_small_stack() {
flow_testing::on_a_small_stack(|| {
let deep = nested_term(DEPTH, Term::Int);
let copy = deep.clone();
assert!(copy == deep, "a clone is equal to its term");
assert!(
nested_term(DEPTH, Term::Text) != deep,
"terms differing at the bottom are unequal"
);
let seen: HashSet<Term> = [deep, copy].into_iter().collect();
assert_eq!(seen.len(), 1, "equal terms hash alike");
});
}
#[test]
fn a_schema_twenty_thousand_deep_is_written_and_dropped_on_a_small_stack() {
flow_testing::on_a_small_stack(|| {
let deep = nested(DEPTH, Type::Int);
let schema = schema(&catalog(), &deep).expect("a list of integers has a schema");
assert_eq!(
schema.canonical(),
format!(
"{{\"$schema\":\"{DIALECT}\",\"items\":{}{{\"type\":\"integer\"}}{}",
"{\"items\":".repeat(DEPTH - 1),
",\"type\":\"array\"}".repeat(DEPTH)
),
"each list's schema holds its item's"
);
});
}
#[test]
fn a_json_value_twenty_thousand_deep_is_written_and_dropped_on_a_small_stack() {
flow_testing::on_a_small_stack(|| {
let deep = (0..DEPTH).fold(Json::Null, |value, index| {
if index % 2 == 0 {
Json::Array(vec![value, Json::Bool(true)])
} else {
Json::Object(vec![("b".to_owned(), Json::Null), ("a".to_owned(), value)])
}
});
let text = deep.canonical();
assert_eq!(
text,
format!(
"{}null{}",
"{\"a\":[".repeat(DEPTH / 2),
",true],\"b\":null}".repeat(DEPTH / 2)
),
"arrays keep their order and objects are written sorted"
);
});
}