Synvael/crates/client/src/tests/chunks.rs

77 lines
2.7 KiB
Rust

// 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(&center));
}
#[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<ChunkPos> = 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());
}