jaroslav@106
|
1 |
/**
|
jaroslav@106
|
2 |
* Back 2 Browser Bytecode Translator
|
jaroslav@106
|
3 |
* Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
|
jaroslav@106
|
4 |
*
|
jaroslav@106
|
5 |
* This program is free software: you can redistribute it and/or modify
|
jaroslav@106
|
6 |
* it under the terms of the GNU General Public License as published by
|
jaroslav@106
|
7 |
* the Free Software Foundation, version 2 of the License.
|
jaroslav@106
|
8 |
*
|
jaroslav@106
|
9 |
* This program is distributed in the hope that it will be useful,
|
jaroslav@106
|
10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
jaroslav@106
|
11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
jaroslav@106
|
12 |
* GNU General Public License for more details.
|
jaroslav@106
|
13 |
*
|
jaroslav@106
|
14 |
* You should have received a copy of the GNU General Public License
|
jaroslav@106
|
15 |
* along with this program. Look for COPYING file in the top folder.
|
jaroslav@106
|
16 |
* If not, see http://opensource.org/licenses/GPL-2.0.
|
jaroslav@106
|
17 |
*/
|
jaroslav@22
|
18 |
package org.apidesign.vm4brwsr;
|
jaroslav@0
|
19 |
|
jaroslav@0
|
20 |
import static org.testng.Assert.*;
|
jaroslav@789
|
21 |
import org.testng.annotations.AfterClass;
|
jaroslav@103
|
22 |
import org.testng.annotations.BeforeClass;
|
jaroslav@0
|
23 |
import org.testng.annotations.Test;
|
jaroslav@0
|
24 |
|
jaroslav@0
|
25 |
/** Checks the basic behavior of the translator.
|
jaroslav@0
|
26 |
*
|
jaroslav@0
|
27 |
* @author Jaroslav Tulach <jtulach@netbeans.org>
|
jaroslav@0
|
28 |
*/
|
jaroslav@0
|
29 |
public class StaticMethodTest {
|
jaroslav@0
|
30 |
@Test public void threePlusFour() throws Exception {
|
jaroslav@2
|
31 |
assertExec(
|
jaroslav@2
|
32 |
"Should be seven",
|
jaroslav@248
|
33 |
StaticMethod.class, "sum__III",
|
jaroslav@2
|
34 |
Double.valueOf(7),
|
jaroslav@2
|
35 |
3, 4
|
jaroslav@2
|
36 |
);
|
jaroslav@0
|
37 |
}
|
jaroslav@0
|
38 |
|
jaroslav@1466
|
39 |
@Test public void cast() throws Exception {
|
jaroslav@1466
|
40 |
assertExec(
|
jaroslav@1466
|
41 |
"Length is four",
|
jaroslav@1466
|
42 |
StaticMethod.class, "castString__ILjava_lang_Object_2",
|
jaroslav@1466
|
43 |
Double.valueOf(4),
|
jaroslav@1466
|
44 |
"Ahoj"
|
jaroslav@1466
|
45 |
);
|
jaroslav@1466
|
46 |
}
|
jaroslav@1466
|
47 |
|
jaroslav@173
|
48 |
@Test public void checkReallyInitializedValues() throws Exception {
|
jaroslav@173
|
49 |
assertExec(
|
jaroslav@173
|
50 |
"Return true",
|
jaroslav@248
|
51 |
StaticMethod.class, "isNull__Z",
|
jaroslav@173
|
52 |
Double.valueOf(1)
|
jaroslav@173
|
53 |
);
|
jaroslav@173
|
54 |
}
|
jaroslav@173
|
55 |
|
jaroslav@1
|
56 |
@Test public void powerOfThree() throws Exception {
|
jaroslav@2
|
57 |
assertExec(
|
jaroslav@2
|
58 |
"Should be nine",
|
jaroslav@248
|
59 |
StaticMethod.class, "power__FF",
|
jaroslav@2
|
60 |
Double.valueOf(9),
|
jaroslav@2
|
61 |
3.0f
|
jaroslav@2
|
62 |
);
|
jaroslav@1
|
63 |
}
|
jaroslav@1
|
64 |
|
jaroslav@48
|
65 |
@Test public void minusOne() throws Exception {
|
jaroslav@48
|
66 |
assertExec(
|
jaroslav@48
|
67 |
"Should be minus one",
|
jaroslav@248
|
68 |
StaticMethod.class, "minusOne__I",
|
jaroslav@48
|
69 |
Double.valueOf(-1)
|
jaroslav@48
|
70 |
);
|
jaroslav@48
|
71 |
}
|
jaroslav@48
|
72 |
|
jaroslav@2
|
73 |
@Test public void doubleWithoutLong() throws Exception {
|
jaroslav@2
|
74 |
assertExec(
|
jaroslav@2
|
75 |
"Should be two",
|
jaroslav@248
|
76 |
StaticMethod.class, "minus__DDJ",
|
jaroslav@2
|
77 |
Double.valueOf(2),
|
jaroslav@2
|
78 |
3.0d, 1l
|
jaroslav@2
|
79 |
);
|
jaroslav@2
|
80 |
}
|
jaroslav@600
|
81 |
|
jaroslav@600
|
82 |
@Test public void rintNegativeUp() throws Exception {
|
jaroslav@600
|
83 |
final double cnts = -453904.634;
|
jaroslav@600
|
84 |
assertExec(
|
jaroslav@600
|
85 |
"Should round up to end with 5",
|
jaroslav@600
|
86 |
Math.class, "rint__DD",
|
jaroslav@600
|
87 |
-453905.0, cnts
|
jaroslav@600
|
88 |
);
|
jaroslav@600
|
89 |
}
|
jaroslav@600
|
90 |
|
jaroslav@600
|
91 |
@Test public void rintNegativeDown() throws Exception {
|
jaroslav@600
|
92 |
final double cnts = -453904.434;
|
jaroslav@600
|
93 |
assertExec(
|
jaroslav@600
|
94 |
"Should round up to end with 4",
|
jaroslav@600
|
95 |
Math.class, "rint__DD",
|
jaroslav@600
|
96 |
-453904.0, cnts
|
jaroslav@600
|
97 |
);
|
jaroslav@600
|
98 |
}
|
jaroslav@600
|
99 |
|
jaroslav@600
|
100 |
@Test public void rintPositiveUp() throws Exception {
|
jaroslav@600
|
101 |
final double cnts = 453904.634;
|
jaroslav@600
|
102 |
assertExec(
|
jaroslav@600
|
103 |
"Should round up to end with 5",
|
jaroslav@600
|
104 |
Math.class, "rint__DD",
|
jaroslav@600
|
105 |
453905.0, cnts
|
jaroslav@600
|
106 |
);
|
jaroslav@600
|
107 |
}
|
jaroslav@600
|
108 |
@Test public void rintPositiveDown() throws Exception {
|
jaroslav@600
|
109 |
final double cnts = 453904.434;
|
jaroslav@600
|
110 |
assertExec(
|
jaroslav@600
|
111 |
"Should round up to end with 4",
|
jaroslav@600
|
112 |
Math.class, "rint__DD",
|
jaroslav@600
|
113 |
453904.0, cnts
|
jaroslav@600
|
114 |
);
|
jaroslav@600
|
115 |
}
|
jaroslav@600
|
116 |
@Test public void rintOneHalf() throws Exception {
|
jaroslav@600
|
117 |
final double cnts = 1.5;
|
jaroslav@600
|
118 |
assertExec(
|
jaroslav@600
|
119 |
"Should round up to end with 2",
|
jaroslav@600
|
120 |
Math.class, "rint__DD",
|
jaroslav@600
|
121 |
2.0, cnts
|
jaroslav@600
|
122 |
);
|
jaroslav@600
|
123 |
}
|
jaroslav@600
|
124 |
@Test public void rintNegativeOneHalf() throws Exception {
|
jaroslav@600
|
125 |
final double cnts = -1.5;
|
jaroslav@600
|
126 |
assertExec(
|
jaroslav@600
|
127 |
"Should round up to end with 2",
|
jaroslav@600
|
128 |
Math.class, "rint__DD",
|
jaroslav@600
|
129 |
-2.0, cnts
|
jaroslav@600
|
130 |
);
|
jaroslav@600
|
131 |
}
|
jaroslav@600
|
132 |
@Test public void rintTwoAndHalf() throws Exception {
|
jaroslav@600
|
133 |
final double cnts = 2.5;
|
jaroslav@600
|
134 |
assertExec(
|
jaroslav@600
|
135 |
"Should round up to end with 2",
|
jaroslav@600
|
136 |
Math.class, "rint__DD",
|
jaroslav@600
|
137 |
2.0, cnts
|
jaroslav@600
|
138 |
);
|
jaroslav@600
|
139 |
}
|
jaroslav@600
|
140 |
@Test public void rintNegativeTwoOneHalf() throws Exception {
|
jaroslav@600
|
141 |
final double cnts = -2.5;
|
jaroslav@600
|
142 |
assertExec(
|
jaroslav@600
|
143 |
"Should round up to end with 2",
|
jaroslav@600
|
144 |
Math.class, "rint__DD",
|
jaroslav@600
|
145 |
-2.0, cnts
|
jaroslav@600
|
146 |
);
|
jaroslav@600
|
147 |
}
|
jaroslav@3
|
148 |
|
jaroslav@605
|
149 |
@Test public void ieeeReminder1() throws Exception {
|
jaroslav@605
|
150 |
assertExec(
|
jaroslav@605
|
151 |
"Same result 1",
|
jaroslav@605
|
152 |
Math.class, "IEEEremainder__DDD",
|
jaroslav@605
|
153 |
Math.IEEEremainder(10.0, 4.5), 10.0, 4.5
|
jaroslav@605
|
154 |
);
|
jaroslav@605
|
155 |
}
|
jaroslav@605
|
156 |
|
jaroslav@605
|
157 |
@Test public void ieeeReminder2() throws Exception {
|
jaroslav@605
|
158 |
assertExec(
|
jaroslav@605
|
159 |
"Same result 1",
|
jaroslav@605
|
160 |
Math.class, "IEEEremainder__DDD",
|
jaroslav@605
|
161 |
Math.IEEEremainder(Integer.MAX_VALUE, -4.5), Integer.MAX_VALUE, -4.5
|
jaroslav@605
|
162 |
);
|
jaroslav@605
|
163 |
}
|
jaroslav@605
|
164 |
|
jaroslav@3
|
165 |
@Test public void divAndRound() throws Exception {
|
jaroslav@3
|
166 |
assertExec(
|
jaroslav@3
|
167 |
"Should be rounded to one",
|
jaroslav@248
|
168 |
StaticMethod.class, "div__IBD",
|
jaroslav@3
|
169 |
Double.valueOf(1),
|
jaroslav@3
|
170 |
3, 3.75
|
jaroslav@3
|
171 |
);
|
jaroslav@3
|
172 |
}
|
jaroslav@1467
|
173 |
|
jaroslav@1467
|
174 |
@Test public void inflaterInit() throws Exception {
|
jaroslav@1467
|
175 |
assertExec(
|
jaroslav@1467
|
176 |
"Down and minus",
|
jaroslav@1467
|
177 |
StaticMethod.class, "initInflater__IIZ",
|
jaroslav@1467
|
178 |
Double.valueOf(-9),
|
jaroslav@1467
|
179 |
10, true
|
jaroslav@1467
|
180 |
);
|
jaroslav@1467
|
181 |
}
|
jaroslav@1467
|
182 |
|
jaroslav@1467
|
183 |
@Test public void inflaterInitNoNeg() throws Exception {
|
jaroslav@1467
|
184 |
assertExec(
|
jaroslav@1467
|
185 |
"One up",
|
jaroslav@1467
|
186 |
StaticMethod.class, "initInflater__IIZ",
|
jaroslav@1467
|
187 |
Double.valueOf(11),
|
jaroslav@1467
|
188 |
10, false
|
jaroslav@1467
|
189 |
);
|
jaroslav@1467
|
190 |
}
|
jaroslav@1467
|
191 |
|
jaroslav@3
|
192 |
@Test public void mixedMethodFourParams() throws Exception {
|
jaroslav@3
|
193 |
assertExec(
|
jaroslav@3
|
194 |
"Should be two",
|
jaroslav@248
|
195 |
StaticMethod.class, "mix__IIJBD",
|
jaroslav@3
|
196 |
Double.valueOf(20),
|
jaroslav@3
|
197 |
2, 10l, 5, 2.0
|
jaroslav@3
|
198 |
);
|
jaroslav@3
|
199 |
}
|
jaroslav@4
|
200 |
@Test public void factRec() throws Exception {
|
jaroslav@4
|
201 |
assertExec(
|
jaroslav@4
|
202 |
"Factorial of 5 is 120",
|
jaroslav@248
|
203 |
StaticMethod.class, "factRec__JI",
|
jaroslav@4
|
204 |
Double.valueOf(120),
|
jaroslav@4
|
205 |
5
|
jaroslav@4
|
206 |
);
|
jaroslav@4
|
207 |
}
|
jaroslav@5
|
208 |
@Test public void factIter() throws Exception {
|
jaroslav@5
|
209 |
assertExec(
|
jaroslav@5
|
210 |
"Factorial of 5 is 120",
|
jaroslav@248
|
211 |
StaticMethod.class, "factIter__JI",
|
jaroslav@5
|
212 |
Double.valueOf(120),
|
jaroslav@5
|
213 |
5
|
jaroslav@5
|
214 |
);
|
jaroslav@5
|
215 |
}
|
jaroslav@2
|
216 |
|
jaroslav@6
|
217 |
@Test public void xor() throws Exception {
|
jaroslav@6
|
218 |
assertExec(
|
jaroslav@6
|
219 |
"Xor is 4",
|
jaroslav@248
|
220 |
StaticMethod.class, "xor__JIJ",
|
jaroslav@6
|
221 |
Double.valueOf(4),
|
jaroslav@6
|
222 |
7,
|
jaroslav@6
|
223 |
3
|
jaroslav@6
|
224 |
);
|
jaroslav@6
|
225 |
}
|
jaroslav@6
|
226 |
|
jaroslav@1468
|
227 |
@Test public void collectionToString() throws Exception {
|
jaroslav@1468
|
228 |
String exp = StaticMethod.toStringArr();
|
jaroslav@1468
|
229 |
assertExec(
|
jaroslav@1468
|
230 |
"0 to 4",
|
jaroslav@1468
|
231 |
StaticMethod.class, "toStringArr__Ljava_lang_String_2",
|
jaroslav@1468
|
232 |
exp
|
jaroslav@1468
|
233 |
);
|
jaroslav@1468
|
234 |
}
|
jaroslav@1468
|
235 |
|
jaroslav@7
|
236 |
@Test public void or() throws Exception {
|
jaroslav@7
|
237 |
assertExec(
|
jaroslav@7
|
238 |
"Or will be 7",
|
jaroslav@248
|
239 |
StaticMethod.class, "orOrAnd__JZII",
|
jaroslav@7
|
240 |
Double.valueOf(7),
|
jaroslav@7
|
241 |
true,
|
jaroslav@7
|
242 |
4,
|
jaroslav@7
|
243 |
3
|
jaroslav@7
|
244 |
);
|
jaroslav@7
|
245 |
}
|
jaroslav@46
|
246 |
@Test public void nullCheck() throws Exception {
|
jaroslav@46
|
247 |
assertExec(
|
jaroslav@46
|
248 |
"Returns nothing",
|
jaroslav@248
|
249 |
StaticMethod.class, "none__Ljava_lang_Object_2II",
|
jaroslav@46
|
250 |
null, 1, 3
|
jaroslav@46
|
251 |
);
|
jaroslav@46
|
252 |
}
|
jaroslav@7
|
253 |
@Test public void and() throws Exception {
|
jaroslav@7
|
254 |
assertExec(
|
jaroslav@7
|
255 |
"And will be 3",
|
jaroslav@248
|
256 |
StaticMethod.class, "orOrAnd__JZII",
|
jaroslav@7
|
257 |
Double.valueOf(3),
|
jaroslav@7
|
258 |
false,
|
jaroslav@7
|
259 |
7,
|
jaroslav@7
|
260 |
3
|
jaroslav@7
|
261 |
);
|
jaroslav@7
|
262 |
}
|
jaroslav@9
|
263 |
@Test public void inc4() throws Exception {
|
jaroslav@9
|
264 |
assertExec(
|
jaroslav@9
|
265 |
"It will be 4",
|
jaroslav@248
|
266 |
StaticMethod.class, "inc4__I",
|
jaroslav@9
|
267 |
Double.valueOf(4)
|
jaroslav@9
|
268 |
);
|
jaroslav@9
|
269 |
}
|
jaroslav@7
|
270 |
|
jaroslav@93
|
271 |
@Test public void shiftLeftInJava() throws Exception {
|
jaroslav@93
|
272 |
int res = StaticMethod.shiftLeft(1, 8);
|
jaroslav@93
|
273 |
assertEquals(res, 256);
|
jaroslav@93
|
274 |
}
|
jaroslav@93
|
275 |
|
jaroslav@93
|
276 |
@Test public void shiftLeftInJS() throws Exception {
|
jaroslav@93
|
277 |
assertExec(
|
jaroslav@93
|
278 |
"Setting 9th bit",
|
jaroslav@248
|
279 |
StaticMethod.class, "shiftLeft__III",
|
jaroslav@93
|
280 |
Double.valueOf(256),
|
jaroslav@93
|
281 |
1, 8
|
jaroslav@93
|
282 |
);
|
jaroslav@93
|
283 |
}
|
jaroslav@93
|
284 |
|
jaroslav@93
|
285 |
@Test public void shiftRightInJava() throws Exception {
|
jaroslav@93
|
286 |
int res = StaticMethod.shiftArithmRight(-8, 3, true);
|
jaroslav@93
|
287 |
assertEquals(res, -1);
|
jaroslav@93
|
288 |
}
|
jaroslav@93
|
289 |
|
jaroslav@93
|
290 |
@Test public void shiftRightInJS() throws Exception {
|
jaroslav@93
|
291 |
assertExec(
|
jaroslav@93
|
292 |
"Get -1",
|
jaroslav@248
|
293 |
StaticMethod.class, "shiftArithmRight__IIIZ",
|
jaroslav@93
|
294 |
Double.valueOf(-1),
|
jaroslav@93
|
295 |
-8, 3, true
|
jaroslav@93
|
296 |
);
|
jaroslav@93
|
297 |
}
|
jaroslav@93
|
298 |
@Test public void unsignedShiftRightInJava() throws Exception {
|
jaroslav@93
|
299 |
int res = StaticMethod.shiftArithmRight(8, 3, false);
|
jaroslav@93
|
300 |
assertEquals(res, 1);
|
jaroslav@93
|
301 |
}
|
jaroslav@93
|
302 |
|
jaroslav@93
|
303 |
@Test public void unsignedShiftRightInJS() throws Exception {
|
jaroslav@93
|
304 |
assertExec(
|
jaroslav@93
|
305 |
"Get -1",
|
jaroslav@248
|
306 |
StaticMethod.class, "shiftArithmRight__IIIZ",
|
jaroslav@93
|
307 |
Double.valueOf(1),
|
jaroslav@93
|
308 |
8, 3, false
|
jaroslav@93
|
309 |
);
|
jaroslav@93
|
310 |
}
|
jaroslav@93
|
311 |
|
jaroslav@94
|
312 |
@Test public void javaScriptBody() throws Exception {
|
jaroslav@94
|
313 |
assertExec(
|
jaroslav@94
|
314 |
"JavaScript string",
|
jaroslav@248
|
315 |
StaticMethod.class, "i2s__Ljava_lang_String_2II",
|
jaroslav@94
|
316 |
"333",
|
jaroslav@99
|
317 |
330, 3
|
jaroslav@94
|
318 |
);
|
jaroslav@94
|
319 |
}
|
jaroslav@94
|
320 |
|
jaroslav@115
|
321 |
@Test public void switchJarda() throws Exception {
|
jaroslav@115
|
322 |
assertExec(
|
jaroslav@115
|
323 |
"The expected value",
|
jaroslav@248
|
324 |
StaticMethod.class, "swtch__Ljava_lang_String_2I",
|
jaroslav@115
|
325 |
"Jarda",
|
jaroslav@115
|
326 |
0
|
jaroslav@115
|
327 |
);
|
jaroslav@115
|
328 |
}
|
jaroslav@115
|
329 |
|
jaroslav@115
|
330 |
@Test public void switchDarda() throws Exception {
|
jaroslav@115
|
331 |
assertExec(
|
jaroslav@115
|
332 |
"The expected value",
|
jaroslav@248
|
333 |
StaticMethod.class, "swtch__Ljava_lang_String_2I",
|
jaroslav@115
|
334 |
"Darda",
|
jaroslav@115
|
335 |
1
|
jaroslav@115
|
336 |
);
|
jaroslav@115
|
337 |
}
|
jaroslav@115
|
338 |
@Test public void switchParda() throws Exception {
|
jaroslav@115
|
339 |
assertExec(
|
jaroslav@115
|
340 |
"The expected value",
|
jaroslav@248
|
341 |
StaticMethod.class, "swtch2__Ljava_lang_String_2I",
|
jaroslav@115
|
342 |
"Parda",
|
jaroslav@115
|
343 |
22
|
jaroslav@115
|
344 |
);
|
jaroslav@115
|
345 |
}
|
jaroslav@115
|
346 |
@Test public void switchMarda() throws Exception {
|
jaroslav@115
|
347 |
assertExec(
|
jaroslav@115
|
348 |
"The expected value",
|
jaroslav@248
|
349 |
StaticMethod.class, "swtch__Ljava_lang_String_2I",
|
jaroslav@115
|
350 |
"Marda",
|
jaroslav@115
|
351 |
-433
|
jaroslav@115
|
352 |
);
|
jaroslav@115
|
353 |
}
|
jaroslav@115
|
354 |
|
jaroslav@291
|
355 |
@Test public void checkNullCast() throws Exception {
|
jaroslav@291
|
356 |
assertExec("Null can be cast to any type",
|
jaroslav@291
|
357 |
StaticMethod.class, "castNull__Ljava_lang_String_2Z",
|
jaroslav@291
|
358 |
null, true
|
jaroslav@291
|
359 |
);
|
jaroslav@291
|
360 |
}
|
jaroslav@291
|
361 |
|
jaroslav@1473
|
362 |
@Test public void stringConstantIsCopied() throws Exception {
|
jaroslav@1473
|
363 |
assertExec("String constants are copied between class pools",
|
jaroslav@1473
|
364 |
StaticMethod.class, "helloWorldLength__ILjava_lang_String_2",
|
jaroslav@1473
|
365 |
17, "Jardo"
|
jaroslav@1473
|
366 |
);
|
jaroslav@1473
|
367 |
}
|
jaroslav@1473
|
368 |
|
jaroslav@708
|
369 |
private static TestVM code;
|
jaroslav@103
|
370 |
|
jaroslav@103
|
371 |
@BeforeClass
|
jaroslav@789
|
372 |
public static void compileTheCode() throws Exception {
|
jaroslav@103
|
373 |
StringBuilder sb = new StringBuilder();
|
jaroslav@708
|
374 |
code = TestVM.compileClass(sb, "org/apidesign/vm4brwsr/StaticMethod");
|
jaroslav@2
|
375 |
}
|
jaroslav@789
|
376 |
@AfterClass
|
jaroslav@789
|
377 |
public static void releaseTheCode() {
|
jaroslav@789
|
378 |
code = null;
|
jaroslav@789
|
379 |
}
|
jaroslav@2
|
380 |
|
jaroslav@708
|
381 |
private void assertExec(
|
jaroslav@708
|
382 |
String msg, Class<?> clazz, String method,
|
jaroslav@708
|
383 |
Object ret, Object... args
|
jaroslav@708
|
384 |
) throws Exception {
|
jaroslav@708
|
385 |
code.assertExec(msg, clazz, method, ret, args);
|
jaroslav@298
|
386 |
}
|
jaroslav@0
|
387 |
}
|