X-Git-Url: http://git.ozlabs.org/?a=blobdiff_plain;f=ccan%2Farray%2Ftest%2FtestLits_generate.pl;fp=ccan%2Farray%2Ftest%2FtestLits_generate.pl;h=af41b8869ed2811b5ade13ede2f6eae8a73969a9;hb=c1daa044b22fce3ca80d3430e3e1ad9360f8a4f1;hp=0000000000000000000000000000000000000000;hpb=09e4858bdbd026244ab250ef11d82a2189d46e49;p=ccan diff --git a/ccan/array/test/testLits_generate.pl b/ccan/array/test/testLits_generate.pl new file mode 100755 index 00000000..af41b886 --- /dev/null +++ b/ccan/array/test/testLits_generate.pl @@ -0,0 +1,78 @@ +#!/usr/bin/perl +use strict; +use warnings; + +my $amount = 10; +my $maxLen = 509; +srand(0); + +my $templateFile = 'testLits.h.template'; +my $outFile = 'testLits.h'; + +open(TF, $templateFile); + +open(OUT, '>'.$outFile); +select OUT; + +my $inLoop = 0; +my $loopText = ''; + +foreach my $line () { + $line =~ s/\@amount/$amount/g; + if (!$inLoop) { + if ($line =~ /\@forEachRandomString/) { + $inLoop = 1; + next; + } + print $line; + } elsif ($inLoop == 1) { + if ($line =~ /\@end/) { + $inLoop = 0; + #handle $loopText + for (my $i=0; $i<$amount; $i++) { + my $str = randomCString($maxLen); + my $lt = $loopText; + $lt =~ s/\@i/$i/g; + $lt =~ s/\@str/\"$str\"/g; + print "$lt\n"; + } + $loopText = ''; + next; + } + $loopText .= $line; + } +} + +close(OUT); +close(TF); + +#argument: maxLen +sub randomCString { + my $len = int(rand($_[0]+1)); + my $lastWasHex = 0; + my $str = ''; + + for (my $i=0; $i<$len; $i++) { + my $cn = int(rand(255)) + 1; + my $c = chr($cn); + if ($lastWasHex && ($c =~ /[0-9A-Fa-f]/)) { + $lastWasHex = 1; + $str .= sprintf("\\x%02X", $cn); + } elsif ($c =~ /[\t\n\013\f\r]/) { + $lastWasHex = 0; + $c =~ tr/\t\n\013\f\r/tnvfr/; + $str .= '\\'.$c; + } elsif ($cn<32 || $cn>126) { + $lastWasHex = 1; + $str .= sprintf("\\x%02X", $cn); + } else { + $lastWasHex = 0; + if ($c =~ /[\"\\]/) { + $str .= '\\'.$c; + } else { + $str .= $c; + } + } + } + return $str; +}