1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 package org.millscript.millscript;
23
24 import org.millscript.commons.alert.Alert;
25 import org.millscript.commons.util.Maplet;
26 import org.millscript.millscript.expr.ApplyExpr;
27 import org.millscript.millscript.expr.NameExpr;
28 import org.millscript.millscript.expr.SkipExpr;
29 import org.millscript.millscript.vm.CompilerState;
30 import org.millscript.millscript.vm.Package;
31
32 import junit.framework.Test;
33 import junit.framework.TestSuite;
34
35 /**
36 * Unit test for Millscript for loops.
37 *
38 * @author <a href="mailto:moya@users.sourceforge.net">Kevin Rogers</a>
39 */
40 public class ForLoopTest extends AbstractTestCase {
41
42 /**
43 * bug53 Package.
44 */
45 private Package forloopPackage;
46
47 /**
48 * Create the test case
49 *
50 * @param testName name of the test case
51 */
52 public ForLoopTest( String testName ) {
53 super( testName );
54 }
55
56 /**
57 * @return the suite of tests being tested
58 */
59 public static Test suite() {
60 return new TestSuite( ForLoopTest.class );
61 }
62
63 /**
64 * Initialize the test fixture. In this instance we require a machine with a
65 * standard environment, from which we get the "bug53" and "bug53a" package
66 * environments(i.e. we import it). We can then proceed with testing that
67 * NameExpr, or at least NameExpr.shareIdent, works properly.
68 */
69 @Override
70 public void setUp() {
71
72 super.setUp();
73
74
75 engine.addInventory( getResource( "test/millscript" ) );
76
77
78 forloopPackage = engine.getPackage( "forloop" );
79
80
81 assertNotNull( forloopPackage );
82
83 }
84
85 /**
86 * Clean up the test fixture by reseting the millscript machine.
87 */
88 @Override
89 public void tearDown() {
90 engine.getMachine().reset();
91 }
92
93 /**
94 * Checks for the <tt><name> from <expr></tt> binding. We cannot
95 * thoroughly test this binding independantly, as alone it specifies an
96 * infinite loop.
97 */
98 public void testBindingFrom() {
99
100 try {
101 CompilerState state = forloopPackage.getCompilerState();
102
103 state.compileExpr(
104 new ApplyExpr(
105 new NameExpr( "bindingFrom1" ),
106 new SkipExpr()
107 )
108 );
109
110 } catch ( Alert ex ) {
111
112
113
114 }
115
116 }
117
118 /**
119 * Checks for the <tt><name> from <expr> to <expr></tt>
120 * binding.
121 */
122 public void testBindingFromTo() {
123
124 CompilerState state = forloopPackage.getCompilerState();
125
126 Object result = state.compileExprToValue(
127 new ApplyExpr(
128 new NameExpr( "bindingFromTo1" ),
129 new SkipExpr()
130 )
131 );
132
133
134 assertEquals( new Integer( 500500 ), result );
135
136
137 result = state.compileExprToValue(
138 new ApplyExpr(
139 new NameExpr( "bindingFromTo2" ),
140 new SkipExpr()
141 )
142 );
143
144 assertTrue( result instanceof Maplet );
145
146 Maplet testResult = (Maplet)result;
147
148 assertEquals( new Integer( 1000 ), testResult.getValue() );
149 assertEquals( new Integer( 1000 ), testResult.getKey() );
150
151 }
152
153 /**
154 * Checks for the <tt><name> in <expr></tt> binding.
155 */
156 public void testBindingInList() {
157
158 CompilerState state = forloopPackage.getCompilerState();
159
160 Object result = state.compileExprToValue(
161 new ApplyExpr(
162 new NameExpr( "bindingInList1" ),
163 new SkipExpr()
164 )
165 );
166
167
168 assertEquals( new Integer( 55 ), result );
169
170
171 result = state.compileExprToValue(
172 new ApplyExpr(
173 new NameExpr( "bindingInList2" ),
174 new SkipExpr()
175 )
176 );
177
178 assertTrue( result instanceof Maplet );
179
180 Maplet testResult = (Maplet)result;
181
182 assertEquals( new Integer( 10 ), testResult.getValue() );
183 assertEquals( new Integer( 10 ), testResult.getKey() );
184
185
186
187
188 state.compileExpr(
189 new ApplyExpr(
190 new NameExpr( "bindingInList3" ),
191 new SkipExpr()
192 )
193 );
194
195
196
197
198 state.compileExpr(
199 new ApplyExpr(
200 new NameExpr( "bindingInList4" ),
201 new SkipExpr()
202 )
203 );
204
205 }
206
207 /**
208 * Checks for the <tt><name> & <name> in <expr></tt>
209 * binding.
210 */
211 public void testBindingInMap() {
212
213 CompilerState state = forloopPackage.getCompilerState();
214
215 Object result = state.compileExprToValue(
216 new ApplyExpr(
217 new NameExpr( "bindingInMap1" ),
218 new SkipExpr()
219 )
220 );
221
222
223 assertEquals( new Integer( 55 ), result );
224
225
226
227
228 state.compileExpr(
229 new ApplyExpr(
230 new NameExpr( "bindingInMap2" ),
231 new SkipExpr()
232 )
233 );
234
235
236
237
238 state.compileExpr(
239 new ApplyExpr(
240 new NameExpr( "bindingInMap3" ),
241 new SkipExpr()
242 )
243 );
244
245 }
246
247 /**
248 * Checks for the <tt>where <expr></tt> condition.
249 */
250 public void testConditionWhere() {
251
252 CompilerState state = forloopPackage.getCompilerState();
253
254 Object result = state.compileExprToValue(
255 new ApplyExpr(
256 new NameExpr( "conditionWhere1" ),
257 new SkipExpr()
258 )
259 );
260
261 assertTrue( result instanceof Maplet );
262
263 Maplet testResult = (Maplet)result;
264
265 assertEquals( new Integer( 15 ), testResult.getValue() );
266 assertEquals( new Integer( 25 ), testResult.getKey() );
267
268 }
269
270 /**
271 * Checks for the <tt>while <expr> else <expr></tt> and
272 * <tt>until <expr> then <expr></tt> conditions.
273 */
274 public void testConditionWhile() {
275
276 CompilerState state = forloopPackage.getCompilerState();
277
278 Object result = state.compileExprToValue(
279 new ApplyExpr(
280 new NameExpr( "conditionWhile1" ),
281 new SkipExpr()
282 )
283 );
284
285 assertEquals( new Integer( 6 ), result );
286
287 result = state.compileExprToValue(
288 new ApplyExpr(
289 new NameExpr( "conditionUntil1" ),
290 new SkipExpr()
291 )
292 );
293
294 assertEquals( new Integer( 7 ), result );
295
296 }
297
298 /**
299 * Checks for the for loop body.
300 */
301 public void testBody() {
302 }
303
304 /**
305 * Checks for the for loop finally statement.
306 */
307 public void testFinally() {
308
309 CompilerState state = forloopPackage.getCompilerState();
310
311 Object result = state.compileExprToValue(
312 new ApplyExpr(
313 new NameExpr( "finally1" ),
314 new SkipExpr()
315 )
316 );
317
318 assertEquals( "success", result );
319
320 }
321
322 }