array-concat.js 8.67 KB
Newer Older
1
// Copyright 2008 the V8 project authors. All rights reserved.
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
//     * Redistributions of source code must retain the above copyright
//       notice, this list of conditions and the following disclaimer.
//     * Redistributions in binary form must reproduce the above
//       copyright notice, this list of conditions and the following
//       disclaimer in the documentation and/or other materials provided
//       with the distribution.
//     * Neither the name of Google Inc. nor the names of its
//       contributors may be used to endorse or promote products derived
//       from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

/**
 * @fileoverview Test concat on small and large arrays
 */

32 33 34 35 36 37 38 39 40 41 42 43 44

(function testStringWrapperConcat() {
  var concat = Array.prototype.concat;
  var str = new String('abcd');
  assertEquals([1,2,3,new String('abcd')], [1, 2, 3].concat(str));
  assertEquals([new String("abcd")], concat.call(str));

  var array = [1, 2, 3];
  array.__proto__ = str;
  array.length = 4;
  assertEquals([1,2,3,'d'], concat.call(array));
})()

45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
var poses;

poses = [140, 4000000000];
while (pos = poses.shift()) {
  var a = new Array(pos);
  var array_proto = [];
  a.__proto__ = array_proto;
  assertEquals(pos, a.length);
  a.push('foo');
  assertEquals(pos + 1, a.length);
  var b = ['bar'];
  var c = a.concat(b);
  assertEquals(pos + 2, c.length);
  assertEquals("undefined", typeof(c[pos - 1]));
  assertEquals("foo", c[pos]);
  assertEquals("bar", c[pos + 1]);

  // Can we fool the system by putting a number in a string?
  var onetwofour = "124";
  a[onetwofour] = 'doo';
  assertEquals(a[124], 'doo');
  c = a.concat(b);
  assertEquals(c[124], 'doo');

  // If we put a number in the prototype, then the spec says it should be
  // copied on concat.
  array_proto["123"] = 'baz';
  assertEquals(a[123], 'baz');

  c = a.concat(b);
  assertEquals(pos + 2, c.length);
  assertEquals("baz", c[123]);
  assertEquals("undefined", typeof(c[pos - 1]));
  assertEquals("foo", c[pos]);
  assertEquals("bar", c[pos + 1]);

  // When we take the number off the prototype it disappears from a, but
  // the concat put it in c itself.
  array_proto["123"] = undefined;
  assertEquals("undefined", typeof(a[123]));
  assertEquals("baz", c[123]);

  // If the element of prototype is shadowed, the element on the instance
  // should be copied, but not the one on the prototype.
  array_proto[123] = 'baz';
  a[123] = 'xyz';
  assertEquals('xyz', a[123]);
  c = a.concat(b);
  assertEquals('xyz', c[123]);

  // Non-numeric properties on the prototype or the array shouldn't get
  // copied.
  array_proto.moe = 'joe';
  a.ben = 'jerry';
  assertEquals(a["moe"], 'joe');
  assertEquals(a["ben"], 'jerry');
  c = a.concat(b);
  // ben was not copied
  assertEquals("undefined", typeof(c.ben));

  // When we take moe off the prototype it disappears from all arrays.
  array_proto.moe = undefined;
  assertEquals("undefined", typeof(c.moe));

  // Negative indices don't get concated.
  a[-1] = 'minus1';
  assertEquals("minus1", a[-1]);
  assertEquals("undefined", typeof(a[0xffffffff]));
  c = a.concat(b);
  assertEquals("undefined", typeof(c[-1]));
  assertEquals("undefined", typeof(c[0xffffffff]));
  assertEquals(c.length, a.length + 1);
}

