authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2025-07-28 16:53:01-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2025-07-31 22:10:11-07:00
log5f571f53d63e8742374c3c3cdda1ca0bb432d8fc
treeb5362fda807a6fe7c2d23ef23b76b6158893a00c
parente73ca2444e1d7a42266af5496f24ec122fd18f2c

refactor gzip test cases

zig newbies love using for loops in unit tests

1 files changed, 47 insertions(+), 57 deletions(-)

lib/std/compress/flate/Decompress.zig+47-57
......@@ -864,7 +864,7 @@ test "dynamic block (type 2)" {
864864fn testBasicCase(in: []const u8, out: []const u8) !void {
865865 var reader: Reader = .fixed(in);
866866 var aw: Writer.Allocating = .init(testing.allocator);
867 try aw.ensureUnusedCapacity(flate.history_len + 1);
867 try aw.ensureUnusedCapacity(flate.history_len);
868868 defer aw.deinit();
869869
870870 var decompress: Decompress = .init(&reader, .raw, &.{});
......@@ -873,63 +873,53 @@ fn testBasicCase(in: []const u8, out: []const u8) !void {
873873 try testing.expectEqualStrings(out, aw.getWritten());
874874}
875875
876test "gzip decompress" {
877 const cases = [_]struct {
878 in: []const u8,
879 out: []const u8,
880 }{
881 // non compressed block (type 0)
882 .{
883 .in = &[_]u8{
884 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, // gzip header (10 bytes)
885 0b0000_0001, 0b0000_1100, 0x00, 0b1111_0011, 0xff, // deflate fixed buffer header len, nlen
886 'H', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd', 0x0a, // non compressed data
887 0xd5, 0xe0, 0x39, 0xb7, // gzip footer: checksum
888 0x0c, 0x00, 0x00, 0x00, // gzip footer: size
889 },
890 .out = "Hello world\n",
891 },
892 // fixed code block (type 1)
893 .{
894 .in = &[_]u8{
895 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x03, // gzip header (10 bytes)
896 0xf3, 0x48, 0xcd, 0xc9, 0xc9, 0x57, 0x28, 0xcf, // deflate data block type 1
897 0x2f, 0xca, 0x49, 0xe1, 0x02, 0x00,
898 0xd5, 0xe0, 0x39, 0xb7, 0x0c, 0x00, 0x00, 0x00, // gzip footer (chksum, len)
899 },
900 .out = "Hello world\n",
901 },
902 // dynamic block (type 2)
903 .{
904 .in = &[_]u8{
905 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, // gzip header (10 bytes)
906 0x3d, 0xc6, 0x39, 0x11, 0x00, 0x00, 0x0c, 0x02, // deflate data block type 2
907 0x30, 0x2b, 0xb5, 0x52, 0x1e, 0xff, 0x96, 0x38,
908 0x16, 0x96, 0x5c, 0x1e, 0x94, 0xcb, 0x6d, 0x01,
909 0x17, 0x1c, 0x39, 0xb4, 0x13, 0x00, 0x00, 0x00, // gzip footer (chksum, len)
910 },
911 .out = "ABCDEABCD ABCDEABCD",
912 },
913 // gzip header with name
914 .{
915 .in = &[_]u8{
916 0x1f, 0x8b, 0x08, 0x08, 0xe5, 0x70, 0xb1, 0x65, 0x00, 0x03, 0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x2e,
917 0x74, 0x78, 0x74, 0x00, 0xf3, 0x48, 0xcd, 0xc9, 0xc9, 0x57, 0x28, 0xcf, 0x2f, 0xca, 0x49, 0xe1,
918 0x02, 0x00, 0xd5, 0xe0, 0x39, 0xb7, 0x0c, 0x00, 0x00, 0x00,
919 },
920 .out = "Hello world\n",
921 },
922 };
923 for (cases) |c| {
924 var fb: Reader = .fixed(c.in);
925 var aw: Writer.Allocating = .init(testing.allocator);
926 defer aw.deinit();
876test "gzip non compressed block (type 0)" {
877 try testGzipDecompress(&[_]u8{
878 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, // gzip header (10 bytes)
879 0b0000_0001, 0b0000_1100, 0x00, 0b1111_0011, 0xff, // deflate fixed buffer header len, nlen
880 'H', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd', 0x0a, // non compressed data
881 0xd5, 0xe0, 0x39, 0xb7, // gzip footer: checksum
882 0x0c, 0x00, 0x00, 0x00, // gzip footer: size
883 }, "Hello world\n");
884}
927885
928 var decompress: Decompress = .init(&fb, .gzip, &.{});
929 const r = &decompress.reader;
930 _ = try r.streamRemaining(&aw.writer);
931 try testing.expectEqualStrings(c.out, aw.getWritten());
932 }
886test "gzip fixed code block (type 1)" {
887 try testGzipDecompress(&[_]u8{
888 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x03, // gzip header (10 bytes)
889 0xf3, 0x48, 0xcd, 0xc9, 0xc9, 0x57, 0x28, 0xcf, // deflate data block type 1
890 0x2f, 0xca, 0x49, 0xe1, 0x02, 0x00,
891 0xd5, 0xe0, 0x39, 0xb7, 0x0c, 0x00, 0x00, 0x00, // gzip footer (chksum, len)
892 }, "Hello world\n");
893}
894
895test "gzip dynamic block (type 2)" {
896 try testGzipDecompress(&[_]u8{
897 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, // gzip header (10 bytes)
898 0x3d, 0xc6, 0x39, 0x11, 0x00, 0x00, 0x0c, 0x02, // deflate data block type 2
899 0x30, 0x2b, 0xb5, 0x52, 0x1e, 0xff, 0x96, 0x38,
900 0x16, 0x96, 0x5c, 0x1e, 0x94, 0xcb, 0x6d, 0x01,
901 0x17, 0x1c, 0x39, 0xb4, 0x13, 0x00, 0x00, 0x00, // gzip footer (chksum, len)
902 }, "ABCDEABCD ABCDEABCD");
903}
904
905test "gzip header with name" {
906 try testGzipDecompress(&[_]u8{
907 0x1f, 0x8b, 0x08, 0x08, 0xe5, 0x70, 0xb1, 0x65, 0x00, 0x03, 0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x2e,
908 0x74, 0x78, 0x74, 0x00, 0xf3, 0x48, 0xcd, 0xc9, 0xc9, 0x57, 0x28, 0xcf, 0x2f, 0xca, 0x49, 0xe1,
909 0x02, 0x00, 0xd5, 0xe0, 0x39, 0xb7, 0x0c, 0x00, 0x00, 0x00,
910 }, "Hello world\n");
911}
912
913fn testGzipDecompress(in: []const u8, out: []const u8) !void {
914 var reader: Reader = .fixed(in);
915 var aw: Writer.Allocating = .init(testing.allocator);
916 try aw.ensureUnusedCapacity(flate.history_len);
917 defer aw.deinit();
918
919 var decompress: Decompress = .init(&reader, .gzip, &.{});
920 const r = &decompress.reader;
921 _ = try r.streamRemaining(&aw.writer);
922 try testing.expectEqualStrings(out, aw.getWritten());
933923}
934924
935925test "zlib decompress" {