1 package jrre.unittests;
2
3 import junit.framework.*;
4 import jrre.*;
5 import jrre.instructionset.*;
6 import jrre.types.*;
7
8 import jrre.unittests.datastructures.*;
9
10 import java.io.*;
11
12 public class MathmaticsTest extends TestCase {
13
14 public void testBasicMath(){
15
16 System.out.println("\nS T A R T I N G M A T H T E S T:\n");
17
18 JRRERunner runtime = new JRRERunner();
19
20 // Load class containing method.
21 runtime.loadClass("target/test-classes/jrre/unittests/datastructures/Algorithems");
22
23
24 // Build list of methods to call.
25 String [] methods = {"add(II)I",
26 "sub(II)I",
27 "mul(II)I",
28 "div(II)I"};
29
30 // Build test case input values [
31 int [] [] testInput = {{ 4, 2},
32 { 2, 2},
33 { 6, 3},
34 { 400, 2},
35 { 120, 5}};
36
37 // Build results list.
38 int [] results = {6, // 4 + 2 = 6
39 4, // 2 + 2 = 6
40 9, // 6 + 3 = 9
41 402, // 400 + 2 = 402
42 125, // 120 + 5 = 125
43
44 2, // 4 - 2 = 2
45 0, // 2 - 2 = 0
46 3, // 6 - 3 = 3
47 398, // 400 - 2 = 398
48 115, // 120 - 5 = 115
49
50 8, // 4 * 2 = 8
51 4, // 2 * 2 = 4
52 18, // 6 * 3 = 18
53 800, // 400 * 2 = 800
54 600, // 120 * 5 = 600
55
56 2, // 4 / 2 = 2
57 1, // 2 / 2 = 1
58 2, // 6 / 3 = 2
59 200, // 400 / 2 = 200
60 24 // 120 / 5 = 24
61 };
62
63 int resultsIndex = 0;
64
65 for(int j=0;j < methods.length;j++){
66
67 String method = methods[j];
68 for(int i=0;i < testInput.length;i++){
69
70 int parameterOne = testInput[i][0];
71 int parameterTwo = testInput[i][1];
72 int correctResult = results[resultsIndex++];
73
74 // Build parameters for method call.
75 Type [] parameters = new Type[] { new PrimitiveType(parameterOne), new PrimitiveType(parameterTwo) };
76
77 // Call method and get the top of the operand stack
78 // after it returns (its return value).
79 PrimitiveType result = (PrimitiveType) runtime.callMethod(method, parameters);
80
81 System.out.println("\t"+method.substring(0,method.indexOf("("))+"("+parameterOne+", "+
82 parameterTwo+") \treturned: "+result.getValue());
83
84 // Check that the result is correct.
85 Assert.assertTrue("Testing that "+method+"("+parameterOne+", "+parameterTwo+") == "+correctResult,
86 result.getValue() == correctResult);
87 }
88 System.out.println("");
89 }
90 }
91
92 public void testFactorial(){
93
94 System.out.println("\nS T A R T I N G F A C T O R I A L T E S T:\n");
95
96 JRRERunner runtime = new JRRERunner();
97
98 // Load class containing factorial method.
99 runtime.loadClass("target/test-classes/jrre/unittests/datastructures/Algorithems");
100
101 // Build test case values.
102 int [] [] testCases = {{ 1, 1},
103 { 2, 2},
104 { 3, 6},
105 { 4, 24},
106 { 5, 120}};
107
108 for(int i=0;i < testCases.length;i++){
109
110 int parameter = testCases[i][0];
111 int correctResult = testCases[i][1];
112
113 // Build parameters for method call.
114 Type [] parameters = new Type[] { new PrimitiveType(parameter) };
115
116 // Call method and get the top of the operand stack
117 // after it returns (its return value).
118 PrimitiveType result = (PrimitiveType) runtime.callMethod("factorial(I)I", parameters);
119
120 // Check that the result is correct.
121 Assert.assertTrue("Testing that factorial("+parameter+") == "+correctResult,
122 result.getValue() == correctResult);
123
124 System.out.println("\tfactorial("+parameter+")\t returned: "+result.getValue());
125
126 }
127 }
128
129 public void testEuclidsGCD(){
130
131 System.out.println("\nS T A R T I N G E U C L I D S A L G R O I T H M T E S T:\n");
132
133 JRRERunner runtime = new JRRERunner();
134
135 // Load class containing factorial method.
136 runtime.loadClass("target/test-classes/jrre/unittests/datastructures/Algorithems");
137
138 // Build test case input values
139 int [] [] testInput = {{ 4, 2},
140 { 2, 2},
141 { 6, 3},
142 { 400, 225},
143 { 121, 11}};
144
145 int [] results = {2,
146 2,
147 3,
148 25,
149 11
150 };
151 int resultsIndex = 0;
152
153 for(int i=0;i < testInput.length;i++){
154
155 int parameterOne = testInput[i][0];
156 int parameterTwo = testInput[i][1];
157 int correctResult = results[resultsIndex++];
158
159 // Build parameters for method call.
160 Type [] parameters = new Type[] { new PrimitiveType(parameterOne), new PrimitiveType(parameterTwo) };
161
162 // Call method and get the top of the operand stack
163 // after it returns (its return value).
164 PrimitiveType result = (PrimitiveType) runtime.callMethod("euclidsGCD(II)I", parameters);
165
166 // Check that the result is correct.
167 Assert.assertTrue("Testing that euclidsGCD("+parameterOne+", "+parameterTwo+") == "+correctResult,
168 result.getValue() == correctResult);
169
170 System.out.println("\teuclidsGCD("+parameterOne+", "+
171 parameterTwo+") \treturned: "+result.getValue());
172 }
173
174 }
175
176 public void testFibonacci(){
177
178 System.out.println("\nS T A R T I N G F I B O N A C C I T E S T:\n");
179
180 JRRERunner runtime = new JRRERunner();
181
182 // Load class containing method.
183 runtime.loadClass("target/test-classes/jrre/unittests/datastructures/Algorithems");
184
185 // Build test case values [
186 int [] [] testCases = {{ 1, 1},
187 { 2, 1},
188 { 3, 2},
189 { 4, 3},
190 { 5, 5},
191 { 6, 8},
192 { 7, 13},
193 { 8, 21},
194 { 9, 34},
195 { 10, 55},
196 { 11, 89}};
197
198 for(int i=0;i < testCases.length;i++){
199
200 int parameter = testCases[i][0];
201 int correctResult = testCases[i][1];
202
203 // Build parameters for method call.
204 Type [] parameters = new Type[] { new PrimitiveType(parameter) };
205
206 // Call method and get the top of the operand stack
207 // after it returns (its return value).
208 PrimitiveType result = (PrimitiveType) runtime.callMethod("fibonacci(I)I", parameters);
209
210 // Check that the result is correct.
211 Assert.assertTrue("Testing that fibonacci("+parameter+") == "+correctResult,
212 result.getValue() == correctResult);
213
214 System.out.println("\tfibonacci("+parameter+")\treturned: "+result.getValue());
215 }
216
217 }
218
219 }
220
This page was automatically generated by Maven