Playing around with testing (unsuccessfully)
This commit is contained in:
3
src/errors.zig
Normal file
3
src/errors.zig
Normal file
@@ -0,0 +1,3 @@
|
||||
pub const Entity = error {
|
||||
AlreadyReferenced,
|
||||
};
|
||||
@@ -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);
|
||||
|
||||
@@ -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;
|
||||
@@ -38,3 +40,26 @@ pub const Road = struct {
|
||||
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);
|
||||
}
|
||||
@@ -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 {
|
||||
|
||||
Reference in New Issue
Block a user