rt/vm/src/test/java/org/apidesign/vm4brwsr/MathTest.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Fri, 27 Feb 2015 19:28:07 +0100
changeset 1800 65cab8539582
parent 1787 ea12a3bb4b33
permissions -rw-r--r--
Probably faster to use the conditional operator than to call additional compact function
     1 /**
     2  * Back 2 Browser Bytecode Translator
     3  * Copyright (C) 2012-2015 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
     4  *
     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.
     8  *
     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.
    13  *
    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.
    17  */
    18 package org.apidesign.vm4brwsr;
    19 
    20 import org.testng.annotations.AfterClass;
    21 import org.testng.annotations.BeforeClass;
    22 import org.testng.annotations.Test;
    23 
    24 /** Checks behavior on Math class.
    25  *
    26  * @author Jaroslav Tulach <jtulach@netbeans.org>
    27  */
    28 public class MathTest {
    29     @Test public void rintNegativeUp() throws Exception {
    30         final double cnts = -453904.634;
    31         assertExec("Should round up to end with 5", mathClass(), "rint__DD", 
    32             -453905.0, cnts
    33         );
    34     }
    35 
    36     protected Class<?> mathClass() {
    37         return Math.class;
    38     }
    39 
    40     @Test public void rintNegativeDown() throws Exception {
    41         final double cnts = -453904.434;
    42         assertExec("Should round up to end with 4", mathClass(), "rint__DD", 
    43             -453904.0, cnts
    44         );
    45     }
    46 
    47     @Test public void rintPositiveUp() throws Exception {
    48         final double cnts = 453904.634;
    49         assertExec("Should round up to end with 5", mathClass(), "rint__DD", 
    50             453905.0, cnts
    51         );
    52     }
    53     @Test public void rintPositiveDown() throws Exception {
    54         final double cnts = 453904.434;
    55         assertExec("Should round up to end with 4", mathClass(), "rint__DD", 
    56             453904.0, cnts
    57         );
    58     }
    59     @Test public void rintOneHalf() throws Exception {
    60         final double cnts = 1.5;
    61         assertExec("Should round up to end with 2", mathClass(), "rint__DD", 
    62             2.0, cnts
    63         );
    64     }
    65     @Test public void rintNegativeOneHalf() throws Exception {
    66         final double cnts = -1.5;
    67         assertExec("Should round up to end with 2", mathClass(), "rint__DD", 
    68             -2.0, cnts
    69         );
    70     }
    71     @Test public void rintTwoAndHalf() throws Exception {
    72         final double cnts = 2.5;
    73         assertExec("Should round up to end with 2", mathClass(), "rint__DD", 
    74             2.0, cnts
    75         );
    76     }
    77     @Test public void rintNegativeTwoOneHalf() throws Exception {
    78         final double cnts = -2.5;
    79         assertExec("Should round up to end with 2", mathClass(), "rint__DD", 
    80             -2.0, cnts
    81         );
    82     }
    83 
    84     @Test public void ieeeReminder1() throws Exception {
    85         assertExec("Same result 1", mathClass(), "IEEEremainder__DDD", 
    86             Math.IEEEremainder(10.0, 4.5), 10.0, 4.5
    87         );
    88     }
    89 
    90     @Test public void ieeeReminder2() throws Exception {
    91         assertExec("Same result 1", mathClass(), "IEEEremainder__DDD", 
    92             Math.IEEEremainder(Integer.MAX_VALUE, -4.5), Integer.MAX_VALUE, -4.5
    93         );
    94     }
    95     
    96     @Test public void isNaN() throws Exception {
    97         boolean nan = Double.isNaN(Double.NaN); 
    98         assertExec("Same result 1", Double.class, "isNaN__ZD", 
    99             1.0, Double.NaN
   100         );
   101     }
   102 
   103     @Test public void isInfinite() throws Exception {
   104         assertExec("Same result 1", Double.class, "isInfinite__ZD", 
   105             1.0, Double.POSITIVE_INFINITY
   106         );
   107     }
   108 
   109     @Test public void isNegInfinite() throws Exception {
   110         assertExec("Same result 1", Double.class, "isInfinite__ZD", 
   111             1.0, Double.NEGATIVE_INFINITY
   112         );
   113     }
   114 
   115 
   116     private static TestVM code;
   117     
   118     @BeforeClass 
   119     public static void compileTheCode() throws Exception {
   120         StringBuilder sb = new StringBuilder();
   121         code = TestVM.compileClass(sb);
   122     }
   123     @AfterClass
   124     public static void releaseTheCode() {
   125         code = null;
   126     }
   127 
   128     private void assertExec(
   129         String msg, Class<?> clazz, String method, 
   130         Object ret, Object... args
   131     ) throws Exception {
   132         code.assertExec(msg, clazz, method, ret, args);
   133     }
   134     
   135 }