authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2025-07-28 18:05:29-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2025-07-31 22:10:11-07:00
logc684b21b4f8f13b32535d799d2c76cdb2dccc8f0
tree224919a73663a8c8f150167bf7147776679aa404
parentac4fbb427ba71e20fe8042b86179409598241d6a

simplify test cases


1 files changed, 17 insertions(+), 55 deletions(-)

lib/std/compress/flate/Decompress.zig+17-55
......@@ -842,40 +842,29 @@ test "encode/decode literals" {
842842}
843843
844844test "non compressed block (type 0)" {
845 try testBasicCase(&[_]u8{
845 try testDecompress(.raw, &[_]u8{
846846 0b0000_0001, 0b0000_1100, 0x00, 0b1111_0011, 0xff, // deflate fixed buffer header len, nlen
847847 'H', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd', 0x0a, // non compressed data
848848 }, "Hello world\n");
849849}
850850
851851test "fixed code block (type 1)" {
852 try testBasicCase(&[_]u8{
852 try testDecompress(.raw, &[_]u8{
853853 0xf3, 0x48, 0xcd, 0xc9, 0xc9, 0x57, 0x28, 0xcf, // deflate data block type 1
854854 0x2f, 0xca, 0x49, 0xe1, 0x02, 0x00,
855855 }, "Hello world\n");
856856}
857857
858858test "dynamic block (type 2)" {
859 try testBasicCase(&[_]u8{
859 try testDecompress(.raw, &[_]u8{
860860 0x3d, 0xc6, 0x39, 0x11, 0x00, 0x00, 0x0c, 0x02, // deflate data block type 2
861861 0x30, 0x2b, 0xb5, 0x52, 0x1e, 0xff, 0x96, 0x38,
862862 0x16, 0x96, 0x5c, 0x1e, 0x94, 0xcb, 0x6d, 0x01,
863863 }, "ABCDEABCD ABCDEABCD");
864864}
865865
866fn testBasicCase(in: []const u8, out: []const u8) !void {
867 var reader: Reader = .fixed(in);
868 var aw: Writer.Allocating = .init(testing.allocator);
869 try aw.ensureUnusedCapacity(flate.history_len);
870 defer aw.deinit();
871
872 var decompress: Decompress = .init(&reader, .raw, &.{});
873 _ = try decompress.reader.streamRemaining(&aw.writer);
874 try testing.expectEqualStrings(out, aw.getWritten());
875}
876
877866test "gzip non compressed block (type 0)" {
878 try testGzipDecompress(&[_]u8{
867 try testDecompress(.gzip, &[_]u8{
879868 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, // gzip header (10 bytes)
880869 0b0000_0001, 0b0000_1100, 0x00, 0b1111_0011, 0xff, // deflate fixed buffer header len, nlen
881870 'H', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd', 0x0a, // non compressed data
......@@ -885,7 +874,7 @@ test "gzip non compressed block (type 0)" {
885874}
886875
887876test "gzip fixed code block (type 1)" {
888 try testGzipDecompress(&[_]u8{
877 try testDecompress(.gzip, &[_]u8{
889878 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x03, // gzip header (10 bytes)
890879 0xf3, 0x48, 0xcd, 0xc9, 0xc9, 0x57, 0x28, 0xcf, // deflate data block type 1
891880 0x2f, 0xca, 0x49, 0xe1, 0x02, 0x00,
......@@ -894,7 +883,7 @@ test "gzip fixed code block (type 1)" {
894883}
895884
896885test "gzip dynamic block (type 2)" {
897 try testGzipDecompress(&[_]u8{
886 try testDecompress(.gzip, &[_]u8{
898887 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, // gzip header (10 bytes)
899888 0x3d, 0xc6, 0x39, 0x11, 0x00, 0x00, 0x0c, 0x02, // deflate data block type 2
900889 0x30, 0x2b, 0xb5, 0x52, 0x1e, 0xff, 0x96, 0x38,
......@@ -904,50 +893,20 @@ test "gzip dynamic block (type 2)" {
904893}
905894
906895test "gzip header with name" {
907 try testGzipDecompress(&[_]u8{
896 try testDecompress(.gzip, &[_]u8{
908897 0x1f, 0x8b, 0x08, 0x08, 0xe5, 0x70, 0xb1, 0x65, 0x00, 0x03, 0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x2e,
909898 0x74, 0x78, 0x74, 0x00, 0xf3, 0x48, 0xcd, 0xc9, 0xc9, 0x57, 0x28, 0xcf, 0x2f, 0xca, 0x49, 0xe1,
910899 0x02, 0x00, 0xd5, 0xe0, 0x39, 0xb7, 0x0c, 0x00, 0x00, 0x00,
911900 }, "Hello world\n");
912901}
913902
914fn testGzipDecompress(in: []const u8, out: []const u8) !void {
915 var reader: Reader = .fixed(in);
916 var aw: Writer.Allocating = .init(testing.allocator);
917 try aw.ensureUnusedCapacity(flate.history_len);
918 defer aw.deinit();
919
920 var decompress: Decompress = .init(&reader, .gzip, &.{});
921 _ = try decompress.reader.streamRemaining(&aw.writer);
922 try testing.expectEqualStrings(out, aw.getWritten());
923}
924
925test "zlib decompress" {
926 const cases = [_]struct {
927 in: []const u8,
928 out: []const u8,
929 }{
930 // non compressed block (type 0)
931 .{
932 .in = &[_]u8{
933 0x78, 0b10_0_11100, // zlib header (2 bytes)
934 0b0000_0001, 0b0000_1100, 0x00, 0b1111_0011, 0xff, // deflate fixed buffer header len, nlen
935 'H', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd', 0x0a, // non compressed data
936 0x1c, 0xf2, 0x04, 0x47, // zlib footer: checksum
937 },
938 .out = "Hello world\n",
939 },
940 };
941 for (cases) |c| {
942 var fb: Reader = .fixed(c.in);
943 var aw: Writer.Allocating = .init(testing.allocator);
944 defer aw.deinit();
945
946 var decompress: Decompress = .init(&fb, .zlib, &.{});
947 const r = &decompress.reader;
948 _ = try r.streamRemaining(&aw.writer);
949 try testing.expectEqualStrings(c.out, aw.getWritten());
950 }
903test "zlib decompress non compressed block (type 0)" {
904 try testDecompress(.zlib, &[_]u8{
905 0x78, 0b10_0_11100, // zlib header (2 bytes)
906 0b0000_0001, 0b0000_1100, 0x00, 0b1111_0011, 0xff, // deflate fixed buffer header len, nlen
907 'H', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd', 0x0a, // non compressed data
908 0x1c, 0xf2, 0x04, 0x47, // zlib footer: checksum
909 }, "Hello world\n");
951910}
952911
953912test "fuzzing tests" {
......@@ -1001,6 +960,7 @@ test "fuzzing tests" {
1001960 inline for (cases) |c| {
1002961 var in: Reader = .fixed(@embedFile("testdata/fuzz/" ++ c.input ++ ".input"));
1003962 var aw: Writer.Allocating = .init(testing.allocator);
963 try aw.ensureUnusedCapacity(flate.history_len);
1004964 defer aw.deinit();
1005965
1006966 var decompress: Decompress = .init(&in, .raw, &.{});
......@@ -1021,6 +981,7 @@ test "bug 18966" {
1021981
1022982 var in: Reader = .fixed(input);
1023983 var aw: Writer.Allocating = .init(testing.allocator);
984 try aw.ensureUnusedCapacity(flate.history_len);
1024985 defer aw.deinit();
1025986
1026987 var decompress: Decompress = .init(&in, .gzip, &.{});
......@@ -1146,6 +1107,7 @@ test "gzip header" {
11461107fn testDecompress(container: Container, compressed: []const u8, expected_plain: []const u8) !void {
11471108 var in: std.Io.Reader = .fixed(compressed);
11481109 var aw: std.Io.Writer.Allocating = .init(testing.allocator);
1110 try aw.ensureUnusedCapacity(flate.history_len);
11491111 defer aw.deinit();
11501112
11511113 var decompress: Decompress = .init(&in, container, &.{});