#! Contains the tests of `ecs`: entities and their components, the #! tables they move between, closing what components hold, passes and their #! filters, by type and by path, deferring, chunks, what changed, passes on #! several threads, a world shared by threads, resources, commands, schedules, #! components of one name in two modules, components reached by their types' #! keys, entities written by a numbering, what was written and given of each #! entity, observers, parents and children, and entities spawned by threads. import std::json; import std::serial; import std::testing; import std::thread; import malunal::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 filters_by_paths(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); } } # A type named by its path is the type. t.expects(key_of_path(path_of::()) == component_key::()); t.expects(w.query(with_path(path_of::())).count() == 4); t.expects(w.query(with::().without_path(path_of::())).count() == 2); # No entity has a type the world hasn't met, and every one lacks it, until # it's met. t.expects(w.query(with_path("game::nothing")).count() == 0); t.expects(w.query(without_path("game::nothing")).count() == 12); var healthy := w.query(with_path(path_of::())); t.expects(healthy.count() == 0); _ = w.spawn_with(health{}); t.expects(healthy.count() == 1); # What changed, by path. var moved := w.query(changed_path(path_of::())); var seen := 0u; moved.each(def(p: &position) { seen++; }); t.expects(seen == 12); seen = 0; moved.each(def(p: &position) { seen++; }); t.expects(seen == 0); } @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, entities # spawned among them. w.par_each(def(e: entity, h: &health) { if h.points % 4 == 0 { _ = w.despawn(e); } if h.points % 100 == 1 { _ = w.spawn_with(frozen{}, health{ points = -1 }); } }); t.expects(w.count == count - count / 4 + count / 100); t.expects(w.query(with::()).count() == count / 100); } # What the threads sharing a world counted, each writing its own. var shared_frozen: u64 = 0; var shared_rows: u64 = 0; var shared_unfrozen: u64 = 0; # Moves each position by its velocity, twenty times. def moves(at: *mut world): void { val w := &mut through(at); for _ in 0..20u { w.each(def(p: &mut position, v: &velocity) { p.x += v.x; }); } } # Takes ten points from each health, then despawns those left with none. def ages(at: *mut world): void { val w := &mut through(at); for _ in 0..10u { w.each(def(h: &mut health) { h.points -= 1; }); } w.each(def(e: entity, h: &health) { if h.points <= 0 { _ = w.despawn(e); } }); } # Counts by queries the world hasn't seen: by a path, by `par_each`, and by # chunks. def counts(at: *mut world): void { val w := &mut through(at); shared_frozen = w.query(with_path(path_of::())).count(); var rows := 0u; w.par_each(def(v: &velocity) { rows++; }); shared_rows = rows; for c in w.query(with::().without::()).chunks() { shared_unfrozen += c.length; } } # Makes every fifth entity a player, which the world defers. def picks(at: *mut world): void { val w := &mut through(at); w.each(def(e: entity, v: &velocity) { if e.index % 5 == 0 { w.add::(e); } }); } @test def shares_the_world_between_threads(t: &mut tester): void { var w := world::new(); val count: u64 = 40000; for i in 0..count { val e := w.spawn_with(position{}, velocity{ x = 1 }, health{ points = (i % 20) as i64 }); if i % 4 == 0 { w.add::(e); } } w.begin_sharing(); t.expects(w.shared); val at := address(&mut w); var a := thread::spawn_with(moves, at) else (_) { return; }; var b := thread::spawn_with(ages, at) else (_) { return; }; var c := thread::spawn_with(counts, at) else (_) { return; }; var d := thread::spawn_with(picks, at) else (_) { return; }; a.join(); b.join(); c.join(); d.join(); t.expects(shared_frozen == count / 4 && shared_rows == count && shared_unfrozen == count - count / 4); # What the threads deferred is made as the sharing ends: those of health # 10 or less despawned, and a player made of every fifth left. t.expects(w.count == count); w.end_sharing(); t.expects(!w.shared && w.count == count * 9 / 20); var wrong := 0u; w.each(def(p: &position, h: &health) { if p.x != 20 || h.points <= 0 { wrong++; } }); t.expects(wrong == 0); t.expects(w.query(with::()).count() == count / 20); } @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); # Closed the last given first, as one made of another closes before it. closed_order = 0; hold_two_and_close(); t.expects(closed_order == 21); } # The marks of the resources closed, a digit each, in the order they closed. var closed_order: u64 = 0; # Resources noting their closing, as a window, and a renderer drawing into it. struct held_first : owning { var mark: u64; } def held_first::close(&mut this): void { if this.mark != 0 { closed_order = closed_order * 10 + this.mark; } } struct held_second : owning { var mark: u64; } def held_second::close(&mut this): void { if this.mark != 0 { closed_order = closed_order * 10 + this.mark; } } # Gives a world two resources, which close with it. def hold_two_and_close(): void { var w := world::new(); w.set_resource(held_first{ mark = 1 }); w.set_resource(held_second{ mark = 2 }); } # 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 resource of a type the world never met, and one replaced. w.set_resource_by_info(&made, (&mut speed) as *mut u8); var faster := velocity{ x = 5, y = 6 }; w.set_resource_by_info(&made, (&mut faster) as *mut u8); t.expects(w.resource::() is some(&mut v) && v.x == 5 && v.y == 6); var less := health{ points = 2 }; w.set_resource_by_info(w.resource_info_by_key(component_key::()), (&mut less) as *mut u8); t.expects(w.resource::() is some(&mut h) && h.points == 2); # 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); } # How many entities a query passes, by a pass. def passed(q: &mut query): u64 { var seen := 0u; q.each(def(e: entity) { seen++; }); return seen; } @test def knows_what_changed_of_each_entity(t: &mut tester): void { var w := world::new(); val a := w.spawn_with(position{}, velocity{ x = 1 }); val b := w.spawn_with(position{}, velocity{}); val c := w.spawn_with(position{}, frozen{}); var moved := w.query(changed::()); # Every entity, the first time; none since. t.expects(moved.count() == 3 && passed(&mut moved) == 3); t.expects(moved.count() == 0 && passed(&mut moved) == 0); # One set, and not the other of its table. w.set(b, position{ x = 2 }); t.expects(moved.count() == 1); moved.each(def(e: entity) { t.expects(e == b); }); # A pass writing positions writes those it passes. w.each(def(p: &mut position, v: &velocity) { p.x += v.x; }); t.expects(passed(&mut moved) == 2); # One moved to another table keeps when its value was written. w.add::(c); t.expects(passed(&mut moved) == 0); w.set(c, position{ x = 1 }); t.expects(passed(&mut moved) == 1); # A pass of those whose velocity changed writes the positions of those alone. var fast := w.query(changed::()); t.expects(passed(&mut fast) == 2); w.set(a, velocity{ x = 3 }); fast.each(def(p: &mut position, v: &velocity) { p.x += v.x; }); moved.each(def(e: entity) { t.expects(e == a); }); # Chunks say which of their entities pass. w.set(b, position{ x = 7 }); var walked := 0u; for ch in moved.chunks() { val es := ch.entities(); for i in 0..ch.length { if ch.passes(i) { t.expects(es[i] == b); walked++; } } } t.expects(walked == 1); } @test def knows_what_was_added_to_each_entity(t: &mut tester): void { var w := world::new(); val a := w.spawn_with(position{}); var given := w.query(added::()); t.expects(passed(&mut given) == 0); # Tags as well as values, given or spawned with. w.add::(a); val b := w.spawn_with(position{}, frozen{}); t.expects(given.count() == 2 && passed(&mut given) == 2); t.expects(passed(&mut given) == 0); # Moving to another table, it isn't given it again; taken and given, it is. w.add::(b); _ = w.remove::(a); w.add::(a); given.each(def(e: entity) { t.expects(e == a); }); t.expects(passed(&mut given) == 0); # Of a value, as `set` gives one the entity lacked, and not as it replaces it. var speeds := w.query(added::()); t.expects(passed(&mut speeds) == 0); w.set(a, velocity{ x = 1 }); w.set(a, velocity{ x = 2 }); t.expects(passed(&mut speeds) == 1); } # What the observers of the tests saw. var observed_added: u64 = 0; var observed_speed: f32 = 0; def count_added(w: &mut world, e: entity): void { observed_added++; } @test def runs_observers_as_components_are_given_and_taken(t: &mut tester): void { observed_added = 0; observed_speed = 0; var w := world::new(); val counting := w.observe::(change::added, count_added); # Run as it's taken, the value still there to read. _ = w.observe::(change::removed, def(at, e) { if at.get::(e) is some(&mut v) { observed_speed += v.x; } }); val a := w.spawn_with(position{}, velocity{ x = 2 }); w.set(a, velocity{ x = 5 }); t.expects(observed_added == 1); _ = w.remove::(a); w.set(a, velocity{ x = 1 }); t.expects(observed_added == 2 && observed_speed == 5); _ = w.despawn(a); t.expects(observed_speed == 6); # What an observer changes is made once the change it saw is done. _ = w.observe::(change::added, def(at, e) { at.add::(e); }); val b := w.spawn_with(position{}); w.add::(b); t.expects(w.has::(b)); # Given within a pass, as it ends. w.each(def(e: entity, p: &position) { w.set(e, velocity{}); }); t.expects(observed_added == 3); # Taken out, it runs no more. t.expects(w.unobserve(counting) && !w.unobserve(counting)); _ = w.remove::(b); w.set(b, velocity{}); t.expects(observed_added == 3); } @test def keeps_parents_and_children(t: &mut tester): void { var w := world::new(); val root := w.spawn_with(position{}); val arm := w.spawn_with(position{}); val hand := w.spawn_with(position{}); val other := w.spawn(); w.set_parent(arm, root); w.set_parent(hand, arm); t.expects(w.parent_of(hand) is some(p) && p == arm); t.expects(w.children_of(root).length == 1 && w.children_of(root)[0] == arm); # Given another parent, it leaves the one it had; taken from it, it's of the # world. w.set_parent(hand, other); t.expects(w.children_of(arm).length == 0 && w.children_of(other).length == 1); t.expects(w.remove_parent(hand) && w.parent_of(hand) is none && w.children_of(other).length == 0); t.expects(!w.remove_parent(hand)); w.set_parent(hand, arm); # Despawned, those within it are too, and it leaves its parent. _ = w.despawn(root); t.expects(!w.alive(root) && !w.alive(arm) && !w.alive(hand) && w.alive(other)); val p := w.spawn(); val first := w.spawn(); val second := w.spawn(); w.set_parent(first, p); w.set_parent(second, p); _ = w.despawn(first); t.expects(w.children_of(p).length == 1 && w.children_of(p)[0] == second); # Commands spawn children; within a pass, a child is given as it ends. var later := w.commands(); val kid := later.spawn_with(position{ x = 3 }); later.set_parent(kid, p); w.apply(&mut later); t.expects(w.parent_of(kid) is some(q) && q == p && given(&mut w, kid, def(x: &position) => x.x == 3)); w.each(def(e: entity, x: &position) { w.set_parent(e, second); }); t.expects(w.parent_of(kid) is some(q) && q == second && w.children_of(p).length == 1); # Their `children` taken, they're of the world. _ = w.remove::(second); t.expects(w.parent_of(kid) is none); } # Spawns a child of each entity of an index a multiple of 10, and of a parity, # and spawns by commands of the world as well. def spawns_children(at: *mut world, parity: u32, later: *mut commands): void { val w := &mut through(at); w.each(def(e: entity, v: &velocity) { if e.index % 10 == 0 && e.index % 20 == parity * 10 { val made := w.spawn_with(position{ x = e.index as f32 }); w.set_parent(made, e); _ = through(later).spawn_with(health{ points = 1 }); } }); } # The commands of each of two threads. var even_commands: commands; var odd_commands: commands; def spawns_even(at: *mut world): void { spawns_children(at, 0, address(&mut even_commands)); } def spawns_odd(at: *mut world): void { spawns_children(at, 1, address(&mut odd_commands)); } @test def spawns_while_threads_share_the_world(t: &mut tester): void { var w := world::new(); for i in 0..1000 { val e := w.spawn_with(velocity{ x = 1 }); # Some freed, so threads take those numbers too. if i % 200 == 3 { _ = w.despawn(e); } } even_commands = w.commands(); odd_commands = w.commands(); w.begin_sharing(); val at := address(&mut w); var a := thread::spawn_with(spawns_even, at) else (_) { return; }; var b := thread::spawn_with(spawns_odd, at) else (_) { return; }; a.join(); b.join(); w.end_sharing(); w.apply(&mut even_commands); w.apply(&mut odd_commands); # A child of each of a hundred, and a hundred by commands. t.expects(w.query(with::()).count() == 100 && w.query(with::()).count() == 100); var wrong := 0u; w.each(def(e: entity, p: &position) { if !(w.parent_of(e) is some(above) && above.index as f32 == p.x && w.has::(above)) { wrong++; } }); t.expects(wrong == 0 && w.count == 995 + 200); even_commands.close(); odd_commands.close(); } @test def tracks_what_queries_first_ask_while_shared(t: &mut tester): void { var w := world::new(); val a := w.spawn_with(health{ points = 1 }); _ = w.spawn_with(health{ points = 2 }); var hurt := w.query(changed::()); w.begin_sharing(); t.expects(passed(&mut hurt) == 2); w.end_sharing(); t.expects(passed(&mut hurt) == 0); w.set(a, health{ points = 0 }); t.expects(passed(&mut hurt) == 1); } module elsewhere; pub struct position { pub var x: f32; }