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 * NASHORN-556: Need more tests to exercise code in jdk.nashorn.internal.runtime.array package. 26 * 27 * @test 28 * @run 29 */ 30 31 function p() { 32 var s = ""; 33 for each (var i in arguments) { 34 s += ((i !== undefined) ? i : "") + ","; 35 } 36 s = s.length != 0 ? s.substr(0, s.length - 1) : s; 37 print(s); 38 } 39 40 function assertEq(expected, actual) { 41 if (actual !== expected && !(isNaN(actual) && isNaN(expected))) { 42 throw "expected=" + expected + " actual=" + actual; 43 } 44 } 45 46 function f1() { 47 // (NoTypeArrayData) 48 var empty = {}; 49 empty.length = 10; 50 Java.to(empty); 51 delete empty[0]; 52 Array.prototype.slice.call(empty, 0, 1); 53 Array.prototype.pop.call(empty); 54 Array.prototype.shift.call(empty); 55 empty = {}; 56 empty[0] = eval("84") >>> 1; assertEq(42, empty[0]); 57 empty = {}; 58 Array.prototype.unshift.call(empty, "x"); assertEq("x", empty[0]); 59 empty = {}; 60 empty[0] = 8.4; assertEq(8.4, empty[0]); 61 } 62 63 function f2() { 64 // DeletedArrayFilter 65 var deleted = [,1,,2,,3,,4,,]; 66 assertEq(2, Java.to(deleted)[3]); 67 assertEq(undefined, deleted.pop()); 68 assertEq(4, deleted.pop()); 69 deleted.unshift(5); 70 p.apply(null, deleted); 71 assertEq(5, deleted.shift()); 72 print(deleted.slice(0,3), deleted.slice(1,7)); 73 assertEq(1, deleted[3] >>> 1); 74 deleted[3] = eval("84") >>> 1; assertEq(42, deleted[3]); 75 p.apply(null, deleted); 76 } 77 78 function f3() { 79 // DeletedRangeArrayFilter 80 var delrange = [1,2,3,,,,,,,,,,]; 81 Java.to(delrange); 82 delrange.unshift(4); 83 p.apply(null, delrange); 84 print(delrange.slice(1,3), delrange.slice(2,6)); 85 assertEq(4, delrange.shift()); 86 } 87 88 function f4() { 89 // NumberArrayData 90 var num = [1.1,2.2,3.3,4.4,5.5]; 91 Java.to(num); 92 assertEq(2, num[3] >>> 1); 93 assertEq(5, num[4] | 0); 94 assertEq(5.5, num.pop()); 95 num.unshift(13.37); 96 print(num.slice(1,4)); 97 assertEq(13.37, num.shift()); 98 p.apply(null, num); 99 num.length = 20; 100 delete num[0]; 101 num[0] = eval("14") >>> 1; 102 } 103 104 function f5() { 105 // ObjectArrayData 106 var obj = [2,"two",3.14,"pi",14,"fourteen"]; 107 Java.to(obj); 108 assertEq(-12.86, obj[2] - 16); 109 assertEq(7, obj[4] >>> 1); 110 obj.unshift("one"); 111 obj[0] = 1.3; 112 obj[0] = eval("14") >>> 1; 113 assertEq(7, obj.shift()); 114 p.apply(null, obj); 115 } 116 117 function f6() { 118 // SparseArrayData 119 var sparse = [9,8,7,6,5,4,3,2,1]; 120 sparse[0x76543210] = 84; 121 assertEq(42, sparse[0x76543210] >>> 1); 122 assertEq(42, sparse[0x76543210] - 42); 123 assertEq(85, sparse[0x76543210] | 1); 124 sparse[0x76543210] = 7.2; 125 sparse[0x76543210] = eval("84") >>> 1; 126 sparse.unshift(10); 127 print(sparse.slice(0,12)); 128 print(sparse.slice(0x76543209, 0x76543213)); 129 assertEq(10, sparse.shift()); 130 assertEq(42, sparse.pop()); 131 sparse.length = 1024*1024; 132 sparse.push(sparse.length); 133 delete sparse[sparse.length-1]; 134 //print(Java.to(sparse).length); 135 (function(){}).apply(null, sparse); 136 } 137 138 function f7() { 139 // UndefinedArrayFilter 140 var undef = [1,2,3,4,5,undefined,7,8,9,19]; 141 Java.to(undef); 142 assertEq(4, undef[8] >>> 1); 143 var tmp = undef[9] >>> 1; 144 undef[8] = tmp; 145 undef.unshift(21); 146 print(undef.slice(0, 4), undef.slice(4, 5)); 147 assertEq(21, undef.shift()); 148 undef.push(20); 149 assertEq(20, undef.pop()); 150 assertEq(19, undef.pop()); 151 p.apply(null, undef); 152 undef.length = 20; 153 } 154 155 function f8() { 156 // LongArrayData 157 var j = Java.from(Java.to([23,37,42,86,47], "long[]")); 158 Java.to(j); 159 p.apply(null, j); 160 assertEq(43, j[3] >>> 1); 161 assertEq(36, j[4] - 11); 162 j.unshift(eval("14") >>> 1); 163 print(j.slice(0,4)); 164 assertEq(7, j.shift()); 165 assertEq(47, j.pop()); 166 j.push("asdf"); 167 j = Java.from(Java.to([23,37,42,86,47], "long[]")); 168 j.length = 3; 169 j[0] = 13; 170 j = Java.from(Java.to([23,37,42,86,47], "long[]")); 171 delete j[0]; 172 j = Java.from(Java.to([23,37,42,86,47], "long[]")); 173 j.length = 20; 174 j[0] = 13.37; 175 } 176 177 function f9() { 178 // FrozenArrayFilter 179 var a1 = [10,11,12,13,14,15]; 180 Object.freeze(a1); 181 assertEq(true, Object.isFrozen(a1)); 182 Object.getOwnPropertyDescriptor(a1, 0); 183 a1[1] = 1; 184 a1[2] = eval("14") >>> 1; 185 a1[3] = 3.14; 186 a1[4] = "asdf"; 187 print(a1.slice(1,4)); 188 a1.length = 20; 189 } 190 191 function f10() { 192 // SealedArrayFilter 193 var a1 = [10,11,12,13,14,15]; 194 Object.seal(a1); 195 assertEq(true, Object.isSealed(a1)); 196 Object.getOwnPropertyDescriptor(a1, 0); 197 a1[1] = 1; 198 a1[2] = eval("14") >>> 1; 199 a1[3] = 3.14; 200 a1[4] = "asdf"; 201 print(a1.slice(1,4)); 202 delete a1[0]; 203 a1.length = 20; 204 } 205 206 f1(); 207 f2(); 208 f3(); 209 f4(); 210 f5(); 211 f6(); 212 f7(); 213 f8(); 214 f9(); 215 f10(); 216