lib/pdf/src/filter/capacity.zig
daab053ee43316e1809a84551d573ddd1e5bf3d2
1 const std = @import("std");
2
3 const model = @import("model.zig");
4
5 pub const storage_alignment: usize = @alignOf(usize);
6
7 pub const Limits = struct {
8 bounds: model.Bounds,
9 };
10
11 pub const Capacity = struct {
12 bounds: model.Bounds,
13 storage_bytes: usize,
14
15 pub fn derive(limits: Limits) Capacity {
16 return .{
17 .bounds = limits.bounds,
18 .storage_bytes = limits.bounds.decoded_bytes,
19 };
20 }
21 };
22
23 fn modelCapacity(limits: Limits) Capacity {
24 return .{
25 .bounds = limits.bounds,
26 .storage_bytes = limits.bounds.decoded_bytes,
27 };
28 }
29
30 test "PDF filter capacity matches an independent byte model" {
31 comptime {
32 @stardustClaim(
33 @import("alloc_phase").capacity.witness(@import("./root.zig").Storage, "pdf_filter_capacity"),
34 null,
35 null,
36 null,
37 null,
38 null,
39 null,
40 );
41 }
42
43 const limits = Limits{ .bounds = .{ .input_bytes = 8_063, .decoded_bytes = 1_049_600 } };
44 const capacity = Capacity.derive(limits);
45 try std.testing.expectEqual(modelCapacity(limits), capacity);
46 try std.testing.expectEqual(
47 capacity.bounds.decoded_bytes,
48 capacity.storage_bytes,
49 );
50 }
51
52 test "PDF filter capacity retains the strongest finite decoded bound" {
53 const capacity = Capacity.derive(.{ .bounds = .{
54 .input_bytes = 0,
55 .decoded_bytes = std.math.maxInt(usize),
56 } });
57 try std.testing.expectEqual(std.math.maxInt(usize), capacity.storage_bytes);
58 }