From 5f571f53d63e8742374c3c3cdda1ca0bb432d8fc Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Mon, 28 Jul 2025 16:53:01 -0700 Subject: [PATCH] refactor gzip test cases zig newbies love using for loops in unit tests --- lib/std/compress/flate/Decompress.zig | 104 ++++++++++++-------------- 1 file changed, 47 insertions(+), 57 deletions(-) diff --git a/lib/std/compress/flate/Decompress.zig b/lib/std/compress/flate/Decompress.zig index 26a8a5a1ed..a1cf438f8e 100644 --- a/lib/std/compress/flate/Decompress.zig +++ b/lib/std/compress/flate/Decompress.zig @@ -864,7 +864,7 @@ test "dynamic block (type 2)" { fn testBasicCase(in: []const u8, out: []const u8) !void { var reader: Reader = .fixed(in); var aw: Writer.Allocating = .init(testing.allocator); - try aw.ensureUnusedCapacity(flate.history_len + 1); + try aw.ensureUnusedCapacity(flate.history_len); defer aw.deinit(); var decompress: Decompress = .init(&reader, .raw, &.{}); @@ -873,63 +873,53 @@ fn testBasicCase(in: []const u8, out: []const u8) !void { try testing.expectEqualStrings(out, aw.getWritten()); } -test "gzip decompress" { - const cases = [_]struct { - in: []const u8, - out: []const u8, - }{ - // non compressed block (type 0) - .{ - .in = &[_]u8{ - 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, // gzip header (10 bytes) - 0b0000_0001, 0b0000_1100, 0x00, 0b1111_0011, 0xff, // deflate fixed buffer header len, nlen - 'H', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd', 0x0a, // non compressed data - 0xd5, 0xe0, 0x39, 0xb7, // gzip footer: checksum - 0x0c, 0x00, 0x00, 0x00, // gzip footer: size - }, - .out = "Hello world\n", - }, - // fixed code block (type 1) - .{ - .in = &[_]u8{ - 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x03, // gzip header (10 bytes) - 0xf3, 0x48, 0xcd, 0xc9, 0xc9, 0x57, 0x28, 0xcf, // deflate data block type 1 - 0x2f, 0xca, 0x49, 0xe1, 0x02, 0x00, - 0xd5, 0xe0, 0x39, 0xb7, 0x0c, 0x00, 0x00, 0x00, // gzip footer (chksum, len) - }, - .out = "Hello world\n", - }, - // dynamic block (type 2) - .{ - .in = &[_]u8{ - 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, // gzip header (10 bytes) - 0x3d, 0xc6, 0x39, 0x11, 0x00, 0x00, 0x0c, 0x02, // deflate data block type 2 - 0x30, 0x2b, 0xb5, 0x52, 0x1e, 0xff, 0x96, 0x38, - 0x16, 0x96, 0x5c, 0x1e, 0x94, 0xcb, 0x6d, 0x01, - 0x17, 0x1c, 0x39, 0xb4, 0x13, 0x00, 0x00, 0x00, // gzip footer (chksum, len) - }, - .out = "ABCDEABCD ABCDEABCD", - }, - // gzip header with name - .{ - .in = &[_]u8{ - 0x1f, 0x8b, 0x08, 0x08, 0xe5, 0x70, 0xb1, 0x65, 0x00, 0x03, 0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x2e, - 0x74, 0x78, 0x74, 0x00, 0xf3, 0x48, 0xcd, 0xc9, 0xc9, 0x57, 0x28, 0xcf, 0x2f, 0xca, 0x49, 0xe1, - 0x02, 0x00, 0xd5, 0xe0, 0x39, 0xb7, 0x0c, 0x00, 0x00, 0x00, - }, - .out = "Hello world\n", - }, - }; - for (cases) |c| { - var fb: Reader = .fixed(c.in); - var aw: Writer.Allocating = .init(testing.allocator); - defer aw.deinit(); +test "gzip non compressed block (type 0)" { + try testGzipDecompress(&[_]u8{ + 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, // gzip header (10 bytes) + 0b0000_0001, 0b0000_1100, 0x00, 0b1111_0011, 0xff, // deflate fixed buffer header len, nlen + 'H', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd', 0x0a, // non compressed data + 0xd5, 0xe0, 0x39, 0xb7, // gzip footer: checksum + 0x0c, 0x00, 0x00, 0x00, // gzip footer: size + }, "Hello world\n"); +} - var decompress: Decompress = .init(&fb, .gzip, &.{}); - const r = &decompress.reader; - _ = try r.streamRemaining(&aw.writer); - try testing.expectEqualStrings(c.out, aw.getWritten()); - } +test "gzip fixed code block (type 1)" { + try testGzipDecompress(&[_]u8{ + 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x03, // gzip header (10 bytes) + 0xf3, 0x48, 0xcd, 0xc9, 0xc9, 0x57, 0x28, 0xcf, // deflate data block type 1 + 0x2f, 0xca, 0x49, 0xe1, 0x02, 0x00, + 0xd5, 0xe0, 0x39, 0xb7, 0x0c, 0x00, 0x00, 0x00, // gzip footer (chksum, len) + }, "Hello world\n"); +} + +test "gzip dynamic block (type 2)" { + try testGzipDecompress(&[_]u8{ + 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, // gzip header (10 bytes) + 0x3d, 0xc6, 0x39, 0x11, 0x00, 0x00, 0x0c, 0x02, // deflate data block type 2 + 0x30, 0x2b, 0xb5, 0x52, 0x1e, 0xff, 0x96, 0x38, + 0x16, 0x96, 0x5c, 0x1e, 0x94, 0xcb, 0x6d, 0x01, + 0x17, 0x1c, 0x39, 0xb4, 0x13, 0x00, 0x00, 0x00, // gzip footer (chksum, len) + }, "ABCDEABCD ABCDEABCD"); +} + +test "gzip header with name" { + try testGzipDecompress(&[_]u8{ + 0x1f, 0x8b, 0x08, 0x08, 0xe5, 0x70, 0xb1, 0x65, 0x00, 0x03, 0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x2e, + 0x74, 0x78, 0x74, 0x00, 0xf3, 0x48, 0xcd, 0xc9, 0xc9, 0x57, 0x28, 0xcf, 0x2f, 0xca, 0x49, 0xe1, + 0x02, 0x00, 0xd5, 0xe0, 0x39, 0xb7, 0x0c, 0x00, 0x00, 0x00, + }, "Hello world\n"); +} + +fn testGzipDecompress(in: []const u8, out: []const u8) !void { + var reader: Reader = .fixed(in); + var aw: Writer.Allocating = .init(testing.allocator); + try aw.ensureUnusedCapacity(flate.history_len); + defer aw.deinit(); + + var decompress: Decompress = .init(&reader, .gzip, &.{}); + const r = &decompress.reader; + _ = try r.streamRemaining(&aw.writer); + try testing.expectEqualStrings(out, aw.getWritten()); } test "zlib decompress" {