git-trace

romele / git-trace public

62 commits · 1 branch · 1 tag · 1 contributor (top: Romele) · first commit 3d ago · last push 54m ago

tests/mutate.pl

#!/usr/bin/perl
# mutate.pl <seed-file> <seed-int> — emit one mutated copy on stdout.
# Mutations: byte flip, insert, delete, truncate, chunk duplicate.
use strict; use warnings;
my ($file, $seed) = @ARGV;
srand(($seed // 0) + ($$ << 8));
open my $f, '<', $file or die $!;
binmode $f; local $/;
my $d = <$f>; close $f;
$d //= '';
my $ops = 1 + int(rand(8));
for (1 .. $ops) {
    last unless length $d;
    my $p = int(rand(length $d));
    my $op = int(rand(6));
    if ($op == 0)    { substr($d, $p, 1) = chr(int(rand(256))); }
    elsif ($op == 1) { substr($d, $p, 1) = ''; }
    elsif ($op == 2) { substr($d, $p, 0) = chr(int(rand(256))); }
    elsif ($op == 3) { $d = substr($d, 0, $p); }
    elsif ($op == 4) {
        my $clen = int(rand(length($d) - $p)) + 1;
        $d .= substr($d, $p, $clen);            # duplicate tail chunk
    }
    else             { substr($d, $p, 1) = substr($d, $p, 1) x (1 + int(rand(64))); }
}
binmode STDOUT; print $d;