//! The audio scheme of the second marking layer, `wai-mark:band64` (extensions/synthesis.md //! §7.10), and its corpus `live-conformance/audio-mark/`: every generated file regenerates byte //! for byte and no other file but the measured records is there; every embedding, detection, //! case and record gets what it states; §7.10.1's definition, §7.10.3's shuffle and §7.10.6's //! bound are the reference's; and §7.10.7's tables are the measurements' and its claims attacks //! every measurement shows survived. use serde_json::Value; use std::collections::BTreeMap; use std::path::PathBuf; use wai::audio_mark::corpus; use wai::synthesis::mark::audio as band; fn wai_dir() -> PathBuf { PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("..") } fn tree(root: &PathBuf) -> BTreeMap> { let mut out = BTreeMap::new(); let mut stack = vec![root.clone()]; while let Some(d) = stack.pop() { for e in std::fs::read_dir(&d).expect("the corpus directory") { let p = e.expect("an entry").path(); if p.is_dir() { stack.push(p); continue; } let rel = p.strip_prefix(root).expect("under the root").to_string_lossy().replace('\\', "/"); if rel != "README.md" { out.insert(rel, std::fs::read(&p).expect("a file")); } } } out } fn committed() -> BTreeMap> { tree(&wai_dir().join("live-conformance/audio-mark")) } fn section(text: &str, head: &str) -> String { let at = text.find(&format!("\n{head}")).unwrap_or_else(|| panic!("a section {head}")); let level = head.chars().take_while(|c| *c == '#').count(); let end = text[at + 1..] .match_indices("\n#") .find(|(i, _)| text[at + 1 + i + 1..].chars().take_while(|c| *c == '#').count() <= level) .map_or(text.len(), |(i, _)| at + 1 + i); text[at..end].to_string() } fn fold(s: &str) -> String { s.split_whitespace().collect::>().join(" ") } fn synthesis() -> String { std::fs::read_to_string(wai_dir().join("extensions/synthesis.md")).expect("synthesis.md") } #[test] fn the_committed_corpus_regenerates_from_its_seeds() { let generated: BTreeMap> = corpus::generate().into_iter().collect(); let files = committed(); for k in generated.keys() { assert!(files.get(k) == generated.get(k), "{k} differs from its regeneration, or is missing"); } for k in files.keys() { assert!(generated.contains_key(k) || k.starts_with("robustness/measured/") || corpus::is_stream_measured(k), "{k} is not generated"); } } #[test] fn every_vector_case_and_record_gets_what_it_states() { let fails = corpus::verify(&committed()); assert!(fails.is_empty(), "{fails:#?}"); } #[test] fn the_extension_states_the_references_definition_shuffle_and_bound() { let s = synthesis(); let def = section(&s, "#### 7.10.1 "); let block = def.split("```").nth(1).expect("the definition's block"); let joined: String = block.lines().map(str::trim).collect(); assert_eq!(joined, band::definition_text(), "§7.10.1's definition is the reference's"); let digest: String = band::digest().iter().map(|b| format!("{b:02x}")).collect(); assert!(fold(&def).contains(&format!("its digest is `{digest}`")), "§7.10.1 states the digest"); let lay = band::layout(); let head = format!("π begins {}, {}, {}, {}", lay.perm[0], lay.perm[1], lay.perm[2], lay.perm[3]); assert!(fold(§ion(&s, "#### 7.10.3 ")).contains(&head), "§7.10.3 states the shuffle's first entries"); let bound = corpus::false_positive_log2(corpus::ATTEMPTS); assert!(fold(§ion(&s, "#### 7.10.6 ")).contains(&format!("2^−{}", bound.trim_start_matches('-'))), "§7.10.6 states {bound}"); } /// §7.10.7's tables, by their first data column's header: attack → column → cell. fn tables(s: &str) -> BTreeMap>> { let mut out: BTreeMap>> = BTreeMap::new(); let mut head: Option> = None; for line in s.lines() { let cells: Vec = line.trim().trim_matches('|').split('|').map(|c| c.trim().trim_matches('`').to_string()).collect(); if line.starts_with("| attack") { out.entry(cells[1].clone()).or_default(); head = Some(cells); } else if let (Some(h), true) = (&head, line.starts_with("| `")) { let row: BTreeMap = h[1..].iter().cloned().zip(cells[1..].iter().cloned()).collect(); out.get_mut(&h[1]).expect("a table").insert(cells[0].clone(), row); } else if !line.starts_with('|') { head = None; } } out } #[test] fn the_tables_are_the_measurements_and_the_claims_were_survived() { let files = committed(); let mut rows: BTreeMap<(String, String), Value> = BTreeMap::new(); let mut add = |b: &[u8]| { let v: Value = serde_json::from_slice(b).expect("a measurement"); for r in v["table"].as_array().expect("a table") { rows.insert((r["attack"].as_str().unwrap_or("").into(), r["set"].as_str().unwrap_or("").into()), r.clone()); } }; add(&files["battery-band64.json"]); for (k, b) in &files { if k.starts_with("robustness/measured/measurement-") { add(b); } } let text = section(&synthesis(), "#### 7.10.7 "); let t = tables(&text); assert!(t.contains_key("voice"), "§7.10.7 has the pinned attacks' table"); for table in t.values() { for (attack, cells) in table { for (col, cell) in cells { let r = rows.get(&(attack.clone(), col.clone())).unwrap_or_else(|| panic!("no measurement of {attack} on {col}")); let mut want = format!("{} / {}", r["survived"], r["items"]); if r["survived"].as_u64() != Some(0) { want += &format!(" ({})", r["errors_max"]); } assert_eq!(cell, &want, "§7.10.7 {attack}, {col}"); } } } let f = fold(&text); let claims = f.split("**claims to survive** ").nth(1).and_then(|x| x.split('.').next()).expect("§7.10.7's claims"); for a in claims.split('`').skip(1).step_by(2) { let sets: Vec<&Value> = rows.iter().filter(|((at, _), _)| at == a).map(|(_, r)| r).collect(); assert!(!sets.is_empty(), "{a} is measured"); for r in sets { assert!(r["survived"] == r["items"] && r["false_positives"] == 0, "§7.10.7 claims {a}, which {} did not survive", r["set"]); } } } /// §7.10.8's table, by delivery and column: each cell is the streaming measurement's tally of its /// attack (``, `-lkfs14`, `-lkfs16`), every attack of the measurement /// has its cell, and every attack §7.10.8 claims every piece survived with no false positive. #[test] fn the_streaming_table_is_the_streaming_measurement_and_the_claims_were_survived() { let files = committed(); let m: Value = serde_json::from_slice(files.get("robustness/streaming/measurement-music.json").expect("the streaming measurement")).expect("JSON"); let rows: BTreeMap = m["table"].as_array().expect("a table").iter().map(|r| (r["attack"].as_str().unwrap_or("").to_string(), r.clone())).collect(); let text = section(&synthesis(), "#### 7.10.8 "); let suffix = |head: &str| match head { "as delivered" => Some(""), "−14 LKFS" => Some("-lkfs14"), "−16 LKFS" => Some("-lkfs16"), _ => None, }; let mut head: Option> = None; let mut seen = 0; for line in text.lines() { let cells: Vec = line.trim().trim_matches('|').split('|').map(|c| c.trim().trim_matches('`').to_string()).collect(); if line.starts_with("| delivery") { head = Some(cells); } else if let (Some(h), true) = (&head, line.starts_with("| `")) { for (col, cell) in h[1..].iter().zip(&cells[1..]) { let attack = format!("{}{}", cells[0], suffix(col).unwrap_or_else(|| panic!("a column {col}"))); let r = rows.get(&attack).unwrap_or_else(|| panic!("no measurement of {attack}")); let mut want = format!("{} / {}", r["survived"], r["items"]); if r["survived"].as_u64() != Some(0) { want += &format!(" ({})", r["errors_max"]); } assert_eq!(cell, &want, "§7.10.8 {attack}"); seen += 1; } } } assert_eq!(seen, rows.len(), "§7.10.8's table has a cell for every attack of the streaming measurement"); let f = fold(&text); if let Some(claims) = f.split("**claims to survive** ").nth(1).and_then(|x| x.split('.').next()) { for a in claims.split('`').skip(1).step_by(2) { let r = rows.get(a).unwrap_or_else(|| panic!("§7.10.8 claims {a}, which is not measured")); assert!(r["survived"] == r["items"] && r["false_positives"] == 0 && r["wrong"] == 0, "§7.10.8 claims {a}, which a piece did not survive"); } } }