#![cfg(feature = "graph")]
use vyre_primitives::graph::sheaf::sheaf_diffusion_step;
use vyre_primitives::wire::pack_u32_slice as pack_u32;
use vyre_reference::value::Value;
const FIXED_ONE: f64 = 65536.0;
fn xorshift(state: &mut u32) -> u32 {
*state ^= *state << 13;
*state ^= *state >> 17;
*state ^= *state << 5;
*state
}
fn to_fixed(v: f64) -> u32 {
(v * FIXED_ONE).round() as i64 as u32
}
fn from_fixed(v: u32) -> f64 {
f64::from(v as i32) / FIXED_ONE
}
fn signed_half(state: &mut u32) -> f64 {
0.5 * f64::from((xorshift(state) % 13) as i32 - 6)
}
fn run_via_reference(stalks: &[u32], restriction: &[u32], damping: u32, cells: u32) -> Vec<u32> {
let program = sheaf_diffusion_step("stalks", "restriction", "damping", "stalks_next", cells, 1);
let outputs = vyre_reference::reference_eval(
&program,
&[
Value::from(pack_u32(stalks)),
Value::from(pack_u32(restriction)),
Value::from(pack_u32(&[damping])),
Value::from(pack_u32(&vec![0u32; cells as usize])),
],
)
.expect("sheaf_diffusion_step reference evaluation must succeed");
outputs[0]
.to_bytes()
.chunks_exact(4)
.map(|c| u32::from_le_bytes([c[0], c[1], c[2], c[3]]))
.collect()
}
#[test]
fn sheaf_diffusion_step_bit_exact_over_signed_stalks_and_restrictions() {
let mut state = 0x5E_AF_00_01u32;
let mut neg_restriction = 0u32;
let mut neg_stalk = 0u32;
let mut neg_output = 0u32;
for case in 0..400u32 {
let cells = 1 + case % 5; let damping_real = if case & 1 == 0 { 0.5 } else { 0.25 };
let damping = to_fixed(damping_real);
let stalks_f: Vec<f64> = (0..cells).map(|_| signed_half(&mut state)).collect();
let restriction_f: Vec<f64> = (0..cells).map(|_| signed_half(&mut state)).collect();
let stalks_fx: Vec<u32> = stalks_f.iter().map(|&v| to_fixed(v)).collect();
let restriction_fx: Vec<u32> = restriction_f.iter().map(|&v| to_fixed(v)).collect();
let got = run_via_reference(&stalks_fx, &restriction_fx, damping, cells);
assert_eq!(got.len(), cells as usize, "case {case}: output length");
for i in 0..cells as usize {
let s = stalks_f[i];
let r = restriction_f[i];
let want = s - damping_real * r * s;
let want_word = to_fixed(want);
assert_eq!(
got[i],
want_word,
"case {case} cell {i}: signed sheaf diffusion must be BIT-EXACT to s−damping·r·s; \
got={} want={want} (s={s} r={r} damping={damping_real})",
from_fixed(got[i])
);
if r < 0.0 {
neg_restriction += 1;
}
if s < 0.0 {
neg_stalk += 1;
}
if from_fixed(got[i]) < 0.0 {
neg_output += 1;
}
}
}
assert!(
neg_restriction > 200,
"sweep must feed negative restriction maps (signed-mul regime), got {neg_restriction}"
);
assert!(
neg_stalk > 200,
"sweep must feed negative stalk features, got {neg_stalk}"
);
assert!(
neg_output > 100,
"sweep must produce negative diffused stalks, got {neg_output}"
);
}
#[test]
fn sheaf_diffusion_step_hand_checked_negative_restriction() {
let got = run_via_reference(&[to_fixed(2.0)], &[to_fixed(-1.0)], to_fixed(0.5), 1);
assert_eq!(
from_fixed(got[0]),
3.0,
"negative restriction: heterophilic coupling INCREASES the stalk (2 → 3)"
);
let got = run_via_reference(&[to_fixed(-2.0)], &[to_fixed(1.0)], to_fixed(0.5), 1);
assert_eq!(
from_fixed(got[0]),
-1.0,
"negative stalk stays negative through the signed multiply (−2 → −1)"
);
let got = run_via_reference(&[to_fixed(-1.0)], &[to_fixed(-2.0)], to_fixed(0.5), 1);
assert_eq!(
from_fixed(got[0]),
-2.0,
"both-negative operands: the signed product damping·r·s = +1 subtracts to −2"
);
}