array-iteration.js 11.5 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 32 33 34 35 36 37 38 39 40 41 42
// 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.

// Tests for non-standard array iteration functions.
//
// See
//
// <http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Reference:Global_Objects:Array>
//
// for an explanation of each of the functions.

//
// Array.prototype.filter
//
(function() {
  // Simple use.
  var a = [0,1];
  assertArrayEquals([0], a.filter(function(n) { return n == 0; }));
43
  assertArrayEquals([0,1], a);
44 45 46 47

  // Use specified object as this object when calling the function.
  var o = { value: 42 }
  a = [1,42,3,42,4];
48 49
  assertArrayEquals([42,42],
      a.filter(function(n) { return this.value == n }, o))
50 51 52

  // Modify original array.
  a = [1,42,3,42,4];
53 54 55 56
  assertArrayEquals([42,42],
      a.filter(function(n, index, array) {
          array[index] = 43; return 42 == n;
      }));
57 58 59 60 61
  assertArrayEquals([43,43,43,43,43], a);

  // Only loop through initial part of array eventhough elements are
  // added.
  a = [1,1];
62 63
  assertArrayEquals([],
      a.filter(function(n, index, array) { array.push(n+1); return n == 2; }));
64 65 66 67 68 69 70 71 72 73 74 75
  assertArrayEquals([1,1,2,2], a);

  // Respect holes.
  a = new Array(20);
  var count = 0;
  a[2] = 2;
  a[15] = 2;
  a[17] = 4;
  var a = a.filter(function(n) { count++; return n == 2; });
  assertEquals(3, count);
  for (var i in a) assertEquals(2, a[i]);

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
  // Skip over missing properties.
  a = {
    "0": 0,
    "2": 2,
    length: 3
  };
  var received = [];
  assertArrayEquals([2],
      Array.prototype.filter.call(a, function(n) {
        received.push(n);
        return n == 2;
      }));
  assertArrayEquals([0, 2], received);

  // Modify array prototype
  a = [0, , 2];
  received = [];
  assertArrayEquals([2],
      Array.prototype.filter.call(a, function(n) {
        a.__proto__ = null;
        received.push(n);
        return n == 2;
      }));
  assertArrayEquals([0, 2], received);

101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
  // Create a new object in each function call when receiver is a
  // primitive value. See ECMA-262, Annex C.
  a = [];
  [1, 2].filter(function() { a.push(this) }, "");
  assertTrue(a[0] !== a[1]);

  // Do not create a new object otherwise.
  a = [];
  [1, 2].filter(function() { a.push(this) }, {});
  assertEquals(a[0], a[1]);

  // In strict mode primitive values should not be coerced to an object.
  a = [];
  [1, 2].filter(function() { 'use strict'; a.push(this); }, "");
  assertEquals("", a[0]);
  assertEquals(a[0], a[1]);

118 119 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 157 158
})();


//
// Array.prototype.forEach
//
(function() {
  // Simple use.
  var a = [0,1];
  var count = 0;
  a.forEach(function(n) { count++; });
  assertEquals(2, count);

  // Use specified object as this object when calling the function.
  var o = { value: 42 }
  var result = [];
  a.forEach(function(n) { result.push(this.value); }, o);
  assertArrayEquals([42,42], result);

  // Modify original array.
  a = [0,1];
  count = 0;
  a.forEach(function(n, index, array) { array[index] = n + 1; count++; });
  assertEquals(2, count);
  assertArrayEquals([1,2], a);

  // Only loop through initial part of array eventhough elements are
  // added.
  a = [1,1];
  count = 0;
  a.forEach(function(n, index, array) { array.push(n+1); count++; });
  assertEquals(2, count);
  assertArrayEquals([1,1,2,2], a);

  // Respect holes.
  a = new Array(20);
  count = 0;
  a[15] = 2;
  a.forEach(function(n) { count++; });
  assertEquals(1, count);

159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
  // Skip over missing properties.
  a = {
    "0": 0,
    "2": 2,
    length: 3
  };
  var received = [];
  Array.prototype.forEach.call(a, function(n) { received.push(n); });
  assertArrayEquals([0, 2], received);

  // Modify array prototype
  a = [0, , 2];
  received = [];
  Array.prototype.forEach.call(a, function(n) {
    a.__proto__ = null;
    received.push(n);
    return n == 2;
  });
  assertArrayEquals([0, 2], received);

179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195
  // Create a new object in each function call when receiver is a
  // primitive value. See ECMA-262, Annex C.
  a = [];
  [1, 2].forEach(function() { a.push(this) }, "");
  assertTrue(a[0] !== a[1]);

  // Do not create a new object otherwise.
  a = [];
  [1, 2].forEach(function() { a.push(this) }, {});
  assertEquals(a[0], a[1]);

  // In strict mode primitive values should not be coerced to an object.
  a = [];
  [1, 2].forEach(function() { 'use strict'; a.push(this); }, "");
  assertEquals("", a[0]);
  assertEquals(a[0], a[1]);

196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218
})();


