-our ($seed) = 1;
-sub my_rand {
- my ($modulo) = @_;
- my ($a) = 16807;
- my ($m) = 2147483647;
- my ($tmp) = $a * $seed;
- $seed = $tmp - $m * int ($tmp / $m);
- return $seed % $modulo;
-}
-EOF
- cat > test-my-rand.pl <<'EOF'
-#! /usr/bin/perl
-use strict;
-use warnings;
-do './my-rand.pl';
-my_rand (1) foreach 1...10000;
-our $seed;
-die $seed if $seed != 1043618065;
-EOF
-}
-]
-m4_divert_pop([PREPARE_TESTS])
-
-AT_SETUP([numeric input formats])
-AT_KEYWORDS([data-in])
-data_in_prng
-AT_CHECK([$PERL test-my-rand.pl])
-AT_DATA([num-in.pl],
-[[#! /usr/bin/perl
-
-use POSIX;
-use strict;
-use warnings;
-
-do './my-rand.pl';
-
-for my $number (0, 1, .5, .015625, 123) {
- my ($base_exp) = floor ($number ? log10 ($number) : 0);
- for my $offset (-3...3) {
- my ($exponent) = $base_exp + $offset;
- my ($fraction) = $number / 10**$offset;
-
- permute_zeros ($fraction, $exponent);
- }
-}
-
-sub permute_zeros {
- my ($fraction, $exponent) = @_;
-
- my ($frac_rep) = sprintf ("%f", $fraction);
- my ($leading_zeros) = length (($frac_rep =~ /^(0*)/)[0]);
- my ($trailing_zeros) = length (($frac_rep =~ /(\.?0*)$/)[0]);
- for my $i (0...$leading_zeros) {
- for my $j (0...$trailing_zeros) {
- my ($trimmed) = substr ($frac_rep, $i,
- length ($frac_rep) - $i - $j);
- next if $trimmed eq '.' || $trimmed eq '';
-
- permute_commas ($trimmed, $exponent);
- }
- }
-}
-
-sub permute_commas {
- my ($frac_rep, $exponent) = @_;
- permute_dot_comma ($frac_rep, $exponent);
- my ($pos) = int (my_rand (length ($frac_rep) + 1));
- $frac_rep = substr ($frac_rep, 0, $pos) . "," . substr ($frac_rep, $pos);
- permute_dot_comma ($frac_rep, $exponent);
-}
-
-sub permute_dot_comma {
- my ($frac_rep, $exponent) = @_;
- permute_exponent_syntax ($frac_rep, $exponent);
- if ($frac_rep =~ /[,.]/) {
- $frac_rep =~ tr/.,/,./;
- permute_exponent_syntax ($frac_rep, $exponent);
- }
-}
-
-sub permute_exponent_syntax {
- my ($frac_rep, $exponent) = @_;
- my (@exp_reps);
- if ($exponent == 0) {
- @exp_reps = pick ('', 'e0', 'e-0', 'e+0', '-0', '+0');
- } elsif ($exponent > 0) {
- @exp_reps = pick ("e$exponent", "e+$exponent", "+$exponent");
- } else {
- my ($abs_exp) = -$exponent;
- @exp_reps = pick ("e-$abs_exp", , "e-$abs_exp", "-$abs_exp");
- }
- permute_sign_and_affix ($frac_rep, $_) foreach @exp_reps;
-}
-
-sub permute_sign_and_affix {
- my ($frac_rep, $exp_rep) = @_;
- for my $prefix (pick ('', '$'),
- pick ('-', '-$', '$-', '$-$'),
- pick ('+', '+$', '$+', '$+$')) {
- for my $suffix ('', '%') {
- permute_spaces ("$prefix$frac_rep$exp_rep$suffix");
- }
- }
-}
-
-sub permute_spaces {
- my ($s) = @_;
- $s =~ s/([-+\$e%])/ $1 /g;
- my (@fields) = split (' ', $s);
- print join ('', @fields), "\n";
-
- if ($#fields > 0) {
- my ($pos) = int (my_rand ($#fields)) + 1;
- print join ('', @fields[0...$pos - 1]);
- print " ";
- print join ('', @fields[$pos...$#fields]);
- print "\n";
- }
-}
-
-sub pick {
- return $_[int (my_rand ($#_ + 1))];
-}