DO REPEAT: Accept duplicate names as substitution variables.
[pspp.git] / tests / language / commands / do-repeat.at
blob32c9a0531eeacbb996951698a929738ce4d2057a
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([DO REPEAT])
19 AT_SETUP([DO REPEAT -- simple])
20 AT_DATA([do-repeat.sps], [dnl
21 INPUT PROGRAM.
22 STRING y(A1).
23 DO REPEAT xval = 1 2 3 / yval = 'a' 'b' 'c' / var = a b c.
24 COMPUTE x=xval.
25 COMPUTE y=yval.
26 COMPUTE var=xval.
27 END CASE.
28 END REPEAT PRINT.
29 END FILE.
30 END INPUT PROGRAM.
31 LIST.
33 AT_CHECK([pspp -o pspp.csv do-repeat.sps])
34 AT_CHECK([cat pspp.csv], [0], [dnl
35 COMPUTE x=1.
36 COMPUTE y='a'.
37 COMPUTE a=1.
38 END CASE.
40 COMPUTE x=2.
41 COMPUTE y='b'.
42 COMPUTE b=2.
43 END CASE.
45 COMPUTE x=3.
46 COMPUTE y='c'.
47 COMPUTE c=3.
48 END CASE.
50 Table: Data List
51 y,x,a,b,c
52 a,1.00,1.00,.  ,.  @&t@
53 b,2.00,.  ,2.00,.  @&t@
54 c,3.00,.  ,.  ,3.00
56 AT_CLEANUP
58 AT_SETUP([DO REPEAT -- containing BEGIN DATA])
59 AT_DATA([do-repeat.sps], [dnl
60 DO REPEAT offset = 1 2 3.
61 DATA LIST NOTABLE /x 1-2.
62 BEGIN DATA.
66 END DATA.
67 COMPUTE x = x + offset.
68 LIST.
69 END REPEAT.
71 AT_CHECK([pspp -o pspp.csv do-repeat.sps])
72 AT_CHECK([cat pspp.csv], [0], [dnl
73 Table: Data List
79 Table: Data List
85 Table: Data List
91 AT_CLEANUP
93 AT_SETUP([DO REPEAT -- dummy vars not expanded in include files])
94 AT_DATA([include.sps], [dnl
95 COMPUTE y = y + x + 10.
97 AT_DATA([do-repeat.sps], [dnl
98 INPUT PROGRAM.
99 COMPUTE x = 0.
100 COMPUTE y = 0.
101 END CASE.
102 END FILE.
103 END INPUT PROGRAM.
105 DO REPEAT x = 1 2 3.
106 INCLUDE include.sps.
107 END REPEAT.
109 LIST.
111 AT_CHECK([pspp -o pspp.csv do-repeat.sps], [0], [dnl
112 do-repeat.sps:8.11: warning: DO REPEAT: Dummy variable name `x' hides dictionary variable `x'.
113     8 | DO REPEAT x = 1 2 3.
114       |           ^
116 AT_CHECK([cat pspp.csv], [0], [dnl
117 "do-repeat.sps:8.11: warning: DO REPEAT: Dummy variable name `x' hides dictionary variable `x'.
118     8 | DO REPEAT x = 1 2 3.
119       |           ^"
121 Table: Data List
123 .00,30.00
125 AT_CLEANUP
127 AT_SETUP([DO REPEAT -- nested])
128 AT_DATA([do-repeat.sps], [dnl
129 DATA LIST NOTABLE /a 1.
130 BEGIN DATA.
132 END DATA.
134 DO REPEAT h = h0 TO h3 / x = 0 TO 3 / y = 8, 7.5, 6, 5.
135         COMPUTE h = x + y.
136 END REPEAT.
138 VECTOR v(6).
139 COMPUTE #idx = 0.
140 DO REPEAT i = 1 TO 2.
141         DO REPEAT j = 3 TO 5.
142                 COMPUTE #x = i + j.
143                 COMPUTE #idx = #idx + 1.
144                 COMPUTE v(#idx) = #x.
145         END REPEAT.
146 END REPEAT.
148 LIST.
150 AT_CHECK([pspp -o pspp.csv do-repeat.sps])
151 AT_CHECK([cat pspp.csv], [0], [dnl
152 Table: Data List
153 a,h0,h1,h2,h3,v1,v2,v3,v4,v5,v6
154 0,8.00,8.50,8.00,8.00,4.00,5.00,6.00,5.00,6.00,7.00
156 AT_CLEANUP
158 dnl This program tests for a bug that crashed PSPP given an empty DO
159 dnl REPEAT...END REPEAT block.  See bug #18407.
160 AT_SETUP([DO REPEAT -- empty])
161 AT_DATA([do-repeat.sps], [dnl
162 DATA LIST NOTABLE /a 1.
163 BEGIN DATA.
165 END DATA.
167 DO REPEAT h = a.
168 END REPEAT.
170 AT_CHECK([pspp -o pspp.csv do-repeat.sps])
171 AT_CHECK([cat pspp.csv], [0], [dnl
173 AT_CLEANUP
175 dnl This program tests for a bug that crashed PSPP when END REPEAT
176 dnl was missing.  See bug #31016.
177 AT_SETUP([DO REPEAT -- missing END REPEAT])
178 AT_DATA([do-repeat.sps], [dnl
179 DATA LIST NOTABLE /x 1.
180 DO REPEAT y = 1 TO 10.
182 AT_CHECK([pspp -O format=csv do-repeat.sps], [1], [dnl
183 error: DO REPEAT: At end of input: Syntax error expecting END REPEAT.
185 AT_CLEANUP
187 AT_SETUP([DO REPEAT -- syntax errors])
188 AT_DATA([do-repeat.sps], [dnl
189 DATA LIST LIST NOTABLE /x.
190 DO REPEAT **.
191 END REPEAT.
192 DO REPEAT x **.
193 END REPEAT.
194 DO REPEAT y=1/y=2.
195 END REPEAT.
196 DO REPEAT y=a b c **.
197 END REPEAT.
198 DO REPEAT y=1 2 **.
199 END REPEAT.
200 DO REPEAT y='a' 'b' **.
201 END REPEAT.
202 DO REPEAT y=**.
203 END REPEAT.
204 DO REPEAT y=1 2 3/z=4.
206 AT_DATA([insert.sps], [dnl
207 INSERT FILE='do-repeat.sps' ERROR=IGNORE.
209 AT_CHECK([pspp --testing-mode -O format=csv insert.sps], [1], [dnl
210 "do-repeat.sps:2.11-2.12: error: DO REPEAT: Syntax error expecting identifier.
211     2 | DO REPEAT **.
212       |           ^~"
214 "do-repeat.sps:4.11: warning: DO REPEAT: Dummy variable name `x' hides dictionary variable `x'.
215     4 | DO REPEAT x **.
216       |           ^"
218 "do-repeat.sps:4.13-4.14: error: DO REPEAT: Syntax error expecting `='.
219     4 | DO REPEAT x **.
220       |             ^~"
222 "do-repeat.sps:6.15: error: DO REPEAT: Dummy variable name `y' is given twice.
223     6 | DO REPEAT y=1/y=2.
224       |               ^"
226 "do-repeat.sps:8.19-8.20: error: DO REPEAT: Syntax error expecting `/' or end of command.
227     8 | DO REPEAT y=a b c **.
228       |                   ^~"
230 "do-repeat.sps:10.17-10.18: error: DO REPEAT: Syntax error expecting number.
231    10 | DO REPEAT y=1 2 **.
232       |                 ^~"
234 "do-repeat.sps:12.21-12.22: error: DO REPEAT: Syntax error expecting string.
235    12 | DO REPEAT y='a' 'b' **.
236       |                     ^~"
238 "do-repeat.sps:14.13-14.14: error: DO REPEAT: Syntax error expecting substitution values.
239    14 | DO REPEAT y=**.
240       |             ^~"
242 do-repeat.sps:16: error: DO REPEAT: Each dummy variable must have the same number of substitutions.
244 "do-repeat.sps:16.11-16.17: note: DO REPEAT: Dummy variable y had 3 substitutions.
245    16 | DO REPEAT y=1 2 3/z=4.
246       |           ^~~~~~~"
248 "do-repeat.sps:16.19-16.21: note: DO REPEAT: Dummy variable z had 1 substitution.
249    16 | DO REPEAT y=1 2 3/z=4.
250       |                   ^~~"
252 error: DO REPEAT: At end of input: Syntax error expecting END REPEAT.
254 AT_CLEANUP
256 AT_SETUP([DO REPEAT -- duplicate substitutions])
257 AT_DATA([do-repeat.sps], [dnl
258 DATA LIST LIST NOTABLE / numer1 numer2 denom1.
259 BEGIN DATA
260 30 25 100
261 20 15 100
262 10 40 100
263 20 10 100
264 END DATA.
266 * Check that duplicates are OK for both existing (denom1)
267   and nonexistent (perc1) variables.
268 DO REPEAT n=numer1 numer2
269      /d = denom1 denom1
270      /p = perc1 perc1.
271    COMPUTE p = n / d * 100.
272 END REPEAT PRINT.
274 AT_CHECK([pspp do-repeat.sps], [0], [dnl
275    COMPUTE perc1 = numer1 / denom1 * 100.
277    COMPUTE perc1 = numer2 / denom1 * 100.
279 AT_CLEANUP