summaryrefslogtreecommitdiff
path: root/core/src/test/java/coffee/liz/ecs/DAGWorldTest.java
blob: 7f3dc2f654bb0025e498b4e406cb1ddf964929f8 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
package coffee.liz.ecs;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;

import coffee.liz.ecs.model.Component;
import coffee.liz.ecs.model.Entity;
import coffee.liz.ecs.model.System;
import coffee.liz.ecs.model.World;
import coffee.liz.ecs.model.Query;

import lombok.RequiredArgsConstructor;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;

import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class DAGWorldTest {
    @ParameterizedTest
    @MethodSource("queryScenarios")
    public void queryResolvesExpectedEntities(final Query query, final Set<String> expectedEntityKeys) {
        final World world = new DAGWorld();
        final Entity both = world.createEntity();
        both.add(new PositionComponent());
        both.add(new VelocityComponent());
        final Entity positionOnly = world.createEntity();
        positionOnly.add(new PositionComponent());
        final Entity velocityOnly = world.createEntity();
        velocityOnly.add(new VelocityComponent());
        final Entity neither = world.createEntity();

        world.update(0);

        final Map<String, Entity> entities = Map.of("both", both, "positionOnly", positionOnly, "velocityOnly",
                velocityOnly, "neither", neither);
        final Set<Entity> expectedEntities = expectedEntityKeys.stream().map(entities::get).collect(Collectors.toSet());
        assertEquals(expectedEntities, world.resolve(query));
    }

    private static Stream<Arguments> queryScenarios() {
        return Stream.of(Arguments.of(Query.allOf(PositionComponent.class, VelocityComponent.class), Set.of("both")),
                Arguments.of(Query.allOf(), Set.of("both", "positionOnly", "velocityOnly", "neither")),
                Arguments.of(Query.anyOf(PositionComponent.class, VelocityComponent.class),
                        Set.of("both", "positionOnly", "velocityOnly")),
                Arguments.of(Query.noneOf(PositionComponent.class, VelocityComponent.class), Set.of("neither")));
    }

    @Test
    public void updateExecutesSystemsInTopologicalOrder() {
        final CopyOnWriteArrayList<String> executionLog = new CopyOnWriteArrayList<>();

        final DAGWorld world = new DAGWorld(new SystemC(executionLog), new SystemA(executionLog),
                new SystemB(executionLog));
        world.update(0);

        assertEquals(List.of("A", "B", "C"), executionLog);
    }

    @Test
    public void cacheTracksComponentMutationsViaEntityEvents() {
        final DAGWorld world = new DAGWorld();
        final Entity subject = world.createEntity();

        assertTrue(world.resolve(Query.allOf(PositionComponent.class)).isEmpty());

        subject.add(new PositionComponent());
        assertEquals(1, world.resolve(Query.allOf(PositionComponent.class)).size());

        subject.remove(PositionComponent.class);
        assertTrue(world.resolve(Query.allOf(PositionComponent.class)).isEmpty());
    }

    @Test
    public void removedEntityNoLongerMutatesWorldCache() {
        final DAGWorld world = new DAGWorld();
        final Entity subject = world.createEntity();

        subject.add(new PositionComponent());
        assertEquals(Set.of(subject), world.resolve(Query.allOf(PositionComponent.class)));

        world.removeEntity(subject);
        subject.remove(PositionComponent.class);
        subject.add(new PositionComponent());

        assertTrue(world.resolve(Query.allOf(PositionComponent.class)).isEmpty());
    }

    @Test
    public void queryFindsComponentsAddedByEarlierSystemInSameTick() {
        final List<Set<Entity>> queryResults = new CopyOnWriteArrayList<>();

        final DAGWorld world = new DAGWorld(new ComponentAdderSystem(), new ComponentReaderSystem(queryResults));

        final Entity entity = world.createEntity();
        entity.add(new PositionComponent());

        world.update(0);

        assertEquals(1, queryResults.size());
        assertEquals(Set.of(entity), queryResults.get(0));
    }

    @Test
    public void circularDependencyDetectionThrowsIllegalStateException() {
        assertThrows(IllegalStateException.class, () -> new DAGWorld(new SystemCycleA(), new SystemCycleB()));
    }

    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<String> log;
        private final String label;

        @Override
        public final void update(final World world, final float deltaSeconds) {
            log.add(label);
        }
    }

    private static final class SystemA extends RecordingSystem {
        private SystemA(final List<String> log) {
            super(log, "A");
        }

        @Override
        public Collection<Class<? extends System>> getDependencies() {
            return List.of();
        }
    }

    private static final class SystemB extends RecordingSystem {
        private SystemB(final List<String> log) {
            super(log, "B");
        }

        @Override
        public Collection<Class<? extends System>> getDependencies() {
            return Set.of(SystemA.class);
        }
    }

    private static final class SystemC extends RecordingSystem {
        private SystemC(final List<String> log) {
            super(log, "C");
        }

        @Override
        public Collection<Class<? extends System>> getDependencies() {
            return Set.of(SystemB.class);
        }
    }

    private static final class SystemCycleA implements System {
        @Override
        public Collection<Class<? extends System>> getDependencies() {
            return Set.of(SystemCycleB.class);
        }

        @Override
        public void update(final World world, final float deltaSeconds) {
        }
    }

    private static final class SystemCycleB implements System {
        @Override
        public Collection<Class<? extends System>> getDependencies() {
            return Set.of(SystemCycleA.class);
        }

        @Override
        public void update(final World world, final float deltaSeconds) {
        }
    }

    private static final class ComponentAdderSystem implements System {
        @Override
        public Collection<Class<? extends System>> getDependencies() {
            return List.of();
        }

        @Override
        public void update(final World world, final float deltaSeconds) {
            world.resolve(Query.allOf(PositionComponent.class)).forEach(e -> e.add(new VelocityComponent()));
        }
    }

    @RequiredArgsConstructor
    private static final class ComponentReaderSystem implements System {
        private final List<Set<Entity>> queryResults;

        @Override
        public Collection<Class<? extends System>> getDependencies() {
            return Set.of(ComponentAdderSystem.class);
        }

        @Override
        public void update(final World world, final float deltaSeconds) {
            queryResults.add(world.resolve(Query.allOf(VelocityComponent.class, PositionComponent.class)));
        }
    }
}