From a673b749a5816392652785457dd1cf3603368628 Mon Sep 17 00:00:00 2001 From: Elizabeth Hunt Date: Sun, 8 Feb 2026 12:33:16 -0800 Subject: chore: Use actors --- .../src/test/java/coffee/liz/ecs/DAGWorldTest.java | 368 ++++++++++----------- .../test/java/coffee/liz/ecs/model/EntityTest.java | 152 ++++----- 2 files changed, 260 insertions(+), 260 deletions(-) (limited to 'core/src/test/java/coffee/liz/ecs') diff --git a/core/src/test/java/coffee/liz/ecs/DAGWorldTest.java b/core/src/test/java/coffee/liz/ecs/DAGWorldTest.java index a656d28..648745b 100644 --- a/core/src/test/java/coffee/liz/ecs/DAGWorldTest.java +++ b/core/src/test/java/coffee/liz/ecs/DAGWorldTest.java @@ -22,188 +22,188 @@ import java.util.Set; import java.util.concurrent.CopyOnWriteArrayList; final class DAGWorldTest { - @Test - public void queryReturnsEntitiesMatchingAllRequestedComponents() { - final DAGWorld world = new DAGWorld<>(Map.of()); - final Entity matching = world.createEntity(); - matching.add(new PositionComponent()); - matching.add(new VelocityComponent()); - final Entity partial = world.createEntity(); - partial.add(new PositionComponent()); - final Entity nonMatching = world.createEntity(); - nonMatching.add(new VelocityComponent()); - - world.update("state", Duration.ZERO); - - final Set results = world.query(Set.of(PositionComponent.class, VelocityComponent.class)); - - assertEquals(Set.of(matching), results); - } - - @Test - public void queryWithoutComponentsReturnsAllEntities() { - final DAGWorld world = new DAGWorld<>(Map.of()); - final Entity entityOne = world.createEntity(); - final Entity entityTwo = world.createEntity(); - - final Set results = world.query(List.>of()); - - assertEquals(Set.of(entityOne, entityTwo), results); - } - - @Test - public void updateExecutesSystemsInTopologicalOrder() { - final CopyOnWriteArrayList executionLog = new CopyOnWriteArrayList<>(); - - final DAGWorld world = new DAGWorld<>(Map.of(SystemC.class, new SystemC(executionLog), SystemA.class, - new SystemA(executionLog), SystemB.class, new SystemB(executionLog))); - world.update("state", Duration.ZERO); - - assertEquals(List.of("A", "B", "C"), executionLog); - } - - @Test - public void updateRefreshesComponentCacheAfterEntityMutations() { - final DAGWorld world = new DAGWorld<>(Map.of()); - final Entity subject = world.createEntity(); - - world.update("state", Duration.ZERO); - assertTrue(world.query(Set.of(PositionComponent.class)).isEmpty()); - - subject.add(new PositionComponent()); - world.update("state", Duration.ZERO); - assertEquals(1, world.query(Set.of(PositionComponent.class)).size()); - - subject.remove(PositionComponent.class); - world.update("state", Duration.ZERO); - assertTrue(world.query(Set.of(PositionComponent.class)).isEmpty()); - } - - @Test - public void queryFindsComponentsAddedByEarlierSystemInSameTick() { - final List> queryResults = new CopyOnWriteArrayList<>(); - - final Map>, System> systems = new LinkedHashMap<>(); - systems.put(ComponentAdderSystem.class, new ComponentAdderSystem()); - systems.put(ComponentReaderSystem.class, new ComponentReaderSystem(queryResults)); - final DAGWorld world = new DAGWorld<>(systems); - - final Entity entity = world.createEntity(); - entity.add(new PositionComponent()); - - world.update("state", Duration.ZERO); - - assertEquals(1, queryResults.size()); - assertEquals(Set.of(entity), queryResults.getFirst()); - } - - @Test - public void circularDependencyDetectionThrowsIllegalStateException() { - final Map>, System> systems = new LinkedHashMap<>(); - final SystemCycleA systemA = new SystemCycleA(); - final SystemCycleB systemB = new SystemCycleB(); - systems.put(SystemCycleA.class, systemA); - systems.put(SystemCycleB.class, systemB); - - assertThrows(IllegalStateException.class, () -> new DAGWorld<>(systems)); - } - - private static final class PositionComponent implements Component { - } - - private static final class VelocityComponent implements Component { - } - - @RequiredArgsConstructor - private abstract static class RecordingSystem implements System { - private final List log; - private final String label; - - @Override - public final void update(final World world, final String state, final Duration duration) { - log.add(label); - } - } - - private static final class SystemA extends RecordingSystem { - private SystemA(final List log) { - super(log, "A"); - } - - @Override - public Collection>> getDependencies() { - return List.of(); - } - } - - private static final class SystemB extends RecordingSystem { - private SystemB(final List log) { - super(log, "B"); - } - - @Override - public Collection>> getDependencies() { - return Set.of(SystemA.class); - } - } - - private static final class SystemC extends RecordingSystem { - private SystemC(final List log) { - super(log, "C"); - } - - @Override - public Collection>> getDependencies() { - return Set.of(SystemB.class); - } - } - - private static final class SystemCycleA implements System { - @Override - public Collection>> getDependencies() { - return Set.of(SystemCycleB.class); - } - - @Override - public void update(final World world, final String state, final Duration duration) { - } - } - - private static final class SystemCycleB implements System { - @Override - public Collection>> getDependencies() { - return Set.of(SystemCycleA.class); - } - - @Override - public void update(final World world, final String state, final Duration duration) { - } - } - - private static final class ComponentAdderSystem implements System { - @Override - public Collection>> getDependencies() { - return List.of(); - } - - @Override - public void update(final World world, final String state, final Duration duration) { - world.query(Set.of(PositionComponent.class)).forEach(e -> e.add(new VelocityComponent())); - } - } - - @RequiredArgsConstructor - private static final class ComponentReaderSystem implements System { - private final List> queryResults; - - @Override - public Collection>> getDependencies() { - return Set.of(ComponentAdderSystem.class); - } - - @Override - public void update(final World world, final String state, final Duration duration) { - queryResults.add(world.query(Set.of(VelocityComponent.class, PositionComponent.class))); - } - } + @Test + public void queryReturnsEntitiesMatchingAllRequestedComponents() { + final DAGWorld world = new DAGWorld<>(Map.of()); + final Entity matching = world.createEntity(); + matching.add(new PositionComponent()); + matching.add(new VelocityComponent()); + final Entity partial = world.createEntity(); + partial.add(new PositionComponent()); + final Entity nonMatching = world.createEntity(); + nonMatching.add(new VelocityComponent()); + + world.update("state", Duration.ZERO); + + final Set results = world.query(Set.of(PositionComponent.class, VelocityComponent.class)); + + assertEquals(Set.of(matching), results); + } + + @Test + public void queryWithoutComponentsReturnsAllEntities() { + final DAGWorld world = new DAGWorld<>(Map.of()); + final Entity entityOne = world.createEntity(); + final Entity entityTwo = world.createEntity(); + + final Set results = world.query(List.>of()); + + assertEquals(Set.of(entityOne, entityTwo), results); + } + + @Test + public void updateExecutesSystemsInTopologicalOrder() { + final CopyOnWriteArrayList executionLog = new CopyOnWriteArrayList<>(); + + final DAGWorld world = new DAGWorld<>(Map.of(SystemC.class, new SystemC(executionLog), SystemA.class, + new SystemA(executionLog), SystemB.class, new SystemB(executionLog))); + world.update("state", Duration.ZERO); + + assertEquals(List.of("A", "B", "C"), executionLog); + } + + @Test + public void updateRefreshesComponentCacheAfterEntityMutations() { + final DAGWorld world = new DAGWorld<>(Map.of()); + final Entity subject = world.createEntity(); + + world.update("state", Duration.ZERO); + assertTrue(world.query(Set.of(PositionComponent.class)).isEmpty()); + + subject.add(new PositionComponent()); + world.update("state", Duration.ZERO); + assertEquals(1, world.query(Set.of(PositionComponent.class)).size()); + + subject.remove(PositionComponent.class); + world.update("state", Duration.ZERO); + assertTrue(world.query(Set.of(PositionComponent.class)).isEmpty()); + } + + @Test + public void queryFindsComponentsAddedByEarlierSystemInSameTick() { + final List> queryResults = new CopyOnWriteArrayList<>(); + + final Map>, System> systems = new LinkedHashMap<>(); + systems.put(ComponentAdderSystem.class, new ComponentAdderSystem()); + systems.put(ComponentReaderSystem.class, new ComponentReaderSystem(queryResults)); + final DAGWorld world = new DAGWorld<>(systems); + + final Entity entity = world.createEntity(); + entity.add(new PositionComponent()); + + world.update("state", Duration.ZERO); + + assertEquals(1, queryResults.size()); + assertEquals(Set.of(entity), queryResults.getFirst()); + } + + @Test + public void circularDependencyDetectionThrowsIllegalStateException() { + final Map>, System> systems = new LinkedHashMap<>(); + final SystemCycleA systemA = new SystemCycleA(); + final SystemCycleB systemB = new SystemCycleB(); + systems.put(SystemCycleA.class, systemA); + systems.put(SystemCycleB.class, systemB); + + assertThrows(IllegalStateException.class, () -> new DAGWorld<>(systems)); + } + + private static final class PositionComponent implements Component { + } + + private static final class VelocityComponent implements Component { + } + + @RequiredArgsConstructor + private abstract static class RecordingSystem implements System { + private final List log; + private final String label; + + @Override + public final void update(final World world, final String state, final Duration duration) { + log.add(label); + } + } + + private static final class SystemA extends RecordingSystem { + private SystemA(final List log) { + super(log, "A"); + } + + @Override + public Collection>> getDependencies() { + return List.of(); + } + } + + private static final class SystemB extends RecordingSystem { + private SystemB(final List log) { + super(log, "B"); + } + + @Override + public Collection>> getDependencies() { + return Set.of(SystemA.class); + } + } + + private static final class SystemC extends RecordingSystem { + private SystemC(final List log) { + super(log, "C"); + } + + @Override + public Collection>> getDependencies() { + return Set.of(SystemB.class); + } + } + + private static final class SystemCycleA implements System { + @Override + public Collection>> getDependencies() { + return Set.of(SystemCycleB.class); + } + + @Override + public void update(final World world, final String state, final Duration duration) { + } + } + + private static final class SystemCycleB implements System { + @Override + public Collection>> getDependencies() { + return Set.of(SystemCycleA.class); + } + + @Override + public void update(final World world, final String state, final Duration duration) { + } + } + + private static final class ComponentAdderSystem implements System { + @Override + public Collection>> getDependencies() { + return List.of(); + } + + @Override + public void update(final World world, final String state, final Duration duration) { + world.query(Set.of(PositionComponent.class)).forEach(e -> e.add(new VelocityComponent())); + } + } + + @RequiredArgsConstructor + private static final class ComponentReaderSystem implements System { + private final List> queryResults; + + @Override + public Collection>> getDependencies() { + return Set.of(ComponentAdderSystem.class); + } + + @Override + public void update(final World world, final String state, final Duration duration) { + queryResults.add(world.query(Set.of(VelocityComponent.class, PositionComponent.class))); + } + } } diff --git a/core/src/test/java/coffee/liz/ecs/model/EntityTest.java b/core/src/test/java/coffee/liz/ecs/model/EntityTest.java index c9ce59a..a8fd1e3 100644 --- a/core/src/test/java/coffee/liz/ecs/model/EntityTest.java +++ b/core/src/test/java/coffee/liz/ecs/model/EntityTest.java @@ -18,80 +18,80 @@ import java.util.List; import java.util.stream.Stream; final class EntityTest { - @ParameterizedTest - @MethodSource("componentCombinationProvider") - public void hasAllReportsPresenceForComponentSets(final Collection> query, - final boolean expected) { - final Entity entity = Entity.builder().id(7).build(); - entity.add(new AlphaComponent("first")); - entity.add(new BetaComponent(3)); - entity.add(new GammaKeyedComponent()); - - assertEquals(expected, entity.hasAll(query)); - } - - private static Stream componentCombinationProvider() { - return Stream - .of(Arguments.of(List.of(AlphaComponent.class), true), Arguments.of(List.of(BetaComponent.class), true), - Arguments.of(List.of(AlphaComponent.class, BetaComponent.class, GammaComponent.class), true), - Arguments.of(List.of(AlphaComponent.class, BetaComponent.class, GammaKeyedComponent.class), - false), - Arguments.of(List.of(GammaComponent.class), true), - Arguments.of(List.of(GammaKeyedComponent.class), false)); - } - - @Test - public void getThrowsForMissingComponentsWithHelpfulMessage() { - final Entity entity = Entity.builder().id(99).build(); - - final IllegalArgumentException thrown = assertThrows(IllegalArgumentException.class, - () -> entity.get(AlphaComponent.class)); - - assertTrue(thrown.getMessage().contains("AlphaComponent")); - assertTrue(thrown.getMessage().contains("99")); - } - - @Test - public void addReplacesExistingComponentInstance() { - final Entity entity = Entity.builder().id(17).build(); - final AlphaComponent initial = new AlphaComponent("initial"); - entity.add(initial); - - final AlphaComponent replacement = new AlphaComponent("replacement"); - entity.add(replacement); - - assertSame(replacement, entity.get(AlphaComponent.class)); - } - - @Test - public void removeClearsComponentPresence() { - final Entity entity = Entity.builder().id(45).build(); - entity.add(new BetaComponent(2)); - assertTrue(entity.has(BetaComponent.class)); - - entity.remove(BetaComponent.class); - - assertFalse(entity.has(BetaComponent.class)); - assertTrue(entity.componentTypes().isEmpty()); - } - - private record AlphaComponent(String name) implements Component { - } - - private record BetaComponent(int level) implements Component { - } - - @RequiredArgsConstructor - private class GammaComponent implements Component { - @Override - public Class getKey() { - return GammaComponent.class; - } - } - - private class GammaKeyedComponent extends GammaComponent { - } - - private record ContextualComponent(int ownerId) implements Component { - } + @ParameterizedTest + @MethodSource("componentCombinationProvider") + public void hasAllReportsPresenceForComponentSets(final Collection> query, + final boolean expected) { + final Entity entity = Entity.builder().id(7).build(); + entity.add(new AlphaComponent("first")); + entity.add(new BetaComponent(3)); + entity.add(new GammaKeyedComponent()); + + assertEquals(expected, entity.hasAll(query)); + } + + private static Stream componentCombinationProvider() { + return Stream + .of(Arguments.of(List.of(AlphaComponent.class), true), Arguments.of(List.of(BetaComponent.class), true), + Arguments.of(List.of(AlphaComponent.class, BetaComponent.class, GammaComponent.class), true), + Arguments.of(List.of(AlphaComponent.class, BetaComponent.class, GammaKeyedComponent.class), + false), + Arguments.of(List.of(GammaComponent.class), true), + Arguments.of(List.of(GammaKeyedComponent.class), false)); + } + + @Test + public void getThrowsForMissingComponentsWithHelpfulMessage() { + final Entity entity = Entity.builder().id(99).build(); + + final IllegalArgumentException thrown = assertThrows(IllegalArgumentException.class, + () -> entity.get(AlphaComponent.class)); + + assertTrue(thrown.getMessage().contains("AlphaComponent")); + assertTrue(thrown.getMessage().contains("99")); + } + + @Test + public void addReplacesExistingComponentInstance() { + final Entity entity = Entity.builder().id(17).build(); + final AlphaComponent initial = new AlphaComponent("initial"); + entity.add(initial); + + final AlphaComponent replacement = new AlphaComponent("replacement"); + entity.add(replacement); + + assertSame(replacement, entity.get(AlphaComponent.class)); + } + + @Test + public void removeClearsComponentPresence() { + final Entity entity = Entity.builder().id(45).build(); + entity.add(new BetaComponent(2)); + assertTrue(entity.has(BetaComponent.class)); + + entity.remove(BetaComponent.class); + + assertFalse(entity.has(BetaComponent.class)); + assertTrue(entity.componentTypes().isEmpty()); + } + + private record AlphaComponent(String name) implements Component { + } + + private record BetaComponent(int level) implements Component { + } + + @RequiredArgsConstructor + private class GammaComponent implements Component { + @Override + public Class getKey() { + return GammaComponent.class; + } + } + + private class GammaKeyedComponent extends GammaComponent { + } + + private record ContextualComponent(int ownerId) implements Component { + } } -- cgit v1.3