#! Contains the tests of `ecs`: entities and their components, the #! tables they move between, closing what components hold, passes and their #! filters, deferring, chunks, what changed, passes on several threads, #! resources, commands, schedules, components of one name in two modules, #! components reached by their types' keys, and entities written by a #! numbering. import std::json; import std::serial; import std::testing; import ecs; struct position { var x: f32; var y: f32; } struct velocity { var x: f32; var y: f32; } struct health { var points: i64; } struct frozen {} struct player {} # Counts how many `named` were closed holding a name. var names_closed: u64 = 0; # A component holding what closes, which counts its closing. struct named : owning { var text: string; } def named::close(&mut this): void { if this.text.length > 0 { names_closed++; } this.text.close(); } def named::of(text: strview): named { return named{ text = string::from(text, nothing{}) }; } # Whether an entity has a component a test passes. def given(w: &mut world, e: entity, test: F): bool where F: invokable { if w.get::(e) is some(&mut value) { return test(value); } return false; } # Whether the world holds a resource a test passes. def held(w: &mut world, test: F): bool where F: invokable { if w.resource::() is some(&mut value) { return test(value); } return false; } @test def spawns_and_despawns_entities(t: &mut tester): void { var w := world::new(); val a := w.spawn(); val b := w.spawn(); t.expects(w.alive(a) && w.alive(b) && a != b && w.count == 2); t.expects(!w.alive(entity::none)); t.expects(w.despawn(a)); t.expects(!w.alive(a) && !w.despawn(a) && w.count == 1); # The index is used again, by a generation after. val c := w.spawn(); t.expects(c.index == a.index && c.generation == a.generation + 1); t.expects(w.alive(c) && !w.alive(a)); } @test def sets_gets_and_removes_components(t: &mut tester): void { var w := world::new(); val e := w.spawn(); w.set(e, position{ x = 1, y = 2 }); t.expects(w.has::(e) && !w.has::(e)); if w.get::(e) is some(&mut p) { t.expects(p.x == 1 && p.y == 2); p.x = 5; } t.expects(given(&mut w, e, def(p: &position) => p.x == 5)); t.expects(w.get::(e).is_none); w.set(e, position{ x = 7, y = 8 }); t.expects(given(&mut w, e, def(p: &position) => p.x == 7 && p.y == 8)); w.add::(e); t.expects(w.has::(e) && w.get::(e).is_none); t.expects(given(&mut w, e, def(p: &position) => p.x == 7)); t.expects(w.remove::(e)); t.expects(!w.has::(e) && w.has::(e) && !w.remove::(e)); t.expects(w.describe(e) == "frozen"); } @test def moves_entities_between_tables(t: &mut tester): void { var w := world::new(); var all := vector::::new(); for i in 0..100u { val e := w.spawn(); w.set(e, position{ x = i as f32 }); if i % 2 == 0 { w.set(e, velocity{ y = i as f32 }); } if i % 3 == 0 { w.set(e, health{ points = i as i64 }); } all.append(e); } # Despawning some moves others within their tables. for i in 0..100u { if i % 5 == 0 { _ = w.despawn(all[i]); } } for i in 0..100u { val e := all[i]; if i % 5 == 0 { t.expects(!w.alive(e)); continue; } t.expects(given(&mut w, e, def(p: &position) => p.x == i as f32)); t.expects(w.has::(e) == (i % 2 == 0)); if i % 2 == 0 { t.expects(given(&mut w, e, def(v: &velocity) => v.y == i as f32)); } t.expects(w.has::(e) == (i % 3 == 0)); if i % 3 == 0 { t.expects(given(&mut w, e, def(h: &health) => h.points == i as i64)); } } t.expects(w.count == 80); } @test def spawns_with_components(t: &mut tester): void { var w := world::new(); val e := w.spawn_with(velocity{ x = 3 }, position{ x = 1 }, frozen{}); t.expects(given(&mut w, e, def(p: &position) => p.x == 1)); t.expects(given(&mut w, e, def(v: &velocity) => v.x == 3)); t.expects(w.has::(e)); val tables := w.archetypes; # The same set in another order is the same table. val f := w.spawn_with(frozen{}, position{ x = 2 }, velocity{}); t.expects(w.archetypes == tables); t.expects(given(&mut w, f, def(p: &position) => p.x == 2)); } @test def closes_what_components_hold(t: &mut tester): void { names_closed = 0; { var w := world::new(); val a := w.spawn_with(named::of("a"), position{}); val b := w.spawn_with(named::of("b")); val c := w.spawn(); w.set(c, named::of("c")); t.expects(given(&mut w, a, def(n: &named) => n.text == "a")); t.expects(names_closed == 0); w.set(a, named::of("a2")); t.expects(names_closed == 1); t.expects(given(&mut w, a, def(n: &named) => n.text == "a2")); # Moving to another table moves what it holds, without closing it. w.set(b, position{}); t.expects(names_closed == 1 && given(&mut w, b, def(n: &named) => n.text == "b")); t.expects(w.remove::(b)); t.expects(names_closed == 2); t.expects(w.despawn(a)); t.expects(names_closed == 3); w.set_resource(named::of("kept")); } # The world closes what's left: `c`'s, and the resource. t.expects(names_closed == 5); } @test def passes_over_entities_with_each(t: &mut tester): void { var w := world::new(); for i in 0..10u { val e := w.spawn_with(position{ x = i as f32 }, velocity{ x = 1, y = 2 }); if i % 2 == 0 { w.set(e, health{ points = 10 }); } } _ = w.spawn_with(position{ x = 100 }); w.each(def(p: &mut position, v: &velocity) { p.x += v.x; p.y += v.y; }); var sum: f32 = 0; var count := 0u; w.each(def(p: &position) { sum += p.x; count++; }); t.expects(count == 11 && sum == 45 + 10 + 100); # The entity, and what an entity may lack, in any order. var with_health := 0u; var without := 0u; w.each(def(h: maybe<&mut health>, e: entity, v: &velocity) { t.expects(w.alive(e)); if h is some(&mut got) { got.points -= 1; with_health++; } else { without++; } }); t.expects(with_health == 5 && without == 5); var points: i64 = 0; w.each(def(h: &health) { points += h.points; }); t.expects(points == 45); } @test def filters_by_with_and_without(t: &mut tester): void { var w := world::new(); for i in 0..12u { val e := w.spawn_with(position{ x = i as f32 }); if i % 3 == 0 { w.add::(e); } if i % 2 == 0 { w.add::(e); } } t.expects(w.query(with::()).count() == 4); t.expects(w.query(without::()).count() == 6); t.expects(w.query(with::().without::()).count() == 2); var seen := vector::::new(); w.query(with::().without::()).each(def(p: &position) { seen.append(p.x); }); seen.sort(); t.expects(seen.length == 2 && seen[0] == 3 && seen[1] == 9); # A tag taken as a parameter is asked for too. var tagged := 0u; w.each(def(p: &position, f: &frozen) { tagged++; }); t.expects(tagged == 6); } @test def defers_changes_while_passing(t: &mut tester): void { var w := world::new(); var all := vector::::new(); for i in 0..20u { all.append(w.spawn_with(health{ points = i as i64 })); } var passed := 0u; w.each(def(e: entity, h: &mut health) { passed++; if h.points % 2 == 0 { t.expects(w.despawn(e)); # Despawning is deferred, so the entity is alive until the pass ends. t.expects(w.alive(e)); } else { # A component it lacks is given as the pass ends, and one it has at once. w.set(e, velocity{ x = 1 }); t.expects(!w.has::(e)); w.set(e, health{ points = 100 }); t.expects(given(&mut w, e, def(got: &health) => got.points == 100)); } val spawned := w.spawn(); t.expects(w.alive(spawned) && !w.has::(spawned)); }); t.expects(passed == 20); t.expects(w.count == 10 + 20); t.expects(w.query(with::()).count() == 10); for i in 0..20u { t.expects(w.alive(all[i]) == (i % 2 == 1)); } } @test def walks_chunks(t: &mut tester): void { var w := world::new(); for i in 0..50u { val e := w.spawn_with(position{ x = i as f32 }, velocity{ x = 2 }); if i >= 30 { w.add::(e); } } var chunks := 0u; for c in w.query(with::().with::()).chunks() { chunks++; val ps := c.write::(); val vs := c.read::(); for i in 0..c.length { ps[i].x += vs[i].x; } t.expects(c.entities().length == c.length && c.has::()); } t.expects(chunks == 2); var sum: f32 = 0; w.each(def(p: &position) { sum += p.x; }); t.expects(sum == 1225 + 100); } @test def sees_what_changed(t: &mut tester): void { var w := world::new(); val a := w.spawn_with(position{}, velocity{ x = 1 }); val b := w.spawn_with(position{}, frozen{}); var moved := w.query(changed::()); # Every table is new to the query, and was written as its entities were made. t.expects(moved.count() == 2); var seen := 0u; moved.each(def(p: &position) { seen++; }); t.expects(seen == 2); # Nothing was written since. seen = 0; moved.each(def(p: &position) { seen++; }); t.expects(seen == 0); # A pass writing positions writes those of its tables. w.each(def(p: &mut position, v: &velocity) { p.x += v.x; }); seen = 0; moved.each(def(e: entity, p: &position) { t.expects(e == a); seen++; }); t.expects(seen == 1); # As does `set`. w.set(b, position{ x = 4 }); seen = 0; moved.each(def(e: entity, p: &position) { t.expects(e == b); seen++; }); t.expects(seen == 1); } @test def passes_in_parallel(t: &mut tester): void { var w := world::new(); w.threads(4); val count: u64 = 100000; for i in 0..count { _ = w.spawn_with(position{ x = i as f32 }, velocity{ x = 1, y = 2 }, health{ points = i as i64 }); } w.par_each(def(p: &mut position, v: &velocity) { p.x += v.x; p.y += v.y; }); var wrong := 0u; var total: i64 = 0; w.each(def(p: &position, h: &health) { if p.x != h.points as f32 + 1 || p.y != 2 { wrong++; } total += h.points; }); t.expects(wrong == 0 && total == (count * (count - 1) / 2) as i64); # Changes deferred on several threads are made as the pass ends. w.par_each(def(e: entity, h: &health) { if h.points % 4 == 0 { _ = w.despawn(e); } }); t.expects(w.count == count - count / 4); } @test def holds_resources(t: &mut tester): void { var w := world::new(); t.expects(w.resource::().is_none); w.set_resource(health{ points = 3 }); if w.resource::() is some(&mut h) { h.points++; } t.expects(held(&mut w, def(h: &health) => h.points == 4)); w.set_resource(health{ points = 9 }); t.expects(held(&mut w, def(h: &health) => h.points == 9)); t.expects(w.remove_resource::() && !w.remove_resource::()); t.expects(w.resource::().is_none); } # A system of its own type, keeping its query between runs, so it sees what # changed since it last ran. struct watcher : invokable { var moved: query; var counts: *mut vector; # How many it saw, each run. } def watcher::invoke(&mut this, w: &mut world): void { var seen := 0u; this.moved.each(def(p: &position) { seen++; }); through(this.counts).append(seen); } @test def runs_systems_in_order(t: &mut tester): void { var w := world::new(); for _ in 0..3u { _ = w.spawn_with(position{}, velocity{ x = 1 }); } _ = w.spawn_with(position{}); var order := vector::::new(); var counts := vector::::new(); var plan := schedule::new(); plan.add("watch", watcher{ moved = w.query(changed::()), counts = &mut counts }); plan.add("move", def(into: &mut world) { order.append(1); into.each(def(p: &mut position, v: &velocity) { p.x += v.x; }); }); plan.add("count", def(into: &mut world) { order.append(2); }); t.expects(plan.length == 3); plan.run(&mut w); plan.run(&mut w); t.expects(order.length == 4 && order[0] == 1 && order[1] == 2 && order[2] == 1 && order[3] == 2); w.each(def(p: &position, v: &velocity) { t.expects(p.x == 2); }); # The watcher saw every position first, then those `move` wrote since. t.expects(counts.length == 2 && counts[0] == 4 && counts[1] == 3); val timings := plan.timings(); t.expects(timings.view().find("watch: ").is_some && timings.view().find("count: ").is_some); } @test def applies_commands(t: &mut tester): void { names_closed = 0; var w := world::new(); val a := w.spawn_with(position{}); val b := w.spawn_with(position{}, velocity{}); var later := commands::new(); later.set(a, velocity{ x = 3 }); later.remove::(b); later.despawn(b); later.set(b, named::of("never")); later.add::(a); t.expects(later.length == 5 && !w.has::(a)); w.apply(&mut later); t.expects(later.length == 0); t.expects(given(&mut w, a, def(v: &velocity) => v.x == 3) && w.has::(a)); t.expects(!w.alive(b)); # The value given the despawned entity was closed. t.expects(names_closed == 1); } @test def tells_apart_components_of_one_name(t: &mut tester): void { # Types are known by their paths, so another module's `position` is a # component of its own, which a function names as its module does. var w := world::new(); val e := w.spawn_with(position{ x = 1 }, elsewhere::position{ x = 2 }); var mine: f32 = 0; var theirs: f32 = 0; w.each(def(p: &position, q: &mut elsewhere::position) { mine = p.x; theirs = q.x; q.x = 3; }); t.expects(mine == 1 && theirs == 2); t.expects(given(&mut w, e, def(q: &elsewhere::position) => q.x == 3)); t.expects(w.describe(e) == "position, elsewhere::position"); } @test def reaches_components_by_their_keys(t: &mut tester): void { # By its type's key, and the bytes of a value, as a program does for code it # runs that names a type the program doesn't. var w := world::new(); val e := w.spawn_with(position{ x = 1 }, named::of("box")); val key := component_key::(); t.expects(w.info_by_key(key) != null && w.info_by_key(key)[0].size == 8); val at := w.get_by_key(e, key) as *mut position; t.expects(at != null && at[0].x == 1); at[0].x = 4; t.expects(given(&mut w, e, def(p: &position) => p.x == 4)); t.expects(w.has_by_key(e, component_key::()) && !w.has_by_key(e, component_key::())); # A type of plain data the world never met, as the program makes it. var speed := velocity{ x = 2, y = 3 }; val made := component_info::plain(component_key::(), "velocity", "velocity", size_of::(), align_of::()); w.set_by_info(e, &made, (&mut speed) as *mut u8); t.expects(given(&mut w, e, def(v: &velocity) => v.x == 2 && v.y == 3)); var found := vector::::new(); w.entities_by_key(component_key::(), &mut found); t.expects(found.length == 1 && found[0] == e); t.expects(w.remove_by_key(e, component_key::()) && !w.has::(e)); w.set_resource(health{ points = 9 }); val held := w.resource_by_key(component_key::()) as *mut health; t.expects(held != null && held[0].points == 9 && w.resource_info_by_key(component_key::()) != null); t.expects(w.resource_by_key(component_key::()) == null); # A schedule's systems are taken out by name. var plan := schedule::new(); plan.add("first", def(into: &mut world) {}); plan.add("second", def(into: &mut world) {}); t.expects(plan.remove("first") && !plan.remove("first") && plan.length == 1); } # What follows another entity, as a camera its player: a component naming an # entity, which a format writes by the numbering applied. @serial struct follows { @serial var target: entity; @serial var gap: f32 = 2; } # The JSON of a `follows` naming a number, then the text after it. def naming(number: u64, after: strview): string { var made := string::from("{\"target\":", nothing{}); made.append($"{number}".view()); made.append(after); return made; } # The entity a `follows` read from JSON names, or `entity::none`. def target_read(text: strview): entity { val read := json::from_text::(text) else (_) { return entity::none; }; return read.target; } # Reads with another numbering applied, which names an entity 20 too. def read_within(w: &mut world): entity { var other := entity_numbering::new(); val c := w.spawn(); other.set(c, 20); val applied := other.apply(); seen_within = c; return target_read("{\"target\": 20, \"gap\": 1}"); } var seen_within := entity::none; @test def writes_entities_by_a_numbering(t: &mut tester): void { var w := world::new(); val a := w.spawn(); val b := w.spawn(); # With none applied, an entity is written as its bits, and none as nothing. t.expects(json::to_text(&follows{ target = b }).value.view() == naming(b.bits, ",\"gap\":2}").view()); t.expects(json::to_text(&follows{ target = entity::none }).value.view() == "{\"target\":null,\"gap\":2}"); t.expects(target_read(naming(b.bits, ",\"gap\":1}").view()) == b); # Applied, each is written as its number, and read as the entity its number # names; one it gives no number is written as nothing. var ids := entity_numbering::new(); ids.set(a, 10); ids.set(b, 20); t.expects(ids.number_of(b).some == 20 && ids.entity_of(10).some == a && ids.entity_of(30) is none); val applied := ids.apply(); t.expects(json::to_text(&follows{ target = b, gap = 1.5 }).value.view() == "{\"target\":20,\"gap\":1.5}"); t.expects(json::to_text(&follows{ target = w.spawn() }).value.view() == "{\"target\":null,\"gap\":2}"); t.expects(target_read("{\"target\": 20, \"gap\": 1}") == b); t.expects(json::from_text::("{\"target\": 30, \"gap\": 1}").error.kind == json::error_kind::out_of_range); # One applied within it is applied until it closes, then this one again. t.expects(read_within(&mut w) == seen_within && seen_within != b); t.expects(target_read("{\"target\": 20, \"gap\": 1}") == b); } module elsewhere; pub struct position { pub var x: f32; }