aien-protocols-mv
Canonical specifications, wire protocols, and schemas for the AIEN sovereign ecosystem
Brought by drakestapleton
aien-protocols-mv / crates / aien-microjob / tests / vectors.rs
| 1 | //! Walks `specs/microjob/vectors/expected.txt` and requires the Rust reader to |
| 2 | //! print the same verdict line as the C reference checker |
| 3 | //! (`tools/mjb_verify.c`) for every file, and to write every accepted document |
| 4 | //! back out as the identical bytes. |
| 5 | |
| 6 | use std::path::PathBuf; |
| 7 | |
| 8 | use aien_microjob::{decode, encode, verdict_line}; |
| 9 | |
| 10 | fn vectors_dir() -> PathBuf { |
| 11 | PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("../../specs/microjob/vectors") |
| 12 | } |
| 13 | |
| 14 | #[test] |
| 15 | fn every_expected_line_matches_the_c_checker() { |
| 16 | let dir = vectors_dir(); |
| 17 | let list = std::fs::read_to_string(dir.join("expected.txt")).expect("read expected.txt"); |
| 18 | let mut checked = 0usize; |
| 19 | let mut accepted = 0usize; |
| 20 | let mut failures = Vec::new(); |
| 21 | for line in list.lines() { |
| 22 | if line.is_empty() || line.starts_with('#') { |
| 23 | continue; |
| 24 | } |
| 25 | let (name, want) = line |
| 26 | .split_once(' ') |
| 27 | .expect("line has a file name and a verdict"); |
| 28 | let bytes = std::fs::read(dir.join(name)).expect("read vector"); |
| 29 | let got = verdict_line(&bytes); |
| 30 | if got != want { |
| 31 | failures.push(format!("{name}\n expected: {want}\n got: {got}")); |
| 32 | } |
| 33 | if let Ok(dag) = decode(&bytes) { |
| 34 | let again = encode(&dag).expect("re-encode accepted document"); |
| 35 | if again != bytes { |
| 36 | failures.push(format!("{name}: round trip gave different bytes")); |
| 37 | } |
| 38 | accepted += 1; |
| 39 | } |
| 40 | checked += 1; |
| 41 | } |
| 42 | assert!( |
| 43 | failures.is_empty(), |
| 44 | "{} of {checked} vectors differ:\n{}", |
| 45 | failures.len(), |
| 46 | failures.join("\n") |
| 47 | ); |
| 48 | assert!( |
| 49 | checked >= 100, |
| 50 | "expected at least 100 vectors, found {checked}" |
| 51 | ); |
| 52 | assert!( |
| 53 | accepted >= 8, |
| 54 | "expected at least 8 accepted vectors, found {accepted}" |
| 55 | ); |
| 56 | } |