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