167286257bdb6c68f20cec526b3f969ee43fc977
[pspp] / tests / language / stats / aggregate.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([AGGREGATE procedure])
17
18 dnl CHECK_AGGREGATE(OUTFILE, SORT, MISSING)
19 dnl
20 dnl Checks the AGGREGATE procedure with the specified combination of:
21 dnl
22 dnl - OUTFILE: One of "dataset", "active", or "external" according to
23 dnl   where AGGREGATE's output should be directed.
24 dnl 
25 dnl - SORT: Either "presorted" or "unsorted" according to whether
26 dnl   AGGREGATE should received presorted input.
27 dnl
28 dnl - MISSING: Either "itemwise" or "columnwise" according to the basis
29 dnl   on which missing values should be eliminated.
30 dnl
31 m4_define([CHECK_AGGREGATE], [
32   AT_SETUP([AGGREGATE $2 data to $1 file, $3 missing])
33   AT_DATA([aggregate.data],
34   [2 42
35 1001
36 4 41
37 3112
38 1112
39 2661
40 1221
41 2771
42 1331
43 1441
44 2881
45 1551
46 ])
47   AT_DATA([aggregate.sps],
48     [DATA LIST NOTABLE FILE='aggregate.data' /G N 1-2 S 3(a) W 4.
49 WEIGHT BY w.
50 MISSING VALUES n(4) s('4').
51 m4_if([$1], [dataset], [DATASET DECLARE aggregate.])
52 m4_if([$2], [presorted], [SORT CASES BY g.])
53 AGGREGATE dnl
54 m4_if([$1], [active], [OUTFILE=*],
55       [$1], [external], [OUTFILE='aggregate.sys'],
56       [outfile=aggregate]) dnl
57 m4_if([$2], [presorted], [/PRESORTED]) dnl
58 m4_if([$3], [columnwise], [/MISSING=COLUMNWISE])
59         /DOCUMENT
60         /BREAK=g
61         /N = n
62         /NI = n./
63         NU = nu
64         /NUI = nu./
65         NFGT2 = fgt(n, 2)
66         /NFGT2I = fgt.(n, 2)
67         /SFGT2 = fgt(s, '2')
68         /SFGT2I = fgt.(s, '2')
69         /NFIN23 = fin(n, 2, 3)
70         /NFIN23I = fin.(n, 2, 3)
71         /SFIN23 = fin(s, '2', '3')
72         /SFIN23I = fin.(s, '2', '3')
73         /NFLT2 = flt(n, 2)
74         /NFLT2I = flt.(n, 2)
75         /SFLT2 = flt(s, '2')
76         /SFLT2I = flt.(s, '2')
77         /NFIRST = first(n)
78         /NFIRSTI = first.(n)
79         /SFIRST = first(s)
80         /SFIRSTI = first.(s)
81         /NFOUT23 = fout(n, 3, 2)
82         /NFOUT23I = fout.(n, 3, 2)
83         /SFOUT23 = fout(s, '3', '2')
84         /SFOUT23I = fout.(s, '3', '2')
85         /NLAST = last(n)
86         /NLASTI = last.(n)
87         /SLAST = last(s)
88         /SLASTI = last.(s)
89         /NMAX = max(n)
90         /NMAXI = max.(n)
91         /SMAX = max(s)
92         /SMAXI = max.(s)
93         /NMEAN = mean(n)
94         /NMEANI = mean.(n)
95         /NMIN = min(n)
96         /NMINI = min.(n)
97         /SMIN = min(s)
98         /SMINI = min.(s)
99         /NN = n(n)
100         /NNI = n.(n)
101         /SN = n(s)
102         /SNI = n.(s)
103         /NNMISS = nmiss(n)
104         /NNMISSI = nmiss.(n)
105         /SNMISS = nmiss(s)
106         /SNMISSI = nmiss.(s)
107         /NNU = nu(n)
108         /NNUI = nu.(n)
109         /SNU = nu(s)
110         /SNUI = nu.(s)
111         /NNUMISS = numiss(n)
112         /NNUMISSI = numiss.(n)
113         /SNUMISS = numiss(s)
114         /SNUMISSI = numiss.(s)
115         /NPGT2 = pgt(n, 2)
116         /NPGT2I = pgt.(n, 2)
117         /SPGT2 = pgt(s, '2')
118         /SPGT2I = pgt.(s, '2')
119         /NPIN23 = pin(n, 2, 3)
120         /NPIN23I = pin.(n, 2, 3)
121         /SPIN23 = pin(s, '2', '3')
122         /SPIN23I = pin.(s, '2', '3')
123         /NPLT2 = plt(n, 2)
124         /NPLT2I = plt.(n, 2)
125         /SPLT2 = plt(s, '2')
126         /SPLT2I = plt.(s, '2')
127         /NPOUT23 = pout(n, 2, 3)
128         /NPOUT23I = pout.(n, 2, 3)
129         /SPOUT23 = pout(s, '2', '3')
130         /SPOUT23I = pout.(s, '2', '3')
131         /NMEDIAN = median(n)
132         /NMEDIANI = median.(n)
133         /NSD = sd(n)
134         /NSDI = sd.(n)
135         /NSUM = sum(n)
136         /NSUMI = sum.(n).
137 m4_if([$1], [external], [GET FILE='aggregate.sys'.],
138       [$1], [dataset], [DATASET ACTIVATE aggregate.])
139 LIST.
140 ])
141   AT_CHECK([pspp -O format=csv aggregate.sps], [0], [stdout])
142   AT_CHECK([[sed 's/^[^:]*:[0-9]*: //' < stdout]], [0],
143     [m4_if([$3], [itemwise],
144       [warning: AGGREGATE: The value arguments passed to the FOUT function are out-of-order.  They will be treated as if they had been specified in the correct order.
145
146 warning: AGGREGATE: The value arguments passed to the FOUT function are out-of-order.  They will be treated as if they had been specified in the correct order.
147
148 warning: AGGREGATE: The value arguments passed to the FOUT function are out-of-order.  They will be treated as if they had been specified in the correct order.
149
150 warning: AGGREGATE: The value arguments passed to the FOUT function are out-of-order.  They will be treated as if they had been specified in the correct order.
151
152 Table: Data List
153 G,N,NI,NU,NUI,NFGT2,NFGT2I,SFGT2,SFGT2I,NFIN23,NFIN23I,SFIN23,SFIN23I,NFLT2,NFLT2I,SFLT2,SFLT2I,NFIRST,NFIRSTI,SFIRST,SFIRSTI,NFOUT23,NFOUT23I,SFOUT23,SFOUT23I,NLAST,NLASTI,SLAST,SLASTI,NMAX,NMAXI,SMAX,SMAXI,NMEAN,NMEANI,NMIN,NMINI,SMIN,SMINI,NN,NNI,SN,SNI,NNMISS,NNMISSI,SNMISS,SNMISSI,NNU,NNUI,SNU,SNUI,NNUMISS,NNUMISSI,SNUMISS,SNUMISSI,NPGT2,NPGT2I,SPGT2,SPGT2I,NPIN23,NPIN23I,SPIN23,SPIN23I,NPLT2,NPLT2I,SPLT2,SPLT2I,NPOUT23,NPOUT23I,SPOUT23,SPOUT23I,NMEDIAN,NMEDIANI,NSD,NSDI,NSUM,NSUMI
154 1,7.00,7.00,6,6,.333,.429,.333,.429,.333,.286,.333,.286,.500,.429,.500,.429,0,0,0,0,.667,.714,.667,.714,5,5,5,5,5,5,5,5,2.00,2.29,0,0,0,0,6.00,7.00,6.00,7.00,1.00,.00,1.00,.00,5,6,5,6,1,0,1,0,33.3,42.9,33.3,42.9,33.3,28.6,33.3,28.6,50.0,42.9,50.0,42.9,66.7,71.4,66.7,71.4,1.50,2.00,1.79,1.80,12.00,16.00
155 2,5.00,5.00,4,4,1.000,1.000,1.000,1.000,.000,.000,.000,.000,.000,.000,.000,.000,6,6,6,4,1.000,1.000,1.000,1.000,8,8,8,8,8,8,8,8,7.00,7.00,6,6,6,4,3.00,3.00,3.00,5.00,2.00,2.00,2.00,.00,3,3,3,4,1,1,1,0,100.0,100.0,100.0,100.0,.0,.0,.0,.0,.0,.0,.0,.0,100.0,100.0,100.0,100.0,7.00,7.00,1.00,1.00,21.00,21.00
156 3,2.00,2.00,1,1,.000,.000,.000,.000,.000,.000,.000,.000,1.000,1.000,1.000,1.000,1,1,1,1,1.000,1.000,1.000,1.000,1,1,1,1,1,1,1,1,1.00,1.00,1,1,1,1,2.00,2.00,2.00,2.00,.00,.00,.00,.00,1,1,1,1,0,0,0,0,.0,.0,.0,.0,.0,.0,.0,.0,100.0,100.0,100.0,100.0,100.0,100.0,100.0,100.0,1.00,1.00,.00,.00,2.00,2.00
157 4,1.00,1.00,1,1,.   ,.   ,.   ,1.000,.   ,.   ,.   ,.000,.   ,.   ,.   ,.000,.,.,,4,.   ,.   ,.   ,1.000,.,.,,4,.,.,,4,.  ,.  ,.,.,,4,.00,.00,.00,1.00,1.00,1.00,1.00,.00,0,0,0,1,1,1,1,0,. ,. ,. ,100.0,. ,. ,. ,.0,. ,. ,. ,.0,. ,. ,. ,100.0,NaN,NaN,.  ,.  ,.  ,.  @&t@
158 ],
159       [warning: AGGREGATE: The value arguments passed to the FOUT function are out-of-order.  They will be treated as if they had been specified in the correct order.
160
161 warning: AGGREGATE: The value arguments passed to the FOUT function are out-of-order.  They will be treated as if they had been specified in the correct order.
162
163 warning: AGGREGATE: The value arguments passed to the FOUT function are out-of-order.  They will be treated as if they had been specified in the correct order.
164
165 warning: AGGREGATE: The value arguments passed to the FOUT function are out-of-order.  They will be treated as if they had been specified in the correct order.
166
167 Table: Data List
168 G,N,NI,NU,NUI,NFGT2,NFGT2I,SFGT2,SFGT2I,NFIN23,NFIN23I,SFIN23,SFIN23I,NFLT2,NFLT2I,SFLT2,SFLT2I,NFIRST,NFIRSTI,SFIRST,SFIRSTI,NFOUT23,NFOUT23I,SFOUT23,SFOUT23I,NLAST,NLASTI,SLAST,SLASTI,NMAX,NMAXI,SMAX,SMAXI,NMEAN,NMEANI,NMIN,NMINI,SMIN,SMINI,NN,NNI,SN,SNI,NNMISS,NNMISSI,SNMISS,SNMISSI,NNU,NNUI,SNU,SNUI,NNUMISS,NNUMISSI,SNUMISS,SNUMISSI,NPGT2,NPGT2I,SPGT2,SPGT2I,NPIN23,NPIN23I,SPIN23,SPIN23I,NPLT2,NPLT2I,SPLT2,SPLT2I,NPOUT23,NPOUT23I,SPOUT23,SPOUT23I,NMEDIAN,NMEDIANI,NSD,NSDI,NSUM,NSUMI
169 1,7.00,7.00,6,6,.   ,.429,.   ,.429,.   ,.286,.   ,.286,.   ,.429,.   ,.429,.,0,,0,.   ,.714,.   ,.714,.,5,,5,.,5,,5,.  ,2.29,.,0,,0,6.00,7.00,6.00,7.00,1.00,.00,1.00,.00,5,6,5,6,1,0,1,0,. ,42.9,. ,42.9,. ,28.6,. ,28.6,. ,42.9,. ,42.9,. ,71.4,. ,71.4,.  ,2.00,.  ,1.80,.  ,16.00
170 2,5.00,5.00,4,4,.   ,.   ,.   ,1.000,.   ,.   ,.   ,.000,.   ,.   ,.   ,.000,.,.,,4,.   ,.   ,.   ,1.000,.,.,,8,.,.,,8,.  ,.  ,.,.,,4,3.00,3.00,3.00,5.00,2.00,2.00,2.00,.00,3,3,3,4,1,1,1,0,. ,. ,. ,100.0,. ,. ,. ,.0,. ,. ,. ,.0,. ,. ,. ,100.0,.  ,.  ,.  ,.  ,.  ,.  @&t@
171 3,2.00,2.00,1,1,.000,.000,.000,.000,.000,.000,.000,.000,1.000,1.000,1.000,1.000,1,1,1,1,1.000,1.000,1.000,1.000,1,1,1,1,1,1,1,1,1.00,1.00,1,1,1,1,2.00,2.00,2.00,2.00,.00,.00,.00,.00,1,1,1,1,0,0,0,0,.0,.0,.0,.0,.0,.0,.0,.0,100.0,100.0,100.0,100.0,100.0,100.0,100.0,100.0,1.00,1.00,.00,.00,2.00,2.00
172 4,1.00,1.00,1,1,.   ,.   ,.   ,1.000,.   ,.   ,.   ,.000,.   ,.   ,.   ,.000,.,.,,4,.   ,.   ,.   ,1.000,.,.,,4,.,.,,4,.  ,.  ,.,.,,4,.00,.00,.00,1.00,1.00,1.00,1.00,.00,0,0,0,1,1,1,1,0,. ,. ,. ,100.0,. ,. ,. ,.0,. ,. ,. ,.0,. ,. ,. ,100.0,.  ,.  ,.  ,.  ,.  ,.  @&t@
173 ])])
174   AT_CLEANUP])
175
176 CHECK_AGGREGATE([dataset], [presorted], [itemwise])
177 CHECK_AGGREGATE([dataset], [presorted], [columnwise])
178 CHECK_AGGREGATE([dataset], [unsorted], [itemwise])
179 CHECK_AGGREGATE([dataset], [unsorted], [columnwise])
180 CHECK_AGGREGATE([active], [presorted], [itemwise])
181 CHECK_AGGREGATE([active], [presorted], [columnwise])
182 CHECK_AGGREGATE([active], [unsorted], [itemwise])
183 CHECK_AGGREGATE([active], [unsorted], [columnwise])
184 CHECK_AGGREGATE([external], [presorted], [itemwise])
185 CHECK_AGGREGATE([external], [presorted], [columnwise])
186 CHECK_AGGREGATE([external], [unsorted], [itemwise])
187 CHECK_AGGREGATE([external], [unsorted], [columnwise])
188
189 AT_SETUP([AGGREGATE crash with MAX function])
190 AT_DATA([aggregate.sps], 
191   [DATA LIST LIST /X (F8.2) Y (a25).
192
193 BEGIN DATA.
194 87.50 foo
195 87.34 bar
196 1 bar
197 END DATA.
198
199 AGGREGATE OUTFILE=* /BREAK=y /X=MAX(x).
200 LIST /x y.
201 ])
202 AT_CHECK([pspp -O format=csv aggregate.sps], [0],
203   [Table: Reading free-form data from INLINE.
204 Variable,Format
205 X,F8.2
206 Y,A25
207
208 Table: Data List
209 X,Y
210 87.34,bar                      @&t@
211 87.50,foo                      @&t@
212 ])
213 AT_CLEANUP
214
215 AT_SETUP([AGGREGATE crash with invalid syntax])
216 AT_DATA([aggregate.sps],
217   [INPUT PROGRAM.
218 LOOP c=1 TO 20.
219   COMPUTE x=UNIFORM(10)
220   END CASE.
221 END LOOP.
222 END FILE.
223 END INPUT PROGRAM.
224
225 AGGREGATE /BREAK=x .
226 ])
227 AT_CHECK([pspp -O format=csv aggregate.sps], [1], [ignore], [])
228 AT_CLEANUP
229
230
231 AT_SETUP([AGGREGATE mode=addvariables])
232 AT_DATA([addvariables.sps],
233   [data list notable list /x * cn * y *.
234 begin data.
235 1 1 2
236 3 2 3
237 3 3 4
238 5 4 6
239 7 5 8
240 7 6 9
241 7 7 20
242 9 8 11
243 end data.
244
245 aggregate outfile=* mode=addvariables
246         /break = x
247         /sum = sum(y)
248         /mean = mean (y)
249         /median = median (y).
250
251 list.
252 ])
253
254 AT_CHECK([pspp -O format=csv addvariables.sps], [0],
255   [Table: Data List
256 x,cn,y,sum,mean,median
257 1.00,1.00,2.00,2.00,2.00,2.00
258 3.00,2.00,3.00,7.00,3.50,3.50
259 3.00,3.00,4.00,7.00,3.50,3.50
260 5.00,4.00,6.00,6.00,6.00,6.00
261 7.00,5.00,8.00,37.00,12.33,9.00
262 7.00,6.00,9.00,37.00,12.33,9.00
263 7.00,7.00,20.00,37.00,12.33,9.00
264 9.00,8.00,11.00,11.00,11.00,11.00
265 ])
266
267 AT_CLEANUP
268
269
270 AT_SETUP([AGGREGATE buggy duplicate variables])
271 dnl Test for a bug which crashed when duplicated
272 dnl variables were attempted.
273 AT_DATA([dup-variables.sps],
274   [DATA LIST NOTABLE LIST /x * .
275 begin data
276 1
277 1
278 1
279 1
280 2
281 2
282 2
283 3
284 3
285 3
286 3
287 3
288 3
289 end data.
290
291 AGGREGATE OUTFILE=* MODE=ADDVARIABLES
292         /BREAK= x
293         /N_BREAK = N.
294
295 AGGREGATE OUTFILE=* MODE=ADDVARIABLES
296         /BREAK= x
297         /N_BREAK = N.
298 ])
299
300 AT_CHECK([pspp -O format=csv dup-variables.sps], [1],
301 ["dup-variables.sps:24: error: AGGREGATE: Variable name N_BREAK is not unique within the aggregate file dictionary, which contains the aggregate variables and the break variables."
302 ])
303
304 AT_CLEANUP