1 ////////////////////////////////////////////////////////////////////////////////
2 // MillScript: an Open Spice interpreter and batch website creation tool
3 // Copyright (C) 2001-2004 Open World Ltd
4 //
5 // This file is part of MillScript.
6 //
7 // MillScript is free software; you can redistribute it and/or modify it under
8 // the terms of the GNU General Public License as published by the Free
9 // Software Foundation; either version 2 of the License, or (at your option)
10 // any later version.
11 //
12 // MillScript is distributed in the hope that it will be useful, but WITHOUT
13 // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15 // more details.
16 //
17 // You should have received a copy of the GNU General Public License along with
18 // MillScript; if not, write to the Free Software Foundation, Inc., 59 Temple
19 // Place, Suite 330, Boston, MA 02111-1307 USA
20 ////////////////////////////////////////////////////////////////////////////////
21 package org.millscript.millscript;
22
23 import org.millscript.millscript.expr.ArithExpr;
24 import org.millscript.millscript.expr.ConstantExpr;
25
26 import junit.framework.Test;
27 import junit.framework.TestSuite;
28
29 /**
30 * Unit test for Millscript arithmetic
31 *
32 * @author <a href="mailto:moya@users.sourceforge.net">Kevin Rogers</a>
33 */
34 public class ArithmeticTest extends AbstractTestCase {
35
36 /**
37 * Create the test case
38 *
39 * @param testName name of the test case
40 */
41 public ArithmeticTest( String testName ) {
42 super( testName );
43 }
44
45 /**
46 * @return the suite of tests being tested
47 */
48 public static Test suite() {
49 return new TestSuite( ArithmeticTest.class );
50 }
51
52 /**
53 * Simple addition tests
54 */
55 public void testAdd() {
56
57 // Do a simple sum using the first method for creating an expression
58 assertEquals(
59 actExpr(
60 new ArithExpr(
61 "+",
62 new ConstantExpr( new Integer( 2 ) ),
63 new ConstantExpr( new Integer( 5 ) )
64 )
65 ),
66 new Integer( 7 )
67 );
68
69 engine.getMachine().reset();
70
71 // Do the same sum using a different method for creating an expression
72 assertEquals(
73 actExpr(
74 ArithExpr.make(
75 "+",
76 new ConstantExpr( new Integer( 2 ) ),
77 new ConstantExpr( new Integer( 5 ) )
78 )
79 ),
80 new Integer( 7 )
81 );
82
83 engine.getMachine().reset();
84
85 }
86
87 /**
88 * Simple subtraction tests
89 */
90 public void testSub() {
91
92 // Do a simple sum using the first method for creating an expression
93 assertEquals(
94 actExpr(
95 new ArithExpr(
96 "-",
97 new ConstantExpr( new Integer( 7 ) ),
98 new ConstantExpr( new Integer( 6 ) )
99 )
100 ),
101 new Integer( 1 )
102 );
103
104 engine.getMachine().reset();
105
106 // Do the same sum using a different method for creating an expression
107 assertEquals(
108 actExpr(
109 ArithExpr.make(
110 "-",
111 new ConstantExpr( new Integer( 7 ) ),
112 new ConstantExpr( new Integer( 6 ) )
113 )
114 ),
115 new Integer( 1 )
116 );
117
118 engine.getMachine().reset();
119
120 }
121
122 /**
123 * Simple multiplication tests
124 */
125 public void testMultiplication() {
126
127 // Do a simple sum using the first method for creating an expression
128 assertEquals(
129 actExpr(
130 new ArithExpr(
131 "*",
132 new ConstantExpr( new Integer( 4 ) ),
133 new ConstantExpr( new Integer( 6 ) )
134 )
135 ),
136 new Integer( 24 )
137 );
138
139 engine.getMachine().reset();
140
141 // Do the same sum using a different method for creating an expression
142 assertEquals(
143 actExpr(
144 ArithExpr.make(
145 "*",
146 new ConstantExpr( new Integer( 4 ) ),
147 new ConstantExpr( new Integer( 6 ) )
148 )
149 ),
150 new Integer( 24 )
151 );
152
153 engine.getMachine().reset();
154
155 }
156
157 /**
158 * Simple div tests
159 */
160 public void testDiv() {
161
162 // Do a simple sum using the first method for creating an expression
163 assertEquals(
164 actExpr(
165 new ArithExpr(
166 "div",
167 new ConstantExpr( new Integer( 9 ) ),
168 new ConstantExpr( new Integer( 2 ) )
169 )
170 ),
171 new Integer( 4 )
172 );
173
174 engine.getMachine().reset();
175
176 // Do the same sum using a different method for creating an expression
177 assertEquals(
178 actExpr(
179 ArithExpr.make(
180 "div",
181 new ConstantExpr( new Integer( 9 ) ),
182 new ConstantExpr( new Integer( 2 ) )
183 )
184 ),
185 new Integer( 4 )
186 );
187
188 engine.getMachine().reset();
189
190 }
191
192 /**
193 * Simple mod tests
194 */
195 public void testMod() {
196
197 // Do a simple sum using the first method for creating an expression
198 assertEquals(
199 actExpr(
200 new ArithExpr(
201 "mod",
202 new ConstantExpr( new Integer( 9 ) ),
203 new ConstantExpr( new Integer( 2 ) )
204 )
205 ),
206 new Integer( 1 )
207 );
208
209 engine.getMachine().reset();
210
211 // Do the same sum using a different method for creating an expression
212 assertEquals(
213 actExpr(
214 ArithExpr.make(
215 "mod",
216 new ConstantExpr( new Integer( 9 ) ),
217 new ConstantExpr( new Integer( 2 ) )
218 )
219 ),
220 new Integer( 1 )
221 );
222
223 engine.getMachine().reset();
224
225 }
226
227 }