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