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 */ 23 24/** 25 * Basic Array tests. 26 * 27 * @test 28 * @run 29 */ 30 31var arr = new Array(3); 32print(arr.length); 33 34print("isArray.length = " + Array.isArray.length); 35print(Array.isArray(44)); 36print(Array.isArray([44])); 37 38function even(num) { 39 return (num % 2) == 0; 40} 41 42print("join.length = " + Array.prototype.join.length); 43print(["javascript", "is", "great"].join("<->")); 44 45var arr = [4, 56, 5]; 46print("every.length = " + Array.prototype.every.length); 47print(arr.toString() + " every even? = " + arr.every(even)); 48arr = [4, 56, 688]; 49print(arr.toString() + " every even? = " + arr.every(even)); 50 51print("some.length = " + Array.prototype.some.length); 52arr = [4, 56, 5]; 53print(arr.toString() + " some even? = " + arr.some(even)); 54arr = [3, 5, 17]; 55print(arr.toString() + " some even? = " + arr.some(even)); 56 57print("forEach.length = " + Array.prototype.forEach.length); 58arr = [ "java", "javascript", "jython", "jruby"]; 59arr.forEach(function(val, idx, obj) { 60 print(obj.toString() + "[" + idx + "] is " + val); 61}); 62 63print(arr.map(function(val) { return val.toUpperCase(); })); 64print("shifted is " + arr.shift() + ", remaining is " + arr.toString() + ", length is " + arr.length); 65 66arr = [ "c++", "java", "javascript", "objective c" ]; 67print(arr.filter(function(val) { return val.charAt(0) == 'j'; })); 68 69print([3, 66, 2, 44].reduce(function (acc, e) { return acc + e; })); 70print([1, 2, 3, 4, 5].reduce(function (acc, e) { return acc * e; })); 71 72print(arr.reduce( 73 function(acc, e) { return acc + " " + e; } 74)); 75 76print(["javascript", "from", "world", "hello"].reduceRight( 77 function(acc, x) { return acc + " " + x; } 78)); 79 80var langs = ["java", "javascript", "jython", "jruby", "c"]; 81print("indexOf.length = " + Array.prototype.indexOf.length); 82print("indexOf('java') = " + langs.indexOf("java")); 83print("indexOf('javascript') = " + langs.indexOf("javascript")); 84print("indexOf('javascript', 3) = " + langs.indexOf("javascript", 3)); 85print("indexOf('c++') = " + langs.indexOf("c++")); 86print("[].indexOf('any') = " + [].indexOf("any")); 87 88langs = ["java", "javascript", "jython", "jruby", "java", "jython", "c"]; 89print("lastIndexOf.length = " + Array.prototype.lastIndexOf.length); 90print("lastIndexOf('java') = " + langs.lastIndexOf("java")); 91print("lastIndexOf('jython') = " + langs.lastIndexOf("jython")); 92print("lastIndexOf('c') = " + langs.lastIndexOf("c")); 93print("lastIndexOf('c++') = " + langs.lastIndexOf("c++")); 94print("[].lastIndexOf('any') = " + [].lastIndexOf("any")); 95 96print("concat.length = " + Array.prototype.concat.length); 97print(["foo", "bar"].concat(["x", "y"], 34, "sss", [3, 4, 2])); 98 99 100// Check various array length arguments to constructor 101 102function expectRangeError(length) { 103 try { 104 var arr = new Array(length); 105 print("range error expected for " + length); 106 } catch (e) { 107 if (! (e instanceof RangeError)) { 108 print("range error expected for " + length); 109 } 110 } 111} 112 113expectRangeError(NaN); 114expectRangeError(Infinity); 115expectRangeError(-Infinity); 116expectRangeError(-10); 117 118var arr = new Array("10"); 119if (arr.length != 1 && arr[0] != '10') { 120 throw new Error("expected length 1 array"); 121} 122 123arr = new Array(new Number(34)); 124if (arr.length != 1 && arr[0] != new Number(34)) { 125 throw new Error("expected length 1 array"); 126} 127 128arr = new Array(15); 129if (arr.length != 15) { 130 throw new Error("expected length 15 array"); 131} 132 133print("Array.length = " + Array.length); 134 135print([NaN,NaN,NaN]); 136 137// check setting array's length 138arr = [3,2,1]; 139arr.length = 1; 140print(arr); 141print(arr.length); 142 143// test typeof array 144var numberArray = []; 145numberArray[0] = 1; 146print(typeof numberArray[0]); 147 148print(numberArray.toLocaleString()); 149 150// Array functions on non-array objects 151 152print(Array.prototype.join.call(new java.lang.Object())); 153print(Array.prototype.concat.call("hello", "world")); 154print(Array.prototype.map.call("hello", function() {})); 155print(Array.prototype.reduce.call("hello", function() {})); 156print(Array.prototype.toString.call(new java.lang.Object())); 157print(Array.prototype.toLocaleString.call(new java.lang.Object())); 158print(Array.prototype.reduceRight.call(new java.lang.Object(), 159 function() {}, 33)); 160 161