aoqi@0: /* aoqi@0: * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved. aoqi@0: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. aoqi@0: * aoqi@0: * This code is free software; you can redistribute it and/or modify it aoqi@0: * under the terms of the GNU General Public License version 2 only, as aoqi@0: * published by the Free Software Foundation. aoqi@0: * aoqi@0: * This code is distributed in the hope that it will be useful, but WITHOUT aoqi@0: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or aoqi@0: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License aoqi@0: * version 2 for more details (a copy is included in the LICENSE file that aoqi@0: * accompanied this code). aoqi@0: * aoqi@0: * You should have received a copy of the GNU General Public License version aoqi@0: * 2 along with this work; if not, write to the Free Software Foundation, aoqi@0: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. aoqi@0: * aoqi@0: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA aoqi@0: * or visit www.oracle.com if you need additional information or have any aoqi@0: * questions. aoqi@0: */ aoqi@0: aoqi@0: /** aoqi@0: * Basic Array tests. aoqi@0: * aoqi@0: * @test aoqi@0: * @run aoqi@0: */ aoqi@0: aoqi@0: var arr = new Array(3); aoqi@0: print(arr.length); aoqi@0: aoqi@0: print("isArray.length = " + Array.isArray.length); aoqi@0: print(Array.isArray(44)); aoqi@0: print(Array.isArray([44])); aoqi@0: aoqi@0: function even(num) { aoqi@0: return (num % 2) == 0; aoqi@0: } aoqi@0: aoqi@0: print("join.length = " + Array.prototype.join.length); aoqi@0: print(["javascript", "is", "great"].join("<->")); aoqi@0: aoqi@0: var arr = [4, 56, 5]; aoqi@0: print("every.length = " + Array.prototype.every.length); aoqi@0: print(arr.toString() + " every even? = " + arr.every(even)); aoqi@0: arr = [4, 56, 688]; aoqi@0: print(arr.toString() + " every even? = " + arr.every(even)); aoqi@0: aoqi@0: print("some.length = " + Array.prototype.some.length); aoqi@0: arr = [4, 56, 5]; aoqi@0: print(arr.toString() + " some even? = " + arr.some(even)); aoqi@0: arr = [3, 5, 17]; aoqi@0: print(arr.toString() + " some even? = " + arr.some(even)); aoqi@0: aoqi@0: print("forEach.length = " + Array.prototype.forEach.length); aoqi@0: arr = [ "java", "javascript", "jython", "jruby"]; aoqi@0: arr.forEach(function(val, idx, obj) { aoqi@0: print(obj.toString() + "[" + idx + "] is " + val); aoqi@0: }); aoqi@0: aoqi@0: print(arr.map(function(val) { return val.toUpperCase(); })); aoqi@0: print("shifted is " + arr.shift() + ", remaining is " + arr.toString() + ", length is " + arr.length); aoqi@0: aoqi@0: arr = [ "c++", "java", "javascript", "objective c" ]; aoqi@0: print(arr.filter(function(val) { return val.charAt(0) == 'j'; })); aoqi@0: aoqi@0: print([3, 66, 2, 44].reduce(function (acc, e) { return acc + e; })); aoqi@0: print([1, 2, 3, 4, 5].reduce(function (acc, e) { return acc * e; })); aoqi@0: aoqi@0: print(arr.reduce( aoqi@0: function(acc, e) { return acc + " " + e; } aoqi@0: )); aoqi@0: aoqi@0: print(["javascript", "from", "world", "hello"].reduceRight( aoqi@0: function(acc, x) { return acc + " " + x; } aoqi@0: )); aoqi@0: aoqi@0: var langs = ["java", "javascript", "jython", "jruby", "c"]; aoqi@0: print("indexOf.length = " + Array.prototype.indexOf.length); aoqi@0: print("indexOf('java') = " + langs.indexOf("java")); aoqi@0: print("indexOf('javascript') = " + langs.indexOf("javascript")); aoqi@0: print("indexOf('javascript', 3) = " + langs.indexOf("javascript", 3)); aoqi@0: print("indexOf('c++') = " + langs.indexOf("c++")); aoqi@0: print("[].indexOf('any') = " + [].indexOf("any")); aoqi@0: aoqi@0: langs = ["java", "javascript", "jython", "jruby", "java", "jython", "c"]; aoqi@0: print("lastIndexOf.length = " + Array.prototype.lastIndexOf.length); aoqi@0: print("lastIndexOf('java') = " + langs.lastIndexOf("java")); aoqi@0: print("lastIndexOf('jython') = " + langs.lastIndexOf("jython")); aoqi@0: print("lastIndexOf('c') = " + langs.lastIndexOf("c")); aoqi@0: print("lastIndexOf('c++') = " + langs.lastIndexOf("c++")); aoqi@0: print("[].lastIndexOf('any') = " + [].lastIndexOf("any")); aoqi@0: aoqi@0: print("concat.length = " + Array.prototype.concat.length); aoqi@0: print(["foo", "bar"].concat(["x", "y"], 34, "sss", [3, 4, 2])); aoqi@0: aoqi@0: aoqi@0: // Check various array length arguments to constructor aoqi@0: aoqi@0: function expectRangeError(length) { aoqi@0: try { aoqi@0: var arr = new Array(length); aoqi@0: print("range error expected for " + length); aoqi@0: } catch (e) { aoqi@0: if (! (e instanceof RangeError)) { aoqi@0: print("range error expected for " + length); aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: expectRangeError(NaN); aoqi@0: expectRangeError(Infinity); aoqi@0: expectRangeError(-Infinity); aoqi@0: expectRangeError(-10); aoqi@0: aoqi@0: var arr = new Array("10"); aoqi@0: if (arr.length != 1 && arr[0] != '10') { aoqi@0: throw new Error("expected length 1 array"); aoqi@0: } aoqi@0: aoqi@0: arr = new Array(new Number(34)); aoqi@0: if (arr.length != 1 && arr[0] != new Number(34)) { aoqi@0: throw new Error("expected length 1 array"); aoqi@0: } aoqi@0: aoqi@0: arr = new Array(15); aoqi@0: if (arr.length != 15) { aoqi@0: throw new Error("expected length 15 array"); aoqi@0: } aoqi@0: aoqi@0: print("Array.length = " + Array.length); aoqi@0: aoqi@0: print([NaN,NaN,NaN]); aoqi@0: aoqi@0: // check setting array's length aoqi@0: arr = [3,2,1]; aoqi@0: arr.length = 1; aoqi@0: print(arr); aoqi@0: print(arr.length); aoqi@0: aoqi@0: // test typeof array aoqi@0: var numberArray = []; aoqi@0: numberArray[0] = 1; aoqi@0: print(typeof numberArray[0]); aoqi@0: aoqi@0: print(numberArray.toLocaleString()); aoqi@0: aoqi@0: // Array functions on non-array objects aoqi@0: aoqi@0: print(Array.prototype.join.call(new java.lang.Object())); aoqi@0: print(Array.prototype.concat.call("hello", "world")); aoqi@0: print(Array.prototype.map.call("hello", function() {})); aoqi@0: print(Array.prototype.reduce.call("hello", function() {})); aoqi@0: print(Array.prototype.toString.call(new java.lang.Object())); aoqi@0: print(Array.prototype.toLocaleString.call(new java.lang.Object())); aoqi@0: print(Array.prototype.reduceRight.call(new java.lang.Object(), aoqi@0: function() {}, 33)); aoqi@0: