2 * Back 2 Browser Bytecode Translator
3 * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, version 2 of the License.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. Look for COPYING file in the top folder.
16 * If not, see http://opensource.org/licenses/GPL-2.0.
18 package org.apidesign.vm4brwsr;
20 import static org.testng.Assert.*;
21 import org.testng.annotations.AfterClass;
22 import org.testng.annotations.BeforeClass;
23 import org.testng.annotations.Test;
25 /** Checks the basic behavior of the translator.
27 * @author Jaroslav Tulach <jtulach@netbeans.org>
29 public class StaticMethodTest {
30 @Test public void threePlusFour() throws Exception {
33 StaticMethod.class, "sum__III",
39 @Test public void cast() throws Exception {
42 StaticMethod.class, "castString__ILjava_lang_Object_2",
48 @Test public void checkReallyInitializedValues() throws Exception {
51 StaticMethod.class, "isNull__Z",
56 @Test public void powerOfThree() throws Exception {
59 StaticMethod.class, "power__FF",
65 @Test public void minusOne() throws Exception {
67 "Should be minus one",
68 StaticMethod.class, "minusOne__I",
73 @Test public void doubleWithoutLong() throws Exception {
76 StaticMethod.class, "minus__DDJ",
82 @Test public void divAndRound() throws Exception {
84 "Should be rounded to one",
85 StaticMethod.class, "div__IBD",
91 @Test public void inflaterInit() throws Exception {
94 StaticMethod.class, "initInflater__IIZ",
100 @Test public void inflaterInitNoNeg() throws Exception {
103 StaticMethod.class, "initInflater__IIZ",
109 @Test public void mixedMethodFourParams() throws Exception {
112 StaticMethod.class, "mix__IIJBD",
117 @Test public void factRec() throws Exception {
119 "Factorial of 5 is 120",
120 StaticMethod.class, "factRec__JI",
125 @Test public void factIter() throws Exception {
127 "Factorial of 5 is 120",
128 StaticMethod.class, "factIter__JI",
134 @Test public void xor() throws Exception {
137 StaticMethod.class, "xor__JIJ",
144 @Test public void collectionToString() throws Exception {
145 String exp = StaticMethod.toStringArr();
148 StaticMethod.class, "toStringArr__Ljava_lang_String_2",
153 @Test public void or() throws Exception {
156 StaticMethod.class, "orOrAnd__JZII",
163 @Test public void nullCheck() throws Exception {
166 StaticMethod.class, "none__Ljava_lang_Object_2II",
170 @Test public void and() throws Exception {
173 StaticMethod.class, "orOrAnd__JZII",
180 @Test public void inc4() throws Exception {
183 StaticMethod.class, "inc4__I",
188 @Test public void shiftLeftInJava() throws Exception {
189 int res = StaticMethod.shiftLeft(1, 8);
190 assertEquals(res, 256);
193 @Test public void shiftLeftInJS() throws Exception {
196 StaticMethod.class, "shiftLeft__III",
202 @Test public void shiftRightInJava() throws Exception {
203 int res = StaticMethod.shiftArithmRight(-8, 3, true);
204 assertEquals(res, -1);
207 @Test public void shiftRightInJS() throws Exception {
210 StaticMethod.class, "shiftArithmRight__IIIZ",
215 @Test public void unsignedShiftRightInJava() throws Exception {
216 int res = StaticMethod.shiftArithmRight(8, 3, false);
217 assertEquals(res, 1);
220 @Test public void unsignedShiftRightInJS() throws Exception {
223 StaticMethod.class, "shiftArithmRight__IIIZ",
229 @Test public void javaScriptBody() throws Exception {
232 StaticMethod.class, "i2s__Ljava_lang_String_2II",
238 @Test public void switchJarda() throws Exception {
240 "The expected value",
241 StaticMethod.class, "swtch__Ljava_lang_String_2I",
247 @Test public void switchDarda() throws Exception {
249 "The expected value",
250 StaticMethod.class, "swtch__Ljava_lang_String_2I",
255 @Test public void switchParda() throws Exception {
257 "The expected value",
258 StaticMethod.class, "swtch2__Ljava_lang_String_2I",
263 @Test public void switchMarda() throws Exception {
265 "The expected value",
266 StaticMethod.class, "swtch__Ljava_lang_String_2I",
272 @Test public void checkNullCast() throws Exception {
273 assertExec("Null can be cast to any type",
274 StaticMethod.class, "castNull__Ljava_lang_String_2Z",
279 @Test public void stringConstantIsCopied() throws Exception {
280 assertExec("String constants are copied between class pools",
281 StaticMethod.class, "helloWorldLength__ILjava_lang_String_2",
286 private static TestVM code;
289 public static void compileTheCode() throws Exception {
290 StringBuilder sb = new StringBuilder();
291 code = TestVM.compileClass(sb, "org/apidesign/vm4brwsr/StaticMethod");
294 public static void releaseTheCode() {
298 private void assertExec(
299 String msg, Class<?> clazz, String method,
300 Object ret, Object... args
302 code.assertExec(msg, clazz, method, ret, args);