From 59fbf04da6fa68a0b5b7a163cc1d38001ea4db09 Mon Sep 17 00:00:00 2001 From: Emil Lerch Date: Mon, 3 Apr 2023 15:12:31 -0700 Subject: [PATCH] add tests --- build.zig | 6 +++++- src/main.zig | 37 ++++++++++++++++++++++++++++++++++--- 2 files changed, 39 insertions(+), 4 deletions(-) diff --git a/build.zig b/build.zig index c454d20..c9a75aa 100644 --- a/build.zig +++ b/build.zig @@ -66,9 +66,13 @@ pub fn build(b: *std.build.Builder) !void { .target = target, .optimize = optimize, }); + exe_tests.linkLibrary(im_dep.artifact("MagickWand")); + exe_tests.linkLibrary(z_dep.artifact("z")); + exe_tests.linkLibrary(i2cdriver); + exe_tests.addIncludePath("lib/i2cdriver"); const test_step = b.step("test", "Run unit tests"); - test_step.dependOn(&exe_tests.step); + test_step.dependOn(&exe_tests.run().step); } // Should be able to remove this diff --git a/src/main.zig b/src/main.zig index e1be6b4..9314879 100644 --- a/src/main.zig +++ b/src/main.zig @@ -52,7 +52,7 @@ pub fn main() !void { defer alloc.destroy(opts); if (opts.background_filename.len > 0) try stdout.print("Converting {s}\n", .{opts.background_filename}); var pixels: [WIDTH * HEIGHT]u8 = undefined; - try convertImage(alloc, opts.background_filename, &pixels, textForLine); + try convertImage(opts.background_filename, &pixels, textForLine); try bw.flush(); // We should take the linux device file here, then inspect for ttyUSB vs @@ -276,8 +276,7 @@ fn reportMagickError(mw: ?*c.MagickWand) !void { fn textForLine(line: usize) []u8 { return lines[line].*; } -fn convertImage(alloc: std.mem.Allocator, filename: [:0]u8, pixels: *[WIDTH * HEIGHT]u8, text_fn: *const fn (usize) []u8) !void { - _ = alloc; +fn convertImage(filename: [:0]u8, pixels: *[WIDTH * HEIGHT]u8, text_fn: *const fn (usize) []u8) !void { c.MagickWandGenesis(); defer c.MagickWandTerminus(); var mw = c.NewMagickWand(); @@ -506,6 +505,38 @@ fn logo() !void { c.MagickWandTerminus(); } +test "gets correct bytes" { + const bg_file: [:0]u8 = @constCast("logo:"); + const opts = .{ .background_filename = bg_file, .device_file = "-" }; + const empty: [:0]u8 = @constCast(""); + for (&lines) |*line| { + line.* = ∅ + } + const line: [:0]u8 = @constCast("Hello\\!"); + lines[5] = &line; + var pixels: [WIDTH * HEIGHT]u8 = undefined; + + var expected_pixels: *const [WIDTH * HEIGHT]u8 = @embedFile("testExpectedBytes.bin"); + + // [_]u8{..,..,..} + try convertImage(opts.background_filename, &pixels, textForLine); + // try writeBytesToFile("testExpectedBytes.bin", &pixels); + try std.testing.expectEqualSlices(u8, expected_pixels, &pixels); +} +fn writeBytesToFile(filename: []const u8, bytes: []u8) !void { + const file = try std.fs.cwd().createFile(filename, .{ + .read = false, + .truncate = true, + .lock = .Exclusive, + .lock_nonblocking = false, + .mode = 0o666, + .intended_io_mode = .blocking, + }); + defer file.close(); + const writer = file.writer(); + try writer.writeAll(bytes); + // try writer.print("pub const chars = &[_][]const u8{{\n", .{}); +} test "simple test" { var list = std.ArrayList(i32).init(std.testing.allocator); defer list.deinit(); // try commenting this out and see if zig detects the memory leak!