1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/test/script/basic/NASHORN-556.js Wed Apr 27 01:36:41 2016 +0800 1.3 @@ -0,0 +1,216 @@ 1.4 +/* 1.5 + * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved. 1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 1.7 + * 1.8 + * This code is free software; you can redistribute it and/or modify it 1.9 + * under the terms of the GNU General Public License version 2 only, as 1.10 + * published by the Free Software Foundation. 1.11 + * 1.12 + * This code is distributed in the hope that it will be useful, but WITHOUT 1.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1.14 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1.15 + * version 2 for more details (a copy is included in the LICENSE file that 1.16 + * accompanied this code). 1.17 + * 1.18 + * You should have received a copy of the GNU General Public License version 1.19 + * 2 along with this work; if not, write to the Free Software Foundation, 1.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 1.21 + * 1.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 1.23 + * or visit www.oracle.com if you need additional information or have any 1.24 + * questions. 1.25 + */ 1.26 + 1.27 +/* 1.28 + * NASHORN-556: Need more tests to exercise code in jdk.nashorn.internal.runtime.array package. 1.29 + * 1.30 + * @test 1.31 + * @run 1.32 + */ 1.33 + 1.34 +function p() { 1.35 + var s = ""; 1.36 + for each (var i in arguments) { 1.37 + s += ((i !== undefined) ? i : "") + ","; 1.38 + } 1.39 + s = s.length != 0 ? s.substr(0, s.length - 1) : s; 1.40 + print(s); 1.41 +} 1.42 + 1.43 +function assertEq(expected, actual) { 1.44 + if (actual !== expected && !(isNaN(actual) && isNaN(expected))) { 1.45 + throw "expected=" + expected + " actual=" + actual; 1.46 + } 1.47 +} 1.48 + 1.49 +function f1() { 1.50 + // (NoTypeArrayData) 1.51 + var empty = {}; 1.52 + empty.length = 10; 1.53 + Java.to(empty); 1.54 + delete empty[0]; 1.55 + Array.prototype.slice.call(empty, 0, 1); 1.56 + Array.prototype.pop.call(empty); 1.57 + Array.prototype.shift.call(empty); 1.58 + empty = {}; 1.59 + empty[0] = eval("84") >>> 1; assertEq(42, empty[0]); 1.60 + empty = {}; 1.61 + Array.prototype.unshift.call(empty, "x"); assertEq("x", empty[0]); 1.62 + empty = {}; 1.63 + empty[0] = 8.4; assertEq(8.4, empty[0]); 1.64 +} 1.65 + 1.66 +function f2() { 1.67 + // DeletedArrayFilter 1.68 + var deleted = [,1,,2,,3,,4,,]; 1.69 + assertEq(2, Java.to(deleted)[3]); 1.70 + assertEq(undefined, deleted.pop()); 1.71 + assertEq(4, deleted.pop()); 1.72 + deleted.unshift(5); 1.73 + p.apply(null, deleted); 1.74 + assertEq(5, deleted.shift()); 1.75 + print(deleted.slice(0,3), deleted.slice(1,7)); 1.76 + assertEq(1, deleted[3] >>> 1); 1.77 + deleted[3] = eval("84") >>> 1; assertEq(42, deleted[3]); 1.78 + p.apply(null, deleted); 1.79 +} 1.80 + 1.81 +function f3() { 1.82 + // DeletedRangeArrayFilter 1.83 + var delrange = [1,2,3,,,,,,,,,,]; 1.84 + Java.to(delrange); 1.85 + delrange.unshift(4); 1.86 + p.apply(null, delrange); 1.87 + print(delrange.slice(1,3), delrange.slice(2,6)); 1.88 + assertEq(4, delrange.shift()); 1.89 +} 1.90 + 1.91 +function f4() { 1.92 + // NumberArrayData 1.93 + var num = [1.1,2.2,3.3,4.4,5.5]; 1.94 + Java.to(num); 1.95 + assertEq(2, num[3] >>> 1); 1.96 + assertEq(5, num[4] | 0); 1.97 + assertEq(5.5, num.pop()); 1.98 + num.unshift(13.37); 1.99 + print(num.slice(1,4)); 1.100 + assertEq(13.37, num.shift()); 1.101 + p.apply(null, num); 1.102 + num.length = 20; 1.103 + delete num[0]; 1.104 + num[0] = eval("14") >>> 1; 1.105 +} 1.106 + 1.107 +function f5() { 1.108 + // ObjectArrayData 1.109 + var obj = [2,"two",3.14,"pi",14,"fourteen"]; 1.110 + Java.to(obj); 1.111 + assertEq(-12.86, obj[2] - 16); 1.112 + assertEq(7, obj[4] >>> 1); 1.113 + obj.unshift("one"); 1.114 + obj[0] = 1.3; 1.115 + obj[0] = eval("14") >>> 1; 1.116 + assertEq(7, obj.shift()); 1.117 + p.apply(null, obj); 1.118 +} 1.119 + 1.120 +function f6() { 1.121 + // SparseArrayData 1.122 + var sparse = [9,8,7,6,5,4,3,2,1]; 1.123 + sparse[0x76543210] = 84; 1.124 + assertEq(42, sparse[0x76543210] >>> 1); 1.125 + assertEq(42, sparse[0x76543210] - 42); 1.126 + assertEq(85, sparse[0x76543210] | 1); 1.127 + sparse[0x76543210] = 7.2; 1.128 + sparse[0x76543210] = eval("84") >>> 1; 1.129 + sparse.unshift(10); 1.130 + print(sparse.slice(0,12)); 1.131 + print(sparse.slice(0x76543209, 0x76543213)); 1.132 + assertEq(10, sparse.shift()); 1.133 + assertEq(42, sparse.pop()); 1.134 + sparse.length = 1024*1024; 1.135 + sparse.push(sparse.length); 1.136 + delete sparse[sparse.length-1]; 1.137 + //print(Java.to(sparse).length); 1.138 + (function(){}).apply(null, sparse); 1.139 +} 1.140 + 1.141 +function f7() { 1.142 + // UndefinedArrayFilter 1.143 + var undef = [1,2,3,4,5,undefined,7,8,9,19]; 1.144 + Java.to(undef); 1.145 + assertEq(4, undef[8] >>> 1); 1.146 + var tmp = undef[9] >>> 1; 1.147 + undef[8] = tmp; 1.148 + undef.unshift(21); 1.149 + print(undef.slice(0, 4), undef.slice(4, 5)); 1.150 + assertEq(21, undef.shift()); 1.151 + undef.push(20); 1.152 + assertEq(20, undef.pop()); 1.153 + assertEq(19, undef.pop()); 1.154 + p.apply(null, undef); 1.155 + undef.length = 20; 1.156 +} 1.157 + 1.158 +function f8() { 1.159 + // LongArrayData 1.160 + var j = Java.from(Java.to([23,37,42,86,47], "long[]")); 1.161 + Java.to(j); 1.162 + p.apply(null, j); 1.163 + assertEq(43, j[3] >>> 1); 1.164 + assertEq(36, j[4] - 11); 1.165 + j.unshift(eval("14") >>> 1); 1.166 + print(j.slice(0,4)); 1.167 + assertEq(7, j.shift()); 1.168 + assertEq(47, j.pop()); 1.169 + j.push("asdf"); 1.170 + j = Java.from(Java.to([23,37,42,86,47], "long[]")); 1.171 + j.length = 3; 1.172 + j[0] = 13; 1.173 + j = Java.from(Java.to([23,37,42,86,47], "long[]")); 1.174 + delete j[0]; 1.175 + j = Java.from(Java.to([23,37,42,86,47], "long[]")); 1.176 + j.length = 20; 1.177 + j[0] = 13.37; 1.178 +} 1.179 + 1.180 +function f9() { 1.181 + // FrozenArrayFilter 1.182 + var a1 = [10,11,12,13,14,15]; 1.183 + Object.freeze(a1); 1.184 + assertEq(true, Object.isFrozen(a1)); 1.185 + Object.getOwnPropertyDescriptor(a1, 0); 1.186 + a1[1] = 1; 1.187 + a1[2] = eval("14") >>> 1; 1.188 + a1[3] = 3.14; 1.189 + a1[4] = "asdf"; 1.190 + print(a1.slice(1,4)); 1.191 + a1.length = 20; 1.192 +} 1.193 + 1.194 +function f10() { 1.195 + // SealedArrayFilter 1.196 + var a1 = [10,11,12,13,14,15]; 1.197 + Object.seal(a1); 1.198 + assertEq(true, Object.isSealed(a1)); 1.199 + Object.getOwnPropertyDescriptor(a1, 0); 1.200 + a1[1] = 1; 1.201 + a1[2] = eval("14") >>> 1; 1.202 + a1[3] = 3.14; 1.203 + a1[4] = "asdf"; 1.204 + print(a1.slice(1,4)); 1.205 + delete a1[0]; 1.206 + a1.length = 20; 1.207 +} 1.208 + 1.209 +f1(); 1.210 +f2(); 1.211 +f3(); 1.212 +f4(); 1.213 +f5(); 1.214 +f6(); 1.215 +f7(); 1.216 +f8(); 1.217 +f9(); 1.218 +f10(); 1.219 +