//! The tensor-file prior forms (`extensions/prior-carriage.md` §9 to §14): the committed //! corpus regenerates byte for byte, the extension's §13 lists the codes the reference //! reports, and a prior carried in every form loads into the integer kernels as the native //! artifact does: the same `WSR1` prior and the same derivation output, the same `WHM1` //! model. use std::collections::BTreeMap; use std::path::{Path, PathBuf}; use wai::prior_wire::{self, corpus, Form, CODES}; fn root() -> PathBuf { Path::new(env!("CARGO_MANIFEST_DIR")).join("../live-conformance/prior-forms") } fn walk(root: &Path, rel: &str, out: &mut BTreeMap>) { for e in std::fs::read_dir(root.join(rel)).unwrap().flatten() { let name = e.file_name().to_string_lossy().into_owned(); let key = if rel.is_empty() { name } else { format!("{rel}/{name}") }; if e.path().is_dir() { walk(root, &key, out); } else if key != "README.md" && key != ".gitattributes" { out.insert(key, std::fs::read(e.path()).unwrap()); } } } fn natives() -> BTreeMap> { corpus::INPUT_NATIVES.iter().map(|(n, _, p, _)| (n.to_string(), std::fs::read(root().join(p)).unwrap())).collect() } #[test] fn the_committed_corpus_regenerates_byte_for_byte() { let mut files = BTreeMap::new(); walk(&root(), "", &mut files); let bad = corpus::verify(&files, &natives()); assert!(bad.is_empty(), "{bad:#?}"); } #[test] fn the_extension_lists_every_code_the_reference_reports() { let text = std::fs::read_to_string(Path::new(env!("CARGO_MANIFEST_DIR")).join("../extensions/prior-carriage.md")).unwrap(); let sec = text.split("\n## 13.").nth(1).expect("§13").split("\n## ").next().unwrap(); let listed: Vec<&str> = sec.lines().filter_map(|l| l.strip_prefix("| `")).filter_map(|l| l.split('`').next()).collect(); assert_eq!(listed, CODES, "§13's table, in order"); } #[test] fn a_prior_in_every_form_loads_into_the_kernels_as_the_native_does() { let nat = natives(); // A picture, every sample an integer function of its position. let (w, h) = (12usize, 9usize); let pic: Vec = (0..w * h * 3).map(|i| ((i * 37 + i / 7 * 11) % 251) as u8).collect(); for &(name, binding, _, _) in corpus::INPUT_NATIVES { let native = &nat[name]; for form in Form::ALL { let file = prior_wire::encode(binding, native, form).unwrap(); let (b, back) = prior_wire::load(form, &file).unwrap(); assert_eq!(b, binding); assert_eq!(&back, native, "{name} in {}: the native bytes", form.label()); if binding == "wsr1/1" { assert_eq!(wai::int_sr::parse(&back).unwrap(), wai::int_sr::parse(native).unwrap()); let (g0, g1) = (wai::int_sr::load(native).unwrap(), wai::int_sr::load(&back).unwrap()); let (o0, o1) = (wai::int_sr::sr2(&g0, &pic, w, h).unwrap(), wai::int_sr::sr2(&g1, &pic, w, h).unwrap()); assert_eq!(o0, o1, "{name} in {}: the derivation's output", form.label()); } else { // The same bytes, so the kernel's model reads the same; it reads at all. let (m0, m1) = (wai::int_hyper_synth::IntModel::from_bytes(native).unwrap(), wai::int_hyper_synth::IntModel::from_bytes(&back).unwrap()); assert_eq!((m0.afrac, m0.gf, m0.shift, m0.layers.len()), (m1.afrac, m1.gf, m1.shift, m1.layers.len())); } } } } #[test] fn one_prior_has_one_file_per_form_and_three_digests() { let nat = natives(); let native = &nat["generic"]; let digests: Vec = Form::ALL.iter().map(|&f| wai::pin::sha256_hex(&prior_wire::encode("wsr1/1", native, f).unwrap())).collect(); assert_eq!(digests.iter().collect::>().len(), 3); for &f in &Form::ALL { assert_eq!(prior_wire::encode("wsr1/1", native, f).unwrap(), prior_wire::encode("wsr1/1", native, f).unwrap()); } }