jaroslav@106: /** jaroslav@106: * Back 2 Browser Bytecode Translator jaroslav@106: * Copyright (C) 2012 Jaroslav Tulach jaroslav@106: * jaroslav@106: * This program is free software: you can redistribute it and/or modify jaroslav@106: * it under the terms of the GNU General Public License as published by jaroslav@106: * the Free Software Foundation, version 2 of the License. jaroslav@106: * jaroslav@106: * This program is distributed in the hope that it will be useful, jaroslav@106: * but WITHOUT ANY WARRANTY; without even the implied warranty of jaroslav@106: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the jaroslav@106: * GNU General Public License for more details. jaroslav@106: * jaroslav@106: * You should have received a copy of the GNU General Public License jaroslav@106: * along with this program. Look for COPYING file in the top folder. jaroslav@106: * If not, see http://opensource.org/licenses/GPL-2.0. jaroslav@106: */ jaroslav@22: package org.apidesign.vm4brwsr; jaroslav@0: jaroslav@0: import static org.testng.Assert.*; jaroslav@103: import org.testng.annotations.BeforeClass; jaroslav@0: import org.testng.annotations.Test; jaroslav@0: jaroslav@0: /** Checks the basic behavior of the translator. jaroslav@0: * jaroslav@0: * @author Jaroslav Tulach jaroslav@0: */ jaroslav@0: public class StaticMethodTest { jaroslav@0: @Test public void threePlusFour() throws Exception { jaroslav@2: assertExec( jaroslav@2: "Should be seven", jaroslav@248: StaticMethod.class, "sum__III", jaroslav@2: Double.valueOf(7), jaroslav@2: 3, 4 jaroslav@2: ); jaroslav@0: } jaroslav@0: jaroslav@173: @Test public void checkReallyInitializedValues() throws Exception { jaroslav@173: assertExec( jaroslav@173: "Return true", jaroslav@248: StaticMethod.class, "isNull__Z", jaroslav@173: Double.valueOf(1) jaroslav@173: ); jaroslav@173: } jaroslav@173: jaroslav@1: @Test public void powerOfThree() throws Exception { jaroslav@2: assertExec( jaroslav@2: "Should be nine", jaroslav@248: StaticMethod.class, "power__FF", jaroslav@2: Double.valueOf(9), jaroslav@2: 3.0f jaroslav@2: ); jaroslav@1: } jaroslav@1: jaroslav@48: @Test public void minusOne() throws Exception { jaroslav@48: assertExec( jaroslav@48: "Should be minus one", jaroslav@248: StaticMethod.class, "minusOne__I", jaroslav@48: Double.valueOf(-1) jaroslav@48: ); jaroslav@48: } jaroslav@48: jaroslav@2: @Test public void doubleWithoutLong() throws Exception { jaroslav@2: assertExec( jaroslav@2: "Should be two", jaroslav@248: StaticMethod.class, "minus__DDJ", jaroslav@2: Double.valueOf(2), jaroslav@2: 3.0d, 1l jaroslav@2: ); jaroslav@2: } jaroslav@600: jaroslav@600: @Test public void rintNegativeUp() throws Exception { jaroslav@600: final double cnts = -453904.634; jaroslav@600: assertExec( jaroslav@600: "Should round up to end with 5", jaroslav@600: Math.class, "rint__DD", jaroslav@600: -453905.0, cnts jaroslav@600: ); jaroslav@600: } jaroslav@600: jaroslav@600: @Test public void rintNegativeDown() throws Exception { jaroslav@600: final double cnts = -453904.434; jaroslav@600: assertExec( jaroslav@600: "Should round up to end with 4", jaroslav@600: Math.class, "rint__DD", jaroslav@600: -453904.0, cnts jaroslav@600: ); jaroslav@600: } jaroslav@600: jaroslav@600: @Test public void rintPositiveUp() throws Exception { jaroslav@600: final double cnts = 453904.634; jaroslav@600: assertExec( jaroslav@600: "Should round up to end with 5", jaroslav@600: Math.class, "rint__DD", jaroslav@600: 453905.0, cnts jaroslav@600: ); jaroslav@600: } jaroslav@600: @Test public void rintPositiveDown() throws Exception { jaroslav@600: final double cnts = 453904.434; jaroslav@600: assertExec( jaroslav@600: "Should round up to end with 4", jaroslav@600: Math.class, "rint__DD", jaroslav@600: 453904.0, cnts jaroslav@600: ); jaroslav@600: } jaroslav@600: @Test public void rintOneHalf() throws Exception { jaroslav@600: final double cnts = 1.5; jaroslav@600: assertExec( jaroslav@600: "Should round up to end with 2", jaroslav@600: Math.class, "rint__DD", jaroslav@600: 2.0, cnts jaroslav@600: ); jaroslav@600: } jaroslav@600: @Test public void rintNegativeOneHalf() throws Exception { jaroslav@600: final double cnts = -1.5; jaroslav@600: assertExec( jaroslav@600: "Should round up to end with 2", jaroslav@600: Math.class, "rint__DD", jaroslav@600: -2.0, cnts jaroslav@600: ); jaroslav@600: } jaroslav@600: @Test public void rintTwoAndHalf() throws Exception { jaroslav@600: final double cnts = 2.5; jaroslav@600: assertExec( jaroslav@600: "Should round up to end with 2", jaroslav@600: Math.class, "rint__DD", jaroslav@600: 2.0, cnts jaroslav@600: ); jaroslav@600: } jaroslav@600: @Test public void rintNegativeTwoOneHalf() throws Exception { jaroslav@600: final double cnts = -2.5; jaroslav@600: assertExec( jaroslav@600: "Should round up to end with 2", jaroslav@600: Math.class, "rint__DD", jaroslav@600: -2.0, cnts jaroslav@600: ); jaroslav@600: } jaroslav@3: jaroslav@605: @Test public void ieeeReminder1() throws Exception { jaroslav@605: assertExec( jaroslav@605: "Same result 1", jaroslav@605: Math.class, "IEEEremainder__DDD", jaroslav@605: Math.IEEEremainder(10.0, 4.5), 10.0, 4.5 jaroslav@605: ); jaroslav@605: } jaroslav@605: jaroslav@605: @Test public void ieeeReminder2() throws Exception { jaroslav@605: assertExec( jaroslav@605: "Same result 1", jaroslav@605: Math.class, "IEEEremainder__DDD", jaroslav@605: Math.IEEEremainder(Integer.MAX_VALUE, -4.5), Integer.MAX_VALUE, -4.5 jaroslav@605: ); jaroslav@605: } jaroslav@605: jaroslav@3: @Test public void divAndRound() throws Exception { jaroslav@3: assertExec( jaroslav@3: "Should be rounded to one", jaroslav@248: StaticMethod.class, "div__IBD", jaroslav@3: Double.valueOf(1), jaroslav@3: 3, 3.75 jaroslav@3: ); jaroslav@3: } jaroslav@3: @Test public void mixedMethodFourParams() throws Exception { jaroslav@3: assertExec( jaroslav@3: "Should be two", jaroslav@248: StaticMethod.class, "mix__IIJBD", jaroslav@3: Double.valueOf(20), jaroslav@3: 2, 10l, 5, 2.0 jaroslav@3: ); jaroslav@3: } jaroslav@4: @Test public void factRec() throws Exception { jaroslav@4: assertExec( jaroslav@4: "Factorial of 5 is 120", jaroslav@248: StaticMethod.class, "factRec__JI", jaroslav@4: Double.valueOf(120), jaroslav@4: 5 jaroslav@4: ); jaroslav@4: } jaroslav@5: @Test public void factIter() throws Exception { jaroslav@5: assertExec( jaroslav@5: "Factorial of 5 is 120", jaroslav@248: StaticMethod.class, "factIter__JI", jaroslav@5: Double.valueOf(120), jaroslav@5: 5 jaroslav@5: ); jaroslav@5: } jaroslav@2: jaroslav@6: @Test public void xor() throws Exception { jaroslav@6: assertExec( jaroslav@6: "Xor is 4", jaroslav@248: StaticMethod.class, "xor__JIJ", jaroslav@6: Double.valueOf(4), jaroslav@6: 7, jaroslav@6: 3 jaroslav@6: ); jaroslav@6: } jaroslav@6: jaroslav@7: @Test public void or() throws Exception { jaroslav@7: assertExec( jaroslav@7: "Or will be 7", jaroslav@248: StaticMethod.class, "orOrAnd__JZII", jaroslav@7: Double.valueOf(7), jaroslav@7: true, jaroslav@7: 4, jaroslav@7: 3 jaroslav@7: ); jaroslav@7: } jaroslav@46: @Test public void nullCheck() throws Exception { jaroslav@46: assertExec( jaroslav@46: "Returns nothing", jaroslav@248: StaticMethod.class, "none__Ljava_lang_Object_2II", jaroslav@46: null, 1, 3 jaroslav@46: ); jaroslav@46: } jaroslav@7: @Test public void and() throws Exception { jaroslav@7: assertExec( jaroslav@7: "And will be 3", jaroslav@248: StaticMethod.class, "orOrAnd__JZII", jaroslav@7: Double.valueOf(3), jaroslav@7: false, jaroslav@7: 7, jaroslav@7: 3 jaroslav@7: ); jaroslav@7: } jaroslav@9: @Test public void inc4() throws Exception { jaroslav@9: assertExec( jaroslav@9: "It will be 4", jaroslav@248: StaticMethod.class, "inc4__I", jaroslav@9: Double.valueOf(4) jaroslav@9: ); jaroslav@9: } jaroslav@7: jaroslav@93: @Test public void shiftLeftInJava() throws Exception { jaroslav@93: int res = StaticMethod.shiftLeft(1, 8); jaroslav@93: assertEquals(res, 256); jaroslav@93: } jaroslav@93: jaroslav@93: @Test public void shiftLeftInJS() throws Exception { jaroslav@93: assertExec( jaroslav@93: "Setting 9th bit", jaroslav@248: StaticMethod.class, "shiftLeft__III", jaroslav@93: Double.valueOf(256), jaroslav@93: 1, 8 jaroslav@93: ); jaroslav@93: } jaroslav@93: jaroslav@93: @Test public void shiftRightInJava() throws Exception { jaroslav@93: int res = StaticMethod.shiftArithmRight(-8, 3, true); jaroslav@93: assertEquals(res, -1); jaroslav@93: } jaroslav@93: jaroslav@93: @Test public void shiftRightInJS() throws Exception { jaroslav@93: assertExec( jaroslav@93: "Get -1", jaroslav@248: StaticMethod.class, "shiftArithmRight__IIIZ", jaroslav@93: Double.valueOf(-1), jaroslav@93: -8, 3, true jaroslav@93: ); jaroslav@93: } jaroslav@93: @Test public void unsignedShiftRightInJava() throws Exception { jaroslav@93: int res = StaticMethod.shiftArithmRight(8, 3, false); jaroslav@93: assertEquals(res, 1); jaroslav@93: } jaroslav@93: jaroslav@93: @Test public void unsignedShiftRightInJS() throws Exception { jaroslav@93: assertExec( jaroslav@93: "Get -1", jaroslav@248: StaticMethod.class, "shiftArithmRight__IIIZ", jaroslav@93: Double.valueOf(1), jaroslav@93: 8, 3, false jaroslav@93: ); jaroslav@93: } jaroslav@93: jaroslav@94: @Test public void javaScriptBody() throws Exception { jaroslav@94: assertExec( jaroslav@94: "JavaScript string", jaroslav@248: StaticMethod.class, "i2s__Ljava_lang_String_2II", jaroslav@94: "333", jaroslav@99: 330, 3 jaroslav@94: ); jaroslav@94: } jaroslav@94: jaroslav@115: @Test public void switchJarda() throws Exception { jaroslav@115: assertExec( jaroslav@115: "The expected value", jaroslav@248: StaticMethod.class, "swtch__Ljava_lang_String_2I", jaroslav@115: "Jarda", jaroslav@115: 0 jaroslav@115: ); jaroslav@115: } jaroslav@115: jaroslav@115: @Test public void switchDarda() throws Exception { jaroslav@115: assertExec( jaroslav@115: "The expected value", jaroslav@248: StaticMethod.class, "swtch__Ljava_lang_String_2I", jaroslav@115: "Darda", jaroslav@115: 1 jaroslav@115: ); jaroslav@115: } jaroslav@115: @Test public void switchParda() throws Exception { jaroslav@115: assertExec( jaroslav@115: "The expected value", jaroslav@248: StaticMethod.class, "swtch2__Ljava_lang_String_2I", jaroslav@115: "Parda", jaroslav@115: 22 jaroslav@115: ); jaroslav@115: } jaroslav@115: @Test public void switchMarda() throws Exception { jaroslav@115: assertExec( jaroslav@115: "The expected value", jaroslav@248: StaticMethod.class, "swtch__Ljava_lang_String_2I", jaroslav@115: "Marda", jaroslav@115: -433 jaroslav@115: ); jaroslav@115: } jaroslav@115: jaroslav@291: @Test public void checkNullCast() throws Exception { jaroslav@291: assertExec("Null can be cast to any type", jaroslav@291: StaticMethod.class, "castNull__Ljava_lang_String_2Z", jaroslav@291: null, true jaroslav@291: ); jaroslav@291: } jaroslav@291: jaroslav@708: private static TestVM code; jaroslav@103: jaroslav@103: @BeforeClass jaroslav@103: public void compileTheCode() throws Exception { jaroslav@103: StringBuilder sb = new StringBuilder(); jaroslav@708: code = TestVM.compileClass(sb, "org/apidesign/vm4brwsr/StaticMethod"); jaroslav@2: } jaroslav@2: jaroslav@708: private void assertExec( jaroslav@708: String msg, Class clazz, String method, jaroslav@708: Object ret, Object... args jaroslav@708: ) throws Exception { jaroslav@708: code.assertExec(msg, clazz, method, ret, args); jaroslav@298: } jaroslav@0: }