Skip to content

Commit

Permalink
extend roundtrip example to run for all levels
Browse files Browse the repository at this point in the history
  • Loading branch information
ianic committed Feb 10, 2024
1 parent 4a238ee commit 4390a62
Showing 1 changed file with 62 additions and 20 deletions.
82 changes: 62 additions & 20 deletions bin/roundtrip.zig
Original file line number Diff line number Diff line change
Expand Up @@ -11,24 +11,66 @@ pub fn main() !void {
const data = try stdin.readToEndAlloc(allocator, std.math.maxInt(usize));
defer allocator.free(data);

var fbs = std.io.fixedBufferStream(data);
const reader = fbs.reader();

// Compress the data
var buf = std.ArrayList(u8).init(allocator);
defer buf.deinit();
// TODO: vary the level
try flate.raw.compress(reader, buf.writer(), .default);

// Now try to decompress it
var buf_fbs = std.io.fixedBufferStream(buf.items);
var inflate = flate.raw.decompressor(buf_fbs.reader());
const inflated = inflate.reader().readAllAlloc(allocator, std.math.maxInt(usize)) catch |err| {
std.debug.print("{}\n", .{err});
return;
};
defer allocator.free(inflated);

std.debug.print("OK {d}\n", .{data.len});
try std.testing.expectEqualSlices(u8, data, inflated);
const levels = [_]flate.raw.Level{ .level_4, .level_5, .level_6, .level_7, .level_8, .level_9 };

// For each compression level
for (levels) |level| {
var fbs = std.io.fixedBufferStream(data);

// Compress the data
var buf = std.ArrayList(u8).init(allocator);
defer buf.deinit();
try flate.raw.compress(fbs.reader(), buf.writer(), level);

// Now try to decompress it
var buf_fbs = std.io.fixedBufferStream(buf.items);
var inflate = flate.raw.decompressor(buf_fbs.reader());
const inflated = try inflate.reader().readAllAlloc(allocator, std.math.maxInt(usize));
defer allocator.free(inflated);

try std.testing.expectEqualSlices(u8, data, inflated);
std.debug.print("{}, original: {d}, compressed: {d}\n", .{ level, data.len, buf.items.len });
}

// Huffman only compression
{
var fbs = std.io.fixedBufferStream(data);
var buf = std.ArrayList(u8).init(allocator);
defer buf.deinit();

// Compress the data
var cmp = try flate.raw.huffmanCompressor(buf.writer());
try cmp.compress(fbs.reader());
try cmp.close();

// Now try to decompress it
var buf_fbs = std.io.fixedBufferStream(buf.items);
var inflate = flate.raw.decompressor(buf_fbs.reader());
const inflated = try inflate.reader().readAllAlloc(allocator, std.math.maxInt(usize));
defer allocator.free(inflated);

try std.testing.expectEqualSlices(u8, data, inflated);
std.debug.print("huffman only original: {d}, compressed {d}\n", .{ data.len, buf.items.len });
}

// Store only, no compression
{
var fbs = std.io.fixedBufferStream(data);
var buf = std.ArrayList(u8).init(allocator);
defer buf.deinit();

// Compress the data
var cmp = try flate.raw.storeCompressor(buf.writer());
try cmp.compress(fbs.reader());
try cmp.close();

// Now try to decompress it
var buf_fbs = std.io.fixedBufferStream(buf.items);
var inflate = flate.raw.decompressor(buf_fbs.reader());
const inflated = try inflate.reader().readAllAlloc(allocator, std.math.maxInt(usize));
defer allocator.free(inflated);

try std.testing.expectEqualSlices(u8, data, inflated);
std.debug.print("store original: {d}, stored {d}\n", .{ data.len, buf.items.len });
}
}

0 comments on commit 4390a62

Please sign in to comment.