Hi, so I’m currently working on a project, and the backbone of that project is an implementation of Astar. currently I’m just poking around and I’m trying to be rigorous with my testing because I hope to make a generic implementation. Anyway as I’m coding I’m using the testing framework of zig, and I’m also trying to use the new ‘–fuzz’ everything works great so far so good. But I can’t help but feel a bit confused about one error that I’m getting, either I’ve found a bug (I don’t think that’s the case), or I’m not understanding the way the fuzzer works, or I’m not understanding something in the type system. Either way I would greatly appreciate any help.
So the Test in question is this little fella :
fn fakeScores(start: []const u8, end: []const u8) f32 {
var score: f32 = 0;
const len = @min(start.len, end.len);
if (len == 0) return (score);
for (start[0..len], end[0..len]) |s, e| {
score += @floatFromInt((s -| e));
}
return (score);
}
test "compare(self: *const Node, other: *const Node) Order" {
const start = testing.fuzzInput(.{});
const end = testing.fuzzInput(.{});
var node_a = AstarNode([]const u8).init(start);
node_a.h_score = fakeScores(node_a.item, end);
node_a.update();
var node_b = AstarNode([]const u8).init(start);
node_b.h_score = fakeScores(node_b.item, end);
node_b.update();
try testing.expectApproxEqRel(fakeScores(start, end), node_a.f_score, 0.0001);
try testing.expect(node_a.compare(&node_b) == Order.eq);
}
When running the following zig build test --fuzz --port 12345
I’m getting this error message stating that I’ve crashed with a segmentation fault.
info: web interface listening at http://127.0.0.1:44785/
test
└─ run test failure
Segmentation fault at address 0x7e2cda45b000
/home/pollivie/workspace/personnal-playground/a-star/src/astar.zig:71:28: 0x1155da1 in fakeScores (test)
for (start[0..len], end[0..len]) |s, e| {
^
/home/pollivie/workspace/personnal-playground/a-star/src/astar.zig:81:30: 0x115586e in test.compare(self: *const Node, other: *const Node) Order (test)
node.h_score = fakeScores(node.item, end);
^
/home/pollivie/zig/0.14.0-dev.1304+7d54c62c8/files/lib/compiler/test_runner.zig:155:33: 0x107f2b5 in mainServer (test)
test_fn.func() catch |err| switch (err) {
^
/home/pollivie/zig/0.14.0-dev.1304+7d54c62c8/files/lib/compiler/test_runner.zig:55:26: 0x107dfbc in main (test)
return mainServer() catch @panic("internal test runner failure");
^
/home/pollivie/zig/0.14.0-dev.1304+7d54c62c8/files/lib/std/start.zig:605:22: 0x107d643 in posixCallMainAndExit (test)
root.main();
^
/home/pollivie/zig/0.14.0-dev.1304+7d54c62c8/files/lib/std/start.zig:250:5: 0x107d26f in _start (test)
asm volatile (switch (native_arch) {
^
???:?:?: 0x3 in ??? (???)
Unwind information for `???:0x3` was not available, trace may be incomplete
The reason I’m a bit confused is because I can’t really see what I’m doing wrong, as I’m not sure what is causing it, obviously the error message is quite detailed and would be very helpful in other circumstances, but I don’t see why my for loop is leading to a segmentation fault. So I’m probably missing something and would greatly appreciate if anyone could point me in the right direction.