add one more test in example

This commit is contained in:
Igor Anić 2023-12-31 15:21:57 +01:00
parent c00e95736e
commit 24137e1045
2 changed files with 37 additions and 1 deletions

View File

@ -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

View File

@ -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", .{});
}