use polydat::dsl::compile_polydat_kernel;
fn run(src: &str, names: &[&str]) -> Vec<String> {
let mut k = compile_polydat_kernel(src).unwrap_or_else(|e| panic!("{e}\n{src}"));
k.set_inputs(&[0]);
names
.iter()
.map(|n| k.pull(n).to_display_string())
.collect()
}
const ORDS: &str = concat!(
"input cycle: u64\n",
"list := \"[3, 9, 14, 20]\"\n",
"ords := str_to_vec_i32(list)\n",
);
#[test]
fn a_list_answers_its_length_elements_and_bounds() {
let src = format!(
"{ORDS}{}",
concat!(
"n := vec_len_i32(ords)\n",
"first := vec_at_i32(ords, 0)\n",
"third := vec_at_i32(ords, 2)\n",
"past := vec_at_i32(ords, 99)\n",
"hi := vec_max_i32(ords)\n",
"lo := vec_min_i32(ords)\n",
)
);
assert_eq!(
run(&src, &["n", "first", "third", "past", "hi", "lo"]),
vec!["4", "3", "14", "0", "20", "3"]
);
}
#[test]
fn a_rank_is_a_position_not_a_counter() {
let src = format!(
"{ORDS}{}",
concat!(
"rank_by_position := vec_position_i32(ords, to_i64(14))\n",
"rank_by_count := vec_count_below_i32(ords, to_i64(14))\n",
"absent := vec_position_i32(ords, to_i64(7))\n",
"below_absent := vec_count_below_i32(ords, to_i64(7))\n",
"matches := vec_len_i32(ords)\n",
)
);
assert_eq!(
run(
&src,
&[
"rank_by_position",
"rank_by_count",
"absent",
"below_absent",
"matches"
]
),
vec!["2", "2", "4", "1", "4"]
);
}
#[test]
fn membership_and_set_comparisons_ignore_order_and_repetition() {
let src = concat!(
"input cycle: u64\n",
"a := str_to_vec_i32(\"[1, 2, 3]\")\n",
"b := str_to_vec_i32(\"[3, 2, 1, 1]\")\n",
"c := str_to_vec_i32(\"[3, 4]\")\n",
"has := vec_contains_i32(a, to_i64(2))\n",
"hasnt := vec_contains_i32(a, to_i64(42))\n",
"same := vec_set_eq_i32(a, b)\n",
"differ := vec_set_eq_i32(a, c)\n",
"overlap := vec_intersect_count_i32(a, c)\n",
"overlap_dup := vec_intersect_count_i32(a, b)\n",
);
assert_eq!(
run(
src,
&["has", "hasnt", "same", "differ", "overlap", "overlap_dup"]
),
vec!["1", "0", "1", "0", "1", "3"]
);
}
#[test]
fn a_name_yields_the_number_in_its_tail() {
let src = concat!(
"input cycle: u64\n",
"whole := str_to_u64(\"4242\")\n",
"malformed := str_to_u64(\"4242x\")\n",
"label := str_digit_suffix(\"label-17\")\n",
"under := str_digit_suffix(\"profile_8\")\n",
"embedded := str_digit_suffix(\"sift1m\")\n",
"none := str_digit_suffix(\"plain\")\n",
);
assert_eq!(
run(
src,
&["whole", "malformed", "label", "under", "embedded", "none"]
),
vec!["4242", "0", "17", "8", "0", "0"]
);
}
#[test]
fn the_i64_forms_read_the_same_way() {
let src = concat!(
"input cycle: u64\n",
"v := str_to_vec_i64(\"[10, 20, 30]\")\n",
"n := vec_len_i64(v)\n",
"at := vec_at_i64(v, 1)\n",
"hi := vec_max_i64(v)\n",
"lo := vec_min_i64(v)\n",
"has := vec_contains_i64(v, to_i64(20))\n",
"pos := vec_position_i64(v, to_i64(30))\n",
"below := vec_count_below_i64(v, to_i64(25))\n",
"same := vec_set_eq_i64(v, v)\n",
"overlap := vec_intersect_count_i64(v, v)\n",
);
assert_eq!(
run(
src,
&[
"n", "at", "hi", "lo", "has", "pos", "below", "same", "overlap"
]
),
vec!["3", "20", "30", "10", "1", "2", "2", "1", "3"]
);
}
#[test]
fn the_recall_audit_quantities_are_functions_of_the_list() {
use polydat::{Engine, JitMode, Provenance};
let src = concat!(
"input cycle: u64\n",
"matching := str_to_vec_i32(\"[3, 9, 14, 20, 27]\")\n",
"ordinal := to_i64(14)\n",
"rank := vec_count_below_i32(matching, ordinal)\n",
"cardinality := vec_len_i32(matching)\n",
);
for engine in [
Engine::Interpreter(JitMode::Off),
Engine::Interpreter(JitMode::Auto),
Engine::Closures(Provenance::Auto),
Engine::Native(Provenance::Auto),
] {
let Ok(mut k) = polydat::dsl::compile_polydat_with(src, engine) else {
continue; };
k.set_inputs(&[0]);
assert_eq!(k.pull("rank").as_u64(), 2, "{engine}");
assert_eq!(k.pull("cardinality").as_u64(), 5, "{engine}");
}
}