60baac7e9c785af928ebbbc4488524008dba2e6b
[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
42 2.00 @&t@
43
44 --------
45
46 2.00 @&t@
47
48 4.00 @&t@
49
50 --------
51
52 3.00 @&t@
53
54 6.00 @&t@
55
56 9.00 @&t@
57
58 --------
59
60 --------
61 ])
62 AT_CLEANUP
63
64 AT_SETUP([LOOP with IF condition])
65 AT_DATA([loop.sps], [dnl
66 LOOP_DATA
67 compute #j=x.
68 loop if #j <= y.
69 print /#j.
70 compute #j = #j + z.
71 end loop.
72 print/'--------'.
73 execute.
74 ])
75 AT_CHECK([pspp -o pspp.csv loop.sps])
76 AT_CHECK([cat pspp.csv], [0], [dnl
77 1.00 @&t@
78
79 2.00 @&t@
80
81 --------
82
83 2.00 @&t@
84
85 4.00 @&t@
86
87 --------
88
89 3.00 @&t@
90
91 6.00 @&t@
92
93 9.00 @&t@
94
95 --------
96
97 --------
98 ])
99 AT_CLEANUP
100
101 AT_SETUP([LOOP with END IF condition])
102 AT_DATA([loop.sps], [dnl
103 LOOP_DATA
104 compute #k=x.
105 loop.
106 print /#k.
107 compute #k = #k + z.
108 end loop if #k > y.
109 print/'--------'.
110 execute.
111 ])
112 AT_CHECK([pspp -o pspp.csv loop.sps])
113 AT_CHECK([cat pspp.csv], [0], [dnl
114 1.00 @&t@
115
116 2.00 @&t@
117
118 --------
119
120 2.00 @&t@
121
122 4.00 @&t@
123
124 --------
125
126 3.00 @&t@
127
128 6.00 @&t@
129
130 9.00 @&t@
131
132 --------
133
134 4.00 @&t@
135
136 --------
137 ])
138 AT_CLEANUP
139
140 AT_SETUP([LOOP with index and IF based on index])
141 AT_DATA([loop.sps], [dnl
142 LOOP_DATA
143 loop #m=x to y by z if #m < 4.
144 print /#m.
145 end loop.
146 print/'--------'.
147 execute.
148 ])
149 AT_CHECK([pspp -o pspp.csv loop.sps])
150 AT_CHECK([cat pspp.csv], [0], [dnl
151 1.00 @&t@
152
153 2.00 @&t@
154
155 --------
156
157 2.00 @&t@
158
159 --------
160
161 3.00 @&t@
162
163 --------
164
165 --------
166 ])
167 AT_CLEANUP
168
169 AT_SETUP([LOOP with index and END IF based on index])
170 AT_DATA([loop.sps], [dnl
171 LOOP_DATA
172 loop #n=x to y by z.
173 print /#n.
174 end loop if #n >= 4.
175 print/'--------'.
176 execute.
177 ])
178 AT_CHECK([pspp -o pspp.csv loop.sps])
179 AT_CHECK([cat pspp.csv], [0], [dnl
180 1.00 @&t@
181
182 2.00 @&t@
183
184 --------
185
186 2.00 @&t@
187
188 4.00 @&t@
189
190 --------
191
192 3.00 @&t@
193
194 6.00 @&t@
195
196 --------
197
198 --------
199 ])
200 AT_CLEANUP
201
202 AT_SETUP([LOOP with index and IF and END IF based on index])
203 AT_DATA([loop.sps], [dnl
204 LOOP_DATA
205 loop #o=x to y by z if mod(#o,2) = 0.
206 print /#o.
207 end loop if #o >= 4.
208 print/'--------'.
209 execute.
210 ])
211 AT_CHECK([pspp -o pspp.csv loop.sps])
212 AT_CHECK([cat pspp.csv], [0], [dnl
213 --------
214
215 2.00 @&t@
216
217 4.00 @&t@
218
219 --------
220
221 --------
222
223 --------
224 ])
225 AT_CLEANUP
226
227 AT_SETUP([LOOP with no conditions containing BREAK])
228 AT_DATA([loop.sps], [dnl
229 LOOP_DATA
230 compute #p = x.
231 loop.
232 print /#p.
233 compute #p = #p + z.
234 do if #p >= y.
235 break.
236 end if.
237 end loop.
238 print/'--------'.
239 execute.
240 ])
241 AT_CHECK([pspp -o pspp.csv loop.sps])
242 AT_CHECK([cat pspp.csv], [0], [dnl
243 1.00 @&t@
244
245 --------
246
247 2.00 @&t@
248
249 4.00 @&t@
250
251 --------
252
253 3.00 @&t@
254
255 6.00 @&t@
256
257 --------
258
259 4.00 @&t@
260
261 --------
262 ])
263 AT_CLEANUP
264
265 AT_SETUP([LOOP with no conditions that ends due to MXLOOPS])
266 AT_DATA([loop.sps], [dnl
267 LOOP_DATA
268 set mxloops=2.
269 loop.
270 compute #p = #p + 1.
271 print /x #p.
272 end loop.
273 print/'--------'.
274 execute.
275 ])
276 AT_CHECK([pspp -o pspp.csv loop.sps])
277 AT_CHECK([cat pspp.csv], [0], [dnl
278 1     1.00 @&t@
279
280 1     2.00 @&t@
281
282 --------
283
284 2     3.00 @&t@
285
286 2     4.00 @&t@
287
288 --------
289
290 3     5.00 @&t@
291
292 3     6.00 @&t@
293
294 --------
295
296 4     7.00 @&t@
297
298 4     8.00 @&t@
299
300 --------
301 ])
302 AT_CLEANUP
303