mirror of
https://github.com/GTNewHorizons/ForgeMultipart.git
synced 2026-11-18 11:29:33 +00:00
test: characterize TileCache
Plain-JVM coverage for the map half: add stores the tile unflagged, remove replaces the entry rather than dropping it, entries are keyed by coordinate value rather than identity, and clear empties everything. Forge server coverage for findTile, which needs a world. It pins the branch that is easy to misread: the removed flag only suppresses the warning, and a tile flagged removed is still returned, because the Some pattern matches regardless of the flag and the reference's trailing null case is unreachable. The empty-cache path throws with a message starting 'DC: Client multipart @'. 114 plain-JVM tests, 13 Forge server tests.
This commit is contained in:
1 parent
097f527856
commit
b92d4b7af4
2 files changed
+195
No files matched your search
@@ -0,0 +1,119 @@
|
||||
package codechicken.multipart;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertSame;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
import org.junit.jupiter.api.AfterEach;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import codechicken.lib.vec.BlockCoord;
|
||||
|
||||
/**
|
||||
* The cache is global mutable state, so every test clears it on both sides. findTile needs a world and is covered by
|
||||
* the Forge server suite instead.
|
||||
*/
|
||||
class TileCacheCharacterizationTest {
|
||||
|
||||
@BeforeEach
|
||||
@AfterEach
|
||||
void clearCache() {
|
||||
TileCache.clear();
|
||||
}
|
||||
|
||||
@Test
|
||||
void addStoresTheTileAtItsOwnCoordinatesAndNotFlaggedRemoved() {
|
||||
TileMultipart tile = tileAt(3, 4, 5);
|
||||
|
||||
TileCache.add(tile);
|
||||
|
||||
TileCache.FlaggedTile entry = entryAt(new BlockCoord(3, 4, 5));
|
||||
assertNotNull(entry);
|
||||
assertSame(tile, entry.t());
|
||||
assertFalse(entry.removed());
|
||||
}
|
||||
|
||||
@Test
|
||||
void removeStoresTheSameTileFlaggedRemoved() {
|
||||
TileMultipart tile = tileAt(3, 4, 5);
|
||||
TileCache.add(tile);
|
||||
|
||||
TileCache.remove(tile);
|
||||
|
||||
TileCache.FlaggedTile entry = entryAt(new BlockCoord(3, 4, 5));
|
||||
assertNotNull(entry);
|
||||
assertSame(tile, entry.t());
|
||||
assertTrue(entry.removed());
|
||||
assertEquals(1, cacheSize(), "remove replaces the entry rather than dropping it");
|
||||
}
|
||||
|
||||
/** The cache is keyed by coordinate value, which is what makes a lookup from a fresh BlockCoord work at all. */
|
||||
@Test
|
||||
void entriesAreKeyedByCoordinateValueNotByIdentity() {
|
||||
TileMultipart tile = tileAt(-7, 200, 12);
|
||||
TileCache.add(tile);
|
||||
|
||||
assertNotNull(entryAt(new BlockCoord(-7, 200, 12)));
|
||||
assertNull(entryAt(new BlockCoord(-7, 200, 13)));
|
||||
}
|
||||
|
||||
@Test
|
||||
void aSecondTileAtTheSameCoordinateReplacesTheFirst() {
|
||||
TileMultipart first = tileAt(1, 1, 1);
|
||||
TileMultipart second = tileAt(1, 1, 1);
|
||||
TileCache.add(first);
|
||||
|
||||
TileCache.add(second);
|
||||
|
||||
assertSame(second, entryAt(new BlockCoord(1, 1, 1)).t());
|
||||
assertEquals(1, cacheSize());
|
||||
}
|
||||
|
||||
@Test
|
||||
void separateCoordinatesAreHeldIndependently() {
|
||||
TileCache.add(tileAt(0, 0, 0));
|
||||
TileCache.add(tileAt(0, 1, 0));
|
||||
|
||||
assertEquals(2, cacheSize());
|
||||
assertNotNull(entryAt(new BlockCoord(0, 0, 0)));
|
||||
assertNotNull(entryAt(new BlockCoord(0, 1, 0)));
|
||||
}
|
||||
|
||||
@Test
|
||||
void anUnknownCoordinateResolvesToNothing() {
|
||||
assertNull(entryAt(new BlockCoord(9, 9, 9)));
|
||||
}
|
||||
|
||||
@Test
|
||||
void clearEmptiesTheCache() {
|
||||
TileCache.add(tileAt(2, 2, 2));
|
||||
|
||||
TileCache.clear();
|
||||
|
||||
assertEquals(0, cacheSize());
|
||||
assertNull(entryAt(new BlockCoord(2, 2, 2)));
|
||||
}
|
||||
|
||||
private static TileMultipart tileAt(int x, int y, int z) {
|
||||
TileMultipart tile = new TileMultipart();
|
||||
tile.xCoord = x;
|
||||
tile.yCoord = y;
|
||||
tile.zCoord = z;
|
||||
return tile;
|
||||
}
|
||||
|
||||
// The two adapters below are the only places the cache's collection types are named.
|
||||
|
||||
private static TileCache.FlaggedTile entryAt(BlockCoord coord) {
|
||||
scala.Option<TileCache.FlaggedTile> entry = TileCache.apply(coord);
|
||||
return entry.isDefined() ? entry.get() : null;
|
||||
}
|
||||
|
||||
private static int cacheSize() {
|
||||
return TileCache.map().size();
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user