const std = @import("std");
const PF = @import("pathfinder.zig");
fn manhattan(a: [2]usize, b: [2]usize) !usize {
const absx = try std.math.absInt(@intCast(isize, a[0]) - @intCast(isize, b[0]));
const absy = try std.math.absInt(@intCast(isize, a[1]) - @intCast(isize, b[1]));
return @intCast(usize, absx + absy);
}
fn euclidean(a: [2]usize, b: [2]usize) !f32 {
const powx = try std.math.powi(isize, @intCast(isize, a[0]) - @intCast(isize, b[0]), 2);
const powy = try std.math.powi(isize, @intCast(isize, a[1]) - @intCast(isize, b[1]), 2);
return std.math.sqrt(@intToFloat(f32, powx + powy));
}
test "Bench Grid.dijkstra()" {
var grid = PF.Grid(usize, 100, 100).init(std.testing.allocator);
defer grid.deinit();
try grid.parseGridFromCSV("grid-test.txt", ",");
var t = try std.time.Timer.start();
_ = try grid.dijkstra(.{ 0, 0 }, .{ 99, 99 }, PF.D2.all);
const time = t.read();
std.debug.print("\nGrid.dijkstra():\t{d}ms\n", .{time / std.time.ns_per_ms});
}
test "Bench Grid.astar()" {
var grid = PF.Grid(usize, 100, 100).init(std.testing.allocator);
defer grid.deinit();
try grid.parseGridFromCSV("grid-test.txt", ",");
var t = try std.time.Timer.start();
const res = try grid.astar(.{ 0, 0 }, .{ 99, 99 }, PF.D2.all, manhattan);
const time = t.read();
try std.testing.expectEqual(@as(usize, 181), res);
std.debug.print("\nGrid.astar():\t{d}ms\n", .{time / std.time.ns_per_ms});
}
test "Bench Grid.astar()" {
var grid = PF.Grid(f32, 100, 100).init(std.testing.allocator);
defer grid.deinit();
try grid.parseGridFromCSV("grid-test.txt", ",");
var t = try std.time.Timer.start();
const res = try grid.astar(.{ 0, 0 }, .{ 99, 99 }, PF.D2.all, euclidean);
const time = t.read();
try std.testing.expectEqual(@as(f32, 181), res);
std.debug.print("\nGrid.astar():\t{d}ms\n", .{time / std.time.ns_per_ms});
}