Files
2026-05-16 14:37:04 -04:00

79 lines
2.6 KiB
Rust

use openvistapro::terrain::{HeightGrid, TerrainError};
use openvistapro::terrain_gen::{
DeterministicTerrainGenerator, TerrainGenerationSpec, TerrainGenerator,
};
fn assert_same_grid(a: &HeightGrid, b: &HeightGrid) {
assert_eq!(a.width(), b.width());
assert_eq!(a.height(), b.height());
for y in 0..a.height() {
for x in 0..a.width() {
assert_eq!(a.sample(x, y), b.sample(x, y), "mismatch at ({x}, {y})");
}
}
}
#[test]
fn terrain_gen_deterministic_generator_returns_requested_dimensions() {
let spec = TerrainGenerationSpec::new(0xfeed_beef, 4, 3).expect("valid spec");
let generator = DeterministicTerrainGenerator::new();
let grid = generator.generate(&spec).expect("generation succeeds");
assert_eq!(grid.width(), 4);
assert_eq!(grid.height(), 3);
}
#[test]
fn terrain_gen_deterministic_generator_is_stable_for_same_seed_and_size() {
let spec = TerrainGenerationSpec::new(42, 6, 5).expect("valid spec");
let generator = DeterministicTerrainGenerator::new();
let first = generator
.generate(&spec)
.expect("first generation succeeds");
let second = generator
.generate(&spec)
.expect("second generation succeeds");
assert_same_grid(&first, &second);
}
#[test]
fn terrain_gen_deterministic_generator_rejects_zero_dimensions() {
let err = TerrainGenerationSpec::new(7, 0, 4).unwrap_err();
assert_eq!(err, TerrainError::ZeroDimension);
let err = TerrainGenerationSpec::new(7, 4, 0).unwrap_err();
assert_eq!(err, TerrainError::ZeroDimension);
}
#[test]
fn terrain_gen_deterministic_generator_produces_value_noise_fbm_sample() {
let spec = TerrainGenerationSpec::new(1337, 4, 4).expect("valid spec");
let generator = DeterministicTerrainGenerator::new();
let grid = generator.generate(&spec).expect("generation succeeds");
let expected = [
[0.706_783_f32, 0.390_681, 0.571_295, 0.454_375],
[0.667_478, 0.549_327, 0.476_506, 0.335_594],
[0.603_516, 0.451_503, 0.516_397, 0.357_081],
[0.308_838, 0.295_558, 0.570_510, 0.666_683],
];
for (y, row) in expected.iter().enumerate() {
for (x, expected_sample) in row.iter().enumerate() {
let actual = grid.sample(x as u32, y as u32).expect("sample in bounds");
assert!(
(actual - expected_sample).abs() < 1e-6,
"mismatch at ({x}, {y}): expected {expected_sample}, got {actual}"
);
}
}
let (min, max) = grid.min_max().expect("non-empty grid has min/max");
assert!(min >= 0.0);
assert!(max <= 1.0);
}