1 // Copyright 2008 the V8 project authors. All rights reserved.
2 // Redistribution and use in source and binary forms, with or without
3 // modification, are permitted provided that the following conditions are
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.
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.
29 * @fileoverview Test concat on small and large arrays
34 poses = [140, 4000000000];
35 while (pos = poses.shift()) {
36 var a = new Array(pos);
38 a.__proto__ = array_proto;
39 assertEquals(pos, a.length);
41 assertEquals(pos + 1, a.length);
44 assertEquals(pos + 2, c.length);
45 assertEquals("undefined", typeof(c[pos - 1]));
46 assertEquals("foo", c[pos]);
47 assertEquals("bar", c[pos + 1]);
49 // Can we fool the system by putting a number in a string?
50 var onetwofour = "124";
51 a[onetwofour] = 'doo';
52 assertEquals(a[124], 'doo');
54 assertEquals(c[124], 'doo');
56 // If we put a number in the prototype, then the spec says it should be
58 array_proto["123"] = 'baz';
59 assertEquals(a[123], 'baz');
62 assertEquals(pos + 2, c.length);
63 assertEquals("baz", c[123]);
64 assertEquals("undefined", typeof(c[pos - 1]));
65 assertEquals("foo", c[pos]);
66 assertEquals("bar", c[pos + 1]);
68 // When we take the number off the prototype it disappears from a, but
69 // the concat put it in c itself.
70 array_proto["123"] = undefined;
71 assertEquals("undefined", typeof(a[123]));
72 assertEquals("baz", c[123]);
74 // If the element of prototype is shadowed, the element on the instance
75 // should be copied, but not the one on the prototype.
76 array_proto[123] = 'baz';
78 assertEquals('xyz', a[123]);
80 assertEquals('xyz', c[123]);
82 // Non-numeric properties on the prototype or the array shouldn't get
84 array_proto.moe = 'joe';
86 assertEquals(a["moe"], 'joe');
87 assertEquals(a["ben"], 'jerry');
90 assertEquals("undefined", typeof(c.ben));
92 // When we take moe off the prototype it disappears from all arrays.
93 array_proto.moe = undefined;
94 assertEquals("undefined", typeof(c.moe));
96 // Negative indices don't get concated.
98 assertEquals("minus1", a[-1]);
99 assertEquals("undefined", typeof(a[0xffffffff]));
101 assertEquals("undefined", typeof(c[-1]));
102 assertEquals("undefined", typeof(c[0xffffffff]));
103 assertEquals(c.length, a.length + 1);
106 poses = [140, 4000000000];
107 while (pos = poses.shift()) {
108 var a = new Array(pos);
109 assertEquals(pos, a.length);
111 assertEquals(pos + 1, a.length);
114 assertEquals(pos + 2, c.length);
115 assertEquals("undefined", typeof(c[pos - 1]));
116 assertEquals("foo", c[pos]);
117 assertEquals("bar", c[pos + 1]);
119 // Can we fool the system by putting a number in a string?
120 var onetwofour = "124";
121 a[onetwofour] = 'doo';
122 assertEquals(a[124], 'doo');
124 assertEquals(c[124], 'doo');
126 // If we put a number in the prototype, then the spec says it should be
128 Array.prototype["123"] = 'baz';
129 assertEquals(a[123], 'baz');
132 assertEquals(pos + 2, c.length);
133 assertEquals("baz", c[123]);
134 assertEquals("undefined", typeof(c[pos - 1]));
135 assertEquals("foo", c[pos]);
136 assertEquals("bar", c[pos + 1]);
138 // When we take the number off the prototype it disappears from a, but
139 // the concat put it in c itself.
140 Array.prototype["123"] = undefined;
141 assertEquals("undefined", typeof(a[123]));
142 assertEquals("baz", c[123]);
144 // If the element of prototype is shadowed, the element on the instance
145 // should be copied, but not the one on the prototype.
146 Array.prototype[123] = 'baz';
148 assertEquals('xyz', a[123]);
150 assertEquals('xyz', c[123]);
152 // Non-numeric properties on the prototype or the array shouldn't get
154 Array.prototype.moe = 'joe';
156 assertEquals(a["moe"], 'joe');
157 assertEquals(a["ben"], 'jerry');
159 // ben was not copied
160 assertEquals("undefined", typeof(c.ben));
161 // moe was not copied, but we can see it through the prototype
162 assertEquals("joe", c.moe);
164 // When we take moe off the prototype it disappears from all arrays.
165 Array.prototype.moe = undefined;
166 assertEquals("undefined", typeof(c.moe));
168 // Negative indices don't get concated.
170 assertEquals("minus1", a[-1]);
171 assertEquals("undefined", typeof(a[0xffffffff]));
173 assertEquals("undefined", typeof(c[-1]));
174 assertEquals("undefined", typeof(c[0xffffffff]));
175 assertEquals(c.length, a.length + 1);
180 c = a.concat('Hello');
181 assertEquals(1, c.length);
182 assertEquals("Hello", c[0]);
183 assertEquals("Hello", c.toString());
185 // Check that concat preserves holes.
186 var holey = [void 0,'a',,'c'].concat(['d',,'f',[0,,2],void 0])
187 assertEquals(9, holey.length); // hole in embedded array is ignored
188 for (var i = 0; i < holey.length; i++) {
189 if (i == 2 || i == 5) {
190 assertFalse(i in holey);
192 assertTrue(i in holey);
196 // Polluted prototype from prior tests.
197 delete Array.prototype[123];
199 // Check that concat reads getters in the correct order.
202 var r1 = [].concat(arr1, arr2); // [,2,1,3]
203 assertEquals([,2,1,3], r1);
205 // Make first array change length of second array.
206 Object.defineProperty(arr1, 0, {get: function() {
209 }, configurable: true})
210 var r2 = [].concat(arr1, arr2); // [undefined,2,1,3,"X"]
211 assertEquals([undefined,2,1,3,"X"], r2);
213 // Make first array change length of second array massively.
215 Object.defineProperty(arr1, 0, {get: function() {
218 }, configurable: true})
219 var r3 = [].concat(arr1, arr2); // [undefined,2,1,3,"X"]
220 var expected = [undefined,2,1,3];
221 expected[500000 + 2] = "X";
223 assertEquals(expected, r3);
227 var expectedTrace = []
228 function mkGetter(i) { return function() { trace.push(i); }; }
230 for (var i = 0; i < 100; i++) {
231 Object.defineProperty(arr3, i * i, {get: mkGetter(i)});
232 expectedTrace[i] = i;
233 expectedTrace[100 + i] = i;
235 var r4 = [0].concat(arr3, arr3);
236 assertEquals(1 + arr3.length * 2, r4.length);
237 assertEquals(expectedTrace, trace);