data-in: Convert test for MONTH format to Autotest framework.
[pspp-builds.git] / tests / formats / num-in.sh
1 #! /bin/sh
2
3 TEMPDIR=/tmp/pspp-tst-$$
4 mkdir -p $TEMPDIR
5 trap 'cd /; rm -rf $TEMPDIR' 0
6
7 # ensure that top_builddir  are absolute
8 if [ -z "$top_builddir" ] ; then top_builddir=. ; fi
9 if [ -z "$top_srcdir" ] ; then top_srcdir=. ; fi
10 top_builddir=`cd $top_builddir; pwd`
11 PSPP=$top_builddir/src/ui/terminal/pspp$EXEEXT
12 : ${PERL:=perl}
13
14 # ensure that top_srcdir is absolute
15 top_srcdir=`cd $top_srcdir; pwd`
16
17 STAT_CONFIG_PATH=$top_srcdir/config
18 export STAT_CONFIG_PATH
19
20 fail()
21 {
22     echo $activity
23     echo FAILED
24     exit 1;
25 }
26
27
28 no_result()
29 {
30     echo $activity
31     echo NO RESULT;
32     exit 2;
33 }
34
35 pass()
36 {
37     exit 0;
38 }
39
40 cd $TEMPDIR
41
42 activity="write PRNG fragment"
43 cat > my-rand.pl <<'EOF'
44 # This random number generator and the test for it below are drawn
45 # from Park and Miller, "Random Number Generators: Good Ones are Hard
46 # to Come By", Communications of the ACM 31:10 (October 1988).  It is
47 # documented to function properly on systems with a 46-bit or longer
48 # real significand, which includes systems that have 64-bit IEEE reals
49 # (with 53-bit significand).  The test should catch any systems for
50 # which this is not true, in any case.
51
52 our ($seed) = 1;
53 sub my_rand {
54   my ($modulo) = @_;
55   my ($a) = 16807;
56   my ($m) = 2147483647;
57   my ($tmp) = $a * $seed;
58   $seed = $tmp - $m * int ($tmp / $m);
59   return $seed % $modulo;
60 }
61 EOF
62 if [ $? -ne 0 ] ; then no_result ; fi
63
64 activity="write PRNG test program"
65 cat > test-my-rand.pl <<'EOF'
66 #! /usr/bin/perl
67 use strict;
68 use warnings;
69 do 'my-rand.pl';
70 my_rand (1) foreach 1...10000;
71 our $seed;
72 die $seed if $seed != 1043618065;
73 EOF
74 if [ $? -ne 0 ] ; then no_result ; fi
75
76 activity="test PRNG"
77 $PERL test-my-rand.pl
78 if [ $? -ne 0 ] ; then no_result ; fi
79
80 activity="write Perl program"
81 cat > num-in.pl <<'EOF'
82 #! /usr/bin/perl
83
84 use POSIX;
85 use strict;
86 use warnings;
87
88 do 'my-rand.pl';
89
90 for my $number (0, 1, .5, .015625, 123) {
91     my ($base_exp) = floor ($number ? log10 ($number) : 0);
92     for my $offset (-3...3) {
93         my ($exponent) = $base_exp + $offset;
94         my ($fraction) = $number / 10**$offset;
95
96         permute_zeros ($fraction, $exponent);
97     }
98 }
99
100 sub permute_zeros {
101     my ($fraction, $exponent) = @_;
102
103     my ($frac_rep) = sprintf ("%f", $fraction);
104     my ($leading_zeros) = length (($frac_rep =~ /^(0*)/)[0]);
105     my ($trailing_zeros) = length (($frac_rep =~ /(\.?0*)$/)[0]);
106     for my $i (0...$leading_zeros) {
107         for my $j (0...$trailing_zeros) {
108             my ($trimmed) = substr ($frac_rep, $i,
109                                     length ($frac_rep) - $i - $j);
110             next if $trimmed eq '.' || $trimmed eq '';
111
112             permute_commas ($trimmed, $exponent);
113         }
114     }
115 }
116
117 sub permute_commas {
118     my ($frac_rep, $exponent) = @_;
119     permute_dot_comma ($frac_rep, $exponent);
120     my ($pos) = int (my_rand (length ($frac_rep) + 1));
121     $frac_rep = substr ($frac_rep, 0, $pos) . "," . substr ($frac_rep, $pos);
122     permute_dot_comma ($frac_rep, $exponent);
123 }
124
125 sub permute_dot_comma {
126     my ($frac_rep, $exponent) = @_;
127     permute_exponent_syntax ($frac_rep, $exponent);
128     if ($frac_rep =~ /[,.]/) {
129         $frac_rep =~ tr/.,/,./;
130         permute_exponent_syntax ($frac_rep, $exponent);
131     }
132 }
133
134 sub permute_exponent_syntax {
135     my ($frac_rep, $exponent) = @_;
136     my (@exp_reps);
137     if ($exponent == 0) {
138         @exp_reps = pick ('', 'e0', 'e-0', 'e+0', '-0', '+0');
139     } elsif ($exponent > 0) {
140         @exp_reps = pick ("e$exponent", "e+$exponent", "+$exponent");
141     } else {
142         my ($abs_exp) = -$exponent;
143         @exp_reps = pick ("e-$abs_exp", , "e-$abs_exp", "-$abs_exp");
144     }
145     permute_sign_and_affix ($frac_rep, $_) foreach @exp_reps;
146 }
147
148 sub permute_sign_and_affix {
149     my ($frac_rep, $exp_rep) = @_;
150     for my $prefix (pick ('', '$'),
151                     pick ('-', '-$', '$-', '$-$'),
152                     pick ('+', '+$', '$+', '$+$')) {
153         for my $suffix ('', '%') {
154             permute_spaces ("$prefix$frac_rep$exp_rep$suffix");
155         }
156     }
157 }
158
159 sub permute_spaces {
160     my ($s) = @_;
161     $s =~ s/([-+\$e%])/ $1 /g;
162     my (@fields) = split (' ', $s);
163     print join ('', @fields), "\n";
164
165     if ($#fields > 0) {
166         my ($pos) = int (my_rand ($#fields)) + 1;
167         print join ('', @fields[0...$pos - 1]);
168         print " ";
169         print join ('', @fields[$pos...$#fields]);
170         print "\n";
171     }
172 }
173
174 sub pick {
175     return $_[int (my_rand ($#_ + 1))];
176 }
177 EOF
178
179 activity="generate data"
180 $PERL num-in.pl > num-in.data
181 if [ $? -ne 0 ] ; then no_result ; fi
182 echo -n .
183
184 activity="generate pspp syntax"
185 cat > num-in.pspp <<EOF
186 SET ERRORS=NONE.
187 SET MXERRS=10000000.
188 SET MXWARNS=10000000.
189 DATA LIST FILE='num-in.data' /
190         f 1-40 (f)
191         comma 1-40 (comma)
192         dot 1-40 (dot)
193         dollar 1-40 (dollar)
194         pct 1-40 (pct)
195         e 1-40 (e).
196 PRINT OUTFILE='num-in.out'/all (6f10.4).
197 EXECUTE.
198 EOF
199 if [ $? -ne 0 ] ; then no_result ; fi
200 echo -n .
201
202 activity="run program"
203 $SUPERVISOR $PSPP -o pspp.csv num-in.pspp
204 if [ $? -ne 0 ] ; then no_result ; fi
205 echo -n .
206
207 activity="gunzip expected results"
208 gzip -cd < $top_srcdir/tests/formats/num-in.expected.gz > num-in.expected
209 if [ $? -ne 0 ] ; then no_result ; fi
210 echo -n .
211
212 activity="compare output"
213 diff -u num-in.expected num-in.out
214 if [ $? -ne 0 ] ; then fail ; fi
215
216 echo .
217
218 pass