1   ////////////////////////////////////////////////////////////////////////////////
2   // MillScript: an Open Spice interpreter and batch website creation tool
3   // Copyright (C) 2001-2004 Open World Ltd
4   // Copyright (C) 2005 Kevin Rogers
5   //
6   // This file is part of MillScript.
7   //
8   // MillScript is free software; you can redistribute it and/or modify it under
9   // the terms of the GNU General Public License as published by the Free
10  // Software Foundation; either version 2 of the License, or (at your option)
11  // any later version.
12  //
13  // MillScript is distributed in the hope that it will be useful, but WITHOUT
14  // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
15  // FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
16  // more details.
17  //
18  // You should have received a copy of the GNU General Public License along with
19  // MillScript; if not, write to the Free Software Foundation, Inc., 59 Temple
20  // Place, Suite 330, Boston, MA  02111-1307  USA
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          // We want to load packages from here..
75          engine.addInventory( getResource( "test/millscript" ) );
76  
77          // Try to import the test packages
78          forloopPackage = engine.getPackage( "forloop" );
79  
80          // Check the test packages were imported
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>&lt;name&gt; from &lt;expr&gt;</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             // We're expecting to get this exception, as within MillScript the
112             // only way to exit an infinite loop is by calling the mishap
113             // function at the appropriate moment.
114         }
115 
116     }
117 
118     /**
119      * Checks for the <tt>&lt;name&gt; from &lt;expr&gt; to &lt;expr&gt;</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         // The sum of all integers from 1 to n is ( n * ( n + 1 ) ) / 2
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>&lt;name&gt; in &lt;expr&gt;</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         // The sum of all integers from 1 to n is ( n * ( n + 1 ) ) / 2
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         // As long as this doesn't generate an exception everything should be
187         // ok.
188         state.compileExpr(
189             new ApplyExpr(
190                 new NameExpr( "bindingInList3" ),
191                 new SkipExpr()
192             )
193         );
194 
195 
196         // As long as this doesn't generate an exception everything should be
197         // ok.
198         state.compileExpr(
199             new ApplyExpr(
200                 new NameExpr( "bindingInList4" ),
201                 new SkipExpr()
202             )
203         );
204 
205     }
206 
207     /**
208      * Checks for the <tt>&lt;name&gt; &#38; &lt;name&gt; in &lt;expr&gt;</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         // The sum of all integers from 1 to n is ( n * ( n + 1 ) ) / 2
223         assertEquals( new Integer( 55 ), result );
224 
225 
226         // As long as this doesn't generate an exception everything should be
227         // ok.
228         state.compileExpr(
229             new ApplyExpr(
230                 new NameExpr( "bindingInMap2" ),
231                 new SkipExpr()
232             )
233         );
234 
235 
236         // As long as this doesn't generate an exception everything should be
237         // ok.
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 &lt;expr&gt;</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 &lt;expr&gt; else &lt;expr&gt;</tt> and
272      * <tt>until &lt;expr&gt; then &lt;expr&gt;</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 }