#![cfg(feature = "cpu-parity")]
use vyre_primitives::graph::path_reconstruct::cpu_ref;
use vyre_self_substrate::path_reconstruct::reconstruct_path_via;
mod common;
use common::ReferenceEvalDispatcher;
fn xorshift(state: &mut u32) -> u32 {
*state ^= *state << 13;
*state ^= *state >> 17;
*state ^= *state << 5;
*state
}
#[test]
fn reconstruct_path_via_matches_cpu_ref_over_random_forests() {
let d = ReferenceEvalDispatcher;
let mut rng = 0x9A_7C_00_01u32;
let mut real_walk = 0u32; let mut hit_root = 0u32; for case in 0..400u32 {
let n = 2 + (case % 12); let parent: Vec<u32> = (0..n).map(|_| xorshift(&mut rng) % n).collect();
let target = xorshift(&mut rng) % n;
let max_depth = 1 + xorshift(&mut rng) % (n + 2);
let mut got_scratch = Vec::new();
let got_len = reconstruct_path_via(&d, &parent, target, max_depth, &mut got_scratch)
.expect("reconstruct_path_via must dispatch the path walk");
let mut want_scratch = Vec::new();
let want_len = cpu_ref(&parent, target, max_depth, &mut want_scratch);
assert_eq!(
got_len, want_len,
"case {case}: path length must match cpu_ref; n={n} target={target} max_depth={max_depth} parent={parent:?}"
);
assert_eq!(
got_scratch, want_scratch,
"case {case}: the full zero-padded path buffer must match cpu_ref; n={n} target={target} max_depth={max_depth} parent={parent:?}"
);
assert_eq!(
got_scratch[0], target,
"case {case}: the walk starts at the target node"
);
if want_len >= 2 {
real_walk += 1;
}
if want_len < max_depth {
hit_root += 1;
}
}
assert!(
real_walk > 200,
"sweep must exercise multi-node walks, got {real_walk}"
);
assert!(
hit_root > 100,
"sweep must exercise walks that terminate at a root before max_depth, got {hit_root}"
);
}
#[test]
fn reconstruct_path_via_hand_checked_chain_and_root() {
let d = ReferenceEvalDispatcher;
let parent = [0u32, 0, 1, 2];
let mut scratch = Vec::new();
let len = reconstruct_path_via(&d, &parent, 3, 8, &mut scratch).unwrap();
let mut want_scratch = Vec::new();
let want_len = cpu_ref(&parent, 3, 8, &mut want_scratch);
assert_eq!(len, want_len);
assert_eq!(scratch, want_scratch);
assert_eq!(len, 4, "3 -> 2 -> 1 -> 0 is a 4-node path");
assert_eq!(&scratch[..4], &[3, 2, 1, 0], "walk order is target-to-root");
assert!(
scratch[4..].iter().all(|&v| v == 0),
"the tail is zero-padded to max_depth"
);
let mut scratch = Vec::new();
let len = reconstruct_path_via(&d, &parent, 3, 2, &mut scratch).unwrap();
assert_eq!(len, 2, "max_depth=2 truncates the walk to two nodes");
assert_eq!(
scratch,
vec![3, 2],
"truncated walk keeps the first two nodes, no padding needed"
);
let parent = [0u32, 1, 1];
let mut scratch = Vec::new();
let len = reconstruct_path_via(&d, &parent, 1, 4, &mut scratch).unwrap();
assert_eq!(len, 1, "a root node walks to itself only");
assert_eq!(scratch, vec![1, 0, 0, 0], "single node then zero padding");
assert_eq!(len, cpu_ref(&parent, 1, 4, &mut Vec::new()));
}