| 1 | #! /usr/bin/perl |
| 2 | # |
| 3 | # Generate a random test file for dynamic array testing. |
| 4 | # |
| 5 | # Syntax reference: |
| 6 | # |
| 7 | # push n, pop, shift, unshift n -- normal stack ops (pop and shift print) |
| 8 | # insert x y z ... -- insert items at beginning |
| 9 | # append x y z ... -- append items at end |
| 10 | # delete n -- remove n items from beginning |
| 11 | # reduce n -- remove n items from end |
| 12 | # set i n -- assign item at index i to be n |
| 13 | # get i -- display item at index i |
| 14 | # show -- write entire array to stdout, space separated on one line |
| 15 | |
| 16 | sub random ($) { |
| 17 | my $lim = shift; |
| 18 | return int(rand($lim)); |
| 19 | } |
| 20 | |
| 21 | $lines = shift || 100; |
| 22 | $max = 0; # Estimate of size of array |
| 23 | $serial = 1; |
| 24 | while ($lines) { |
| 25 | $what = random(17); |
| 26 | if ($what < 8) { |
| 27 | my $op = (qw(push pop shift unshift))[$what % 4]; |
| 28 | if ($op eq "push" || $op eq "unshift") { |
| 29 | my $n = $serial++; |
| 30 | $max++; |
| 31 | print "$op $n\n"; |
| 32 | } elsif ($max > 0) { |
| 33 | $max--; |
| 34 | print "$op\n"; |
| 35 | } |
| 36 | } elsif ($what < 10) { |
| 37 | my @n = ($serial++); |
| 38 | my $op = (qw(insert append))[$what % 2]; |
| 39 | push(@n, $serial++) while random(4) < 3; |
| 40 | print "$op ", join(" ", @n), "\n"; |
| 41 | $max += @n; |
| 42 | } elsif ($what < 12) { |
| 43 | if ($max < 10000) { next; } |
| 44 | my $n = 1; |
| 45 | my $op = (qw(delete reduce))[$what % 2]; |
| 46 | $n++ while random(4) < 3; |
| 47 | print "$op $n\n"; |
| 48 | $max -= $n; |
| 49 | if ($max < 0) { |
| 50 | $max = 0; |
| 51 | } |
| 52 | } elsif ($what < 16) { |
| 53 | my $i = random($max); |
| 54 | $i++ while random(4) < 2; |
| 55 | if ($what % 2 == 0) { |
| 56 | my $n = $serial++; |
| 57 | print "set $i $n\n"; |
| 58 | if ($i >= $max) { |
| 59 | $max = $i + 1; |
| 60 | } |
| 61 | } else { |
| 62 | print "get $i\n"; |
| 63 | } |
| 64 | } elsif (random(10) == 0) { |
| 65 | print "show\n"; |
| 66 | } else { next; } |
| 67 | $lines--; |
| 68 | } |
| 69 | |
| 70 | print "show\n"; |