poses = [140, 4000000000];
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
while (pos = poses.shift()) {
  var a = new Array(pos);
  assertEquals(pos, a.length);
  a.push('foo');
  assertEquals(pos + 1, a.length);
  var b = ['bar'];
  var c = a.concat(b);
  assertEquals(pos + 2, c.length);
  assertEquals("undefined", typeof(c[pos - 1]));
  assertEquals("foo", c[pos]);
  assertEquals("bar", c[pos + 1]);

  // Can we fool the system by putting a number in a string?
  var onetwofour = "124";
  a[onetwofour] = 'doo';
  assertEquals(a[124], 'doo');
  c = a.concat(b);
  assertEquals(c[124], 'doo');

  // If we put a number in the prototype, then the spec says it should be
  // copied on concat.
  Array.prototype["123"] = 'baz';
  assertEquals(a[123], 'baz');

  c = a.concat(b);
  assertEquals(pos + 2, c.length);
  assertEquals("baz", c[123]);
  assertEquals("undefined", typeof(c[pos - 1]));
  assertEquals("foo", c[pos]);
  assertEquals("bar", c[pos + 1]);

  // When we take the number off the prototype it disappears from a, but
  // the concat put it in c itself.
  Array.prototype["123"] = undefined;
  assertEquals("undefined", typeof(a[123]));
  assertEquals("baz", c[123]);

157 158 159 160 161 162 163 164
  // If the element of prototype is shadowed, the element on the instance
  // should be copied, but not the one on the prototype.
  Array.prototype[123] = 'baz';
  a[123] = 'xyz';
  assertEquals('xyz', a[123]);
  c = a.concat(b);
  assertEquals('xyz', c[123]);

165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
  // Non-numeric properties on the prototype or the array shouldn't get
  // copied.
  Array.prototype.moe = 'joe';
  a.ben = 'jerry';
  assertEquals(a["moe"], 'joe');
  assertEquals(a["ben"], 'jerry');
  c = a.concat(b);
  // ben was not copied
  assertEquals("undefined", typeof(c.ben));
  // moe was not copied, but we can see it through the prototype
  assertEquals("joe", c.moe);

  // When we take moe off the prototype it disappears from all arrays.
  Array.prototype.moe = undefined;
  assertEquals("undefined", typeof(c.moe));

181
  // Negative indices don't get concated.
182 183 184 185 186 187 188 189 190 191 192 193 194 195 196
  a[-1] = 'minus1';
  assertEquals("minus1", a[-1]);
  assertEquals("undefined", typeof(a[0xffffffff]));
  c = a.concat(b);
  assertEquals("undefined", typeof(c[-1]));
  assertEquals("undefined", typeof(c[0xffffffff]));
  assertEquals(c.length, a.length + 1);

}

a = [];
c = a.concat('Hello');
assertEquals(1, c.length);
assertEquals("Hello", c[0]);
assertEquals("Hello", c.toString());
197 198 199 200 201 202 203 204 205 206 207

// Check that concat preserves holes.
var holey = [void 0,'a',,'c'].concat(['d',,'f',[0,,2],void 0])
assertEquals(9, holey.length);  // hole in embedded array is ignored
for (var i = 0; i < holey.length; i++) {
  if (i == 2 || i == 5) {
    assertFalse(i in holey);
  } else {
    assertTrue(i in holey);
  }
}
208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227

// Polluted prototype from prior tests.
delete Array.prototype[123];

// Check that concat reads getters in the correct order.
var arr1 = [,2];
var arr2 = [1,3];
var r1 = [].concat(arr1, arr2);  // [,2,1,3]
assertEquals([,2,1,3], r1);

// Make first array change length of second array.
Object.defineProperty(arr1, 0, {get: function() {
      arr2.push("X");
      return undefined;
    }, configurable: true})
var r2 = [].concat(arr1, arr2);  // [undefined,2,1,3,"X"]
assertEquals([undefined,2,1,3,"X"], r2);

// Make first array change length of second array massively.
arr2.length = 2;
228
var largeLength = 500000;
229
Object.defineProperty(arr1, 0, {get: function() {
230
      arr2[largeLength] = "X";
231 232 233 234
      return undefined;
    }, configurable: true})
var r3 = [].concat(arr1, arr2);  // [undefined,2,1,3,"X"]
var expected = [undefined,2,1,3];
235 236 237 238 239 240
var initialLen = expected.length;
expected[largeLength + 2] = "X";

var numElementsToCheck = 10;

// Checking entire massive array is too slow, so check:
241 242
// - the length,
assertEquals(expected.length, r3.length);
243 244 245 246 247 248 249 250 251 252 253 254
var slicesToCheck = [
  // - the first few elements,
  {start: 0, end: initialLen},
  // - arbitrary number of elements past the first few elements,
  {start: initialLen, end: initialLen + numElementsToCheck},
  // - arbitrary number of elements in the middle of the array
  {start: largeLength / 2, end: largeLength / 2 + numElementsToCheck},
  // - last few elements
  {start: largeLength, end: largeLength + 3}];
for (const {start, end} of slicesToCheck) {
  assertEquals(expected.slice(start, end), r3.slice(start, end));
}
255 256 257 258 259 260 261 262 263 264 265 266 267 268

var arr3 = [];
var trace = [];
var expectedTrace = []
function mkGetter(i) { return function() { trace.push(i); }; }
arr3.length = 10000;
for (var i = 0; i < 100; i++) {
  Object.defineProperty(arr3, i * i, {get: mkGetter(i)});
  expectedTrace[i] = i;
  expectedTrace[100 + i] = i;
}
var r4 = [0].concat(arr3, arr3);
assertEquals(1 + arr3.length * 2, r4.length);
assertEquals(expectedTrace, trace);