lib/png/src/test.zig
daab053ee43316e1809a84551d573ddd1e5bf3d2
1 const subject = @import("root.zig");
2 const decode_test = @import("decode/test.zig");
3 const encode_test = @import("encode/test.zig");
4 const ImageView = subject.ImageView;
5 const EncodeScratch = subject.EncodeScratch;
6 const EncodeLimits = subject.EncodeLimits;
7 const EncodeCapacity = subject.EncodeCapacity;
8 const EncodeStorage = subject.EncodeStorage;
9 const bytesFromImage = subject.bytesFromImage;
10 const DecodeBounds = subject.DecodeBounds;
11 const DecodeStorage = subject.DecodeStorage;
12 const imageFromBytes = subject.imageFromBytes;
13
14 test "png package namespace" {
15 @import("std").testing.refAllDecls(subject);
16 @import("std").testing.refAllDecls(decode_test);
17 @import("std").testing.refAllDecls(encode_test);
18 }
19
20 test "PNG root composes caller-selected encode bounds and storage" {
21 comptime {
22 @stardustClaim(
23 @import("alloc_phase").capacity.witness(@import("./encode/root.zig").Storage, "png_encode_root"),
24 null,
25 null,
26 null,
27 null,
28 null,
29 null,
30 );
31 }
32
33 const std = @import("std");
34 const pixels = [_]u8{ 1, 2, 3, 255 };
35 const image = ImageView{ .rgba8 = &pixels, .width = 1, .height = 1 };
36 var scratch: EncodeScratch = undefined;
37 const limits = EncodeLimits{
38 .image = image,
39 .bounds = .{ .image_pixels = 1 },
40 .scratch = &scratch,
41 };
42 const capacity = try EncodeCapacity.derive(limits);
43 var storage = try EncodeStorage.init(std.testing.allocator, limits);
44 defer storage.deinit(std.testing.allocator);
45 storage.activate();
46 const bytes = try bytesFromImage(&storage, image);
47 defer storage.reset();
48 try std.testing.expectEqual(capacity.storage_bytes, storage.status().storage_bytes);
49 try std.testing.expectEqual(capacity.plan.output_bytes, bytes.len);
50 }
51
52 test "PNG root composes caller-selected decode bounds and storage" {
53 comptime {
54 @stardustClaim(
55 @import("alloc_phase").capacity.witness(@import("./decode/root.zig").Storage, "png_decode_root"),
56 null,
57 null,
58 null,
59 null,
60 null,
61 null,
62 );
63 }
64
65 const std = @import("std");
66 const witness = decode_test.fixture.indexed;
67 var counting = std.testing.FailingAllocator.init(std.testing.allocator, .{});
68 const exact = try DecodeBounds.exact(&witness);
69 const bounds = DecodeBounds{
70 .input_bytes = exact.input_bytes + 1,
71 .image_pixels = exact.image_pixels + 1,
72 .source_row_bytes = exact.source_row_bytes + 1,
73 .idat_bytes = exact.idat_bytes + 1,
74 };
75 var storage = try DecodeStorage.init(counting.allocator(), .{
76 .bytes = &witness,
77 .bounds = bounds,
78 });
79 defer storage.deinit(counting.allocator());
80 try std.testing.expectEqual(@as(usize, 1), counting.alloc_index);
81 storage.activate();
82 const image = try imageFromBytes(&storage, &witness);
83 defer storage.reset();
84 try std.testing.expectEqual(@as(u32, 2), image.width);
85 try std.testing.expectEqual(storage.status().storage_bytes, counting.allocated_bytes);
86 }