| ... | ... | @@ -247,38 +247,46 @@ test "zstandard decompression" { |
| 247 | 247 | const res19 = try decompress.decode(buffer, compressed19, true); |
| 248 | 248 | try std.testing.expectEqual(uncompressed.len, res19); |
| 249 | 249 | try std.testing.expectEqualSlices(u8, uncompressed, buffer); |
| 250 | } |
| 251 | |
| 252 | test "zstandard streaming decompression" { |
| 253 | // default stack size for wasm32 is too low for DecompressStream - slightly |
| 254 | // over 9MiB stack space is needed via the --stack CLI flag |
| 255 | if (@import("builtin").target.cpu.arch == .wasm32) return error.SkipZigTest; |
| 256 | |
| 257 | const uncompressed = @embedFile("testdata/rfc8478.txt"); |
| 258 | const compressed3 = @embedFile("testdata/rfc8478.txt.zst.3"); |
| 259 | const compressed19 = @embedFile("testdata/rfc8478.txt.zst.19"); |
| 250 | 260 | |
| 251 | 261 | try testReader(compressed3, uncompressed); |
| 252 | 262 | try testReader(compressed19, uncompressed); |
| 253 | 263 | } |
| 254 | 264 | |
| 255 | 265 | fn expectEqualDecoded(expected: []const u8, input: []const u8) !void { |
| 256 | | const allocator = std.testing.allocator; |
| 257 | | |
| 258 | 266 | { |
| 259 | | const result = try decompress.decodeAlloc(allocator, input, false, 1 << 23); |
| 260 | | defer allocator.free(result); |
| 267 | const result = try decompress.decodeAlloc(std.testing.allocator, input, false, 1 << 23); |
| 268 | defer std.testing.allocator.free(result); |
| 261 | 269 | try std.testing.expectEqualStrings(expected, result); |
| 262 | 270 | } |
| 263 | 271 | |
| 264 | 272 | { |
| 265 | | var buffer = try allocator.alloc(u8, 2 * expected.len); |
| 266 | | defer allocator.free(buffer); |
| 273 | var buffer = try std.testing.allocator.alloc(u8, 2 * expected.len); |
| 274 | defer std.testing.allocator.free(buffer); |
| 267 | 275 | |
| 268 | 276 | const size = try decompress.decode(buffer, input, false); |
| 269 | 277 | try std.testing.expectEqualStrings(expected, buffer[0..size]); |
| 270 | 278 | } |
| 279 | } |
| 271 | 280 | |
| 272 | | { |
| 273 | | var window_buffer: [DecompressStreamOptions.default_window_size_max]u8 = undefined; |
| 274 | | var in_stream = std.io.fixedBufferStream(input); |
| 275 | | var stream = decompressStream(in_stream.reader(), &window_buffer); |
| 281 | fn expectEqualDecodedStreaming(expected: []const u8, input: []const u8) !void { |
| 282 | var window_buffer: [DecompressStreamOptions.default_window_size_max]u8 = undefined; |
| 283 | var in_stream = std.io.fixedBufferStream(input); |
| 284 | var stream = decompressStream(in_stream.reader(), &window_buffer); |
| 276 | 285 | |
| 277 | | const result = try stream.reader().readAllAlloc(allocator, std.math.maxInt(usize)); |
| 278 | | defer allocator.free(result); |
| 286 | const result = try stream.reader().readAllAlloc(std.testing.allocator, std.math.maxInt(usize)); |
| 287 | defer std.testing.allocator.free(result); |
| 279 | 288 | |
| 280 | | try std.testing.expectEqualStrings(expected, result); |
| 281 | | } |
| 289 | try std.testing.expectEqualStrings(expected, result); |
| 282 | 290 | } |
| 283 | 291 | |
| 284 | 292 | test "zero sized block" { |
| ... | ... | @@ -296,3 +304,23 @@ test "zero sized block" { |
| 296 | 304 | try expectEqualDecoded("", input_raw); |
| 297 | 305 | try expectEqualDecoded("", input_rle); |
| 298 | 306 | } |
| 307 | |
| 308 | test "zero sized block streaming" { |
| 309 | // default stack size for wasm32 is too low for DecompressStream - slightly |
| 310 | // over 9MiB stack space is needed via the --stack CLI flag |
| 311 | if (@import("builtin").target.cpu.arch == .wasm32) return error.SkipZigTest; |
| 312 | |
| 313 | const input_raw = |
| 314 | "\x28\xb5\x2f\xfd" ++ // zstandard frame magic number |
| 315 | "\x20\x00" ++ // frame header: only single_segment_flag set, frame_content_size zero |
| 316 | "\x01\x00\x00"; // block header with: last_block set, block_type raw, block_size zero |
| 317 | |
| 318 | const input_rle = |
| 319 | "\x28\xb5\x2f\xfd" ++ // zstandard frame magic number |
| 320 | "\x20\x00" ++ // frame header: only single_segment_flag set, frame_content_size zero |
| 321 | "\x03\x00\x00" ++ // block header with: last_block set, block_type rle, block_size zero |
| 322 | "\xaa"; // block_content |
| 323 | |
| 324 | try expectEqualDecodedStreaming("", input_raw); |
| 325 | try expectEqualDecodedStreaming("", input_rle); |
| 326 | } |