diff --git a/example/exe/build.zig b/example/exe/build.zig index 6d39d8c..7cfe2c1 100644 --- a/example/exe/build.zig +++ b/example/exe/build.zig @@ -67,8 +67,13 @@ pub fn build(b: *std.Build) void { .root_source_file = .{ .path = "src/main.zig" }, .target = target, .optimize = optimize, + .link_libc = true, }); - + unit_tests.linkLibrary(b.dependency("zlib", .{ + .target = target, + .optimize = optimize, + }).artifact("z")); + unit_tests.addModule("zlib", zlib.module("zlib")); const run_unit_tests = b.addRunArtifact(unit_tests); // Similar to creating the run step earlier, this exposes a `test` step to diff --git a/example/exe/src/main.zig b/example/exe/src/main.zig index 6e37ae2..9f124d9 100644 --- a/example/exe/src/main.zig +++ b/example/exe/src/main.zig @@ -1,5 +1,6 @@ const std = @import("std"); const zlib = @import("zlib"); +const testing = std.testing; pub fn main() !void { var gpa = std.heap.GeneralPurposeAllocator(.{}){}; @@ -27,3 +28,33 @@ test "simple test" { try list.append(42); try std.testing.expectEqual(@as(i32, 42), list.pop()); } + +test "zlib compress/decompress" { + var gpa = std.heap.GeneralPurposeAllocator(.{}){}; + defer _ = gpa.deinit(); + const allocator = gpa.allocator(); + //const allocator = testing.allocator; + const input = "Hello"; + + var cmp = try zlib.Compressor.init(allocator, .{ .header = .none }); + defer cmp.deinit(); + + const compressed = try cmp.compressAllAlloc(input); + defer allocator.free(compressed); + showBuf(compressed); + + var dcmp = try zlib.Decompressor.init(allocator, .{ .header = .none }); + defer dcmp.deinit(); + + const decompressed = try dcmp.decompressAllAlloc(compressed); + defer allocator.free(decompressed); + try testing.expectEqualSlices(u8, input, decompressed); +} + +// debug helper +fn showBuf(buf: []const u8) void { + std.debug.print("\n", .{}); + for (buf) |b| + std.debug.print("0x{x:0>2}, ", .{b}); + std.debug.print("\n", .{}); +}