aboutsummaryrefslogtreecommitdiff
path: root/core/src/test/java/coffee/liz/ecs/DAGWorldTest.java
blob: a656d28ecbdd4e0c4f79bbfc2d8e03cc5c25c089 (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
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 lombok.RequiredArgsConstructor;

import org.junit.jupiter.api.Test;

import java.time.Duration;
import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.CopyOnWriteArrayList;

final class DAGWorldTest {
	@Test
	public void queryReturnsEntitiesMatchingAllRequestedComponents() {
		final DAGWorld<String> 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<Entity> results = world.query(Set.of(PositionComponent.class, VelocityComponent.class));

		assertEquals(Set.of(matching), results);
	}

	@Test
	public void queryWithoutComponentsReturnsAllEntities() {
		final DAGWorld<String> world = new DAGWorld<>(Map.of());
		final Entity entityOne = world.createEntity();
		final Entity entityTwo = world.createEntity();

		final Set<Entity> results = world.query(List.<Class<? extends Component>>of());

		assertEquals(Set.of(entityOne, entityTwo), results);
	}

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

		final DAGWorld<String> 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<String> 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<Set<Entity>> queryResults = new CopyOnWriteArrayList<>();

		final Map<Class<? extends System<String>>, System<String>> systems = new LinkedHashMap<>();
		systems.put(ComponentAdderSystem.class, new ComponentAdderSystem());
		systems.put(ComponentReaderSystem.class, new ComponentReaderSystem(queryResults));
		final DAGWorld<String> 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<Class<? extends System<String>>, System<String>> 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<String> {
		private final List<String> log;
		private final String label;

		@Override
		public final void update(final World<String> world, final String state, final Duration duration) {
			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<String>>> 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<String>>> 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<String>>> getDependencies() {
			return Set.of(SystemB.class);
		}
	}

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

		@Override
		public void update(final World<String> world, final String state, final Duration duration) {
		}
	}

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

		@Override
		public void update(final World<String> world, final String state, final Duration duration) {
		}
	}

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

		@Override
		public void update(final World<String> 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<String> {
		private final List<Set<Entity>> queryResults;

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

		@Override
		public void update(final World<String> world, final String state, final Duration duration) {
			queryResults.add(world.query(Set.of(VelocityComponent.class, PositionComponent.class)));
		}
	}
}