From e76971101b3968d8f3a803e8c481b0093a46ec6a Mon Sep 17 00:00:00 2001 From: Mike Date: Sun, 14 Jun 2020 14:53:48 -0700 Subject: [PATCH] fixes and tests --- zig-ecs/src/ecs/registry.zig | 2 +- zig-ecs/src/ecs/views.zig | 10 ++++----- zig-ecs/tests/groups_test.zig | 42 +++++++++++++++++++++++++++++++++++ 3 files changed, 48 insertions(+), 6 deletions(-) diff --git a/zig-ecs/src/ecs/registry.zig b/zig-ecs/src/ecs/registry.zig index a6c8e0d..58ed069 100644 --- a/zig-ecs/src/ecs/registry.zig +++ b/zig-ecs/src/ecs/registry.zig @@ -231,7 +231,7 @@ pub const Registry = struct { /// Returns the number of existing components of the given type pub fn len(self: *Registry, comptime T: type) usize { - self.assure(T).len(); + return self.assure(T).len(); } /// Increases the capacity of the registry or of the pools for the given component diff --git a/zig-ecs/src/ecs/views.zig b/zig-ecs/src/ecs/views.zig index 65506f4..44f12fa 100644 --- a/zig-ecs/src/ecs/views.zig +++ b/zig-ecs/src/ecs/views.zig @@ -43,12 +43,12 @@ pub fn BasicView(comptime T: type) type { return self.storage.getConst(entity); } - pub fn entityIterator(self: Self) utils.ReverseSliceIterator(Entity) { - return self.storage.set.reverseIterator(); + pub fn iterator(self: Self) utils.ReverseSliceIterator(T) { + return utils.ReverseSliceIterator(T).init(self.storage.instances.items); } - pub fn componentIterator(self: Self) utils.ReverseSliceIterator(T) { - return utils.ReverseSliceIterator(T).init(self.storage.instances.items); + pub fn entityIterator(self: Self) utils.ReverseSliceIterator(Entity) { + return self.storage.set.reverseIterator(); } }; } @@ -165,7 +165,7 @@ test "single basic view" { std.testing.expectEqual(view.len(), 2); var i: usize = 0; - var iter = view.componentIterator(); + var iter = view.iterator(); while (iter.next()) |comp| { if (i == 0) std.testing.expectEqual(comp, 50); if (i == 1) std.testing.expectEqual(comp, 30); diff --git a/zig-ecs/tests/groups_test.zig b/zig-ecs/tests/groups_test.zig index 39b3b4a..94c93f6 100644 --- a/zig-ecs/tests/groups_test.zig +++ b/zig-ecs/tests/groups_test.zig @@ -145,6 +145,48 @@ test "sort OwningGroup by Component" { } } +test "sort OwningGroup by Component ensure unsorted non-matches" { + var reg = Registry.init(std.testing.allocator); + defer reg.deinit(); + + var group = reg.group(.{ Sprite, Renderable }, .{}, .{}); + + var i: usize = 0; + while (i < 5) : (i += 1) { + var e = reg.create(); + reg.add(e, Sprite{ .x = @intToFloat(f32, i) }); + reg.add(e, Renderable{ .x = @intToFloat(f32, i) }); + + var e2 = reg.create(); + reg.add(e2, Sprite{ .x = @intToFloat(f32, i + 1 * 50) }); + } + + std.testing.expectEqual(group.len(), 5); + std.testing.expectEqual(reg.len(Sprite), 10); + + const SortContext = struct { + fn sort(this: void, a: Sprite, b: Sprite) bool { + // sprites with x > 50 shouldnt match in the group + std.testing.expect(a.x < 50 and b.x < 50); + return a.x > b.x; + } + }; + group.sort(Sprite, {}, SortContext.sort); + + // all the + var view = reg.view(.{Sprite}, .{}); + var count: usize = 0; + var iter = view.iterator(); + while (iter.next()) |sprite| { + count += 1; + + // all sprite.x > 50 should be at the end and we iterate backwards + if (count < 6) { + std.testing.expect(sprite.x >= 50); + } + } +} + test "nested OwningGroups add/remove components" { var reg = Registry.init(std.testing.allocator); defer reg.deinit();