diff options
author | Christian Segundo | 2022-12-03 19:28:32 +0100 |
---|---|---|
committer | Christian Segundo | 2022-12-03 19:28:32 +0100 |
commit | ee6c6e975dabc6c14f7e8e62e2de1551a431cc7a (patch) | |
tree | 834829061b796f3e6168ec1d6b7e1e8ff4469934 /main.zig | |
parent | 78fa87cd43cd18f2f9ec4a04c45bcb8036143fd8 (diff) | |
download | advent-of-zig-2022-ee6c6e975dabc6c14f7e8e62e2de1551a431cc7a.tar.gz |
refactor to use a meta test
Diffstat (limited to 'main.zig')
-rw-r--r-- | main.zig | 70 |
1 files changed, 70 insertions, 0 deletions
diff --git a/main.zig b/main.zig new file mode 100644 index 0000000..276e611 --- /dev/null +++ b/main.zig @@ -0,0 +1,70 @@ +const std = @import("std"); + +const t = [_]struct { + day: type, + expect: [2]i32, +}{ + .{ .day = @import("day-1/main.zig"), .expect = [_]i32{ 69912, 208180 } }, + .{ .day = @import("day-2/main.zig"), .expect = [_]i32{ 12855, 13726 } }, + .{ .day = @import("day-3/main.zig"), .expect = [_]i32{ 8039, 2510 } }, +}; + +test { + std.testing.refAllDecls(@This()); +} + +const allocator = std.testing.allocator; +const slurp = @import("util/file.zig").slurp; + +// just here to inline it, see: https://github.com/ziglang/zig/issues/12635 +inline fn comptimePrint(comptime fmt: []const u8, args: anytype) *const [std.fmt.count(fmt, args):0]u8 { + comptime { + var buf: [std.fmt.count(fmt, args):0]u8 = undefined; + _ = std.fmt.bufPrint(&buf, fmt, args) catch unreachable; + buf[buf.len] = 0; + return &buf; + } +} + +test "test" { + std.debug.print("\n", .{}); + + inline for (t) |aoc_day, day| { + std.debug.print("\n", .{}); + const file_buffer = try slurp(std.testing.allocator, comptimePrint("./day-{d}/input", .{day + 1})); + defer allocator.free(file_buffer); + + inline for (aoc_day.expect) |expected, idx| { + const fn_name = comptimePrint("puzzle_{d}", .{idx + 1}); + + const puzzle = @field(aoc_day.day, fn_name); + const result = blk: { + switch (@typeInfo(@TypeOf(puzzle))) { + .Fn => |f| { + switch (@typeInfo(f.return_type.?)) { + .ErrorUnion => { + if (f.args.len == 1) + break :blk try puzzle(file_buffer); + break :blk try puzzle(allocator, file_buffer); + }, + else => { + if (f.args.len == 1) + break :blk puzzle(file_buffer); + break :blk puzzle(allocator, file_buffer); + }, + } + }, + else => unreachable, + } + }; + + if (result == expected) { + std.debug.print("day={d}, puzzle_{d}={d}\n", .{ day + 1, idx + 1, result }); + } else { + std.debug.print("day={d}, puzzle_{d}={d}, expected={d}\n", .{ day + 1, idx + 1, result, expected }); + } + } + } + + std.debug.print("\n", .{}); +} |