active-cubiomes/tests/test_cubiomes.py
2026-10-12 10:23:10 +00:00

309 lines
12 KiB
Python

"""Tests for the cubiomes Python bindings."""
import array
import pytest
from cubiomes import (
Generator, BiomeFilter, StrongholdIterator, StructureVariant,
MCVersion, Dimension, BiomeID, StructureType,
get_structure_config, get_structure_pos, is_viable_structure_pos,
is_viable_feature_biome, get_variant, get_mineshafts,
iter_strongholds, init_first_stronghold,
setup_biome_filter, is_slime_chunk, get_shadow, move_structure,
chunk_generate_rnd, get_population_seed,
biome_exists, is_overworld_biome, get_dimension, get_mutated,
are_similar, is_mesa, is_shallow_ocean, is_deep_ocean,
is_oceanic, is_snowy,
)
SEED = 123456789
MC = MCVersion.MC_1_21
# ─── Generator ───────────────────────────────────────────────────────────────
class TestGenerator:
def test_create(self):
g = Generator(MC)
assert g.mc == MC
def test_apply_seed(self):
g = Generator(MC)
g.apply_seed(Dimension.DIM_OVERWORLD, SEED)
assert g.seed == SEED
assert g.dim == Dimension.DIM_OVERWORLD
def test_large_biomes_flag(self):
from cubiomes import LARGE_BIOMES
g = Generator(MC, LARGE_BIOMES)
assert g.flags & LARGE_BIOMES
def test_get_biome_at_scale4(self):
g = Generator(MC)
g.apply_seed(Dimension.DIM_OVERWORLD, SEED)
biome = g.get_biome_at(0, 0, 0, scale=4)
assert biome == BiomeID.river
def test_get_biome_at_scale1(self):
g = Generator(MC)
g.apply_seed(Dimension.DIM_OVERWORLD, SEED)
biome = g.get_biome_at(0, 63, 0, scale=1)
assert biome >= 0
def test_gen_biomes_returns_array(self):
g = Generator(MC)
g.apply_seed(Dimension.DIM_OVERWORLD, SEED)
result = g.gen_biomes(-64, -64, 32, 32, scale=4)
assert isinstance(result, array.array)
assert result.typecode == 'i'
assert len(result) == 32 * 32
def test_gen_biomes_3d(self):
g = Generator(MC)
g.apply_seed(Dimension.DIM_OVERWORLD, SEED)
result = g.gen_biomes(0, 0, 8, 8, scale=4, y=0, sy=4)
assert len(result) == 8 * 4 * 8
def test_gen_biomes_consistent_with_get_biome_at(self):
g = Generator(MC)
g.apply_seed(Dimension.DIM_OVERWORLD, SEED)
x, z = 0, 0
result = g.gen_biomes(x, z, 4, 4, scale=4)
# result[z_i*sx + x_i] — top-left corner
assert result[0] == g.get_biome_at(x, 0, z, scale=4)
def test_repr(self):
g = Generator(MC)
g.apply_seed(Dimension.DIM_OVERWORLD, SEED)
r = repr(g)
assert "Generator" in r
assert str(SEED) in r
def test_estimate_spawn(self):
g = Generator(MC)
g.apply_seed(Dimension.DIM_OVERWORLD, SEED)
x, z = g.estimate_spawn()
assert isinstance(x, int)
assert isinstance(z, int)
def test_nether_dimension(self):
g = Generator(MC)
g.apply_seed(Dimension.DIM_NETHER, SEED)
assert g.dim == Dimension.DIM_NETHER
biome = g.get_biome_at(0, 0, 0, scale=4)
assert biome >= 0 # nether biome
def test_memory_cleanup(self):
# Create many generators — should not leak memory
for _ in range(100):
g = Generator(MC)
g.apply_seed(Dimension.DIM_OVERWORLD, SEED)
g.get_biome_at(0, 0, 0)
# ─── Structure finding ────────────────────────────────────────────────────────
class TestStructures:
def test_get_structure_config_village(self):
cfg = get_structure_config(StructureType.Village, MC)
assert cfg is not None
assert cfg["dim"] == 0
assert cfg["region_size"] > 0
def test_get_structure_config_nonexistent(self):
# Feature doesn't exist in 1.21
cfg = get_structure_config(StructureType.Feature, MC)
assert cfg is None
def test_get_structure_pos_returns_tuple(self):
pos = get_structure_pos(StructureType.Village, MC, SEED, 0, 0)
assert pos is None or (isinstance(pos, tuple) and len(pos) == 2)
def test_get_structure_pos_village(self):
# We know this seed has a village in this region from manual testing
pos = get_structure_pos(StructureType.Village, MC, SEED, -1, -1)
assert pos is not None
x, z = pos
assert isinstance(x, int) and isinstance(z, int)
def test_is_viable_structure_pos(self):
g = Generator(MC)
g.apply_seed(Dimension.DIM_OVERWORLD, SEED)
pos = get_structure_pos(StructureType.Village, MC, SEED, -1, -1)
assert pos is not None
result = is_viable_structure_pos(StructureType.Village, g, pos[0], pos[1])
assert isinstance(result, bool)
def test_is_viable_feature_biome(self):
assert is_viable_feature_biome(MC, StructureType.Desert_Pyramid,
BiomeID.desert)
assert not is_viable_feature_biome(MC, StructureType.Desert_Pyramid,
BiomeID.plains)
def test_get_variant_village(self):
g = Generator(MC)
g.apply_seed(Dimension.DIM_OVERWORLD, SEED)
pos = get_structure_pos(StructureType.Village, MC, SEED, -1, -1)
if pos:
biome = g.get_biome_at(pos[0] >> 2, 0, pos[1] >> 2, scale=4)
sv = get_variant(StructureType.Village, MC, SEED, pos[0], pos[1], biome)
if sv is not None:
assert isinstance(sv.rotation, int)
assert 0 <= sv.rotation <= 3
def test_get_mineshafts(self):
mines = get_mineshafts(MC, SEED, -10, -10, 20, 20)
assert isinstance(mines, list)
for x, z in mines:
assert isinstance(x, int) and isinstance(z, int)
# ─── Strongholds ─────────────────────────────────────────────────────────────
class TestStrongholds:
def test_init_first_stronghold(self):
x, z = init_first_stronghold(MC, SEED)
assert isinstance(x, int) and isinstance(z, int)
def test_iter_strongholds_yields_tuples(self):
g = Generator(MC)
g.apply_seed(Dimension.DIM_OVERWORLD, SEED)
positions = []
for i, pos in enumerate(iter_strongholds(g, SEED)):
positions.append(pos)
if i >= 4:
break
assert len(positions) == 5
for x, z in positions:
assert isinstance(x, int) and isinstance(z, int)
def test_iter_strongholds_known_positions(self):
g = Generator(MC)
g.apply_seed(Dimension.DIM_OVERWORLD, SEED)
first = next(iter(iter_strongholds(g, SEED)))
assert first == (-1116, -1596)
def test_stronghold_iterator_index(self):
g = Generator(MC)
g.apply_seed(Dimension.DIM_OVERWORLD, SEED)
it = iter_strongholds(g, SEED)
next(it)
# The iterator pre-fetches the next position after each yield,
# so index==2 after the first next() (stronghold 0 yielded, 1 pre-fetched)
assert it.index == 2
# ─── BiomeFilter ─────────────────────────────────────────────────────────────
class TestBiomeFilter:
def test_create_required(self):
bf = setup_biome_filter(MC, required=[BiomeID.plains, BiomeID.forest])
assert isinstance(bf, BiomeFilter)
def test_create_excluded(self):
bf = setup_biome_filter(MC, excluded=[BiomeID.ocean])
assert isinstance(bf, BiomeFilter)
def test_check_for_biomes_returns_int(self):
g = Generator(MC)
bf = setup_biome_filter(MC, required=[BiomeID.plains])
result = g.check_for_biomes(
-256, -256, 128, 128, scale=4,
dim=Dimension.DIM_OVERWORLD, seed=SEED,
biome_filter=bf
)
assert result in (0, 1, 2)
# ─── Biome helpers ────────────────────────────────────────────────────────────
class TestBiomeHelpers:
def test_biome_exists_true(self):
assert biome_exists(MC, BiomeID.plains)
def test_biome_exists_false(self):
# cherry_grove doesn't exist in 1.18
assert not biome_exists(MCVersion.MC_1_18, BiomeID.cherry_grove)
def test_is_overworld_biome(self):
assert is_overworld_biome(MC, BiomeID.plains)
assert not is_overworld_biome(MC, BiomeID.nether_wastes)
def test_get_dimension(self):
assert get_dimension(BiomeID.plains) == 0
assert get_dimension(BiomeID.nether_wastes) == -1
assert get_dimension(BiomeID.the_end) == 1
def test_get_mutated(self):
m = get_mutated(MCVersion.MC_1_17, BiomeID.plains)
assert m == BiomeID.sunflower_plains or m == -1
def test_are_similar(self):
assert are_similar(MC, BiomeID.forest, BiomeID.birch_forest)
def test_is_mesa(self):
assert is_mesa(BiomeID.badlands)
assert not is_mesa(BiomeID.plains)
def test_ocean_checks(self):
assert is_shallow_ocean(BiomeID.ocean)
assert is_deep_ocean(BiomeID.deep_ocean)
assert is_oceanic(BiomeID.ocean)
assert is_oceanic(BiomeID.deep_ocean)
assert not is_oceanic(BiomeID.plains)
def test_is_snowy(self):
assert is_snowy(BiomeID.snowy_tundra)
assert not is_snowy(BiomeID.desert)
# ─── Seed utilities ───────────────────────────────────────────────────────────
class TestSeedUtils:
def test_is_slime_chunk(self):
# Known slime chunk from manual test
assert is_slime_chunk(SEED, 0, 1)
# Known non-slime chunk
assert not is_slime_chunk(SEED, 0, 0)
def test_get_shadow(self):
shadow = get_shadow(SEED)
assert isinstance(shadow, int)
# Shadow is deterministic
assert get_shadow(SEED) == shadow
# Shadow of shadow is original
assert get_shadow(shadow) == SEED
def test_move_structure(self):
result = move_structure(SEED, 1, 0)
assert isinstance(result, int)
assert result != SEED
def test_chunk_generate_rnd(self):
rnd = chunk_generate_rnd(SEED, 0, 0)
assert isinstance(rnd, int)
assert rnd != 0
def test_get_population_seed(self):
pop = get_population_seed(MC, SEED, 0, 0)
assert isinstance(pop, int)
# ─── Enums ───────────────────────────────────────────────────────────────────
class TestEnums:
def test_mc_version_newest(self):
assert MCVersion.MC_NEWEST == MCVersion.MC_26_3
assert int(MCVersion.MC_NEWEST) == 35
def test_biome_id_from_int(self):
assert BiomeID(0) == BiomeID.ocean
assert BiomeID(1) == BiomeID.plains
def test_structure_type_values(self):
assert StructureType.Village == 5
assert StructureType.Stronghold == 27
def test_dimension_values(self):
assert Dimension.DIM_NETHER == -1
assert Dimension.DIM_OVERWORLD == 0
assert Dimension.DIM_END == 1