Zig 0.17.0-dev (Split by item)

Shortcut keys
?
Show these tips
S
Focus search field
Esc
Clear focus & close tips
U
Jump to source code
↑
Move up in search results
↓
Move down in search results
↵
Go to active search result
This is an example of documentation generated by ZigDoc, an alternative to Zig's built-in Auto Doc feature. See also examples in other modes/formats. The project being documented here (as the example) is the Zig standard library. Private Function

testFuzzedMatchLen

Private Function
std.compress.flate.Compress.testFuzzedMatchLen
fn testFuzzedMatchLen(_: void, smith: *std.testing.Smith) !void

Params

File

compress/flate/Compress.zig:642
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
fn testFuzzedMatchLen(_: void, smith: *std.testing.Smith) !void {
    @disableInstrumentation();
    var buf: [1024]u8 = undefined;
    var w: Writer = .fixed(&buf);

    while (w.unusedCapacityLen() != 0 and !smith.eosWeightedSimple(7, 1)) {
        switch (smith.value(enum(u2) { splat, copy, insert })) {
            .splat => w.splatByteAll(
                smith.value(u8),
                smith.valueRangeAtMost(u9, 1, @min(511, w.unusedCapacityLen())),
            ) catch unreachable,
            .copy => write: {
                if (w.buffered().len == 0) continue;
                const start = smith.valueRangeAtMost(u10, 0, @intCast(w.buffered().len - 1));
                const max_len = @min(w.unusedCapacityLen(), w.buffered().len - start);
                const len = smith.valueRangeAtMost(u10, 1, @intCast(max_len));
                break :write w.writeAll(w.buffered()[start..][0..len]) catch unreachable;
            },
            .insert => w.advance(smith.slice(w.unusedCapacitySlice())),
        }
    }
    w.splatByteAll(0, (1 + token.min_length) -| w.buffered().len) catch unreachable;

    const max_start = w.buffered().len - token.min_length;
    const bytes_off = smith.valueRangeAtMost(u10, 1, @intCast(max_start));
    const prev_off = smith.valueRangeAtMost(u10, 0, bytes_off - 1);
    const prev = w.buffered()[prev_off..];
    const bytes = w.buffered()[bytes_off..];
    const old = smith.valueRangeLessThan(u10, 0, @min(bytes.len, token.max_length));

    const diff_index = mem.findDiff(u8, prev, bytes).?; // unwrap since lengths are not same
    const expected_len = @min(diff_index, 258);
    errdefer std.debug.print(
        \\prev : '{any}'
        \\bytes: '{any}'
        \\old     : {}
        \\expected: {?}
        \\actual  : {}
    ++ "\n", .{
        prev,                                           bytes,                            old,
        if (old < expected_len) expected_len else null, betterMatchLen(old, prev, bytes),
    });
    if (old < expected_len) {
        try std.testing.expectEqual(expected_len, betterMatchLen(old, prev, bytes));
    } else {
        try std.testing.expect(betterMatchLen(old, prev, bytes) <= old);
    }
}