4ca00a6aa72822c53b98bd597c282bde3f7eb2ff
[pspp] / tests / language / data-io / data-reader.at
1 dnl PSPP - a program for statistical analysis.
2 dnl Copyright (C) 2017 Free Software Foundation, Inc.
3 dnl 
4 dnl This program is free software: you can redistribute it and/or modify
5 dnl it under the terms of the GNU General Public License as published by
6 dnl the Free Software Foundation, either version 3 of the License, or
7 dnl (at your option) any later version.
8 dnl 
9 dnl This program is distributed in the hope that it will be useful,
10 dnl but WITHOUT ANY WARRANTY; without even the implied warranty of
11 dnl MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 dnl GNU General Public License for more details.
13 dnl 
14 dnl You should have received a copy of the GNU General Public License
15 dnl along with this program.  If not, see <http://www.gnu.org/licenses/>.
16 dnl
17 AT_BANNER([BEGIN DATA])
18
19 # BEGIN DATA can run as a command in itself, or it can appear as part
20 # of the first procedure.  First, test it after a procedure.
21 AT_SETUP([BEGIN DATA as part of a procedure])
22 AT_DATA([begin-data.sps], [dnl
23 TITLE 'Test BEGIN DATA ... END DATA'.
24
25 DATA LIST /a b 1-2.
26 LIST.
27 BEGIN DATA.
28 12
29 34
30 56
31 78
32 90
33 END DATA.
34 ])
35 AT_CHECK([pspp -O format=csv begin-data.sps], [0], [dnl
36 Title: Test BEGIN DATA ... END DATA
37
38 Table: Reading 1 record from INLINE.
39 Variable,Record,Columns,Format
40 a,1,1-  1,F1.0
41 b,1,2-  2,F1.0
42
43 Table: Data List
44 a,b
45 1,2
46 3,4
47 5,6
48 7,8
49 9,0
50 ])
51 AT_CLEANUP
52
53 # Also test BEGIN DATA as an independent command.
54 AT_SETUP([BEGIN DATA as an independent command])
55 AT_DATA([begin-data.sps], [dnl
56 data list /A B 1-2.
57 begin data.
58 09
59 87
60 65
61 43
62 21
63 end data.
64 list.
65 ])
66 AT_CHECK([pspp -O format=csv begin-data.sps], [0], [dnl
67 Table: Reading 1 record from INLINE.
68 Variable,Record,Columns,Format
69 A,1,1-  1,F1.0
70 B,1,2-  2,F1.0
71
72 Table: Data List
73 A,B
74 0,9
75 8,7
76 6,5
77 4,3
78 2,1
79 ])
80 AT_CLEANUP
81
82 m4_define([DATA_READER_BINARY], 
83   [AT_SETUP([read and write files with $1])
84 $3
85    AT_DATA([input.txt], [dnl
86 07-22-2007
87 10-06-2007
88 321
89 07-14-1789
90 08-26-1789
91 4
92 01-01-1972
93 12-31-1999
94 682
95 ])
96    AT_DATA([make-binary.pl], [dnl
97 use strict;
98 use warnings;
99
100 # ASCII to EBCDIC translation table
101 our ($ascii2ebcdic) = ""
102 . "\x00\x01\x02\x03\x37\x2d\x2e\x2f"
103 . "\x16\x05\x25\x0b\x0c\x0d\x0e\x0f"
104 . "\x10\x11\x12\x13\x3c\x3d\x32\x26"
105 . "\x18\x19\x3f\x27\x1c\x1d\x1e\x1f"
106 . "\x40\x5a\x7f\x7b\x5b\x6c\x50\x7d"
107 . "\x4d\x5d\x5c\x4e\x6b\x60\x4b\x61"
108 . "\xf0\xf1\xf2\xf3\xf4\xf5\xf6\xf7"
109 . "\xf8\xf9\x7a\x5e\x4c\x7e\x6e\x6f"
110 . "\x7c\xc1\xc2\xc3\xc4\xc5\xc6\xc7"
111 . "\xc8\xc9\xd1\xd2\xd3\xd4\xd5\xd6"
112 . "\xd7\xd8\xd9\xe2\xe3\xe4\xe5\xe6"
113 . "\xe7\xe8\xe9\xad\xe0\xbd\x9a\x6d"
114 . "\x79\x81\x82\x83\x84\x85\x86\x87"
115 . "\x88\x89\x91\x92\x93\x94\x95\x96"
116 . "\x97\x98\x99\xa2\xa3\xa4\xa5\xa6"
117 . "\xa7\xa8\xa9\xc0\x4f\xd0\x5f\x07"
118 . "\x20\x21\x22\x23\x24\x15\x06\x17"
119 . "\x28\x29\x2a\x2b\x2c\x09\x0a\x1b"
120 . "\x30\x31\x1a\x33\x34\x35\x36\x08"
121 . "\x38\x39\x3a\x3b\x04\x14\x3e\xe1"
122 . "\x41\x42\x43\x44\x45\x46\x47\x48"
123 . "\x49\x51\x52\x53\x54\x55\x56\x57"
124 . "\x58\x59\x62\x63\x64\x65\x66\x67"
125 . "\x68\x69\x70\x71\x72\x73\x74\x75"
126 . "\x76\x77\x78\x80\x8a\x8b\x8c\x8d"
127 . "\x8e\x8f\x90\x6a\x9b\x9c\x9d\x9e"
128 . "\x9f\xa0\xaa\xab\xac\x4a\xae\xaf"
129 . "\xb0\xb1\xb2\xb3\xb4\xb5\xb6\xb7"
130 . "\xb8\xb9\xba\xbb\xbc\xa1\xbe\xbf"
131 . "\xca\xcb\xcc\xcd\xce\xcf\xda\xdb"
132 . "\xdc\xdd\xde\xdf\xea\xeb\xec\xed"
133 . "\xee\xef\xfa\xfb\xfc\xfd\xfe\xff";
134 length ($ascii2ebcdic) == 256 || die;
135
136 open (INPUT, '<', 'input.txt') or die "input.txt: open: $!\n";
137 my (@data) = <INPUT> or die;
138 close (INPUT) or die;
139 chomp $_ foreach @data;
140
141 our @records;
142
143 $2
144
145 sub a2e {
146     local ($_) = @_;
147     my ($s) = "";
148     foreach (split (//)) {
149         $s .= substr ($ascii2ebcdic, ord, 1);
150     }
151     return $s;
152 }
153
154 sub min {
155     my ($a, $b) = @_;
156     return $a < $b ? $a : $b
157 }
158
159 sub dump_records {
160     while (@records) {
161         my ($n) = min (int (rand (5)) + 1, scalar (@records));
162         my (@r) = splice (@records, 0, $n);
163         my ($len) = 0;
164         $len += length foreach @r;
165         print pack ("n xx", $len + 4);
166         print foreach @r;
167     }
168 }
169 ])
170    AT_CHECK([$PERL make-binary.pl < input.txt > input.bin])
171    AT_DATA([data-reader.sps], [dnl
172 FILE HANDLE input/NAME='input.bin'/$1.
173 DATA LIST FIXED FILE=input NOTABLE
174         /1 start 1-10 (ADATE)
175         /2 end 1-10 (ADATE)
176         /3 count 1-3.
177 LIST.
178
179 * Output the data to a new file in the same format.
180 FILE HANDLE OUTPUT/NAME='output.bin'/$1.
181 COMPUTE count=count + 1.
182 PRINT OUTFILE=output/start end count.
183 EXECUTE.
184 ])
185    AT_CHECK([pspp -O format=csv data-reader.sps], [0], [dnl
186 Table: Data List
187 start,end,count
188 07/22/2007,10/06/2007,321
189 07/14/1789,08/26/1789,4
190 01/01/1972,12/31/1999,682
191 ])
192    AT_CHECK([test -s output.bin])
193    AT_DATA([data-reader-2.sps], [dnl
194 * Re-read the new data and list it, to verify that it was written correctly.
195 FILE HANDLE OUTPUT/NAME='output.bin'/$1.
196 DATA LIST FIXED FILE=output NOTABLE/
197         start 2-11 (ADATE)
198         end 13-22 (ADATE)
199         count 24-26.
200 LIST.
201 ])
202    AT_CHECK([pspp -O format=csv data-reader-2.sps], [0], [dnl
203 Table: Data List
204 start,end,count
205 07/22/2007,10/06/2007,322
206 07/14/1789,08/26/1789,5
207 01/01/1972,12/31/1999,683
208 ])
209    AT_CLEANUP])
210
211 DATA_READER_BINARY([MODE=BINARY],
212   [for $_ (@data) {
213        my ($reclen) = pack ("V", length);
214        print $reclen, $_, $reclen;
215    }])
216
217 DATA_READER_BINARY([MODE=360 /RECFORM=FIXED /LRECL=32],
218   [my ($lrecl) = 32;
219    for $_ (@data) {
220        my ($out) = substr ($_, 0, $lrecl);
221        $out .= ' ' x ($lrecl - length ($out));
222        length ($out) == 32 or die;
223        print +a2e ($out);
224    }],
225   [AT_CHECK([i18n-test supports_encodings EBCDIC-US])])
226
227 DATA_READER_BINARY([MODE=360 /RECFORM=VARIABLE],
228   [for $_ (@data) {
229        push (@records, pack ("n xx", length ($_) + 4) . a2e ($_));
230    }
231    dump_records ();],
232   [AT_CHECK([i18n-test supports_encodings EBCDIC-US])])
233
234 DATA_READER_BINARY([MODE=360 /RECFORM=SPANNED],
235   [[for my $line (@data) {
236        local ($_) = $line;
237        my (@r);
238        while (length) {
239            my ($n) = min (int (rand (5)), length);
240            push (@r, substr ($_, 0, $n, ''));
241        }
242        foreach my $i (0...$#r) {
243            my $scc = ($#r == 0 ? 0
244                       : $i == 0 ? 1
245                       : $i == $#r ? 2
246                       : 3);
247            push (@records,
248                  pack ("nCx", length ($r[$i]) + 4, $scc) . a2e ($r[$i]));
249        }
250    }
251    dump_records ();]],
252   [AT_CHECK([i18n-test supports_encodings EBCDIC-US])])