//
// Array.prototype.every
//
(function() {
  // Simple use.
  var a = [0,1];
  assertFalse(a.every(function(n) { return n == 0 }));
  a = [0,0];
  assertTrue(a.every(function(n) { return n == 0 }));
  assertTrue([].every(function(n) { return n == 0}));

  // Use specified object as this object when calling the function.
  var o = { value: 42 }
  a = [0];
  assertFalse(a.every(function(n) { return this.value == n; }, o));
  a = [42];
  assertTrue(a.every(function(n) { return this.value == n; }, o));

  // Modify original array.
  a = [0,1];
219 220 221 222
  assertFalse(
      a.every(function(n, index, array) {
        array[index] = n + 1; return n == 1;
      }));
223
  assertArrayEquals([1,1], a);
224

225 226 227
  // Only loop through initial part of array eventhough elements are
  // added.
  a = [1,1];
228 229 230 231
  assertTrue(
      a.every(function(n, index, array) {
        array.push(n + 1); return n == 1;
      }));
232 233 234 235 236 237 238 239 240 241
  assertArrayEquals([1,1,2,2], a);

  // Respect holes.
  a = new Array(20);
  var count = 0;
  a[2] = 2;
  a[15] = 2;
  assertTrue(a.every(function(n) { count++; return n == 2; }));
  assertEquals(2, count);

242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266
  // Skip over missing properties.
  a = {
    "0": 2,
    "2": 2,
    length: 3
  };
  var received = [];
  assertTrue(
      Array.prototype.every.call(a, function(n) {
        received.push(n);
        return n == 2;
      }));
  assertArrayEquals([2, 2], received);

  // Modify array prototype
  a = [2, , 2];
  received = [];
  assertTrue(
      Array.prototype.every.call(a, function(n) {
        a.__proto__ = null;
        received.push(n);
        return n == 2;
      }));
  assertArrayEquals([2, 2], received);

267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283
  // Create a new object in each function call when receiver is a
  // primitive value. See ECMA-262, Annex C.
  a = [];
  [1, 2].every(function() { a.push(this); return true; }, "");
  assertTrue(a[0] !== a[1]);

  // Do not create a new object otherwise.
  a = [];
  [1, 2].every(function() { a.push(this); return true; }, {});
  assertEquals(a[0], a[1]);

  // In strict mode primitive values should not be coerced to an object.
  a = [];
  [1, 2].every(function() { 'use strict'; a.push(this); return true; }, "");
  assertEquals("", a[0]);
  assertEquals(a[0], a[1]);

284 285 286 287 288 289 290
})();

