#![cfg(test)]
use crate::manifold::{
GraphCompressionKind, LocalAtlas, LocalAtlasConfig, observe_atlas_topology,
tests_topology_fixtures::{circle, cylinder_strip, mobius_strip, sphere, trefoil_knot},
};
use ndarray::{Array2, ArrayView2};
fn splitmix64(state: &mut u64) -> u64 {
*state = state.wrapping_add(0x9E37_79B9_7F4A_7C15);
let mut z = *state;
z = (z ^ (z >> 30)).wrapping_mul(0xBF58_476D_1CE4_E5B9);
z = (z ^ (z >> 27)).wrapping_mul(0x94D0_49BB_1331_11EB);
z ^ (z >> 31)
}
fn next_unit(state: &mut u64) -> f64 {
let bits = splitmix64(state) >> 11;
(bits as f64 + 1.0) / ((1u64 << 53) as f64 + 1.0)
}
fn structureless_cloud(n: usize, p: usize, seed: u64) -> Array2<f64> {
let mut state = seed;
let mut z = Array2::<f64>::zeros((n, p));
for row in 0..n {
for col in 0..p {
let u1 = next_unit(&mut state);
let u2 = next_unit(&mut state);
z[[row, col]] = (-2.0 * u1.ln()).sqrt() * (std::f64::consts::TAU * u2).cos();
}
}
z
}
fn verdict(z: ArrayView2<'_, f64>, d: usize) -> (Option<GraphCompressionKind>, String) {
let config = LocalAtlasConfig::balanced(z.nrows(), d);
let atlas = match LocalAtlas::build(z, config) {
Ok(atlas) => atlas,
Err(error) => return (None, format!("build refused: {error:?}")),
};
match observe_atlas_topology(&atlas) {
Ok(readout) => {
let inv = readout.invariants();
let line = format!(
"{readout} max_mult={} mean_mult={:.3}",
inv.max_cover_multiplicity, inv.mean_cover_multiplicity
);
(readout.observed_manifold(), line)
}
Err(error) => (None, format!("readout errored: {error}")),
}
}
fn matched_pairs() -> Vec<(&'static str, Array2<f64>, GraphCompressionKind, Array2<f64>, usize)> {
vec![
(
"circle",
circle(400, 2.0),
GraphCompressionKind::Circle,
structureless_cloud(400, 3, 0x2280_0001),
1,
),
(
"trefoil",
trefoil_knot(600, 1.0),
GraphCompressionKind::Circle,
structureless_cloud(600, 3, 0x2280_0002),
1,
),
(
"sphere",
sphere(900),
GraphCompressionKind::Sphere,
structureless_cloud(900, 3, 0x2280_0003),
2,
),
(
"cylinder",
cylinder_strip(40, 10),
GraphCompressionKind::Cylinder,
structureless_cloud(400, 3, 0x2280_0004),
2,
),
(
"mobius",
mobius_strip(40, 10),
GraphCompressionKind::MobiusStrip,
structureless_cloud(400, 3, 0x2280_0006),
2,
),
]
}
#[test]
fn structureless_noise_earns_no_topology_and_the_planted_shapes_still_do_2280() {
struct Row {
label: &'static str,
d: usize,
n: usize,
p: usize,
expected: GraphCompressionKind,
planted_named: Option<GraphCompressionKind>,
planted_why: String,
noise_named: Option<GraphCompressionKind>,
noise_why: String,
}
let rows: Vec<Row> = matched_pairs()
.into_iter()
.map(|(label, planted, expected, noise, d)| {
let (planted_named, planted_why) = verdict(planted.view(), d);
let (noise_named, noise_why) = verdict(noise.view(), d);
Row {
label,
d,
n: planted.nrows(),
p: planted.ncols(),
expected,
planted_named,
planted_why,
noise_named,
noise_why,
}
})
.collect();
for row in &rows {
eprintln!(
"[2280-null] {} d={} n={} p={}\n planted: {}\n noise: {}",
row.label, row.d, row.n, row.p, row.planted_why, row.noise_why
);
}
for row in &rows {
assert_eq!(
row.planted_named,
Some(row.expected),
"the {} positive control lost its name at d={}: {}. The null arm is \
only meaningful while this harness can still recognize a manifold it \
is shown.",
row.label,
row.d,
row.planted_why
);
assert_eq!(
row.noise_named, None,
"structureless noise at the {} cell (d={}, same n={} and p={} as the \
planted arm) was NAMED {:?}: {}. The atlas's only promotable property \
on #2280 is that every error it makes is an abstention; asserting a \
manifold in noise refutes it.",
row.label, row.d, row.n, row.p, row.noise_named, row.noise_why
);
}
}
#[test]
fn the_null_is_bit_identical_run_to_run_2280() {
for d in [1usize, 2] {
let first = structureless_cloud(500, 4, 0x2280_0005);
let second = structureless_cloud(500, 4, 0x2280_0005);
assert_eq!(first, second, "the cloud generator is not deterministic");
let (a, why_a) = verdict(first.view(), d);
let (b, why_b) = verdict(second.view(), d);
eprintln!("[2280-null] determinism d={d}: {why_a} | {why_b}");
assert_eq!(a, b, "the null verdict is not bit-identical at d={d}");
assert_eq!(why_a, why_b, "the null refusal is not stable at d={d}");
assert_eq!(a, None, "noise in R^4 read at d={d} was named {a:?}: {why_a}");
}
}
fn rolled_sheet(n_t: usize, n_h: usize, turns: f64) -> Array2<f64> {
const INNER_RADIUS: f64 = 1.0;
const OUTER_RADIUS: f64 = 4.0;
const HEIGHT: f64 = 2.0;
let n = n_t * n_h;
let mut z = Array2::<f64>::zeros((n, 3));
let mut row = 0usize;
for it in 0..n_t {
let s = (it as f64) / (n_t as f64 - 1.0);
let radius = INNER_RADIUS + (OUTER_RADIUS - INNER_RADIUS) * s;
let angle = std::f64::consts::TAU * turns * s;
for ih in 0..n_h {
z[[row, 0]] = radius * angle.cos();
z[[row, 1]] = radius * angle.sin();
z[[row, 2]] = HEIGHT * (ih as f64) / (n_h as f64 - 1.0);
row += 1;
}
}
z
}
#[test]
fn a_rolled_sheet_is_a_disk_at_every_winding_or_nothing_at_all_2280() {
let mut rows = Vec::new();
for turns in [0.25_f64, 0.5, 1.0, 1.5, 2.5] {
let z = rolled_sheet(40, 12, turns);
let (named, why) = verdict(z.view(), 2);
rows.push((turns, named, why));
}
for (turns, _, why) in &rows {
eprintln!("[2280-roll] turns={turns:>4} {why}");
}
let gentle = rows
.first()
.expect("the sweep has a gentle arm by construction");
assert_eq!(
gentle.1,
Some(GraphCompressionKind::Disk),
"a barely-curved sheet must read as a disk, or the sweep below measures a \
broken harness rather than the winding: {}",
gentle.2
);
for (turns, named, why) in &rows {
assert!(
matches!(named, None | Some(GraphCompressionKind::Disk)),
"a rolled sheet is intrinsically a disk at every winding, so the readout \
may name `Disk` or refuse — naming anything else is a misnaming, which is \
the one property this readout has never violated. turns={turns}: {why}"
);
}
}