DO REPEAT: Improve error messages and coding style.
[pspp] / tests / language / control / do-repeat.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([DO REPEAT])
18
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.
32 ])
33 AT_CHECK([pspp -o pspp.csv do-repeat.sps])
34 AT_CHECK([cat pspp.csv], [0], [dnl
35 COMPUTE x=3.
36 COMPUTE y='c'.
37 COMPUTE c=3.
38 END CASE.
39
40 COMPUTE x=2.
41 COMPUTE y='b'.
42 COMPUTE b=2.
43 END CASE.
44
45 COMPUTE x=1.
46 COMPUTE y='a'.
47 COMPUTE a=1.
48 END CASE.
49
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
55 ])
56 AT_CLEANUP
57
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.
63 10
64 20
65 30
66 END DATA.
67 COMPUTE x = x + offset.
68 LIST.
69 END REPEAT.
70 ])
71 AT_CHECK([pspp -o pspp.csv do-repeat.sps])
72 AT_CHECK([cat pspp.csv], [0], [dnl
73 Table: Data List
74 x
75 11
76 21
77 31
78
79 Table: Data List
80 x
81 12
82 22
83 32
84
85 Table: Data List
86 x
87 13
88 23
89 33
90 ])
91 AT_CLEANUP
92
93 AT_SETUP([DO REPEAT -- dummy vars not expanded in include files])
94 AT_DATA([include.sps], [dnl
95 COMPUTE y = y + x + 10.
96 ])
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.
104
105 DO REPEAT x = 1 2 3.
106 INCLUDE include.sps.
107 END REPEAT.
108
109 LIST.
110 ])
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       |           ^
115 ])
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       |           ^"
120
121 Table: Data List
122 x,y
123 .00,30.00
124 ])
125 AT_CLEANUP
126
127 AT_SETUP([DO REPEAT -- nested])
128 AT_DATA([do-repeat.sps], [dnl
129 DATA LIST NOTABLE /a 1.
130 BEGIN DATA.
131 0
132 END DATA.
133
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.
137
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.
147
148 LIST.
149 ])
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
155 ])
156 AT_CLEANUP
157
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.
164 0
165 END DATA.
166
167 DO REPEAT h = a.
168 END REPEAT.
169 ])
170 AT_CHECK([pspp -o pspp.csv do-repeat.sps])
171 AT_CHECK([cat pspp.csv], [0], [dnl
172 ])
173 AT_CLEANUP
174
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.
181 ])
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.
184 ])
185 AT_CLEANUP
186
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.
205 ])
206 AT_DATA([insert.sps], [dnl
207 INSERT FILE='do-repeat.sps' ERROR=IGNORE.
208 ])
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       |           ^~"
213
214 "do-repeat.sps:4.11: warning: DO REPEAT: Dummy variable name `x' hides dictionary variable `x'.
215     4 | DO REPEAT x **.
216       |           ^"
217
218 "do-repeat.sps:4.13-4.14: error: DO REPEAT: Syntax error expecting `='.
219     4 | DO REPEAT x **.
220       |             ^~"
221
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       |               ^"
225
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       |                   ^~"
229
230 "do-repeat.sps:10.17-10.18: error: DO REPEAT: Syntax error expecting number.
231    10 | DO REPEAT y=1 2 **.
232       |                 ^~"
233
234 "do-repeat.sps:12.21-12.22: error: DO REPEAT: Syntax error expecting string.
235    12 | DO REPEAT y='a' 'b' **.
236       |                     ^~"
237
238 "do-repeat.sps:14.13-14.14: error: DO REPEAT: Syntax error expecting substitution values.
239    14 | DO REPEAT y=**.
240       |             ^~"
241
242 do-repeat.sps:16: error: DO REPEAT: Each dummy variable must have the same number of substitutions.
243
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       |           ^~~~~~~"
247
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       |                   ^~~"
251
252 error: DO REPEAT: At end of input: Syntax error expecting END REPEAT.
253 ])
254 AT_CLEANUP