From be066342322fdf10e55da8edd1f2f8f0d7fd0148 Mon Sep 17 00:00:00 2001 From: Marto Date: Wed, 29 Apr 2026 10:17:48 +0200 Subject: [PATCH] Playing around with testing (unsuccessfully) --- src/errors.zig | 3 +++ src/infrastructure/node.zig | 3 ++- src/infrastructure/road.zig | 27 ++++++++++++++++++++++++++- src/main.zig | 3 --- 4 files changed, 31 insertions(+), 5 deletions(-) create mode 100644 src/errors.zig diff --git a/src/errors.zig b/src/errors.zig new file mode 100644 index 0000000..5e5d8f6 --- /dev/null +++ b/src/errors.zig @@ -0,0 +1,3 @@ +pub const Entity = error { + AlreadyReferenced, +}; \ No newline at end of file diff --git a/src/infrastructure/node.zig b/src/infrastructure/node.zig index 2e9d342..6901232 100644 --- a/src/infrastructure/node.zig +++ b/src/infrastructure/node.zig @@ -2,6 +2,7 @@ const std = @import("std"); const rl = @import("raylib"); const c = @import("../constants.zig"); +const e = @import("../errors.zig"); const Road = @import("road.zig").Road; pub const Node = struct { @@ -34,7 +35,7 @@ pub const Node = struct { pub fn referenceRoad(self: *Node, allocator: std.mem.Allocator, road_to_add: *Road) !void { // Note the road_to_add pointer must be one from the roads list as otherwise the pointer is dangling one for (self.roads.items) |road| { - if (road.*.id == road_to_add.*.id) return error.RoadAlreadyReferenced; + if (road == road_to_add) return e.Entity.AlreadyReferenced; } try self.roads.append(allocator, road_to_add); diff --git a/src/infrastructure/road.zig b/src/infrastructure/road.zig index cf60f60..065820b 100644 --- a/src/infrastructure/road.zig +++ b/src/infrastructure/road.zig @@ -1,4 +1,6 @@ +const std = @import("std"); const rl = @import("raylib"); +const expect = std.testing.expect; const c = @import("../constants.zig"); const Node = @import("node.zig").Node; @@ -37,4 +39,27 @@ pub const Road = struct { pub fn draw(self: *const Road) void { rl.drawLineEx(self.nodes[0].*.pos, self.nodes[1].*.pos, c.ROAD_SIZE, c.ROAD_COLOUR); } -}; \ No newline at end of file +}; + +test "valid road nodes" { + var gpa: std.heap.DebugAllocator(.{}) = .init; + defer _ = gpa.deinit(); + const allocator = gpa.allocator(); + + const start: Node = .init(34, .{500, 500}); + const start_ptr = try allocator.create(Node); + start_ptr.* = start; + + const end: Node = .init(227, .{600, 500}); + const end_ptr = try allocator.create(Node); + end_ptr.* = end; + + const road: Road = .init(11, start_ptr, end_ptr); + + try expect(road.nodes[0].id == 34 and road.nodes[1].id == 227); +} + +test "false" { + const truth = false; + try expect(truth); +} \ No newline at end of file diff --git a/src/main.zig b/src/main.zig index 9f7210b..49cd6aa 100644 --- a/src/main.zig +++ b/src/main.zig @@ -5,9 +5,6 @@ const c = @import("constants.zig"); const Simulator = @import("simulator.zig").Simulator; // TODO PLANS -// Implement that each road, node in managers will be stored as pointers (allocated in heap) -// So when appending causes the list to relocate, nothing will have to change -// since the allocated nodes/roads will remain where they were // Additionally add more robust error and test handling pub fn main(init: std.process.Init) !void {