convert tests to use testing allocator

main
cosmonaut 2020-12-05 14:28:19 -08:00
parent 01da174126
commit a7727bf750
4 changed files with 28 additions and 51 deletions

View File

@ -23,6 +23,7 @@ pub const ComponentManager = struct {
pub fn deinit(self: *Self) void { pub fn deinit(self: *Self) void {
var iter = self.component_stores.map.iterator(); var iter = self.component_stores.map.iterator();
while (iter.next()) |ptr| { while (iter.next()) |ptr| {
// HACK: we dont know the Type here but we need to call deinit
var storage = @intToPtr(*ComponentStore(u1), ptr.value); var storage = @intToPtr(*ComponentStore(u1), ptr.value);
storage.deinit(); storage.deinit();
self.allocator.destroy(storage); self.allocator.destroy(storage);
@ -87,12 +88,8 @@ test "read components" {
.number = 14 .number = 14
}; };
var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator); var component_manager = ComponentManager.init(std.testing.allocator);
defer arena.deinit(); defer component_manager.deinit();
const allocator = &arena.allocator;
var component_manager = ComponentManager.init(allocator);
component_manager.register(TestComponent); component_manager.register(TestComponent);
@ -120,12 +117,8 @@ test "read entities" {
.number = 14 .number = 14
}; };
var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator); var component_manager = ComponentManager.init(std.testing.allocator);
defer arena.deinit(); defer component_manager.deinit();
const allocator = &arena.allocator;
var component_manager = ComponentManager.init(allocator);
component_manager.register(TestComponent); component_manager.register(TestComponent);

View File

@ -133,12 +133,9 @@ test "iterate items" {
.number = 15 .number = 15
}; };
var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator); var component_store = ComponentStore(TestComponent).init(std.testing.allocator);
defer arena.deinit(); defer component_store.deinit();
const allocator = &arena.allocator;
var component_store = ComponentStore(TestComponent).init(allocator);
component_store.set(3, component); component_store.set(3, component);
component_store.set(5, componentTwo); component_store.set(5, componentTwo);
component_store.set(8, componentThree); component_store.set(8, componentThree);
@ -155,12 +152,9 @@ test "has item" {
.number = 3 .number = 3
}; };
var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator); var component_store = ComponentStore(TestComponent).init(std.testing.allocator);
defer arena.deinit(); defer component_store.deinit();
const allocator = &arena.allocator;
var component_store = ComponentStore(TestComponent).init(allocator);
component_store.set(5, component); component_store.set(5, component);
testing.expect(component_store.has(5)); testing.expect(component_store.has(5));
@ -180,12 +174,9 @@ test "remove item" {
.number = 124 .number = 124
}; };
var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator); var component_store = ComponentStore(TestComponent).init(std.testing.allocator);
defer arena.deinit(); defer component_store.deinit();
const allocator = &arena.allocator;
var component_store = ComponentStore(TestComponent).init(allocator);
component_store.set(5, component); component_store.set(5, component);
component_store.set(10, componentTwo); component_store.set(10, componentTwo);
component_store.set(15, componentThree); component_store.set(15, componentThree);

View File

@ -21,6 +21,11 @@ pub const EntityManager = struct {
}; };
} }
pub fn deinit(self: *Self) void {
self.idManager.deinit();
self.ids.deinit();
}
pub fn create(self: *Self) !Entity { pub fn create(self: *Self) !Entity {
const id = self.idManager.next(); const id = self.idManager.next();
var entity = Entity { .ID = id }; var entity = Entity { .ID = id };
@ -34,12 +39,8 @@ pub const EntityManager = struct {
}; };
test "create" { test "create" {
var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator); var entity_manager = EntityManager.init(std.testing.allocator);
defer arena.deinit(); defer entity_manager.deinit();
const allocator = &arena.allocator;
var entity_manager = EntityManager.init(allocator);
var entity = try entity_manager.create(); var entity = try entity_manager.create();
@ -47,12 +48,8 @@ test "create" {
} }
test "exists" { test "exists" {
var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator); var entity_manager = EntityManager.init(std.testing.allocator);
defer arena.deinit(); defer entity_manager.deinit();
const allocator = &arena.allocator;
var entity_manager = EntityManager.init(allocator);
var entity = try entity_manager.create(); var entity = try entity_manager.create();

View File

@ -17,6 +17,10 @@ pub const IDManager = struct {
}; };
} }
pub fn deinit(self: *Self) void {
self.availableIDs.deinit();
}
pub fn next(self: *Self) usize { pub fn next(self: *Self) usize {
if (self.availableIDs.items.len > 0) if (self.availableIDs.items.len > 0)
{ {
@ -36,24 +40,16 @@ pub const IDManager = struct {
}; };
test "next" { test "next" {
var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator); var id_manager = IDManager.init(std.testing.allocator);
defer arena.deinit(); defer id_manager.deinit();
const allocator = &arena.allocator;
var id_manager = IDManager.init(allocator);
testing.expect(id_manager.next() == 0); testing.expect(id_manager.next() == 0);
testing.expect(id_manager.next() == 1); testing.expect(id_manager.next() == 1);
} }
test "free" { test "free" {
var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator); var id_manager = IDManager.init(std.testing.allocator);
defer arena.deinit(); defer id_manager.deinit();
const allocator = &arena.allocator;
var id_manager = IDManager.init(allocator);
var id_zero = id_manager.next(); var id_zero = id_manager.next();
var id_one = id_manager.next(); var id_one = id_manager.next();