blob: db89f4d0b8001868d01ba6b21a48941706ce59a0 [file] [log] [blame]
Steve Blocka7e24c12009-10-30 11:49:00 +00001// 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
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
28/**
29 * @fileoverview Test concat on small and large arrays
30 */
31
Kristian Monsen25f61362010-05-21 11:50:48 +010032var poses;
33
34poses = [140, 4000000000];
35while (pos = poses.shift()) {
36 var a = new Array(pos);
37 var array_proto = [];
38 a.__proto__ = array_proto;
39 assertEquals(pos, a.length);
40 a.push('foo');
41 assertEquals(pos + 1, a.length);
42 var b = ['bar'];
43 var c = a.concat(b);
44 assertEquals(pos + 2, c.length);
45 assertEquals("undefined", typeof(c[pos - 1]));
46 assertEquals("foo", c[pos]);
47 assertEquals("bar", c[pos + 1]);
48
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');
53 c = a.concat(b);
54 assertEquals(c[124], 'doo');
55
56 // If we put a number in the prototype, then the spec says it should be
57 // copied on concat.
58 array_proto["123"] = 'baz';
59 assertEquals(a[123], 'baz');
60
61 c = a.concat(b);
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]);
67
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]);
73
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';
77 a[123] = 'xyz';
78 assertEquals('xyz', a[123]);
79 c = a.concat(b);
80 assertEquals('xyz', c[123]);
81
82 // Non-numeric properties on the prototype or the array shouldn't get
83 // copied.
84 array_proto.moe = 'joe';
85 a.ben = 'jerry';
86 assertEquals(a["moe"], 'joe');
87 assertEquals(a["ben"], 'jerry');
88 c = a.concat(b);
89 // ben was not copied
90 assertEquals("undefined", typeof(c.ben));
91
92 // When we take moe off the prototype it disappears from all arrays.
93 array_proto.moe = undefined;
94 assertEquals("undefined", typeof(c.moe));
95
96 // Negative indices don't get concated.
97 a[-1] = 'minus1';
98 assertEquals("minus1", a[-1]);
99 assertEquals("undefined", typeof(a[0xffffffff]));
100 c = a.concat(b);
101 assertEquals("undefined", typeof(c[-1]));
102 assertEquals("undefined", typeof(c[0xffffffff]));
103 assertEquals(c.length, a.length + 1);
104
105}
106
107poses = [140, 4000000000];
Steve Blocka7e24c12009-10-30 11:49:00 +0000108while (pos = poses.shift()) {
109 var a = new Array(pos);
110 assertEquals(pos, a.length);
111 a.push('foo');
112 assertEquals(pos + 1, a.length);
113 var b = ['bar'];
114 var c = a.concat(b);
115 assertEquals(pos + 2, c.length);
116 assertEquals("undefined", typeof(c[pos - 1]));
117 assertEquals("foo", c[pos]);
118 assertEquals("bar", c[pos + 1]);
119
120 // Can we fool the system by putting a number in a string?
121 var onetwofour = "124";
122 a[onetwofour] = 'doo';
123 assertEquals(a[124], 'doo');
124 c = a.concat(b);
125 assertEquals(c[124], 'doo');
126
127 // If we put a number in the prototype, then the spec says it should be
128 // copied on concat.
129 Array.prototype["123"] = 'baz';
130 assertEquals(a[123], 'baz');
131
132 c = a.concat(b);
133 assertEquals(pos + 2, c.length);
134 assertEquals("baz", c[123]);
135 assertEquals("undefined", typeof(c[pos - 1]));
136 assertEquals("foo", c[pos]);
137 assertEquals("bar", c[pos + 1]);
138
139 // When we take the number off the prototype it disappears from a, but
140 // the concat put it in c itself.
141 Array.prototype["123"] = undefined;
142 assertEquals("undefined", typeof(a[123]));
143 assertEquals("baz", c[123]);
144
145 // If the element of prototype is shadowed, the element on the instance
146 // should be copied, but not the one on the prototype.
147 Array.prototype[123] = 'baz';
148 a[123] = 'xyz';
149 assertEquals('xyz', a[123]);
150 c = a.concat(b);
151 assertEquals('xyz', c[123]);
152
153 // Non-numeric properties on the prototype or the array shouldn't get
154 // copied.
155 Array.prototype.moe = 'joe';
156 a.ben = 'jerry';
157 assertEquals(a["moe"], 'joe');
158 assertEquals(a["ben"], 'jerry');
159 c = a.concat(b);
160 // ben was not copied
161 assertEquals("undefined", typeof(c.ben));
162 // moe was not copied, but we can see it through the prototype
163 assertEquals("joe", c.moe);
164
165 // When we take moe off the prototype it disappears from all arrays.
166 Array.prototype.moe = undefined;
167 assertEquals("undefined", typeof(c.moe));
168
Kristian Monsen25f61362010-05-21 11:50:48 +0100169 // Negative indices don't get concated.
Steve Blocka7e24c12009-10-30 11:49:00 +0000170 a[-1] = 'minus1';
171 assertEquals("minus1", a[-1]);
172 assertEquals("undefined", typeof(a[0xffffffff]));
173 c = a.concat(b);
174 assertEquals("undefined", typeof(c[-1]));
175 assertEquals("undefined", typeof(c[0xffffffff]));
176 assertEquals(c.length, a.length + 1);
177
178}
179
180a = [];
181c = a.concat('Hello');
182assertEquals(1, c.length);
183assertEquals("Hello", c[0]);
184assertEquals("Hello", c.toString());
185
186// Check that concat preserves holes.
187var holey = [void 0,'a',,'c'].concat(['d',,'f',[0,,2],void 0])
188assertEquals(9, holey.length); // hole in embedded array is ignored
189for (var i = 0; i < holey.length; i++) {
190 if (i == 2 || i == 5) {
191 assertFalse(i in holey);
192 } else {
193 assertTrue(i in holey);
194 }
195}