Implemented a few basic tests and ideas for future ones
This commit is contained in:
@@ -5,7 +5,6 @@ pub const WIDTH = 1366;
|
||||
/// Screen Height
|
||||
pub const HEIGHT = 768;
|
||||
pub const BACKGROUND_COLOR = clr.light_gray;
|
||||
pub const ALLOC_SIZE = 50;
|
||||
|
||||
/// Base node radius
|
||||
pub const NODE_RADIUS = 20;
|
||||
|
||||
@@ -40,4 +40,10 @@ pub const Node = struct {
|
||||
|
||||
try self.roads.append(allocator, road_to_add);
|
||||
}
|
||||
};
|
||||
};
|
||||
|
||||
// TODO tests
|
||||
// road reference test
|
||||
// pos within node test
|
||||
// deinit test
|
||||
// roads ptr list test
|
||||
@@ -73,4 +73,32 @@ pub const NodeManager = struct {
|
||||
self.nodes.clearRetainingCapacity();
|
||||
self.next_id = 0;
|
||||
}
|
||||
};
|
||||
};
|
||||
|
||||
const expect = std.testing.expect;
|
||||
|
||||
test "id tracking" {
|
||||
var gpa: std.heap.DebugAllocator(.{}) = .init;
|
||||
defer _ = gpa.deinit();
|
||||
const allocator = gpa.allocator();
|
||||
|
||||
var node_man: NodeManager = .init();
|
||||
defer node_man.deinit(allocator);
|
||||
const n = 5;
|
||||
|
||||
for (0..n) |_| {
|
||||
const node: Node = .init(node_man.getNextID(), Vector2 {
|
||||
.x = 500,
|
||||
.y = 500,
|
||||
});
|
||||
const node_ptr = try allocator.create(Node);
|
||||
node_ptr.* = node;
|
||||
try node_man.nodes.append(allocator, node_ptr);
|
||||
}
|
||||
|
||||
try expect(node_man.next_id == n);
|
||||
try expect(node_man.nodes.items.len == n);
|
||||
}
|
||||
|
||||
// TODO tests
|
||||
// force resize pointer test
|
||||
@@ -1,6 +1,4 @@
|
||||
const std = @import("std");
|
||||
const rl = @import("raylib");
|
||||
const expect = std.testing.expect;
|
||||
|
||||
const c = @import("../constants.zig");
|
||||
const Node = @import("node.zig").Node;
|
||||
@@ -41,6 +39,9 @@ pub const Road = struct {
|
||||
}
|
||||
};
|
||||
|
||||
const std = @import("std");
|
||||
const expect = std.testing.expect;
|
||||
|
||||
test "valid road nodes" {
|
||||
var gpa: std.heap.DebugAllocator(.{}) = .init;
|
||||
defer _ = gpa.deinit();
|
||||
@@ -64,5 +65,6 @@ test "valid road nodes" {
|
||||
|
||||
const road: Road = .init(11, start_ptr, end_ptr);
|
||||
|
||||
try expect(road.nodes[0].id == 34 and road.nodes[1].id == 227);
|
||||
try expect(road.nodes[0].id == 34);
|
||||
try expect(road.nodes[1].id == 227);
|
||||
}
|
||||
@@ -51,4 +51,44 @@ pub const RoadManager = struct {
|
||||
self.roads.clearRetainingCapacity();
|
||||
self.next_id = 0;
|
||||
}
|
||||
};
|
||||
};
|
||||
|
||||
const Vector2 = @import("raylib").Vector2;
|
||||
const expect = std.testing.expect;
|
||||
|
||||
test "id tracking" {
|
||||
var gpa: std.heap.DebugAllocator(.{}) = .init;
|
||||
defer _ = gpa.deinit();
|
||||
const allocator = gpa.allocator();
|
||||
|
||||
var road_man: RoadManager = .init();
|
||||
defer road_man.deinit(allocator);
|
||||
const n = 5;
|
||||
|
||||
const start: Node = .init(0, .{.x = 0, .y = 0});
|
||||
const start_ptr = try allocator.create(Node);
|
||||
start_ptr.* = start;
|
||||
|
||||
const end: Node = .init(1, .{.x = 100, .y = 100});
|
||||
const end_ptr = try allocator.create(Node);
|
||||
end_ptr.* = end;
|
||||
|
||||
defer {
|
||||
start_ptr.deinit(allocator);
|
||||
end_ptr.deinit(allocator);
|
||||
|
||||
allocator.destroy(start_ptr);
|
||||
allocator.destroy(end_ptr);
|
||||
}
|
||||
|
||||
for (0..n) |_| {
|
||||
try road_man.addRoad(allocator, start_ptr, end_ptr);
|
||||
}
|
||||
|
||||
try expect(road_man.next_id == n);
|
||||
try expect(road_man.roads.items.len == n);
|
||||
}
|
||||
|
||||
// TODO tests
|
||||
// force resize pointer test
|
||||
// destroy road and then verify the nodes do not have a pointer to it
|
||||
@@ -1,3 +1,5 @@
|
||||
test {
|
||||
_ = @import("infrastructure/road.zig");
|
||||
_ = @import("infrastructure/node_manager.zig");
|
||||
_ = @import("infrastructure/road_manager.zig");
|
||||
}
|
||||
Reference in New Issue
Block a user