Rewrite formatted data input routines to conform to SPSS data formats
[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
12
13 # ensure that top_srcdir is absolute
14 top_srcdir=`cd $top_srcdir; pwd`
15
16 STAT_CONFIG_PATH=$top_srcdir/config
17 export STAT_CONFIG_PATH
18
19 fail()
20 {
21     echo $activity
22     echo FAILED
23     exit 1;
24 }
25
26
27 no_result()
28 {
29     echo $activity
30     echo NO RESULT;
31     exit 2;
32 }
33
34 pass()
35 {
36     exit 0;
37 }
38
39 cd $TEMPDIR
40
41 activity="write Perl program"
42 cat > num-in.pl <<'EOF'
43 #! /usr/bin/perl
44
45 use POSIX;
46 use strict;
47 use warnings;
48
49 our $next = 0;
50
51 for my $number (0, 1, .5, .015625, 123) {
52     my ($base_exp) = floor ($number ? log10 ($number) : 0);
53     for my $offset (-3...3) {
54         my ($exponent) = $base_exp + $offset;
55         my ($fraction) = $number / 10**$offset;
56
57         permute_zeros ($fraction, $exponent);
58     }
59 }
60
61 sub permute_zeros {
62     my ($fraction, $exponent) = @_;
63
64     my ($frac_rep) = sprintf ("%f", $fraction);
65     my ($leading_zeros) = length (($frac_rep =~ /^(0*)/)[0]);
66     my ($trailing_zeros) = length (($frac_rep =~ /(\.?0*)$/)[0]);
67     for my $i (0...$leading_zeros) {
68         for my $j (0...$trailing_zeros) {
69             my ($trimmed) = substr ($frac_rep, $i,
70                                     length ($frac_rep) - $i - $j);
71             next if $trimmed eq '.' || $trimmed eq '';
72
73             permute_commas ($trimmed, $exponent);
74         }
75     }
76 }
77
78 sub permute_commas {
79     my ($frac_rep, $exponent) = @_;
80     permute_dot_comma ($frac_rep, $exponent);
81     my ($pos) = int (my_rand (length ($frac_rep) + 1));
82     $frac_rep = substr ($frac_rep, 0, $pos) . "," . substr ($frac_rep, $pos);
83     permute_dot_comma ($frac_rep, $exponent);
84 }
85
86 sub permute_dot_comma {
87     my ($frac_rep, $exponent) = @_;
88     permute_exponent_syntax ($frac_rep, $exponent);
89     if ($frac_rep =~ /[,.]/) {
90         $frac_rep =~ tr/.,/,./;
91         permute_exponent_syntax ($frac_rep, $exponent);
92     }
93 }
94
95 sub permute_exponent_syntax {
96     my ($frac_rep, $exponent) = @_;
97     my (@exp_reps);
98     if ($exponent == 0) {
99         @exp_reps = pick ('', 'e0', 'e-0', 'e+0', '-0', '+0');
100     } elsif ($exponent > 0) {
101         @exp_reps = pick ("e$exponent", "e+$exponent", "+$exponent");
102     } else {
103         my ($abs_exp) = -$exponent;
104         @exp_reps = pick ("e-$abs_exp", , "e-$abs_exp", "-$abs_exp");
105     }
106     permute_sign_and_affix ($frac_rep, $_) foreach @exp_reps;
107 }
108
109 sub permute_sign_and_affix {
110     my ($frac_rep, $exp_rep) = @_;
111     for my $prefix (pick ('', '$'),
112                     pick ('-', '-$', '$-', '$-$'),
113                     pick ('+', '+$', '$+', '$+$')) {
114         for my $suffix ('', '%') {
115             permute_spaces ("$prefix$frac_rep$exp_rep$suffix");
116         }
117     }
118 }
119
120 sub permute_spaces {
121     my ($s) = @_;
122     $s =~ s/([-+\$e%])/ $1 /g;
123     my (@fields) = split (' ', $s);
124     print join ('', @fields), "\n";
125
126     if ($#fields > 0) {
127         my ($pos) = int (my_rand ($#fields)) + 1;
128         print join ('', @fields[0...$pos - 1]);
129         print " ";
130         print join ('', @fields[$pos...$#fields]);
131         print "\n";
132     }
133 }
134
135 sub pick {
136     return $_[int (my_rand ($#_ + 1))];
137 }
138
139 sub my_rand {
140     my ($modulo) = @_;
141     $next = ($next * 1103515245 + 12345) % (2**32);
142     return int ($next / 65536) % $modulo;
143 }
144 EOF
145
146 activity="generate data"
147 $PERL num-in.pl > num-in.data
148 if [ $? -ne 0 ] ; then no_result ; fi
149 echo -n .
150
151 activity="generate pspp syntax"
152 cat > num-in.pspp <<EOF
153 SET ERRORS=NONE.
154 SET MXERRS=10000000.
155 SET MXWARNS=10000000.
156 DATA LIST FILE='num-in.data' /
157         f 1-40 (f)
158         comma 1-40 (comma)
159         dot 1-40 (dot)
160         dollar 1-40 (dollar)
161         pct 1-40 (pct)
162         e 1-40 (e).
163 PRINT OUTFILE='num-in.out'/all (6f10.4).
164 EXECUTE.
165 EOF
166 if [ $? -ne 0 ] ; then no_result ; fi
167 echo -n .
168
169 activity="run program"
170 $SUPERVISOR $PSPP --testing-mode num-in.pspp
171 if [ $? -ne 0 ] ; then no_result ; fi
172 echo -n .
173
174 activity="gunzip expected results"
175 gzip -cd < $top_srcdir/tests/formats/num-in.expected.gz > num-in.expected
176 if [ $? -ne 0 ] ; then no_result ; fi
177 echo -n .
178
179 activity="compare output"
180 diff -u num-in.expected num-in.out
181 if [ $? -ne 0 ] ; then fail ; fi
182
183 echo .
184
185 pass