blob: 887097db61c151d82a41ffb11f9b17395954130b [file] [log] [blame]
Andrei Popescu402d9372010-02-26 13:31:12 +00001// Copyright 2010 the V8 project authors. All rights reserved.
Steve Blocka7e24c12009-10-30 11:49:00 +00002// Redistribution and use in source and binary forms, with or without
3// modification, are permitted provided that the following conditions are
4// met:
5//
6// * Redistributions of source code must retain the above copyright
7// notice, this list of conditions and the following disclaimer.
8// * Redistributions in binary form must reproduce the above
9// copyright notice, this list of conditions and the following
10// disclaimer in the documentation and/or other materials provided
11// with the distribution.
12// * Neither the name of Google Inc. nor the names of its
13// contributors may be used to endorse or promote products derived
14// from this software without specific prior written permission.
15//
16// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
Andrei Popescu402d9372010-02-26 13:31:12 +000028// Check that splicing array of holes keeps it as array of holes
29(function() {
30 for (var i = 0; i < 7; i++) {
31 var array = new Array(10);
32 var spliced = array.splice(1, 1, 'one', 'two');
33 assertEquals(1, spliced.length);
Steve Block6ded16b2010-05-10 14:33:55 +010034 assertFalse(0 in spliced, "0 in spliced");
Steve Blocka7e24c12009-10-30 11:49:00 +000035
Andrei Popescu402d9372010-02-26 13:31:12 +000036 assertEquals(11, array.length);
Steve Block6ded16b2010-05-10 14:33:55 +010037 assertFalse(0 in array, "0 in array");
Andrei Popescu402d9372010-02-26 13:31:12 +000038 assertTrue(1 in array);
39 assertTrue(2 in array);
Steve Block6ded16b2010-05-10 14:33:55 +010040 assertFalse(3 in array, "3 in array");
41 }
42})();
43
44
45// Check various variants of empty array's splicing.
46(function() {
47 for (var i = 0; i < 7; i++) {
48 assertEquals([], [].splice(0, 0));
49 assertEquals([], [].splice(1, 0));
50 assertEquals([], [].splice(0, 1));
51 assertEquals([], [].splice(-1, 0));
52 }
53})();
54
55
56// Check that even if result array is empty, receiver gets sliced.
57(function() {
58 for (var i = 0; i < 7; i++) {
59 var a = [1, 2, 3];
60 assertEquals([], a.splice(1, 0, 'a', 'b', 'c'));
61 assertEquals([1, 'a', 'b', 'c', 2, 3], a);
Steve Blocka7e24c12009-10-30 11:49:00 +000062 }
Andrei Popescu402d9372010-02-26 13:31:12 +000063})();
Steve Blocka7e24c12009-10-30 11:49:00 +000064
Andrei Popescu402d9372010-02-26 13:31:12 +000065
66// Check various forms of arguments omission.
67(function() {
68 var array;
69 for (var i = 0; i < 7; i++) {
70 // SpiderMonkey and JSC return undefined in the case where no
71 // arguments are given instead of using the implicit undefined
72 // arguments. This does not follow ECMA-262, but we do the same for
73 // compatibility.
74 // TraceMonkey follows ECMA-262 though.
75 array = [1, 2, 3]
76 assertEquals(undefined, array.splice());
77 assertEquals([1, 2, 3], array);
78
79 // SpiderMonkey, TraceMonkey and JSC treat the case where no delete count is
80 // given differently from when an undefined delete count is given.
81 // This does not follow ECMA-262, but we do the same for
82 // compatibility.
83 array = [1, 2, 3]
84 assertEquals([1, 2, 3], array.splice(0));
85 assertEquals([], array);
86
87 array = [1, 2, 3]
88 assertEquals([1, 2, 3], array.splice(undefined));
89 assertEquals([], array);
90
91 array = [1, 2, 3]
92 assertEquals([1, 2, 3], array.splice("foobar"));
93 assertEquals([], array);
94
95 array = [1, 2, 3]
96 assertEquals([], array.splice(undefined, undefined));
97 assertEquals([1, 2, 3], array);
98
99 array = [1, 2, 3]
100 assertEquals([], array.splice("foobar", undefined));
101 assertEquals([1, 2, 3], array);
102
103 array = [1, 2, 3]
104 assertEquals([], array.splice(undefined, "foobar"));
105 assertEquals([1, 2, 3], array);
106
107 array = [1, 2, 3]
108 assertEquals([], array.splice("foobar", "foobar"));
109 assertEquals([1, 2, 3], array);
110 }
111})();
112
113
114// Check variants of negatives and positive indices.
115(function() {
116 var array, spliced;
117 for (var i = 0; i < 7; i++) {
118 array = [1, 2, 3, 4, 5, 6, 7];
119 spliced = array.splice(-100);
120 assertEquals([], array);
121 assertEquals([1, 2, 3, 4, 5, 6, 7], spliced);
122
123 array = [1, 2, 3, 4, 5, 6, 7];
124 spliced = array.splice(-3);
125 assertEquals([1, 2, 3, 4], array);
126 assertEquals([5, 6, 7], spliced);
127
128 array = [1, 2, 3, 4, 5, 6, 7];
129 spliced = array.splice(4);
130 assertEquals([1, 2, 3, 4], array);
131 assertEquals([5, 6, 7], spliced);
132
133 array = [1, 2, 3, 4, 5, 6, 7];
134 spliced = array.splice(6);
135 assertEquals([1, 2, 3, 4, 5, 6], array);
136 assertEquals([7], spliced);
137
138 array = [1, 2, 3, 4, 5, 6, 7];
139 spliced = array.splice(7);
140 assertEquals([1, 2, 3, 4, 5, 6, 7], array);
141 assertEquals([], spliced);
142
143 array = [1, 2, 3, 4, 5, 6, 7];
144 spliced = array.splice(8);
145 assertEquals([1, 2, 3, 4, 5, 6, 7], array);
146 assertEquals([], spliced);
147
148 array = [1, 2, 3, 4, 5, 6, 7];
149 spliced = array.splice(100);
150 assertEquals([1, 2, 3, 4, 5, 6, 7], array);
151 assertEquals([], spliced);
152
153 array = [1, 2, 3, 4, 5, 6, 7];
154 spliced = array.splice(0, -100);
155 assertEquals([1, 2, 3, 4, 5, 6, 7], array);
156 assertEquals([], spliced);
157
158 array = [1, 2, 3, 4, 5, 6, 7];
159 spliced = array.splice(0, -3);
160 assertEquals([1, 2, 3, 4, 5, 6, 7], array);
161 assertEquals([], spliced);
162
163 array = [1, 2, 3, 4, 5, 6, 7];
164 spliced = array.splice(0, 4);
165 assertEquals([5, 6, 7], array);
166 assertEquals([1, 2, 3, 4], spliced);
167
168 array = [1, 2, 3, 4, 5, 6, 7];
169 spliced = array.splice(0, 6);
170 assertEquals([7], array);
171 assertEquals([1, 2, 3, 4, 5, 6], spliced);
172
173 array = [1, 2, 3, 4, 5, 6, 7];
174 spliced = array.splice(0, 7);
175 assertEquals([], array);
176 assertEquals([1, 2, 3, 4, 5, 6, 7], spliced);
177
178 array = [1, 2, 3, 4, 5, 6, 7];
179 spliced = array.splice(0, 8);
180 assertEquals([], array);
181 assertEquals([1, 2, 3, 4, 5, 6, 7], spliced);
182
183 array = [1, 2, 3, 4, 5, 6, 7];
184 spliced = array.splice(0, 100);
185 assertEquals([], array);
186 assertEquals([1, 2, 3, 4, 5, 6, 7], spliced);
187
188 // Some exotic cases.
189 obj = { toString: function() { throw 'Exception'; } };
190
191 // Throwing an exception in conversion:
192 try {
193 [1, 2, 3].splice(obj, 3);
194 throw 'Should have thrown';
195 } catch (e) {
196 assertEquals('Exception', e);
Steve Blocka7e24c12009-10-30 11:49:00 +0000197 }
198
Andrei Popescu402d9372010-02-26 13:31:12 +0000199 try {
200 [1, 2, 3].splice(0, obj, 3);
201 throw 'Should have thrown';
202 } catch (e) {
203 assertEquals('Exception', e);
Steve Blocka7e24c12009-10-30 11:49:00 +0000204 }
205
Andrei Popescu402d9372010-02-26 13:31:12 +0000206 array = [1, 2, 3];
207 array.splice(0, 3, obj);
208 assertEquals(1, array.length);
Steve Blocka7e24c12009-10-30 11:49:00 +0000209
Andrei Popescu402d9372010-02-26 13:31:12 +0000210 // Custom conversion:
211 array = [1, 2, 3];
212 spliced = array.splice({valueOf: function() { return 1; }},
213 {toString: function() { return 2; }},
214 'one', 'two');
215 assertEquals([2, 3], spliced);
216 assertEquals([1, 'one', 'two'], array);
Steve Blocka7e24c12009-10-30 11:49:00 +0000217 }
Andrei Popescu402d9372010-02-26 13:31:12 +0000218})();
Steve Blocka7e24c12009-10-30 11:49:00 +0000219
220
Andrei Popescu402d9372010-02-26 13:31:12 +0000221// Nasty: modify the array in ToInteger.
222(function() {
223 var array = [];
224 var spliced;
Steve Blocka7e24c12009-10-30 11:49:00 +0000225
Andrei Popescu402d9372010-02-26 13:31:12 +0000226 for (var i = 0; i < 13; i++) {
227 bad_start = { valueOf: function() { array.push(2*i); return -1; } };
228 bad_count = { valueOf: function() { array.push(2*i + 1); return 1; } };
229 spliced = array.splice(bad_start, bad_count);
230 // According to the spec (15.4.4.12), length is calculated before
231 // performing ToInteger on arguments. However, v8 ignores elements
232 // we add while converting, so we need corrective pushes.
233 array.push(2*i); array.push(2*i + 1);
234 if (i == 0) {
235 assertEquals([], spliced); // Length was 0, nothing to get.
236 assertEquals([0, 1], array);
237 } else {
238 // When we start splice, array is [0 .. 2*i - 1], so we get
239 // as a result [2*i], this element is removed from the array,
240 // but [2 * i, 2 * i + 1] are added.
241 assertEquals([2 * i - 1], spliced);
242 assertEquals(2 * i, array[i]);
243 assertEquals(2 * i + 1, array[i + 1]);
244 }
245 }
246})();
247
248
249// Now check the case with array of holes and some elements on prototype.
250(function() {
251 var len = 9;
252
253 var at3 = "@3";
254 var at7 = "@7";
255
256 for (var i = 0; i < 7; i++) {
257 var array = new Array(len);
258 Array.prototype[3] = at3;
259 Array.prototype[7] = at7;
260
261 var spliced = array.splice(2, 2, 'one', undefined, 'two');
262
263 // Second hole (at index 3) of array turns into
264 // value of Array.prototype[3] while copying.
265 assertEquals([, at3], spliced);
266 assertEquals([, , 'one', undefined, 'two', , , at7, at7, ,], array);
267
268 // ... but array[7] is actually a hole:
269 assertTrue(delete Array.prototype[7]);
270 assertEquals(undefined, array[7]);
271
272 // and now check hasOwnProperty
Steve Block6ded16b2010-05-10 14:33:55 +0100273 assertFalse(array.hasOwnProperty(0), "array.hasOwnProperty(0)");
274 assertFalse(array.hasOwnProperty(1), "array.hasOwnProperty(1)");
Andrei Popescu402d9372010-02-26 13:31:12 +0000275 assertTrue(array.hasOwnProperty(2));
276 assertTrue(array.hasOwnProperty(3));
277 assertTrue(array.hasOwnProperty(4));
Steve Block6ded16b2010-05-10 14:33:55 +0100278 assertFalse(array.hasOwnProperty(5), "array.hasOwnProperty(5)");
279 assertFalse(array.hasOwnProperty(6), "array.hasOwnProperty(6)");
280 assertFalse(array.hasOwnProperty(7), "array.hasOwnProperty(7)");
Andrei Popescu402d9372010-02-26 13:31:12 +0000281 assertTrue(array.hasOwnProperty(8));
Steve Block6ded16b2010-05-10 14:33:55 +0100282 assertFalse(array.hasOwnProperty(9), "array.hasOwnProperty(9)");
Andrei Popescu402d9372010-02-26 13:31:12 +0000283
284 // and now check couple of indices above length.
Steve Block6ded16b2010-05-10 14:33:55 +0100285 assertFalse(array.hasOwnProperty(10), "array.hasOwnProperty(10)");
286 assertFalse(array.hasOwnProperty(15), "array.hasOwnProperty(15)");
287 assertFalse(array.hasOwnProperty(31), "array.hasOwnProperty(31)");
288 assertFalse(array.hasOwnProperty(63), "array.hasOwnProperty(63)");
289 assertFalse(array.hasOwnProperty(2 << 32 - 1), "array.hasOwnProperty(2 << 31 - 1)");
Andrei Popescu402d9372010-02-26 13:31:12 +0000290 }
291})();
292
293
294// Check the behaviour when approaching maximal values for length.
295(function() {
296 for (var i = 0; i < 7; i++) {
297 try {
298 new Array((1 << 32) - 3).splice(-1, 0, 1, 2, 3, 4, 5);
299 throw 'Should have thrown RangeError';
300 } catch (e) {
301 assertTrue(e instanceof RangeError);
302 }
303
304 // Check smi boundary
305 var bigNum = (1 << 30) - 3;
306 var array = new Array(bigNum);
307 array.splice(-1, 0, 1, 2, 3, 4, 5, 6, 7);
308 assertEquals(bigNum + 7, array.length);
309 }
310})();
Steve Block6ded16b2010-05-10 14:33:55 +0100311
312(function() {
313 for (var i = 0; i < 7; i++) {
314 var a = [7, 8, 9];
315 a.splice(0, 0, 1, 2, 3, 4, 5, 6);
316 assertEquals([1, 2, 3, 4, 5, 6, 7, 8, 9], a);
317 assertFalse(a.hasOwnProperty(10), "a.hasOwnProperty(10)");
318 assertEquals(undefined, a[10]);
319 }
320})();