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