1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/rt/vm/src/test/java/org/apidesign/vm4brwsr/StaticMethodTest.java Tue Feb 26 16:54:16 2013 +0100
1.3 @@ -0,0 +1,338 @@
1.4 +/**
1.5 + * Back 2 Browser Bytecode Translator
1.6 + * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
1.7 + *
1.8 + * This program is free software: you can redistribute it and/or modify
1.9 + * it under the terms of the GNU General Public License as published by
1.10 + * the Free Software Foundation, version 2 of the License.
1.11 + *
1.12 + * This program is distributed in the hope that it will be useful,
1.13 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
1.14 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1.15 + * GNU General Public License for more details.
1.16 + *
1.17 + * You should have received a copy of the GNU General Public License
1.18 + * along with this program. Look for COPYING file in the top folder.
1.19 + * If not, see http://opensource.org/licenses/GPL-2.0.
1.20 + */
1.21 +package org.apidesign.vm4brwsr;
1.22 +
1.23 +import static org.testng.Assert.*;
1.24 +import org.testng.annotations.BeforeClass;
1.25 +import org.testng.annotations.Test;
1.26 +
1.27 +/** Checks the basic behavior of the translator.
1.28 + *
1.29 + * @author Jaroslav Tulach <jtulach@netbeans.org>
1.30 + */
1.31 +public class StaticMethodTest {
1.32 + @Test public void threePlusFour() throws Exception {
1.33 + assertExec(
1.34 + "Should be seven",
1.35 + StaticMethod.class, "sum__III",
1.36 + Double.valueOf(7),
1.37 + 3, 4
1.38 + );
1.39 + }
1.40 +
1.41 + @Test public void checkReallyInitializedValues() throws Exception {
1.42 + assertExec(
1.43 + "Return true",
1.44 + StaticMethod.class, "isNull__Z",
1.45 + Double.valueOf(1)
1.46 + );
1.47 + }
1.48 +
1.49 + @Test public void powerOfThree() throws Exception {
1.50 + assertExec(
1.51 + "Should be nine",
1.52 + StaticMethod.class, "power__FF",
1.53 + Double.valueOf(9),
1.54 + 3.0f
1.55 + );
1.56 + }
1.57 +
1.58 + @Test public void minusOne() throws Exception {
1.59 + assertExec(
1.60 + "Should be minus one",
1.61 + StaticMethod.class, "minusOne__I",
1.62 + Double.valueOf(-1)
1.63 + );
1.64 + }
1.65 +
1.66 + @Test public void doubleWithoutLong() throws Exception {
1.67 + assertExec(
1.68 + "Should be two",
1.69 + StaticMethod.class, "minus__DDJ",
1.70 + Double.valueOf(2),
1.71 + 3.0d, 1l
1.72 + );
1.73 + }
1.74 +
1.75 + @Test public void rintNegativeUp() throws Exception {
1.76 + final double cnts = -453904.634;
1.77 + assertExec(
1.78 + "Should round up to end with 5",
1.79 + Math.class, "rint__DD",
1.80 + -453905.0, cnts
1.81 + );
1.82 + }
1.83 +
1.84 + @Test public void rintNegativeDown() throws Exception {
1.85 + final double cnts = -453904.434;
1.86 + assertExec(
1.87 + "Should round up to end with 4",
1.88 + Math.class, "rint__DD",
1.89 + -453904.0, cnts
1.90 + );
1.91 + }
1.92 +
1.93 + @Test public void rintPositiveUp() throws Exception {
1.94 + final double cnts = 453904.634;
1.95 + assertExec(
1.96 + "Should round up to end with 5",
1.97 + Math.class, "rint__DD",
1.98 + 453905.0, cnts
1.99 + );
1.100 + }
1.101 + @Test public void rintPositiveDown() throws Exception {
1.102 + final double cnts = 453904.434;
1.103 + assertExec(
1.104 + "Should round up to end with 4",
1.105 + Math.class, "rint__DD",
1.106 + 453904.0, cnts
1.107 + );
1.108 + }
1.109 + @Test public void rintOneHalf() throws Exception {
1.110 + final double cnts = 1.5;
1.111 + assertExec(
1.112 + "Should round up to end with 2",
1.113 + Math.class, "rint__DD",
1.114 + 2.0, cnts
1.115 + );
1.116 + }
1.117 + @Test public void rintNegativeOneHalf() throws Exception {
1.118 + final double cnts = -1.5;
1.119 + assertExec(
1.120 + "Should round up to end with 2",
1.121 + Math.class, "rint__DD",
1.122 + -2.0, cnts
1.123 + );
1.124 + }
1.125 + @Test public void rintTwoAndHalf() throws Exception {
1.126 + final double cnts = 2.5;
1.127 + assertExec(
1.128 + "Should round up to end with 2",
1.129 + Math.class, "rint__DD",
1.130 + 2.0, cnts
1.131 + );
1.132 + }
1.133 + @Test public void rintNegativeTwoOneHalf() throws Exception {
1.134 + final double cnts = -2.5;
1.135 + assertExec(
1.136 + "Should round up to end with 2",
1.137 + Math.class, "rint__DD",
1.138 + -2.0, cnts
1.139 + );
1.140 + }
1.141 +
1.142 + @Test public void ieeeReminder1() throws Exception {
1.143 + assertExec(
1.144 + "Same result 1",
1.145 + Math.class, "IEEEremainder__DDD",
1.146 + Math.IEEEremainder(10.0, 4.5), 10.0, 4.5
1.147 + );
1.148 + }
1.149 +
1.150 + @Test public void ieeeReminder2() throws Exception {
1.151 + assertExec(
1.152 + "Same result 1",
1.153 + Math.class, "IEEEremainder__DDD",
1.154 + Math.IEEEremainder(Integer.MAX_VALUE, -4.5), Integer.MAX_VALUE, -4.5
1.155 + );
1.156 + }
1.157 +
1.158 + @Test public void divAndRound() throws Exception {
1.159 + assertExec(
1.160 + "Should be rounded to one",
1.161 + StaticMethod.class, "div__IBD",
1.162 + Double.valueOf(1),
1.163 + 3, 3.75
1.164 + );
1.165 + }
1.166 + @Test public void mixedMethodFourParams() throws Exception {
1.167 + assertExec(
1.168 + "Should be two",
1.169 + StaticMethod.class, "mix__IIJBD",
1.170 + Double.valueOf(20),
1.171 + 2, 10l, 5, 2.0
1.172 + );
1.173 + }
1.174 + @Test public void factRec() throws Exception {
1.175 + assertExec(
1.176 + "Factorial of 5 is 120",
1.177 + StaticMethod.class, "factRec__JI",
1.178 + Double.valueOf(120),
1.179 + 5
1.180 + );
1.181 + }
1.182 + @Test public void factIter() throws Exception {
1.183 + assertExec(
1.184 + "Factorial of 5 is 120",
1.185 + StaticMethod.class, "factIter__JI",
1.186 + Double.valueOf(120),
1.187 + 5
1.188 + );
1.189 + }
1.190 +
1.191 + @Test public void xor() throws Exception {
1.192 + assertExec(
1.193 + "Xor is 4",
1.194 + StaticMethod.class, "xor__JIJ",
1.195 + Double.valueOf(4),
1.196 + 7,
1.197 + 3
1.198 + );
1.199 + }
1.200 +
1.201 + @Test public void or() throws Exception {
1.202 + assertExec(
1.203 + "Or will be 7",
1.204 + StaticMethod.class, "orOrAnd__JZII",
1.205 + Double.valueOf(7),
1.206 + true,
1.207 + 4,
1.208 + 3
1.209 + );
1.210 + }
1.211 + @Test public void nullCheck() throws Exception {
1.212 + assertExec(
1.213 + "Returns nothing",
1.214 + StaticMethod.class, "none__Ljava_lang_Object_2II",
1.215 + null, 1, 3
1.216 + );
1.217 + }
1.218 + @Test public void and() throws Exception {
1.219 + assertExec(
1.220 + "And will be 3",
1.221 + StaticMethod.class, "orOrAnd__JZII",
1.222 + Double.valueOf(3),
1.223 + false,
1.224 + 7,
1.225 + 3
1.226 + );
1.227 + }
1.228 + @Test public void inc4() throws Exception {
1.229 + assertExec(
1.230 + "It will be 4",
1.231 + StaticMethod.class, "inc4__I",
1.232 + Double.valueOf(4)
1.233 + );
1.234 + }
1.235 +
1.236 + @Test public void shiftLeftInJava() throws Exception {
1.237 + int res = StaticMethod.shiftLeft(1, 8);
1.238 + assertEquals(res, 256);
1.239 + }
1.240 +
1.241 + @Test public void shiftLeftInJS() throws Exception {
1.242 + assertExec(
1.243 + "Setting 9th bit",
1.244 + StaticMethod.class, "shiftLeft__III",
1.245 + Double.valueOf(256),
1.246 + 1, 8
1.247 + );
1.248 + }
1.249 +
1.250 + @Test public void shiftRightInJava() throws Exception {
1.251 + int res = StaticMethod.shiftArithmRight(-8, 3, true);
1.252 + assertEquals(res, -1);
1.253 + }
1.254 +
1.255 + @Test public void shiftRightInJS() throws Exception {
1.256 + assertExec(
1.257 + "Get -1",
1.258 + StaticMethod.class, "shiftArithmRight__IIIZ",
1.259 + Double.valueOf(-1),
1.260 + -8, 3, true
1.261 + );
1.262 + }
1.263 + @Test public void unsignedShiftRightInJava() throws Exception {
1.264 + int res = StaticMethod.shiftArithmRight(8, 3, false);
1.265 + assertEquals(res, 1);
1.266 + }
1.267 +
1.268 + @Test public void unsignedShiftRightInJS() throws Exception {
1.269 + assertExec(
1.270 + "Get -1",
1.271 + StaticMethod.class, "shiftArithmRight__IIIZ",
1.272 + Double.valueOf(1),
1.273 + 8, 3, false
1.274 + );
1.275 + }
1.276 +
1.277 + @Test public void javaScriptBody() throws Exception {
1.278 + assertExec(
1.279 + "JavaScript string",
1.280 + StaticMethod.class, "i2s__Ljava_lang_String_2II",
1.281 + "333",
1.282 + 330, 3
1.283 + );
1.284 + }
1.285 +
1.286 + @Test public void switchJarda() throws Exception {
1.287 + assertExec(
1.288 + "The expected value",
1.289 + StaticMethod.class, "swtch__Ljava_lang_String_2I",
1.290 + "Jarda",
1.291 + 0
1.292 + );
1.293 + }
1.294 +
1.295 + @Test public void switchDarda() throws Exception {
1.296 + assertExec(
1.297 + "The expected value",
1.298 + StaticMethod.class, "swtch__Ljava_lang_String_2I",
1.299 + "Darda",
1.300 + 1
1.301 + );
1.302 + }
1.303 + @Test public void switchParda() throws Exception {
1.304 + assertExec(
1.305 + "The expected value",
1.306 + StaticMethod.class, "swtch2__Ljava_lang_String_2I",
1.307 + "Parda",
1.308 + 22
1.309 + );
1.310 + }
1.311 + @Test public void switchMarda() throws Exception {
1.312 + assertExec(
1.313 + "The expected value",
1.314 + StaticMethod.class, "swtch__Ljava_lang_String_2I",
1.315 + "Marda",
1.316 + -433
1.317 + );
1.318 + }
1.319 +
1.320 + @Test public void checkNullCast() throws Exception {
1.321 + assertExec("Null can be cast to any type",
1.322 + StaticMethod.class, "castNull__Ljava_lang_String_2Z",
1.323 + null, true
1.324 + );
1.325 + }
1.326 +
1.327 + private static TestVM code;
1.328 +
1.329 + @BeforeClass
1.330 + public void compileTheCode() throws Exception {
1.331 + StringBuilder sb = new StringBuilder();
1.332 + code = TestVM.compileClass(sb, "org/apidesign/vm4brwsr/StaticMethod");
1.333 + }
1.334 +
1.335 + private void assertExec(
1.336 + String msg, Class<?> clazz, String method,
1.337 + Object ret, Object... args
1.338 + ) throws Exception {
1.339 + code.assertExec(msg, clazz, method, ret, args);
1.340 + }
1.341 +}