Playing around with testing (unsuccessfully)

This commit is contained in:
2026-04-29 10:17:48 +02:00
parent fc67fe3d7e
commit be06634232
4 changed files with 31 additions and 5 deletions

3
src/errors.zig Normal file
View File

@@ -0,0 +1,3 @@
pub const Entity = error {
AlreadyReferenced,
};

View File

@@ -2,6 +2,7 @@ const std = @import("std");
const rl = @import("raylib"); const rl = @import("raylib");
const c = @import("../constants.zig"); const c = @import("../constants.zig");
const e = @import("../errors.zig");
const Road = @import("road.zig").Road; const Road = @import("road.zig").Road;
pub const Node = struct { 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 { 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 // 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| { 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); try self.roads.append(allocator, road_to_add);

View File

@@ -1,4 +1,6 @@
const std = @import("std");
const rl = @import("raylib"); const rl = @import("raylib");
const expect = std.testing.expect;
const c = @import("../constants.zig"); const c = @import("../constants.zig");
const Node = @import("node.zig").Node; const Node = @import("node.zig").Node;
@@ -38,3 +40,26 @@ pub const Road = struct {
rl.drawLineEx(self.nodes[0].*.pos, self.nodes[1].*.pos, c.ROAD_SIZE, c.ROAD_COLOUR); rl.drawLineEx(self.nodes[0].*.pos, self.nodes[1].*.pos, c.ROAD_SIZE, c.ROAD_COLOUR);
} }
}; };
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);
}

View File

@@ -5,9 +5,6 @@ const c = @import("constants.zig");
const Simulator = @import("simulator.zig").Simulator; const Simulator = @import("simulator.zig").Simulator;
// TODO PLANS // 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 // Additionally add more robust error and test handling
pub fn main(init: std.process.Init) !void { pub fn main(init: std.process.Init) !void {