1 /*
   2  * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package jdk.nashorn.internal.runtime.test;
  27 
  28 import jdk.nashorn.internal.runtime.JSType;
  29 import jdk.nashorn.internal.runtime.ScriptRuntime;
  30 import static org.testng.Assert.assertEquals;
  31 import static org.testng.Assert.assertFalse;
  32 import static org.testng.Assert.assertTrue;
  33 
  34 import org.testng.annotations.Test;
  35 
  36 /**
  37  * Tests for JSType methods.
  38  *
  39  * @test
  40  * @modules jdk.scripting.nashorn/jdk.nashorn.internal.runtime
  41  * @run testng jdk.nashorn.internal.runtime.test.JSTypeTest
  42  */
  43 public class JSTypeTest {
  44     /**
  45      * Test of isPrimitive method, of class Runtime.
  46      */
  47     @Test
  48     public void testIsPrimitive() {
  49         assertTrue(JSType.isPrimitive(null));
  50         assertTrue(JSType.isPrimitive(ScriptRuntime.UNDEFINED));
  51         assertTrue(JSType.isPrimitive(Double.NaN));
  52         assertTrue(JSType.isPrimitive(Double.NEGATIVE_INFINITY));
  53         assertTrue(JSType.isPrimitive(Double.POSITIVE_INFINITY));
  54         assertTrue(JSType.isPrimitive(0.0));
  55         assertTrue(JSType.isPrimitive(3.14));
  56         assertTrue(JSType.isPrimitive("hello"));
  57         assertTrue(JSType.isPrimitive(""));
  58         assertFalse(JSType.isPrimitive(new Object()));
  59     }
  60 
  61     /**
  62      * Test of toBoolean method, of class Runtime.
  63      */
  64     @Test
  65     public void testToBoolean() {
  66         assertFalse(JSType.toBoolean(ScriptRuntime.UNDEFINED));
  67         assertFalse(JSType.toBoolean(null));
  68         assertFalse(JSType.toBoolean(Boolean.FALSE));
  69         assertTrue(JSType.toBoolean(Boolean.TRUE));
  70         assertFalse(JSType.toBoolean(-0.0));
  71         assertFalse(JSType.toBoolean(0.0));
  72         assertFalse(JSType.toBoolean(Double.NaN));
  73         assertTrue(JSType.toBoolean(3.14));
  74         assertFalse(JSType.toBoolean(""));
  75         assertTrue(JSType.toBoolean("javascript"));
  76         assertTrue(JSType.toBoolean(new Object()));
  77     }
  78 
  79     /**
  80      * Test of toNumber method, of class Runtime.
  81      */
  82     @Test
  83     public void testToNumber_Object() {
  84         assertTrue(Double.isNaN(JSType.toNumber(ScriptRuntime.UNDEFINED)));
  85         assertEquals(JSType.toNumber((Object)null), 0.0, 0.0);
  86         assertEquals(JSType.toNumber(Boolean.TRUE), 1.0, 0.0);
  87         assertEquals(JSType.toNumber(Boolean.FALSE), 0.0, 0.0);
  88         assertEquals(JSType.toNumber(3.14), 3.14, 0.0);
  89         // FIXME: add more assertions for specific String to number cases
  90         // FIXME: add case for Object type (JSObject with getDefaultValue)
  91     }
  92 
  93     /**
  94      * Test of toString method, of class Runtime.
  95      */
  96     @Test
  97     public void testToString_Object() {
  98         assertEquals(JSType.toString(ScriptRuntime.UNDEFINED), "undefined");
  99         assertEquals(JSType.toString(null), "null");
 100         assertEquals(JSType.toString(Boolean.TRUE), "true");
 101         assertEquals(JSType.toString(Boolean.FALSE), "false");
 102         assertEquals(JSType.toString(""), "");
 103         assertEquals(JSType.toString("nashorn"), "nashorn");
 104         assertEquals(JSType.toString(Double.NaN), "NaN");
 105         assertEquals(JSType.toString(Double.POSITIVE_INFINITY), "Infinity");
 106         assertEquals(JSType.toString(Double.NEGATIVE_INFINITY), "-Infinity");
 107         assertEquals(JSType.toString(0.0), "0");
 108         // FIXME: add more number-to-string test cases
 109         // FIXME: add case for Object type (JSObject with getDefaultValue)
 110     }
 111 
 112     /**
 113      * Test of JSType.toUint32(double)
 114      */
 115     @Test
 116     public void testToUint32() {
 117         assertEquals(JSType.toUint32(+0.0), 0);
 118         assertEquals(JSType.toUint32(-0.0), 0);
 119         assertEquals(JSType.toUint32(Double.NaN), 0);
 120         assertEquals(JSType.toUint32(Double.POSITIVE_INFINITY), 0);
 121         assertEquals(JSType.toUint32(Double.NEGATIVE_INFINITY), 0);
 122         assertEquals(JSType.toUint32(9223372036854775807.0d), 0);
 123         assertEquals(JSType.toUint32(-9223372036854775807.0d), 0);
 124         assertEquals(JSType.toUint32(1099511627776.0d), 0);
 125         assertEquals(JSType.toUint32(-1099511627776.0d), 0);
 126         assertEquals(JSType.toUint32(4294967295.0d), 4294967295l);
 127         assertEquals(JSType.toUint32(4294967296.0d), 0);
 128         assertEquals(JSType.toUint32(4294967297.0d), 1);
 129         assertEquals(JSType.toUint32(-4294967295.0d), 1);
 130         assertEquals(JSType.toUint32(-4294967296.0d), 0);
 131         assertEquals(JSType.toUint32(-4294967297.0d), 4294967295l);
 132         assertEquals(JSType.toUint32(4294967295.6d), 4294967295l);
 133         assertEquals(JSType.toUint32(4294967296.6d), 0);
 134         assertEquals(JSType.toUint32(4294967297.6d), 1);
 135         assertEquals(JSType.toUint32(-4294967295.6d), 1);
 136         assertEquals(JSType.toUint32(-4294967296.6d), 0);
 137         assertEquals(JSType.toUint32(-4294967297.6d), 4294967295l);
 138     }
 139 
 140     /**
 141      * Test of JSType.toInt32(double)
 142      */
 143     @Test
 144     public void testToInt32() {
 145         assertEquals(JSType.toInt32(+0.0), 0);
 146         assertEquals(JSType.toInt32(-0.0), 0);
 147         assertEquals(JSType.toInt32(Double.NaN), 0);
 148         assertEquals(JSType.toInt32(Double.POSITIVE_INFINITY), 0);
 149         assertEquals(JSType.toInt32(Double.NEGATIVE_INFINITY), 0);
 150         assertEquals(JSType.toInt32(9223372036854775807.0d), 0);
 151         assertEquals(JSType.toInt32(-9223372036854775807.0d), 0);
 152         assertEquals(JSType.toInt32(1099511627776.0d), 0);
 153         assertEquals(JSType.toInt32(-1099511627776.0d), 0);
 154         assertEquals(JSType.toInt32(4294967295.0d), -1);
 155         assertEquals(JSType.toInt32(4294967296.0d), 0);
 156         assertEquals(JSType.toInt32(4294967297.0d), 1);
 157         assertEquals(JSType.toInt32(-4294967295.0d), 1);
 158         assertEquals(JSType.toInt32(-4294967296.0d), 0);
 159         assertEquals(JSType.toInt32(-4294967297.d), -1);
 160         assertEquals(JSType.toInt32(4294967295.6d), -1);
 161         assertEquals(JSType.toInt32(4294967296.6d), 0);
 162         assertEquals(JSType.toInt32(4294967297.6d), 1);
 163         assertEquals(JSType.toInt32(-4294967295.6d), 1);
 164         assertEquals(JSType.toInt32(-4294967296.6d), 0);
 165         assertEquals(JSType.toInt32(-4294967297.6d), -1);
 166     }
 167 
 168     /**
 169      * Test of JSType.toUint16(double)
 170      */
 171     @Test
 172     public void testToUint16() {
 173         assertEquals(JSType.toUint16(+0.0), 0);
 174         assertEquals(JSType.toUint16(-0.0), 0);
 175         assertEquals(JSType.toUint16(Double.NaN), 0);
 176         assertEquals(JSType.toUint16(Double.POSITIVE_INFINITY), 0);
 177         assertEquals(JSType.toUint16(Double.NEGATIVE_INFINITY), 0);
 178         assertEquals(JSType.toUint16(9223372036854775807.0d), 0);
 179         assertEquals(JSType.toUint16(-9223372036854775807.0d), 0);
 180         assertEquals(JSType.toUint16(1099511627776.0d), 0);
 181         assertEquals(JSType.toUint16(-1099511627776.0d), 0);
 182         assertEquals(JSType.toUint16(4294967295.0d), 65535);
 183         assertEquals(JSType.toUint16(4294967296.0d), 0);
 184         assertEquals(JSType.toUint16(4294967297.0d), 1);
 185         assertEquals(JSType.toUint16(-4294967295.0d), 1);
 186         assertEquals(JSType.toUint16(-4294967296.0d), 0);
 187         assertEquals(JSType.toUint16(-4294967297.0d), 65535);
 188         assertEquals(JSType.toUint16(4294967295.6d), 65535);
 189         assertEquals(JSType.toUint16(4294967296.6d), 0);
 190         assertEquals(JSType.toUint16(4294967297.6d), 1);
 191         assertEquals(JSType.toUint16(-4294967295.6d), 1);
 192         assertEquals(JSType.toUint16(-4294967296.6d), 0);
 193         assertEquals(JSType.toUint16(-4294967297.6d), 65535);
 194     }
 195 
 196 }