1dnl PSPP - a program for statistical analysis.
2dnl Copyright (C) 2017 Free Software Foundation, Inc.
3dnl
4dnl This program is free software: you can redistribute it and/or modify
5dnl it under the terms of the GNU General Public License as published by
6dnl the Free Software Foundation, either version 3 of the License, or
7dnl (at your option) any later version.
8dnl
9dnl This program is distributed in the hope that it will be useful,
10dnl but WITHOUT ANY WARRANTY; without even the implied warranty of
11dnl MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12dnl GNU General Public License for more details.
13dnl
14dnl You should have received a copy of the GNU General Public License
15dnl along with this program.  If not, see <http://www.gnu.org/licenses/>.
16dnl
17AT_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.
21AT_SETUP([BEGIN DATA as part of a procedure])
22AT_DATA([begin-data.sps], [dnl
23TITLE 'Test BEGIN DATA ... END DATA'.
24
25DATA LIST /a b 1-2.
26LIST.
27BEGIN DATA.
2812
2934
3056
3178
3290
33END DATA.
34])
35AT_CHECK([pspp -O format=csv begin-data.sps], [0], [dnl
36Table: Reading 1 record from INLINE.
37Variable,Record,Columns,Format
38a,1,1-  1,F1.0
39b,1,2-  2,F1.0
40
41Table: Data List
42a,b
431,2
443,4
455,6
467,8
479,0
48])
49AT_CLEANUP
50
51# Also test BEGIN DATA as an independent command.
52AT_SETUP([BEGIN DATA as an independent command])
53AT_DATA([begin-data.sps], [dnl
54data list /A B 1-2.
55begin data.
5609
5787
5865
5943
6021
61end data.
62list.
63])
64AT_CHECK([pspp -O format=csv begin-data.sps], [0], [dnl
65Table: Reading 1 record from INLINE.
66Variable,Record,Columns,Format
67A,1,1-  1,F1.0
68B,1,2-  2,F1.0
69
70Table: Data List
71A,B
720,9
738,7
746,5
754,3
762,1
77])
78AT_CLEANUP
79
80m4_define([DATA_READER_BINARY],
81  [AT_SETUP([read and write files with $1])
82$3
83   AT_DATA([input.txt], [dnl
8407-22-2007
8510-06-2007
86321
8707-14-1789
8808-26-1789
894
9001-01-1972
9112-31-1999
92682
93])
94   AT_DATA([make-binary.pl], [dnl
95use strict;
96use warnings;
97
98# ASCII to EBCDIC translation table
99our ($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";
132length ($ascii2ebcdic) == 256 || die;
133
134open (INPUT, '<', 'input.txt') or die "input.txt: open: $!\n";
135my (@data) = <INPUT> or die;
136close (INPUT) or die;
137chomp $_ foreach @data;
138
139our @records;
140
141$2
142
143sub a2e {
144    local ($_) = @_;
145    my ($s) = "";
146    foreach (split (//)) {
147        $s .= substr ($ascii2ebcdic, ord, 1);
148    }
149    return $s;
150}
151
152sub min {
153    my ($a, $b) = @_;
154    return $a < $b ? $a : $b
155}
156
157sub 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
170FILE HANDLE input/NAME='input.bin'/$1.
171DATA LIST FIXED FILE=input NOTABLE
172	/1 start 1-10 (ADATE)
173	/2 end 1-10 (ADATE)
174	/3 count 1-3.
175LIST.
176
177* Output the data to a new file in the same format.
178FILE HANDLE OUTPUT/NAME='output.bin'/$1.
179COMPUTE count=count + 1.
180PRINT OUTFILE=output/start end count.
181EXECUTE.
182])
183   AT_CHECK([pspp -O format=csv data-reader.sps], [0], [dnl
184Table: Data List
185start,end,count
18607/22/2007,10/06/2007,321
18707/14/1789,08/26/1789,4
18801/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.
193FILE HANDLE OUTPUT/NAME='output.bin'/$1.
194DATA LIST FIXED FILE=output NOTABLE/
195	start 2-11 (ADATE)
196	end 13-22 (ADATE)
197	count 24-26.
198LIST.
199])
200   AT_CHECK([pspp -O format=csv data-reader-2.sps], [0], [dnl
201Table: Data List
202start,end,count
20307/22/2007,10/06/2007,322
20407/14/1789,08/26/1789,5
20501/01/1972,12/31/1999,683
206])
207   AT_CLEANUP])
208
209DATA_READER_BINARY([MODE=BINARY],
210  [for $_ (@data) {
211       my ($reclen) = pack ("V", length);
212       print $reclen, $_, $reclen;
213   }])
214
215DATA_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
225DATA_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
232DATA_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])])
251