* automake.mk: Add new file.
[pspp-builds.git] / tests / formats / 360.sh
1 #! /bin/sh
2
3 # Tests BINARY and 360 data file formats.
4
5 TEMPDIR=/tmp/pspp-tst-$$
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 : ${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
21 cleanup()
22 {
23      cd /
24      rm -rf $TEMPDIR
25 }
26
27
28 fail()
29 {
30     echo $activity
31     echo FAILED
32     cleanup;
33     exit 1;
34 }
35
36
37 no_result()
38 {
39     echo $activity
40     echo NO RESULT;
41     cleanup;
42     exit 2;
43 }
44
45 pass()
46 {
47     cleanup;
48     exit 0;
49 }
50
51 mkdir -p $TEMPDIR
52
53
54
55 cd $TEMPDIR
56
57 activity="create data file"
58 cat > $TEMPDIR/input.data <<EOF
59 07-22-2007
60 10-06-2007
61 321
62 07-14-1789
63 08-26-1789
64 4
65 01-01-1972
66 12-31-1999
67 682
68 EOF
69 if [ $? -ne 0 ] ; then no_result ; fi
70
71 activity="create program to transform data into 360 formats"
72 cat > $TEMPDIR/make-360.pl <<'EOF'
73 use strict;
74 use warnings;
75
76 # ASCII to EBCDIC translation table
77 our ($ascii2ebcdic) = ""
78 . "\x00\x01\x02\x03\x37\x2d\x2e\x2f"
79 . "\x16\x05\x25\x0b\x0c\x0d\x0e\x0f"
80 . "\x10\x11\x12\x13\x3c\x3d\x32\x26"
81 . "\x18\x19\x3f\x27\x1c\x1d\x1e\x1f"
82 . "\x40\x5a\x7f\x7b\x5b\x6c\x50\x7d"
83 . "\x4d\x5d\x5c\x4e\x6b\x60\x4b\x61"
84 . "\xf0\xf1\xf2\xf3\xf4\xf5\xf6\xf7"
85 . "\xf8\xf9\x7a\x5e\x4c\x7e\x6e\x6f"
86 . "\x7c\xc1\xc2\xc3\xc4\xc5\xc6\xc7"
87 . "\xc8\xc9\xd1\xd2\xd3\xd4\xd5\xd6"
88 . "\xd7\xd8\xd9\xe2\xe3\xe4\xe5\xe6"
89 . "\xe7\xe8\xe9\xad\xe0\xbd\x9a\x6d"
90 . "\x79\x81\x82\x83\x84\x85\x86\x87"
91 . "\x88\x89\x91\x92\x93\x94\x95\x96"
92 . "\x97\x98\x99\xa2\xa3\xa4\xa5\xa6"
93 . "\xa7\xa8\xa9\xc0\x4f\xd0\x5f\x07"
94 . "\x20\x21\x22\x23\x24\x15\x06\x17"
95 . "\x28\x29\x2a\x2b\x2c\x09\x0a\x1b"
96 . "\x30\x31\x1a\x33\x34\x35\x36\x08"
97 . "\x38\x39\x3a\x3b\x04\x14\x3e\xe1"
98 . "\x41\x42\x43\x44\x45\x46\x47\x48"
99 . "\x49\x51\x52\x53\x54\x55\x56\x57"
100 . "\x58\x59\x62\x63\x64\x65\x66\x67"
101 . "\x68\x69\x70\x71\x72\x73\x74\x75"
102 . "\x76\x77\x78\x80\x8a\x8b\x8c\x8d"
103 . "\x8e\x8f\x90\x6a\x9b\x9c\x9d\x9e"
104 . "\x9f\xa0\xaa\xab\xac\x4a\xae\xaf"
105 . "\xb0\xb1\xb2\xb3\xb4\xb5\xb6\xb7"
106 . "\xb8\xb9\xba\xbb\xbc\xa1\xbe\xbf"
107 . "\xca\xcb\xcc\xcd\xce\xcf\xda\xdb"
108 . "\xdc\xdd\xde\xdf\xea\xeb\xec\xed"
109 . "\xee\xef\xfa\xfb\xfc\xfd\xfe\xff";
110 length ($ascii2ebcdic) == 256 || die;
111
112 open (INPUT, '<', 'input.data') or die "input.data: open: $!\n";
113 my (@data) = <INPUT> or die;
114 close (INPUT) or die;
115 chomp $_ foreach @data;
116
117 # Binary mode.
118 open (OUTPUT, '>', 'binary.bin') or die "binary.bin: create: $!\n";
119 for $_ (@data) {
120     my ($reclen) = pack ("V", length);
121     print OUTPUT $reclen, $_, $reclen;
122 }
123 close (OUTPUT) or die;
124
125 # Fixed mode.
126 open (OUTPUT, '>', 'fixed.bin') or die "fixed.bin: create: $!\n";
127 my ($lrecl) = 32;
128 for $_ (@data) {
129     my ($out) = substr ($_, 0, $lrecl);
130     $out .= ' ' x ($lrecl - length ($out));
131     length ($out) == 32 or die;
132     print OUTPUT a2e ($out);
133 }
134 close (OUTPUT) or die;
135
136 # Variable mode.
137 open (OUTPUT, '>', 'variable.bin') or die "variable.bin: create: $!\n";
138 our (@records);
139 for $_ (@data) {
140     push (@records, pack ("n xx", length ($_) + 4) . a2e ($_));
141 }
142 dump_records ();
143 close (OUTPUT) or die;
144
145 # Spanned mode.
146 open (OUTPUT, '>', 'spanned.bin') or die "spanned.bin: create: $!\n";
147 for my $line (@data) {
148     local ($_) = $line;
149     my (@r);
150     while (length) {
151         my ($n) = min (int (rand (5)), length);
152         push (@r, substr ($_, 0, $n, ''));
153     }
154     foreach my $i (0...$#r) {
155         my $scc = ($#r == 0 ? 0
156                    : $i == 0 ? 1
157                    : $i == $#r ? 2
158                    : 3);
159         push (@records,
160               pack ("nCx", length ($r[$i]) + 4, $scc) . a2e ($r[$i]));
161     }
162 }
163 dump_records ();
164 close (OUTPUT) or die;
165
166 sub a2e {
167     local ($_) = @_;
168     my ($s) = "";
169     foreach (split (//)) {
170         $s .= substr ($ascii2ebcdic, ord, 1);
171     }
172     return $s;
173 }
174
175 sub min {
176     my ($a, $b) = @_;
177     return $a < $b ? $a : $b
178 }
179
180 sub dump_records {
181     while (@records) {
182         my ($n) = min (int (rand (5)) + 1, scalar (@records));
183         my (@r) = splice (@records, 0, $n);
184         my ($len) = 0;
185         $len += length foreach @r;
186         print OUTPUT pack ("n xx", $len + 4);
187         print OUTPUT foreach @r;
188     }
189 }
190 EOF
191 if [ $? -ne 0 ] ; then no_result ; fi
192
193 activity="running make-360.pl"
194 $PERL make-360.pl
195 if [ $? -ne 0 ] ; then no_result ; fi
196
197 binary_fh='mode=binary'
198 fixed_fh='mode=360 /recform=fixed /lrecl=32'
199 variable_fh='mode=360 /recform=variable'
200 spanned_fh='mode=360 /recform=spanned'
201
202 for type in binary fixed variable spanned; do
203     activity="create $type.pspp"
204     eval fh=\$${type}_fh
205     cat > $type.pspp <<EOF
206 * Read the original file and list its data, to test reading these formats.
207 file handle input/name='$type.bin'/$fh.
208 data list fixed file=input notable
209         /1 start 1-10 (adate)
210         /2 end 1-10 (adate)
211         /3 count 1-3.
212 list.
213
214 * Output the data to a new file in the same format.
215 file handle output/name='${type}2.bin'/$fh.
216 compute count=count + 1.
217 print outfile=output/start end count.
218 execute.
219
220 * Re-read the new data and list it, to verify that it was written correctly.
221 data list fixed file=output notable/
222         start 2-11 (adate)
223         end 13-22 (adate)
224         count 24-26.
225 list.
226
227 EOF
228     if [ $? -ne 0 ] ; then no_result ; fi
229
230     # Make sure that pspp.list isn't left over from another run.
231     rm -f pspp.list
232
233     activity="run $type.pspp"
234     $SUPERVISOR $PSPP --testing-mode $type.pspp
235     if [ $? -ne 0 ] ; then fail ; fi
236
237     activity="compare $type.pspp output"
238     perl -pi -e 's/^\s*$//g' $TEMPDIR/pspp.list
239     diff -b  $TEMPDIR/pspp.list - << EOF
240      start        end count
241 ---------- ---------- -----
242 07/22/2007 10/06/2007   321
243 07/14/1789 08/26/1789     4
244 01/01/1972 12/31/1999   682
245      start        end count
246 ---------- ---------- -----
247 07/22/2007 10/06/2007   322
248 07/14/1789 08/26/1789     5
249 01/01/1972 12/31/1999   683
250 EOF
251     if [ $? -ne 0 ] ; then fail ; fi
252 done
253
254 pass