array-iteration.js 9.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
  // 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]);

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 120 121 122 123 124 125 126 127 128 129 130 131 132 133
})();


//
// 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);

134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
  // 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]);

151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173
})();


//
// 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];
174 175 176 177
  assertFalse(
      a.every(function(n, index, array) {
        array[index] = n + 1; return n == 1;
      }));
178
  assertArrayEquals([1,1], a);
179

180 181 182
  // Only loop through initial part of array eventhough elements are
  // added.
  a = [1,1];
183 184 185 186
  assertTrue(
      a.every(function(n, index, array) {
        array.push(n + 1); return n == 1;
      }));
187 188 189 190 191 192 193 194 195 196
  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);

197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213
  // 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]);

214 215 216 217 218 219 220
})();

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

222 223 224 225
  // Simple use.
  var result = [1,2,3,4,5];
  assertArrayEquals(result, a.map(function(n) { return n + 1; }));
  assertEquals(a, a);
226

227 228 229 230
  // 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));
231

232 233 234
  // Modify original array.
  a = [0,1,2,3,4];
  result = [1,2,3,4,5];
235 236 237 238
  assertArrayEquals(result,
      a.map(function(n, index, array) {
        array[index] = n + 1; return n + 1;
      }));
239
  assertArrayEquals(result, a);
240

241 242 243 244
  // Only loop through initial part of array eventhough elements are
  // added.
  a = [0,1,2,3,4];
  result = [1,2,3,4,5];
245 246
  assertArrayEquals(result,
      a.map(function(n, index, array) { array.push(n); return n + 1; }));
247 248 249 250 251 252 253 254
  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]);

255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271
  // 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]);

272 273 274 275 276 277 278 279 280 281 282
})();

//
// 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}));
283

284 285 286 287 288 289 290 291 292
  // 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];
293 294 295
  assertTrue(
      a.some(function(n, index, array) {
        array[index] = n + 1; return n == 2; }));
296 297 298 299
  assertArrayEquals([1,2,3,3], a);

  // Only loop through initial part when elements are added.
  a = [0,1,2];
300 301
  assertFalse(
      a.some(function(n, index, array) { array.push(42); return n == 42; }));
302 303 304 305 306 307 308 309 310 311 312
  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);

313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329
  // 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]);

330
})();