1 // Copyright 2013 the V8 project authors. All rights reserved. 2 // Copyright (C) 2005, 2006, 2007, 2008, 2009 Apple Inc. All rights reserved. 3 // 4 // Redistribution and use in source and binary forms, with or without 5 // modification, are permitted provided that the following conditions 6 // are met: 7 // 1. Redistributions of source code must retain the above copyright 8 // notice, this list of conditions and the following disclaimer. 9 // 2. Redistributions in binary form must reproduce the above copyright 10 // notice, this list of conditions and the following disclaimer in the 11 // documentation and/or other materials provided with the distribution. 12 // 13 // THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' AND ANY 14 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 15 // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 16 // DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY 17 // DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 18 // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 19 // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON 20 // ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 21 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 22 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 23 24 description("KDE JS Test"); 25 // 15.4 Array Objects 26 // (c) 2001 Harri Porten <porten (a] kde.org> 27 28 shouldBe("Array().length", "0"); 29 shouldBe("(new Array()).length", "0"); 30 shouldBe("(new Array(3)).length", "3"); 31 shouldBe("(new Array(11, 22)).length", "2"); 32 shouldBe("(new Array(11, 22))[0]", "11"); 33 shouldBe("Array(11, 22)[1]", "22"); 34 shouldBeUndefined("(new Array(11, 22))[3]"); 35 shouldBe("String(new Array(11, 22))", "'11,22'"); 36 shouldBe("var a = []; a[0] = 33; a[0]", "33"); 37 shouldBe("var a = []; a[0] = 33; a.length", "1"); 38 shouldBe("var a = [11, 22]; a.length = 1; String(a);", "'11'"); 39 shouldBe("var a = [11, 22]; a.length = 1; a.length;", "1"); 40 41 // range checks 42 var caught = false; 43 var ename = ""; 44 try { 45 [].length = -1; 46 } catch (e) { 47 // expect Range Error 48 caught = true; 49 ename = e.name; 50 } 51 52 shouldBeTrue("caught;"); 53 shouldBe("ename", "'RangeError'"); 54 55 caught = false; 56 ename = ""; 57 try { 58 new Array(Infinity); 59 } catch (e) { 60 // expect Range Error 61 caught = true; 62 ename = e.name; 63 } 64 shouldBeTrue("caught;"); 65 shouldBe("ename", "'RangeError'"); 66 67 shouldBeUndefined("var a = [11, 22]; a.length = 1; a[1];"); 68 shouldBe("Array().toString()", "''"); 69 shouldBe("Array(3).toString()", "',,'"); 70 shouldBe("Array(11, 22).toString()", "'11,22'"); 71 shouldBe("String(Array(11, 22).concat(33))", "'11,22,33'"); 72 shouldBe("String(Array(2).concat(33, 44))", "',,33,44'"); 73 shouldBe("String(Array(2).concat(Array(2)))", "',,,'"); 74 shouldBe("String(Array(11,22).concat(Array(33,44)))", "'11,22,33,44'"); 75 shouldBe("String(Array(1,2).concat(3,Array(4,5)))", "'1,2,3,4,5'"); 76 shouldBe("var a = new Array(1,2,3); delete a[1]; String(a.concat(4))", "'1,,3,4'"); 77 78 shouldBe("[1,2,3,4].slice(1, 3).toString()", "'2,3'"); 79 shouldBe("[1,2,3,4].slice(-3, -1).toString()", "'2,3'"); 80 shouldBe("[1,2].slice(-9, 0).length", "0"); 81 shouldBe("[1,2].slice(1).toString()", "'2'"); 82 shouldBe("[1,2].slice().toString()", "'1,2'"); 83 84 // 2nd set. 85 shouldBe("(new Array('a')).length", "1"); 86 shouldBe("(new Array('a'))[0]", "'a'"); 87 shouldBeUndefined("(new Array('a'))[1]"); 88 89 shouldBe("Array('a').length", "1"); 90 shouldBe("Array('a')[0]", "'a'"); 91 92 shouldBe("String(Array())", "''"); 93 shouldBe("String(Array('a','b'))", "'a,b'"); 94 95 shouldBe("[].length", "0"); 96 shouldBe("['a'].length", "1"); 97 shouldBe("['a'][0]", "'a'"); 98 shouldBe("['a',,'c'][2]", "'c'"); 99 shouldBe("['a',undefined,'c'][1]", "undefined"); 100 shouldBe("['a',,'c'][1]", "undefined"); 101 shouldBe("1 in ['a',,'c']", "false"); 102 shouldBe("1 in ['a',undefined,'c']", "true"); 103 104 var arrayWithDeletion = ['a','b','c']; 105 delete arrayWithDeletion[1]; 106 shouldBe("1 in arrayWithDeletion", "false"); 107 108 function forInSum(_a) { 109 var s = ''; 110 for (i in _a) 111 s += _a[i]; 112 return s; 113 } 114 115 shouldBe("forInSum([])", "''"); 116 shouldBe("forInSum(Array())", "''"); 117 shouldBe("forInSum(Array('a'))", "'a'"); 118 shouldBe("forInSum([,undefined,'x','aa'])", "'undefinedxaa'"); 119 120 var a0 = []; 121 shouldBe("forInSum(a0)", "''"); 122 123 var a1 = [ 'a' ]; 124 shouldBe("forInSum(a1)", "'a'"); 125 126 shouldBe("String([].sort())", "''") 127 shouldBe("String([3,1,'2'].sort())", "'1,2,3'"); 128 shouldBe("String([,'x','aa'].sort())", "'aa,x,'"); 129 shouldBe("String([,undefined,'x','aa'].sort())", "'aa,x,,'"); 130 shouldBe("2 in [,undefined,'x','aa'].sort()", "true"); 131 shouldBe("3 in [,undefined,'x','aa'].sort()", "false"); 132 133 // sort by length 134 function comp(a, b) { 135 var la = String(a).length; 136 var lb = String(b).length; 137 if (la < lb) 138 return -1; 139 else if (la > lb) 140 return 1; 141 else 142 return 0; 143 } 144 shouldBe("var a = ['aa', 'b', 'cccc', 'ddd']; String(a.sort(comp))", "'b,aa,ddd,cccc'"); 145 146 // +/-Infinity as function return value 147 shouldBe("[0, Infinity].sort(function(a, b) { return a - b }).toString()", "'0,Infinity'"); 148 149 // Array.unshift() 150 shouldBe("[].unshift('a')", "1"); 151 shouldBe("['c'].unshift('a', 'b')", "3"); 152 shouldBe("var a = []; a.unshift('a'); String(a)", "'a'"); 153 shouldBe("var a = ['c']; a.unshift('a', 'b'); String(a)", "'a,b,c'"); 154 155 // Array.splice() 156 shouldBe("String(['a', 'b', 'c'].splice(1, 2, 'x', 'y'))", "'b,c'"); 157 158 var maxint = Math.pow(2,32)-1; 159 var arr = new Array(); 160 161 // 2^32 should not be treated as a valid array index, i.e. 162 // setting the property on the array should not result in 163 // the length being modified 164 165 arr.length = 40; 166 arr[maxint] = "test"; 167 shouldBe("arr.length","40"); 168 shouldBe("arr[maxint]","\"test\""); 169 delete arr[maxint]; 170 shouldBe("arr.length","40"); 171 shouldBe("arr[maxint]","undefined"); 172 arr[maxint-1] = "test2"; 173 shouldBe("arr.length","maxint"); 174 shouldBe("arr[maxint-1]","\"test2\""); 175 176 // Floating point numbers also should not be treated as valid array indices. 177 arr.length = 40; 178 arr[55.5] = "test"; // does fit in a JSImmediate number 179 arr[65.11111111111111111111111111111] = "test"; // does not fit in a JSImmediate number 180 shouldBe("arr.length","40"); 181 shouldBe("arr[55.5]","\"test\""); 182 shouldBe("arr[65.11111111111111111111111111111]","\"test\""); 183 delete arr[55.5]; 184 delete arr[65.11111111111111111111111111111]; 185 shouldBe("arr.length","40"); 186 shouldBe("arr[55.5]","undefined"); 187 shouldBe("arr[65.11111111111111111111111111111]","undefined"); 188 189 arr = new Array('a','b','c'); 190 arr.__proto__ = { 1: 'x' }; 191 var propnames = new Array(); 192 for (i in arr) 193 propnames.push(i); 194 propnames.sort(); 195 shouldBe("propnames.length","3"); 196 shouldBe("propnames[0]","'0'"); 197 shouldBe("propnames[1]","'1'"); 198 shouldBe("propnames[2]","'2'"); 199 200 function testToString() { 201 // backup 202 var backupNumberToString = Number.prototype.toString; 203 var backupNumberToLocaleString = Number.prototype.toLocaleString; 204 var backupRegExpToString = RegExp.prototype.toString; 205 var backupRegExpToLocaleString = RegExp.prototype.toLocaleString; 206 207 // change functions 208 Number.prototype.toString = function() { return "toString"; } 209 Number.prototype.toLocaleString = function() { return "toLocaleString"; } 210 RegExp.prototype.toString = function() { return "toString2"; } 211 RegExp.prototype.toLocaleString = function() { return "toLocaleString2"; } 212 213 // the tests 214 shouldBe("[1].toString()", "'1'"); 215 shouldBe("[1].toLocaleString()", "'toLocaleString'"); 216 Number.prototype.toLocaleString = "invalid"; 217 shouldBe("[1].toLocaleString()", "'1'"); 218 shouldBe("[/r/].toString()", "'toString2'"); 219 shouldBe("[/r/].toLocaleString()", "'toLocaleString2'"); 220 RegExp.prototype.toLocaleString = "invalid"; 221 shouldBe("[/r/].toLocaleString()", "'toString2'"); 222 223 var caught = false; 224 try { 225 [{ toString : 0 }].toString(); 226 } catch (e) { 227 caught = true; 228 } 229 shouldBeTrue("caught"); 230 231 // restore 232 Number.prototype.toString = backupNumberToString; 233 Number.prototype.toLocaleString = backupNumberToLocaleString; 234 RegExp.prototype.toString = backupRegExpToString; 235 RegExp.prototype.toLocaleString = backupRegExpToLocaleString; 236 } 237