rt/vm/src/test/java/org/apidesign/vm4brwsr/MathTest.java
changeset 1773 9830c8b761ce
parent 1473 484248c9c1d6
child 1787 ea12a3bb4b33
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/rt/vm/src/test/java/org/apidesign/vm4brwsr/MathTest.java	Tue Jan 20 13:01:31 2015 +0100
     1.3 @@ -0,0 +1,115 @@
     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 org.testng.annotations.AfterClass;
    1.24 +import org.testng.annotations.BeforeClass;
    1.25 +import org.testng.annotations.Test;
    1.26 +
    1.27 +/** Checks behavior on Math class.
    1.28 + *
    1.29 + * @author Jaroslav Tulach <jtulach@netbeans.org>
    1.30 + */
    1.31 +public class MathTest {
    1.32 +    @Test public void rintNegativeUp() throws Exception {
    1.33 +        final double cnts = -453904.634;
    1.34 +        assertExec("Should round up to end with 5", mathClass(), "rint__DD", 
    1.35 +            -453905.0, cnts
    1.36 +        );
    1.37 +    }
    1.38 +
    1.39 +    protected Class<?> mathClass() {
    1.40 +        return Math.class;
    1.41 +    }
    1.42 +
    1.43 +    @Test public void rintNegativeDown() throws Exception {
    1.44 +        final double cnts = -453904.434;
    1.45 +        assertExec("Should round up to end with 4", mathClass(), "rint__DD", 
    1.46 +            -453904.0, cnts
    1.47 +        );
    1.48 +    }
    1.49 +
    1.50 +    @Test public void rintPositiveUp() throws Exception {
    1.51 +        final double cnts = 453904.634;
    1.52 +        assertExec("Should round up to end with 5", mathClass(), "rint__DD", 
    1.53 +            453905.0, cnts
    1.54 +        );
    1.55 +    }
    1.56 +    @Test public void rintPositiveDown() throws Exception {
    1.57 +        final double cnts = 453904.434;
    1.58 +        assertExec("Should round up to end with 4", mathClass(), "rint__DD", 
    1.59 +            453904.0, cnts
    1.60 +        );
    1.61 +    }
    1.62 +    @Test public void rintOneHalf() throws Exception {
    1.63 +        final double cnts = 1.5;
    1.64 +        assertExec("Should round up to end with 2", mathClass(), "rint__DD", 
    1.65 +            2.0, cnts
    1.66 +        );
    1.67 +    }
    1.68 +    @Test public void rintNegativeOneHalf() throws Exception {
    1.69 +        final double cnts = -1.5;
    1.70 +        assertExec("Should round up to end with 2", mathClass(), "rint__DD", 
    1.71 +            -2.0, cnts
    1.72 +        );
    1.73 +    }
    1.74 +    @Test public void rintTwoAndHalf() throws Exception {
    1.75 +        final double cnts = 2.5;
    1.76 +        assertExec("Should round up to end with 2", mathClass(), "rint__DD", 
    1.77 +            2.0, cnts
    1.78 +        );
    1.79 +    }
    1.80 +    @Test public void rintNegativeTwoOneHalf() throws Exception {
    1.81 +        final double cnts = -2.5;
    1.82 +        assertExec("Should round up to end with 2", mathClass(), "rint__DD", 
    1.83 +            -2.0, cnts
    1.84 +        );
    1.85 +    }
    1.86 +
    1.87 +    @Test public void ieeeReminder1() throws Exception {
    1.88 +        assertExec("Same result 1", mathClass(), "IEEEremainder__DDD", 
    1.89 +            Math.IEEEremainder(10.0, 4.5), 10.0, 4.5
    1.90 +        );
    1.91 +    }
    1.92 +
    1.93 +    @Test public void ieeeReminder2() throws Exception {
    1.94 +        assertExec("Same result 1", mathClass(), "IEEEremainder__DDD", 
    1.95 +            Math.IEEEremainder(Integer.MAX_VALUE, -4.5), Integer.MAX_VALUE, -4.5
    1.96 +        );
    1.97 +    }
    1.98 +
    1.99 +    private static TestVM code;
   1.100 +    
   1.101 +    @BeforeClass 
   1.102 +    public static void compileTheCode() throws Exception {
   1.103 +        StringBuilder sb = new StringBuilder();
   1.104 +        code = TestVM.compileClass(sb);
   1.105 +    }
   1.106 +    @AfterClass
   1.107 +    public static void releaseTheCode() {
   1.108 +        code = null;
   1.109 +    }
   1.110 +
   1.111 +    private void assertExec(
   1.112 +        String msg, Class<?> clazz, String method, 
   1.113 +        Object ret, Object... args
   1.114 +    ) throws Exception {
   1.115 +        code.assertExec(msg, clazz, method, ret, args);
   1.116 +    }
   1.117 +    
   1.118 +}