-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtests.zig
More file actions
72 lines (56 loc) · 2.26 KB
/
tests.zig
File metadata and controls
72 lines (56 loc) · 2.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
const std = @import("std");
const zstd_cli = @import("src/main.zig");
fn writeRandomFile(file: std.fs.File, size: usize) !void {
var prng = std.rand.DefaultPrng.init(0x12345678);
var random = prng.random();
const buf = try std.heap.page_allocator.alloc(u8, size);
defer std.heap.page_allocator.free(buf);
random.bytes(buf);
try file.writeAll(buf);
}
test "compress and decompress roundtrip" {
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
defer _ = gpa.deinit();
const allocator = gpa.allocator();
var tmp = std.testing.tmpDir(.{});
defer tmp.cleanup();
const tmp_root = try tmp.dir.realpathAlloc(allocator, ".");
defer allocator.free(tmp_root);
const input_path = try std.fs.path.join(allocator, &.{ tmp_root, "input.bin" });
defer allocator.free(input_path);
const compressed_path = try std.fs.path.join(allocator, &.{ tmp_root, "output.zst" });
defer allocator.free(compressed_path);
const decompressed_path = try std.fs.path.join(allocator, &.{ tmp_root, "output.bin" });
defer allocator.free(decompressed_path);
{
var input_file = try tmp.dir.createFile("input.bin", .{ .truncate = true });
defer input_file.close();
try writeRandomFile(input_file, 256 * 1024);
}
try zstd_cli.compressFile(allocator, .{
.mode = .compress,
.input_path = input_path,
.output_path = compressed_path,
.level = 10,
.progress = false,
.chunk_size = 64 * 1024,
.timing = false,
});
try zstd_cli.decompressFile(allocator, .{
.mode = .decompress,
.input_path = compressed_path,
.output_path = decompressed_path,
.progress = false,
.chunk_size = 64 * 1024,
.timing = false,
});
const original = try tmp.dir.openFile("input.bin", .{});
defer original.close();
const restored = try tmp.dir.openFile("output.bin", .{});
defer restored.close();
const original_bytes = try original.readToEndAlloc(allocator, 10 * 1024 * 1024);
defer allocator.free(original_bytes);
const restored_bytes = try restored.readToEndAlloc(allocator, 10 * 1024 * 1024);
defer allocator.free(restored_bytes);
try std.testing.expectEqualSlices(u8, original_bytes, restored_bytes);
}