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