From 5e1b4de98be6dbf6024105c117b7dfefbba45dde Mon Sep 17 00:00:00 2001 From: Serkyo Date: Thu, 23 Jul 2026 03:45:21 +0200 Subject: [PATCH] test(client): relocate chunk streaming tests into src/tests --- crates/client/src/chunks.rs | 76 +------------------------------ crates/client/src/tests/chunks.rs | 76 +++++++++++++++++++++++++++++++ 2 files changed, 78 insertions(+), 74 deletions(-) create mode 100644 crates/client/src/tests/chunks.rs diff --git a/crates/client/src/chunks.rs b/crates/client/src/chunks.rs index 9198247..ca22e7a 100644 --- a/crates/client/src/chunks.rs +++ b/crates/client/src/chunks.rs @@ -277,77 +277,5 @@ pub fn desired_chunks(center: ChunkPos, radius: i32) -> HashSet { } #[cfg(test)] -mod tests { - use super::*; - - #[test] - fn center_is_always_included() { - let center = ChunkPos::new(0, 0, 0); - assert!(desired_chunks(center, 4).contains(¢er)); - } - - #[test] - fn excludes_columns_beyond_the_disc() { - let set = desired_chunks(ChunkPos::new(0, 0, 0), 4); - // One chunk past the radius along an axis: squared distance 25 > 16. - assert!(!set.contains(&ChunkPos::new(5, 0, 0))); - // The far corner: squared distance 4*4 + 4*4 = 32 > 16. - assert!(!set.contains(&ChunkPos::new(4, 0, 4))); - } - - #[test] - fn vertical_extent_is_half_the_radius() { - let set = desired_chunks(ChunkPos::new(0, 0, 0), 4); - // radius / 2 == 2, so the column at the center spans y in [-2, 2]. - assert!(set.contains(&ChunkPos::new(0, 2, 0))); - assert!(!set.contains(&ChunkPos::new(0, 3, 0))); - } - - #[test] - fn set_is_translation_invariant() { - // Shifting the center shifts every member by the same offset; this also exercises negative coordinates on the shifted side. - let base = desired_chunks(ChunkPos::new(0, 0, 0), 3); - let shifted: HashSet = base - .iter() - .map(|p| ChunkPos::new(p.x - 10, p.y - 10, p.z - 10)) - .collect(); - assert_eq!(shifted, desired_chunks(ChunkPos::new(-10, -10, -10), 3)); - } - - /// Builds a residency predicate over a fixed set of positions. - fn resident_in(set: &[ChunkPos]) -> impl Fn(ChunkPos) -> bool + '_ { - move |pos| set.contains(&pos) - } - - #[test] - fn loaded_chunk_remeshes_self_and_resident_neighbors() { - let p = ChunkPos::new(0, 0, 0); - let east = ChunkPos::new(1, 0, 0); - let down = ChunkPos::new(0, -1, 0); - // p plus two of its six neighbours are resident; the other four are not. - let resident = [p, east, down]; - let targets = remesh_targets(&[p], &[], resident_in(&resident)); - assert_eq!(targets, resident.into_iter().collect()); - } - - #[test] - fn dropped_chunk_remeshes_neighbors_but_not_itself() { - let p = ChunkPos::new(0, 0, 0); - let neighbor = ChunkPos::new(1, 0, 0); - let resident = [neighbor]; - let targets = remesh_targets(&[], &[p], resident_in(&resident)); - // The dropped chunk is never a target; its resident neighbour is. - assert!(!targets.contains(&p)); - assert_eq!(targets, [neighbor].into_iter().collect()); - } - - #[test] - fn remesh_targets_are_deduplicated() { - // Two adjacent chunks loaded in one batch each name the other as a neighbour, but the set holds each once. - let a = ChunkPos::new(0, 0, 0); - let b = ChunkPos::new(1, 0, 0); - let resident = [a, b]; - let targets = remesh_targets(&[a, b], &[], resident_in(&resident)); - assert_eq!(targets, resident.into_iter().collect()); - } -} +#[path = "tests/chunks.rs"] +mod tests; diff --git a/crates/client/src/tests/chunks.rs b/crates/client/src/tests/chunks.rs new file mode 100644 index 0000000..5895e96 --- /dev/null +++ b/crates/client/src/tests/chunks.rs @@ -0,0 +1,76 @@ +// SPDX-License-Identifier: AGPL-3.0-only + +//! Unit tests for the chunk streaming logic in [`crate::chunks`]. + +use super::*; + +#[test] +fn center_is_always_included() { + let center = ChunkPos::new(0, 0, 0); + assert!(desired_chunks(center, 4).contains(¢er)); +} + +#[test] +fn excludes_columns_beyond_the_disc() { + let set = desired_chunks(ChunkPos::new(0, 0, 0), 4); + // One chunk past the radius along an axis: squared distance 25 > 16. + assert!(!set.contains(&ChunkPos::new(5, 0, 0))); + // The far corner: squared distance 4*4 + 4*4 = 32 > 16. + assert!(!set.contains(&ChunkPos::new(4, 0, 4))); +} + +#[test] +fn vertical_extent_is_half_the_radius() { + let set = desired_chunks(ChunkPos::new(0, 0, 0), 4); + // radius / 2 == 2, so the column at the center spans y in [-2, 2]. + assert!(set.contains(&ChunkPos::new(0, 2, 0))); + assert!(!set.contains(&ChunkPos::new(0, 3, 0))); +} + +#[test] +fn set_is_translation_invariant() { + // Shifting the center shifts every member by the same offset; this also exercises negative coordinates on the shifted side. + let base = desired_chunks(ChunkPos::new(0, 0, 0), 3); + let shifted: HashSet = base + .iter() + .map(|p| ChunkPos::new(p.x - 10, p.y - 10, p.z - 10)) + .collect(); + assert_eq!(shifted, desired_chunks(ChunkPos::new(-10, -10, -10), 3)); +} + +/// Builds a residency predicate over a fixed set of positions. +fn resident_in(set: &[ChunkPos]) -> impl Fn(ChunkPos) -> bool + '_ { + move |pos| set.contains(&pos) +} + +#[test] +fn loaded_chunk_remeshes_self_and_resident_neighbors() { + let p = ChunkPos::new(0, 0, 0); + let east = ChunkPos::new(1, 0, 0); + let down = ChunkPos::new(0, -1, 0); + // p plus two of its six neighbours are resident; the other four are not. + let resident = [p, east, down]; + let targets = remesh_targets(&[p], &[], resident_in(&resident)); + assert_eq!(targets, resident.into_iter().collect()); +} + +#[test] +fn dropped_chunk_remeshes_neighbors_but_not_itself() { + let p = ChunkPos::new(0, 0, 0); + let neighbor = ChunkPos::new(1, 0, 0); + let resident = [neighbor]; + let targets = remesh_targets(&[], &[p], resident_in(&resident)); + // The dropped chunk is never a target; its resident neighbour is. + assert!(!targets.contains(&p)); + assert_eq!(targets, [neighbor].into_iter().collect()); +} + +#[test] +fn remesh_targets_are_deduplicated() { + // Two adjacent chunks loaded in one batch each name the other as a neighbour, but the set holds each once. + let a = ChunkPos::new(0, 0, 0); + let b = ChunkPos::new(1, 0, 0); + let resident = [a, b]; + let targets = remesh_targets(&[a, b], &[], resident_in(&resident)); + assert_eq!(targets, resident.into_iter().collect()); +}