From 5d5de1adfffc66bac5872b18b3766bc32bf3464d Mon Sep 17 00:00:00 2001 From: Ben Pfaff Date: Wed, 13 Oct 2010 20:26:42 -0700 Subject: [PATCH] data-in: Convert test for numeric input formats to Autotest framework. --- tests/automake.mk | 3 +- tests/data/data-in.at | 154 +++++++++++++++ tests/{formats => data}/num-in.expected.gz | Bin tests/formats/num-in.sh | 218 --------------------- 4 files changed, 155 insertions(+), 220 deletions(-) rename tests/{formats => data}/num-in.expected.gz (100%) delete mode 100755 tests/formats/num-in.sh diff --git a/tests/automake.mk b/tests/automake.mk index e5202e15..031c39ab 100644 --- a/tests/automake.mk +++ b/tests/automake.mk @@ -11,7 +11,6 @@ TESTS_ENVIRONMENT += EXEEXT=$(EXEEXT) dist_TESTS = \ tests/formats/date-in.sh \ - tests/formats/num-in.sh \ tests/formats/num-out.sh \ tests/formats/time-in.sh \ tests/formats/360.sh @@ -216,9 +215,9 @@ EXTRA_DIST += \ tests/data/binhex-in.expected.cmp.gz \ tests/data/binhex-out.expected.gz \ tests/data/legacy-in.expected.cmp.gz \ + tests/data/num-in.expected.gz \ tests/data/v13.sav \ tests/data/v14.sav \ - tests/formats/num-in.expected.gz \ tests/formats/num-out.expected.cmp.gz \ tests/formats/num-out-cmp.pl \ tests/formats/num-out-compare.pl \ diff --git a/tests/data/data-in.at b/tests/data/data-in.at index e9a91bcc..654c70e3 100644 --- a/tests/data/data-in.at +++ b/tests/data/data-in.at @@ -1,5 +1,159 @@ AT_BANNER([data input (data-in)]) +m4_divert_push([PREPARE_TESTS]) +data_in_prng () { + cat > my-rand.pl <<'EOF' +# This random number generator and the test for it below are drawn +# from Park and Miller, "Random Number Generators: Good Ones are Hard +# to Come By", Communications of the ACM 31:10 (October 1988). It is +# documented to function properly on systems with a 46-bit or longer +# real significand, which includes systems that have 64-bit IEEE reals +# (with 53-bit significand). The test should catch any systems for +# which this is not true, in any case. + +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))]; +} +]]) +AT_CHECK([$PERL num-in.pl > num-in.data]) +AT_DATA([num-in.sps], [dnl +SET ERRORS=NONE. +SET MXERRS=10000000. +SET MXWARNS=10000000. +DATA LIST FILE='num-in.data' NOTABLE/ + f 1-40 (f) + comma 1-40 (comma) + dot 1-40 (dot) + dollar 1-40 (dollar) + pct 1-40 (pct) + e 1-40 (e). +PRINT OUTFILE='num-in.out'/all (6f10.4). +EXECUTE. +]) +AT_CHECK([pspp -O format=csv num-in.sps]) +AT_CHECK([gzip -cd < $top_srcdir/tests/data/num-in.expected.gz > expout]) +AT_CHECK([cat num-in.out], [0], [expout]) +AT_CLEANUP + dnl Some very old version of PSPP crashed reading big numbers, dnl so this checks for regressions. AT_SETUP([reading big numbers]) diff --git a/tests/formats/num-in.expected.gz b/tests/data/num-in.expected.gz similarity index 100% rename from tests/formats/num-in.expected.gz rename to tests/data/num-in.expected.gz diff --git a/tests/formats/num-in.sh b/tests/formats/num-in.sh deleted file mode 100755 index 0461a6ba..00000000 --- a/tests/formats/num-in.sh +++ /dev/null @@ -1,218 +0,0 @@ -#! /bin/sh - -TEMPDIR=/tmp/pspp-tst-$$ -mkdir -p $TEMPDIR -trap 'cd /; rm -rf $TEMPDIR' 0 - -# ensure that top_builddir are absolute -if [ -z "$top_builddir" ] ; then top_builddir=. ; fi -if [ -z "$top_srcdir" ] ; then top_srcdir=. ; fi -top_builddir=`cd $top_builddir; pwd` -PSPP=$top_builddir/src/ui/terminal/pspp$EXEEXT -: ${PERL:=perl} - -# ensure that top_srcdir is absolute -top_srcdir=`cd $top_srcdir; pwd` - -STAT_CONFIG_PATH=$top_srcdir/config -export STAT_CONFIG_PATH - -fail() -{ - echo $activity - echo FAILED - exit 1; -} - - -no_result() -{ - echo $activity - echo NO RESULT; - exit 2; -} - -pass() -{ - exit 0; -} - -cd $TEMPDIR - -activity="write PRNG fragment" -cat > my-rand.pl <<'EOF' -# This random number generator and the test for it below are drawn -# from Park and Miller, "Random Number Generators: Good Ones are Hard -# to Come By", Communications of the ACM 31:10 (October 1988). It is -# documented to function properly on systems with a 46-bit or longer -# real significand, which includes systems that have 64-bit IEEE reals -# (with 53-bit significand). The test should catch any systems for -# which this is not true, in any case. - -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 -if [ $? -ne 0 ] ; then no_result ; fi - -activity="write PRNG test program" -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 -if [ $? -ne 0 ] ; then no_result ; fi - -activity="test PRNG" -$PERL test-my-rand.pl -if [ $? -ne 0 ] ; then no_result ; fi - -activity="write Perl program" -cat > num-in.pl <<'EOF' -#! /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))]; -} -EOF - -activity="generate data" -$PERL num-in.pl > num-in.data -if [ $? -ne 0 ] ; then no_result ; fi -echo -n . - -activity="generate pspp syntax" -cat > num-in.pspp < num-in.expected -if [ $? -ne 0 ] ; then no_result ; fi -echo -n . - -activity="compare output" -diff -u num-in.expected num-in.out -if [ $? -ne 0 ] ; then fail ; fi - -echo . - -pass -- 2.30.2