//
// Array.prototype.map
//
(function() {
  var a = [0,1,2,3,4];
291

292 293 294 295
  // Simple use.
  var result = [1,2,3,4,5];
  assertArrayEquals(result, a.map(function(n) { return n + 1; }));
  assertEquals(a, a);
296

297 298 299 300
  // Use specified object as this object when calling the function.
  var o = { delta: 42 }
  result = [42,43,44,45,46];
  assertArrayEquals(result, a.map(function(n) { return this.delta + n; }, o));
301

302 303 304
  // Modify original array.
  a = [0,1,2,3,4];
  result = [1,2,3,4,5];
305 306 307 308
  assertArrayEquals(result,
      a.map(function(n, index, array) {
        array[index] = n + 1; return n + 1;
      }));
309
  assertArrayEquals(result, a);
310

311 312 313 314
  // Only loop through initial part of array eventhough elements are
  // added.
  a = [0,1,2,3,4];
  result = [1,2,3,4,5];
315 316
  assertArrayEquals(result,
      a.map(function(n, index, array) { array.push(n); return n + 1; }));
317 318 319 320 321 322 323 324
  assertArrayEquals([0,1,2,3,4,0,1,2,3,4], a);

  // Respect holes.
  a = new Array(20);
  a[15] = 2;
  a = a.map(function(n) { return 2*n; });
  for (var i in a) assertEquals(4, a[i]);

325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349
  // Skip over missing properties.
  a = {
    "0": 1,
    "2": 2,
    length: 3
  };
  var received = [];
  assertArrayEquals([2, , 4],
      Array.prototype.map.call(a, function(n) {
        received.push(n);
        return n * 2;
      }));
  assertArrayEquals([1, 2], received);

  // Modify array prototype
  a = [1, , 2];
  received = [];
  assertArrayEquals([2, , 4],
      Array.prototype.map.call(a, function(n) {
        a.__proto__ = null;
        received.push(n);
        return n * 2;
      }));
  assertArrayEquals([1, 2], received);

350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366
  // Create a new object in each function call when receiver is a
  // primitive value. See ECMA-262, Annex C.
  a = [];
  [1, 2].map(function() { a.push(this) }, "");
  assertTrue(a[0] !== a[1]);

  // Do not create a new object otherwise.
  a = [];
  [1, 2].map(function() { a.push(this) }, {});
  assertEquals(a[0], a[1]);

  // In strict mode primitive values should not be coerced to an object.
  a = [];
  [1, 2].map(function() { 'use strict'; a.push(this); }, "");
  assertEquals("", a[0]);
  assertEquals(a[0], a[1]);

367 368 369 370 371 372 373 374 375 376 377
})();

//
// Array.prototype.some
//
(function() {
  var a = [0,1,2,3,4];

  // Simple use.
  assertTrue(a.some(function(n) { return n == 3}));
  assertFalse(a.some(function(n) { return n == 5}));
378

379 380 381 382 383 384 385 386 387
  // Use specified object as this object when calling the function.
  var o = { element: 42 };
  a = [1,42,3];
  assertTrue(a.some(function(n) { return this.element == n; }, o));
  a = [1];
  assertFalse(a.some(function(n) { return this.element == n; }, o));

  // Modify original array.
  a = [0,1,2,3];
388 389 390
  assertTrue(
      a.some(function(n, index, array) {
        array[index] = n + 1; return n == 2; }));
391 392 393 394
  assertArrayEquals([1,2,3,3], a);

  // Only loop through initial part when elements are added.
  a = [0,1,2];
395 396
  assertFalse(
      a.some(function(n, index, array) { array.push(42); return n == 42; }));
397 398 399 400 401 402 403 404 405 406 407
  assertArrayEquals([0,1,2,42,42,42], a);

  // Respect holes.
  a = new Array(20);
  var count = 0;
  a[2] = 42;
  a[10] = 2;
  a[15] = 42;
  assertTrue(a.some(function(n) { count++; return n == 2; }));
  assertEquals(2, count);

408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424
  // Create a new object in each function call when receiver is a
  // primitive value. See ECMA-262, Annex C.
  a = [];
  [1, 2].some(function() { a.push(this) }, "");
  assertTrue(a[0] !== a[1]);

  // Do not create a new object otherwise.
  a = [];
  [1, 2].some(function() { a.push(this) }, {});
  assertEquals(a[0], a[1]);

  // In strict mode primitive values should not be coerced to an object.
  a = [];
  [1, 2].some(function() { 'use strict'; a.push(this); }, "");
  assertEquals("", a[0]);
  assertEquals(a[0], a[1]);

425
})();