test/script/basic/javaarrayconversion.js

Wed, 20 Aug 2014 10:25:28 +0200

author
attila
date
Wed, 20 Aug 2014 10:25:28 +0200
changeset 962
ac62e33a99b0
parent 279
1fd18f40ab52
child 1205
4112748288bb
child 1533
8edb98264b4f
permissions
-rw-r--r--

8044638: Tidy up Nashorn codebase for code standards
8055199: Tidy up Nashorn codebase for code standards (August 2014)
Reviewed-by: lagergren, sundar

     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.
     8  *
     9  * This code is distributed in the hope that it will be useful, but WITHOUT
    10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    12  * version 2 for more details (a copy is included in the LICENSE file that
    13  * accompanied this code).
    14  *
    15  * You should have received a copy of the GNU General Public License version
    16  * 2 along with this work; if not, write to the Free Software Foundation,
    17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    18  *
    19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    20  * or visit www.oracle.com if you need additional information or have any
    21  * questions.
    22  */
    24 /**
    25  * Tests for conversion of JavaScript arrays to Java arrays and the other
    26  * way round. Also generally useful as a JavaScript-to-Java type conversion
    27  * test.
    28  *
    29  * @test
    30  * @run
    31  */
    33 var x; // used for undefined
    34 var testCount = 0;
    36 function testF(inputValue, type, testFn) {
    37   var x = Java.to([inputValue], type + "[]")[0];
    38   if(!testFn(x)) {
    39     throw ("unexpected value: " + x)
    40   }
    41   ++testCount;
    42 }
    44 function test(inputValue, type, expectedValue) {
    45   testF(inputValue, type, function(x) { return x === expectedValue })
    46 }
    48 function testNaN(inputValue, type) {
    49   testF(inputValue, type, isNaN)
    50 }
    52 // Those labeled "Correct?" are not clearly correct conversions. Those
    53 // labeled "TypeError maybe?" could actually throw a TypeError, or only
    54 // throw a TypeError when in strict mode.
    55 // The case of ("false", "boolean") => true is particularly amusing.
    57 test(x, "int", 0) // Correct? TypeError maybe?
    58 test(null, "int", 0) // Correct? TypeError maybe?
    59 test(1234, "int", 1234)
    60 test("1234", "int", 1234)
    61 test("1234.49", "int", 1234)
    62 test("1234.51", "int", 1234) // truncates, not rounds
    63 test(true, "int", 1)
    64 test(false, "int", 0)
    65 test("foo", "int", 0) // Correct? TypeError maybe?
    67 test(x, "boolean", false) // Correct? TypeError maybe?
    68 test(null, "boolean", false) // Correct? TypeError maybe?
    69 test(0, "boolean", false)
    70 test(1234, "boolean", true)
    71 test("foo", "boolean", true)
    72 test("", "boolean", false)
    73 test("false", "boolean", true) // Correct? false maybe?
    75 test(x, "java.lang.String", "undefined") // Correct? TypeError maybe?
    76 test(null, "java.lang.String", null)
    77 test(1234, "java.lang.String", "1234")
    78 test(1234.5, "java.lang.String", "1234.5")
    79 test(true, "java.lang.String", "true")
    80 test(false, "java.lang.String", "false")
    82 test(x, "java.lang.Integer", null) // Correct? TypeError maybe?
    83 test(null, "java.lang.Integer", null)
    84 test(1234, "java.lang.Integer", 1234)
    85 test("1234", "java.lang.Integer", 1234)
    86 test("1234.49", "java.lang.Integer", 1234)
    87 test("1234.51", "java.lang.Integer", 1234) // truncates, not rounds
    88 test(true, "java.lang.Integer", 1)
    89 test(false, "java.lang.Integer", 0)
    90 test("foo", "java.lang.Integer", 0) // Correct? TypeError maybe?
    92 test(x, "java.lang.Boolean", null) // Correct? TypeError maybe?
    93 test(null, "java.lang.Boolean", null)
    94 test(0, "java.lang.Boolean", false)
    95 test(1234, "java.lang.Boolean", true)
    96 test("foo", "java.lang.Boolean", true)
    97 test("", "java.lang.Boolean", false)
    98 test("false", "java.lang.Boolean", true) // Correct? false maybe?
   100 testNaN(x, "double")
   101 test(null, "double", 0)
   102 test(1234, "double", 1234)
   103 test("1234", "double", 1234)
   104 test("1234.5", "double", 1234.5)
   105 test(true, "double", 1)
   106 test(false, "double", 0)
   107 testNaN("foo", "double")
   109 testNaN(x, "java.lang.Double")
   110 test(null, "java.lang.Double", null)
   111 test(1234, "java.lang.Double", 1234)
   112 test("1234", "java.lang.Double", 1234)
   113 test("1234.5", "java.lang.Double", 1234.5)
   114 test(true, "java.lang.Double", 1)
   115 test(false, "java.lang.Double", 0)
   116 testNaN("foo", "java.lang.Double")
   118 test({ valueOf: function() { return 42; } }, "int", 42)
   119 test({ valueOf: function() { return "42"; } }, "int", 42)
   120 // If there's no valueOf, toString is used
   121 test({ toString: function() { return "42"; } }, "int", 42)
   122 // For numbers, valueOf takes precedence over toString
   123 test({ valueOf: function() { return "42"; },  toString: function() { return "43"; } }, "int", 42)
   125 test({ toString: function() { return "foo"; } }, "java.lang.String", "foo")
   126 // Yep, even if we have valueOf, toString from prototype takes precedence
   127 test({ valueOf: function() { return 42; } }, "java.lang.String", "[object Object]")
   128 // Converting to string, toString takes precedence over valueOf
   129 test({ valueOf: function() { return "42"; },  toString: function() { return "43"; } }, "java.lang.String", "43")
   131 function assertCantConvert(sourceType, targetType) {
   132   try {
   133     Java.to([new Java.type(sourceType)()], targetType + "[]")
   134     throw "no TypeError encountered"
   135   } catch(e) {
   136       if(!(e instanceof TypeError)) {
   137         throw e;
   138       }
   139       ++testCount;
   140   }
   141 }
   143 // Arbitrary POJOs can't be converted to Java values
   144 assertCantConvert("java.util.BitSet", "int")
   145 assertCantConvert("java.util.BitSet", "double")
   146 assertCantConvert("java.util.BitSet", "long")
   147 assertCantConvert("java.util.BitSet", "boolean")
   148 assertCantConvert("java.util.BitSet", "java.lang.String")
   149 assertCantConvert("java.util.BitSet", "java.lang.Double")
   150 assertCantConvert("java.util.BitSet", "java.lang.Long")
   152 /***************************************************************************
   153  * Now testing the other way round - Java arrays & collections to JavaScript
   154  **************************************************************************/
   156 function assert(x) {
   157   if(!x) {
   158     throw "Assertion failed"
   159   }
   160   ++testCount;
   161 }
   163 var intArray = new (Java.type("int[]"))(3)
   164 intArray[0] = 1234;
   165 intArray[1] = 42;
   166 intArray[2] = 5;
   167 var jsIntArray = Java.from(intArray)
   168 assert(jsIntArray instanceof Array);
   169 assert(jsIntArray[0] === 1234);
   170 assert(jsIntArray[1] === 42);
   171 assert(jsIntArray[2] === 5);
   173 // The arrays are copies, they don't reflect each other
   174 intArray[2] = 6;
   175 assert(jsIntArray[2] === 5);
   176 jsIntArray[2] = 7;
   177 assert(intArray[2] === 6);
   179 var byteArray = new (Java.type("byte[]"))(2)
   180 byteArray[0] = -128;
   181 byteArray[1] = 127;
   182 var jsByteArray = Java.from(byteArray)
   183 assert(jsByteArray instanceof Array);
   184 assert(jsByteArray[0] === -128);
   185 assert(jsByteArray[1] === 127);
   187 var shortArray = new (Java.type("short[]"))(2)
   188 shortArray[0] = -32768;
   189 shortArray[1] = 32767;
   190 var jsShortArray = Java.from(shortArray)
   191 assert(jsShortArray instanceof Array);
   192 assert(jsShortArray[0] === -32768);
   193 assert(jsShortArray[1] === 32767);
   195 var floatArray = new (Java.type("float[]"))(2)
   196 floatArray[0] = java.lang.Float.MIN_VALUE;
   197 floatArray[1] = java.lang.Float.MAX_VALUE;
   198 var jsFloatArray = Java.from(floatArray)
   199 assert(jsFloatArray instanceof Array);
   200 assert(jsFloatArray[0] == java.lang.Float.MIN_VALUE);
   201 assert(jsFloatArray[1] == java.lang.Float.MAX_VALUE);
   203 var charArray = new (Java.type("char[]"))(3)
   204 charArray[0] = "a";
   205 charArray[1] = "b";
   206 charArray[2] = "1";
   207 var jsCharArray = Java.from(charArray)
   208 assert(jsCharArray instanceof Array);
   209 assert(jsCharArray[0] === 97);
   210 assert(jsCharArray[1] === 98);
   211 assert(jsCharArray[2] === 49);
   213 var booleanArray = new (Java.type("boolean[]"))(2)
   214 booleanArray[0] = true;
   215 booleanArray[1] = false;
   216 var jsBooleanArray = Java.from(booleanArray)
   217 assert(jsBooleanArray instanceof Array);
   218 assert(jsBooleanArray[0] === true);
   219 assert(jsBooleanArray[1] === false);
   221 print(testCount + " tests completed ok")

mercurial