Skip to content

Commit

Permalink
add encode decode for a chunk that compresses
Browse files Browse the repository at this point in the history
  • Loading branch information
g11tech committed Dec 22, 2024
1 parent c7679f2 commit 1d94872
Showing 1 changed file with 24 additions and 1 deletion.
25 changes: 24 additions & 1 deletion src/frames.zig
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ pub fn encode(allocator: Allocator, writer: anytype, data: []u8) !void {
try writer.writeAll(&IDENTIFIER_FRAME);
var i: usize = 0;
while (i < data.len) {
std.debug.print("chunk[{d}..{d}]", .{ i, i + UNCOMPRESSED_CHUNK_SIZE });
const chunk = data[i..@min(i + UNCOMPRESSED_CHUNK_SIZE, data.len)];
const compressed = try snappy.encode(allocator, chunk);
defer allocator.free(compressed);
Expand Down Expand Up @@ -146,6 +145,7 @@ test "decode" {
}

test "encode" {
// this should lead to an uncompressed chunk
const data = "thissNaPpY";
const data_slice = try std.testing.allocator.alloc(u8, data.len);
defer std.testing.allocator.free(data_slice);
Expand All @@ -162,3 +162,26 @@ test "encode" {

try std.testing.expectEqualSlices(std.meta.Child([]const u8), dumped, expected);
}

test "encode<>decode" {
// this should lead to a compressed chunk
const data = "thissNaPpYYYYYYYYYYYYYYYYYYYY";
const data_slice = try std.testing.allocator.alloc(u8, data.len);
defer std.testing.allocator.free(data_slice);

std.mem.copyForwards(u8, data_slice, data);
var arraylistdata = std.ArrayList(u8).init(std.testing.allocator);
defer arraylistdata.deinit();
const fbswriter = arraylistdata.writer();

try encode(std.testing.allocator, fbswriter, data_slice);
const encoded = arraylistdata.items;

var arraylistdata1 = std.ArrayList(u8).init(std.testing.allocator);
defer arraylistdata1.deinit();
const fbswriter1 = arraylistdata1.writer();
try decode(std.testing.allocator, fbswriter1, encoded);
const decoded = arraylistdata1.items;

try std.testing.expectEqualSlices(std.meta.Child([]const u8), data, decoded);
}

0 comments on commit 1d94872

Please sign in to comment.