Pin persisted enum codes

- Give HydrologyFeatureType, ReachStyle and PondKind explicit codes equal to their old ordinals
- Add PersistedCodes lookup tables that reject duplicate, out-of-range and unknown codes
- Write and read codes in HydrologyPlanCodec; unknown codes decode as malformed plans
- Write and read codes in GenerationSemanticIndex shard records and region summaries
- Order summary river types and the river feature comparator by code
- Pin the code tables and a captured plan byte fixture in tests
This commit is contained in:
Brian Neumann-Fopiano committed 2026-11-01 18:31:01 -05:00
1 parent f6fbf9147f
commit 9c95e7901c
10 files changed
+351 -44

No files matched your search

@@ -0,0 +1,129 @@
package art.arcane.iris.generation.hydrology;
import art.arcane.iris.generation.hydrology.query.HydrologyFeatureType;
import art.arcane.iris.generation.hydrology.water.PondKind;
import art.arcane.iris.generation.hydrology.water.ReachStyle;
import org.junit.Test;
import java.util.Collections;
import java.util.EnumSet;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.function.IntFunction;
import java.util.function.ToIntFunction;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
public class PersistedCodesTest {
private static final Map<HydrologyFeatureType, Integer> FEATURE_TYPE_CODES = Map.ofEntries(
Map.entry(HydrologyFeatureType.STREAM, 0),
Map.entry(HydrologyFeatureType.RIVER, 1),
Map.entry(HydrologyFeatureType.MAJOR_RIVER, 2),
Map.entry(HydrologyFeatureType.CASCADE, 3),
Map.entry(HydrologyFeatureType.WATERFALL, 4),
Map.entry(HydrologyFeatureType.MOUTH, 5),
Map.entry(HydrologyFeatureType.POND, 6),
Map.entry(HydrologyFeatureType.LAKE, 7),
Map.entry(HydrologyFeatureType.DAM, 8),
Map.entry(HydrologyFeatureType.SEA_INLET, 9),
Map.entry(HydrologyFeatureType.CAVE_POOL, 10));
private static final Map<ReachStyle, Integer> REACH_STYLE_CODES = Map.of(
ReachStyle.RUN, 0,
ReachStyle.RIFFLE, 1,
ReachStyle.CASCADE, 2,
ReachStyle.WATERFALL, 3,
ReachStyle.POND_OUTLET, 4,
ReachStyle.MOUTH, 5);
private static final Map<PondKind, Integer> POND_KIND_CODES = Map.of(
PondKind.NATURAL, 0,
PondKind.SEEDED, 1,
PondKind.CHAINED, 2,
PondKind.LAKE, 3);
@Test
public void codesAreUniqueAndPinned() {
assertPinned(HydrologyFeatureType.class, FEATURE_TYPE_CODES, HydrologyFeatureType::code, HydrologyFeatureType::fromCode);
assertPinned(ReachStyle.class, REACH_STYLE_CODES, ReachStyle::code, ReachStyle::fromCode);
assertPinned(PondKind.class, POND_KIND_CODES, PondKind::code, PondKind::fromCode);
}
@Test
public void unknownCodesFailClosed() {
assertUnknown("HydrologyFeatureType", FEATURE_TYPE_CODES, HydrologyFeatureType::fromCode);
assertUnknown("ReachStyle", REACH_STYLE_CODES, ReachStyle::fromCode);
assertUnknown("PondKind", POND_KIND_CODES, PondKind::fromCode);
}
@Test
public void conflictingCodesAreRejected() {
try {
PersistedCodes.of(SharedCode.class, SharedCode::code);
fail("two constants share code 7");
} catch (IllegalStateException expected) {
assertTrue(expected.getMessage(), expected.getMessage().contains("SharedCode") && expected.getMessage().contains("7"));
}
try {
PersistedCodes.of(WideCode.class, WideCode::code);
fail("code 256 does not fit one byte");
} catch (IllegalStateException expected) {
assertTrue(expected.getMessage(), expected.getMessage().contains("WideCode") && expected.getMessage().contains("256"));
}
}
private static <E extends Enum<E>> void assertPinned(Class<E> type, Map<E, Integer> pins, ToIntFunction<E> code,
IntFunction<E> fromCode) {
assertEquals(type.getSimpleName() + " constants need a pinned code", EnumSet.allOf(type), pins.keySet());
Set<Integer> seen = new HashSet<>();
for (E constant : type.getEnumConstants()) {
int pinned = pins.get(constant);
assertEquals(type.getSimpleName() + "." + constant.name(), pinned, code.applyAsInt(constant));
assertTrue(type.getSimpleName() + " reuses code " + pinned, seen.add(pinned));
assertSame(constant, fromCode.apply(pinned));
}
}
private static <E extends Enum<E>> void assertUnknown(String name, Map<E, Integer> pins, IntFunction<E> fromCode) {
int unused = Collections.max(pins.values()) + 1;
for (int code : new int[]{-1, unused, 256}) {
try {
fromCode.apply(code);
fail(name + " decoded unknown code " + code);
} catch (IllegalStateException expected) {
assertTrue(expected.getMessage(), expected.getMessage().contains(name) && expected.getMessage().contains(Integer.toString(code)));
}
}
}
private enum SharedCode {
FIRST(7),
SECOND(7);
private final int code;
SharedCode(int code) {
this.code = code;
}
private int code() {
return code;
}
}
private enum WideCode {
ONLY(256);
private final int code;
WideCode(int code) {
this.code = code;
}
private int code() {
return code;
}
}
}
@@ -15,8 +15,10 @@ import art.arcane.iris.generation.hydrology.water.ReachShaper;
import art.arcane.iris.generation.hydrology.water.ReachStyle;
import org.junit.Test;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HexFormat;
import java.util.List;
import java.util.Optional;
import java.util.SplittableRandom;
@@ -33,6 +35,20 @@ public class HydrologyPlanCodecTest {
private static final float NAN_PAYLOAD = Float.intBitsToFloat(0x7fc00123);
private static final float NEGATIVE_NAN = Float.intBitsToFloat(0xffa00001);
private static final WindowKey LAND = new WindowKey(4, 4);
private static final int FINGERPRINT_OFFSET = Integer.BYTES + 1;
private static final String PINNED_WINDOW = """
4948595057000000000000000000000002fffffffd000000010000000000000029000000000000006100000000444b20
0000000006000000004080000041000000414000004180000041a00000c1000000c0c00000c0800000c0000000000000
0040000000000000480000004700000046000000420000003f0000003e3f8000003fa000003fc000003fe00000400000
00402000003f8000003f8000003fc0000040000000400000003f80000000010203040500000004000000000000000500
00000046000000420000000cfffffffc428d0000fffffffc000000080000000400000000000000000000000000000000
0000002900000000ffffffff0000000afffffffa0000000000000006010000004a000000470000001e00000008429480
0000000003fffffffd000000010000004000000001000000018000000000000001ffffffffffffffff00000001000000
0577617465720000001c000000060000000000000007020000004400000040ffffffec0000001042898000fffffffc00
00000800000004000000000000000000000000000000000000002900000002000000046c617661ffffffea0000000e00
0000000000000803000000500000003c00000040ffffffc042a10000fffffffc00000008000000040000000000000000
00000000ffffffffffffffff00000000ffffffff0000003cffffffc400000000
""";
@Test
public void codecRoundTripsBitExact() {
@@ -138,6 +154,29 @@ public class HydrologyPlanCodecTest {
}
}
@Test
public void decodesPinnedBytes() {
HydrologySettings settings = settings(new IrisHydrology());
WindowPlan expected = pinnedWindow(settings.tiers().get(0));
byte[] fixture = HexFormat.of().parseHex(PINNED_WINDOW.replaceAll("\\s", ""));
ByteBuffer.wrap(fixture).putLong(FINGERPRINT_OFFSET, settings.fingerprint());
assertBitExact(expected, HydrologyPlanCodec.decodeWindow(settings, fixture).orElseThrow(), settings.tiers());
assertArrayEquals(fixture, HydrologyPlanCodec.encode(settings, expected));
}
private static WindowPlan pinnedWindow(HydrologyTier tier) {
Reach reach = new Reach(41L, tier, 97L, new float[]{0f, 4f, 8f, 12f, 16f, 20f}, new float[]{-8f, -6f, -4f, -2f, 0f, 2f},
new int[]{72, 71, 70, 66, 63, 62}, new float[]{1f, 1.25f, 1.5f, 1.75f, 2f, 2.5f}, new float[]{1f, 1f, 1.5f, 2f, 2f, 1f},
new ReachStyle[]{ReachStyle.RUN, ReachStyle.RIFFLE, ReachStyle.CASCADE, ReachStyle.WATERFALL, ReachStyle.POND_OUTLET,
ReachStyle.MOUTH}, 812.5f);
List<Pond> ponds = List.of(
new Pond(5L, PondKind.NATURAL, 70, 66, 12, -4, 70.5f, mask(0), 41L, 0, null, 10, -6),
new Pond(6L, PondKind.SEEDED, 74, 71, 30, 8, 74.25f, mask(2), -1L, 1, "water", 28, 6),
new Pond(7L, PondKind.CHAINED, 68, 64, -20, 16, 68.75f, mask(0), 41L, 2, "lava", -22, 14),
new Pond(8L, PondKind.LAKE, 80, 60, 64, -64, 80.5f, mask(0), -1L, 0, null, 60, -60));
return new WindowPlan(new WindowKey(2, -3), List.of(reach), ponds, List.of());
}
private static void assertBitExact(WindowPlan expected, WindowPlan actual, List<HydrologyTier> tiers) {
assertEquals(expected, actual);
assertReachesBitExact(expected.reaches(), actual.reaches(), tiers);
@@ -568,6 +568,31 @@ public final class GenerationSemanticIndexTest {
assertEquals(Set.of("temperate"), semantics.regionKeys());
}
@Test
public void unknownRiverFeatureCodesFailClosed() throws Exception {
Path recordRoot = temporaryFolder.newFolder("unknown-record-river-code").toPath();
Path recordShard = streamShard(recordRoot);
byte[] record = Files.readAllBytes(recordShard);
record[firstRiverTypeOffset(record)] = (byte) 200;
rewriteChecksum(record);
replaceReferencedShard(recordRoot, recordShard, record);
GenerationSemanticIndex recordIndex = GenerationSemanticIndex.load(recordRoot);
IllegalStateException recordError = assertThrows(IllegalStateException.class, () -> recordIndex.get(0, 0));
assertTrue(recordError.getCause().getMessage(),
recordError.getCause().getMessage().contains("Unknown HydrologyFeatureType code 200"));
Path summaryRoot = temporaryFolder.newFolder("unknown-summary-river-code").toPath();
Path summaryShard = streamShard(summaryRoot);
byte[] summary = Files.readAllBytes(summaryShard);
summary[summaryRiverTypeOffset(summary)] = (byte) 200;
rewriteChecksum(summary);
replaceReferencedShard(summaryRoot, summaryShard, summary);
GenerationSemanticIndex summaryIndex = GenerationSemanticIndex.load(summaryRoot);
IllegalStateException summaryError = assertThrows(IllegalStateException.class, () -> summaryIndex.get(0, 0));
assertTrue(summaryError.getCause().getMessage(),
summaryError.getCause().getMessage().contains("Unknown HydrologyFeatureType code 200"));
}
@Test
public void currentShardKeepsEveryHydrologyFeatureType() throws Exception {
Path dimensionRoot = temporaryFolder.newFolder("current-hydrology").toPath();
@@ -879,6 +904,16 @@ public final class GenerationSemanticIndexTest {
.build();
}
private static Path streamShard(Path dimensionRoot) throws Exception {
GenerationSemanticIndex index = GenerationSemanticIndex.load(dimensionRoot);
index.recordAndPersist(ChunkGenerationSemantics.builder(0, 0, 1L)
.addRegion("temperate")
.addRiverFeature("water", HydrologyFeatureType.STREAM, 9L, 3, 70, 4)
.seal()
.build());
return onlyShard(index.storageDirectory());
}
private static Path onlyShard(Path directory) throws Exception {
try (Stream<Path> files = Files.list(directory)) {
List<Path> shards = files.filter(path -> path.getFileName().toString().endsWith(".isem")).toList();