Clean up how transformations work.
[pspp] / tests / language / control / loop.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([LOOP])
18
19 m4_define([LOOP_DATA], [dnl
20 data list notable /x 1 y 2 z 3.
21 begin data.
22 121
23 252
24 393
25 404
26 end data.
27 ])
28
29 AT_SETUP([LOOP with index])
30 AT_DATA([loop.sps], [dnl
31 LOOP_DATA
32 loop #i=x to y by z.
33 print /#i.
34 end loop.
35 print/'--------'.
36 execute.
37 ])
38 AT_CHECK([pspp -o pspp.csv loop.sps])
39 AT_CHECK([cat pspp.csv], [0], [dnl
40 1.00 @&t@
41 2.00 @&t@
42 --------
43 2.00 @&t@
44 4.00 @&t@
45 --------
46 3.00 @&t@
47 6.00 @&t@
48 9.00 @&t@
49 --------
50 --------
51 ])
52 AT_CLEANUP
53
54 AT_SETUP([LOOP with IF condition])
55 AT_DATA([loop.sps], [dnl
56 LOOP_DATA
57 compute #j=x.
58 loop if #j <= y.
59 print /#j.
60 compute #j = #j + z.
61 end loop.
62 print/'--------'.
63 execute.
64 ])
65 AT_CHECK([pspp -o pspp.csv loop.sps])
66 AT_CHECK([cat pspp.csv], [0], [dnl
67 1.00 @&t@
68 2.00 @&t@
69 --------
70 2.00 @&t@
71 4.00 @&t@
72 --------
73 3.00 @&t@
74 6.00 @&t@
75 9.00 @&t@
76 --------
77 --------
78 ])
79 AT_CLEANUP
80
81 AT_SETUP([LOOP with END IF condition])
82 AT_DATA([loop.sps], [dnl
83 LOOP_DATA
84 compute #k=x.
85 loop.
86 print /#k.
87 compute #k = #k + z.
88 end loop if #k > y.
89 print/'--------'.
90 execute.
91 ])
92 AT_CHECK([pspp -o pspp.csv loop.sps])
93 AT_CHECK([cat pspp.csv], [0], [dnl
94 1.00 @&t@
95 2.00 @&t@
96 --------
97 2.00 @&t@
98 4.00 @&t@
99 --------
100 3.00 @&t@
101 6.00 @&t@
102 9.00 @&t@
103 --------
104 4.00 @&t@
105 --------
106 ])
107 AT_CLEANUP
108
109 AT_SETUP([LOOP with index and IF based on index])
110 AT_DATA([loop.sps], [dnl
111 LOOP_DATA
112 loop #m=x to y by z if #m < 4.
113 print /#m.
114 end loop.
115 print/'--------'.
116 execute.
117 ])
118 AT_CHECK([pspp -o pspp.csv loop.sps])
119 AT_CHECK([cat pspp.csv], [0], [dnl
120 1.00 @&t@
121 2.00 @&t@
122 --------
123 2.00 @&t@
124 --------
125 3.00 @&t@
126 --------
127 --------
128 ])
129 AT_CLEANUP
130
131 AT_SETUP([LOOP with index and END IF based on index])
132 AT_DATA([loop.sps], [dnl
133 LOOP_DATA
134 loop #n=x to y by z.
135 print /#n.
136 end loop if #n >= 4.
137 print/'--------'.
138 execute.
139 ])
140 AT_CHECK([pspp -o pspp.csv loop.sps])
141 AT_CHECK([cat pspp.csv], [0], [dnl
142 1.00 @&t@
143 2.00 @&t@
144 --------
145 2.00 @&t@
146 4.00 @&t@
147 --------
148 3.00 @&t@
149 6.00 @&t@
150 --------
151 --------
152 ])
153 AT_CLEANUP
154
155 AT_SETUP([LOOP with index and IF and END IF based on index])
156 AT_DATA([loop.sps], [dnl
157 LOOP_DATA
158 loop #o=x to y by z if mod(#o,2) = 0.
159 print /#o.
160 end loop if #o >= 4.
161 print/'--------'.
162 execute.
163 ])
164 AT_CHECK([pspp -o pspp.csv loop.sps])
165 AT_CHECK([cat pspp.csv], [0], [dnl
166 --------
167 2.00 @&t@
168 4.00 @&t@
169 --------
170 --------
171 --------
172 ])
173 AT_CLEANUP
174
175 AT_SETUP([LOOP with no conditions containing BREAK])
176 AT_DATA([loop.sps], [dnl
177 LOOP_DATA
178 compute #p = x.
179 loop.
180 print /#p.
181 compute #p = #p + z.
182 do if #p >= y.
183 break.
184 end if.
185 end loop.
186 print/'--------'.
187 execute.
188 ])
189 AT_CHECK([pspp -o pspp.csv loop.sps])
190 AT_CHECK([cat pspp.csv], [0], [dnl
191 1.00 @&t@
192 --------
193 2.00 @&t@
194 4.00 @&t@
195 --------
196 3.00 @&t@
197 6.00 @&t@
198 --------
199 4.00 @&t@
200 --------
201 ])
202 AT_CLEANUP
203
204 AT_SETUP([LOOP with no conditions that ends due to MXLOOPS])
205 AT_DATA([loop.sps], [dnl
206 LOOP_DATA
207 set mxloops=2.
208 loop.
209 compute #p = #p + 1.
210 print /x #p.
211 end loop.
212 print/'--------'.
213 execute.
214 ])
215 AT_CHECK([pspp -o pspp.csv loop.sps])
216 AT_CHECK([cat pspp.csv], [0], [dnl
217 1     1.00 @&t@
218 1     2.00 @&t@
219 --------
220 2     3.00 @&t@
221 2     4.00 @&t@
222 --------
223 3     5.00 @&t@
224 3     6.00 @&t@
225 --------
226 4     7.00 @&t@
227 4     8.00 @&t@
228 --------
229 ])
230 AT_CLEANUP
231
232 AT_SETUP([LOOP with IF condition that ends due to MXLOOPS])
233 AT_DATA([loop.sps], [dnl
234 LOOP_DATA
235 set mxloops=3.
236 compute #p = x.
237 loop.
238 print /x #p.
239 compute #p = #p + 1.
240 end loop if #p >= 6.
241 print/'--------'.
242 execute.
243 ])
244 AT_CHECK([pspp -o pspp.csv loop.sps])
245 AT_CHECK([cat pspp.csv], [0], [dnl
246 1     1.00 @&t@
247 1     2.00 @&t@
248 1     3.00 @&t@
249 --------
250 2     2.00 @&t@
251 2     3.00 @&t@
252 2     4.00 @&t@
253 --------
254 3     3.00 @&t@
255 3     4.00 @&t@
256 3     5.00 @&t@
257 --------
258 4     4.00 @&t@
259 4     5.00 @&t@
260 --------
261 ])
262 AT_CLEANUP
263
264 AT_SETUP([LOOP negative])
265 AT_DATA([loop.sps], [dnl
266 DATA LIST NOTABLE /x 1.
267 BREAK.
268 END LOOP.
269
270 LOOP A=1 TO 5 B=1 TO 5.
271 END LOOP.
272 LOOP 5.
273 END LOOP.
274 LOOP B !.
275 END LOOP.
276 LOOP B=!.
277 END LOOP.
278 LOOP A=1 TO !.
279 END LOOP.
280 LOOP A=1 BY !.
281 END LOOP.
282 LOOP A=1 TO 5 BY 5 TO !.
283 END LOOP.
284 LOOP A=1 BY 5 TO 10 BY !.
285 END LOOP.
286 LOOP A=1.
287 END LOOP.
288 LOOP !.
289 END LOOP.
290
291 LOOP IF 1 IF 0.
292 END LOOP.
293
294 LOOP IF !.
295 END LOOP.
296
297 LOOP.
298 END LOOP IF !.
299
300 LOOP.
301 END LOOP !.
302
303 LOOP.
304 ])
305 AT_CHECK([pspp loop.sps], 1, [dnl
306 loop.sps:2: error: BREAK: This command cannot appear outside LOOP...END LOOP.
307
308 loop.sps:3: error: END LOOP: This command cannot appear outside LOOP...END
309 LOOP.
310
311 loop.sps:5: error: LOOP: Only one index clause may be specified.
312
313 loop.sps:7.6: error: LOOP: Syntax error at `5'.
314
315 loop.sps:9.8: error: LOOP: Syntax error at `!': expecting `='.
316
317 loop.sps:11.8: error: LOOP: Syntax error at `!'.
318
319 loop.sps:13.13: error: LOOP: Syntax error at `!'.
320
321 loop.sps:15.13: error: LOOP: Syntax error at `!'.
322
323 loop.sps:17: error: LOOP: Subcommand TO may only be specified once.
324
325 loop.sps:19: error: LOOP: Subcommand BY may only be specified once.
326
327 loop.sps:21: error: LOOP: Required subcommand TO was not specified.
328
329 loop.sps:23.6: error: LOOP: Syntax error at `!'.
330
331 loop.sps:26: error: LOOP: Subcommand IF may only be specified once.
332
333 loop.sps:29.9: error: LOOP: Syntax error at `!'.
334
335 loop.sps:33.13: error: LOOP: Syntax error at `!'.
336
337 loop.sps:36.10: error: LOOP: Syntax error at `!': expecting end of command.
338
339 error: LOOP: Syntax error at end of input.
340 ])
341 AT_CLEANUP