open-standards/wai/wai-vector/tests/canonical.rs
Transaction Science 2f147d2a30
Some checks failed
standards workspaces build + test / jouleclaw-rs (pull_request) Successful in 2m13s
standards workspaces build + test / aac-rs (pull_request) Successful in 1m58s
standards workspaces build + test / joule-code-rs (pull_request) Successful in 50s
standards workspaces build + test / joulecontract-rs (pull_request) Successful in 34s
standards workspaces build + test / map-rs (pull_request) Successful in 31s
standards workspaces build + test / mesh-rs (pull_request) Successful in 31s
no-vanity-metrics / Scan committed Markdown for vanity metrics (pull_request) Has been cancelled
no-vanity-metrics / WAI public text names no comparison (pull_request) Has been cancelled
no-vanity-metrics / WAI public text names no company or product (pull_request) Has been cancelled
standards workspaces build + test / arl-rs (pull_request) Has been cancelled
standards workspaces build + test / eoc-rs (pull_request) Has been cancelled
standards workspaces build + test / jcp-rs (pull_request) Has been cancelled
standards workspaces build + test / joule-ui-rs (pull_request) Has been cancelled
standards workspaces build + test / jouledesk-rs (pull_request) Has been cancelled
standards workspaces build + test / joulehook-rs (pull_request) Has been cancelled
standards workspaces build + test / proof-rs (pull_request) Has been cancelled
standards workspaces build + test / sandbox-rs (pull_request) Has been cancelled
standards workspaces build + test / sense-rs (pull_request) Has been cancelled
standards workspaces build + test / smart-byte-rs (pull_request) Has been cancelled
standards workspaces build + test / macOS energy backends (pull_request) Has been cancelled
WAI conformance / cargo-class corpora · linux (pull_request) Has been cancelled
WAI conformance / cargo-class corpora · linux-arm64 (pull_request) Has been cancelled
WAI conformance / cargo-class corpora · macos (pull_request) Has been cancelled
WAI conformance / quantum core and corpus under wasm32-wasip2 (wasmtime) (pull_request) Has been cancelled
WAI conformance / v2 vocabulary · manifests validate as stated (pull_request) Has been cancelled
WAI conformance / independent verifiers · claims, JWP, key discovery and integer payloads (Python) (pull_request) Has been cancelled
WAI conformance / delivery telemetry · CMCD and CMSD against an independent library (pull_request) Has been cancelled
WAI conformance / cross-arch byte-equality · linux (pull_request) Has been cancelled
WAI conformance / cross-arch byte-equality · linux-arm64 (pull_request) Has been cancelled
WAI conformance / cross-arch byte-equality · macos (pull_request) Has been cancelled
WAI conformance / C2PA manifest emission · signed, read back, validated (pull_request) Has been cancelled
WAI energy receipts / energy receipts · linux (pull_request) Has been cancelled
WAI energy receipts / energy receipts · linux-arm64 (pull_request) Has been cancelled
WAI energy receipts / energy receipts · macos (pull_request) Has been cancelled
WAI section references / cited sections exist (pull_request) Has been cancelled
wai-vector vector graphics / linux-aarch64 (pull_request) Has been cancelled
wai-vector vector graphics / linux-x86_64 (pull_request) Has been cancelled
wai-vector vector graphics / macos-arm64 (pull_request) Has been cancelled
wai-vector vector graphics / wasm32-wasip2 (wasmtime) (pull_request) Has been cancelled
wai-vector vector graphics / wasm32 under Node (pull_request) Has been cancelled
wai-vector vector graphics / fuzz, peak allocation, comparison count (pull_request) Has been cancelled
workflow hygiene / every workflow supersedes its own queue (pull_request) Has been cancelled
standards workspaces build + test / openpay (pull_request) Has been cancelled
wai-vector: WOS1 and WVG1 with the refusal order of §2.7, the clock, tracks, the interval check and the cost (D7-2)
The reference crate of wai.graphics.vector (no_std + alloc, no
dependencies; SHA-256 is wai-ops's, by #[path]):
- WOS1 parsed with §2.1's load checks in order, and written;
- WVG1 split (steps 1-3, nothing allocated before the exact length),
  the record pass (step 4), the cross-record checks (step 5(a)-(h)),
  the sets by digest with at most length + 1 bytes read (step 6), the
  glyphs (7), the interval check (8) and the per-frame cost (9);
- the clock with the frame range by division, track evaluation, and
  placement;
- the JSON authoring form, one member per field, packing back to the
  same bytes;
- the CLI: check, cost, pack, unpack, vectors gen and verify;
- a wasm32 module and verify.mjs for Node.

The corpus wai/vector-conformance/ (arith, canonical, refusals,
geometry, cost) with its README's provenance line and a sha256.json
sidecar naming the div255 table; wai-vector.yml with native, wasip2,
node and fuzz jobs.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-10-11 03:59:47 -04:00

84 lines
3.2 KiB
Rust

//! §2.5: one encoding per scene. Every payload the parser reads back writes the same
//! bytes, the authoring form round-trips, and duplicates are found by sorting.
use std::cell::Cell;
use std::cmp::Ordering;
use wai_vector::vectors::all_vectors;
use wai_vector::wvg1::{self, Doc, Key, Track, cmp_tracks, has_duplicate};
use wai_vector::{form, wos1};
#[test]
fn canonical_round_trip_on_every_vector() {
let (mut n, mut forms) = (0, 0);
for v in all_vectors() {
match v.ext {
"wvg1" => {
// Every payload the form can split goes to the form and back unchanged.
let f = form::to_json(&v.bytes, "");
assert_eq!(form::to_bytes(&f).as_deref(), Ok(&v.bytes[..]), "{}: form round trip", v.name);
forms += 1;
if let Ok(d) = wvg1::parse(&v.bytes) {
assert_eq!(d.write(), v.bytes, "{}: parse then write", v.name);
n += 1;
}
}
"wos1" => {
if let Ok(w) = wos1::parse(&v.bytes) {
assert_eq!(w.write(), v.bytes, "{}: parse then write", v.name);
n += 1;
}
}
_ => {}
}
}
assert!(n > 25 && forms > 200, "{n} round trips, {forms} forms");
}
/// Review A's F10 shape: 65,535 tracks of 64 keys, equal but for the last key, in a
/// fixed shuffle. Detection sorts, so it compares at most 2·n·⌈log2 n⌉ times, never
/// every pair (about 2^31).
#[test]
fn duplicates_by_sorting_comparison_count() {
let n = 65_535usize;
let k = 64u32;
let mut order: Vec<u32> = (0..n as u32).collect();
let mut s = 0x9E37_79B9_7F4A_7C15u64;
for i in (1..n).rev() {
s ^= s << 13;
s ^= s >> 7;
s ^= s << 17;
order.swap(i, (s % (i as u64 + 1)) as usize);
}
let mut d = Doc::default();
for &v in &order {
d.tracks.push(Track { kind: 2, zero1: 0, zero2: 0, n_keys: k });
for j in 0..k {
let value = if j + 1 == k { v as i32 } else { 0 };
d.keys.push(Key { t: j, value, interp: 0, zero: [0; 3] });
}
}
d.index();
let bound = 2 * n as u64 * u64::from(usize::BITS - (n - 1).leading_zeros());
assert_eq!(bound, 2 * 65_535 * 16);
let count = Cell::new(0u64);
let cmp = |a: usize, b: usize| -> Ordering {
count.set(count.get() + 1);
assert!(count.get() <= bound, "more than {bound} comparisons");
cmp_tracks(&d, a, b)
};
assert!(!has_duplicate(n, cmp), "no two tracks are equal");
assert!(count.get() <= bound);
eprintln!("{n} tracks: {} comparisons, bound {bound}, pairs {}", count.get(), n as u64 * (n as u64 - 1) / 2);
// Two equal tracks are found within the same bound.
let last = d.ix.track_key0[n - 1] as usize + k as usize - 1;
let other = d.ix.track_key0[n - 2] as usize + k as usize - 1;
d.keys[last].value = d.keys[other].value;
count.set(0);
let cmp = |a: usize, b: usize| -> Ordering {
count.set(count.get() + 1);
assert!(count.get() <= bound, "more than {bound} comparisons");
cmp_tracks(&d, a, b)
};
assert!(has_duplicate(n, cmp), "the two equal tracks are found");
}