Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 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 |
| 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 | // Tests for non-standard array iteration functions. |
| 29 | // |
| 30 | // See |
| 31 | // |
| 32 | // <http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Reference:Global_Objects:Array> |
| 33 | // |
| 34 | // for an explanation of each of the functions. |
| 35 | |
| 36 | // |
| 37 | // Array.prototype.filter |
| 38 | // |
| 39 | (function() { |
| 40 | // Simple use. |
| 41 | var a = [0,1]; |
| 42 | assertArrayEquals([0], a.filter(function(n) { return n == 0; })); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 43 | assertArrayEquals([0,1], a); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 44 | |
| 45 | // Use specified object as this object when calling the function. |
| 46 | var o = { value: 42 } |
| 47 | a = [1,42,3,42,4]; |
| 48 | assertArrayEquals([42,42], a.filter(function(n) { return this.value == n }, o)) |
| 49 | |
| 50 | // Modify original array. |
| 51 | a = [1,42,3,42,4]; |
| 52 | assertArrayEquals([42,42], a.filter(function(n, index, array) { array[index] = 43; return 42 == n; })); |
| 53 | assertArrayEquals([43,43,43,43,43], a); |
| 54 | |
| 55 | // Only loop through initial part of array eventhough elements are |
| 56 | // added. |
| 57 | a = [1,1]; |
| 58 | assertArrayEquals([], a.filter(function(n, index, array) { array.push(n+1); return n == 2; })); |
| 59 | assertArrayEquals([1,1,2,2], a); |
| 60 | |
| 61 | // Respect holes. |
| 62 | a = new Array(20); |
| 63 | var count = 0; |
| 64 | a[2] = 2; |
| 65 | a[15] = 2; |
| 66 | a[17] = 4; |
| 67 | var a = a.filter(function(n) { count++; return n == 2; }); |
| 68 | assertEquals(3, count); |
| 69 | for (var i in a) assertEquals(2, a[i]); |
| 70 | |
Emily Bernier | 958fae7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 71 | // Create a new object in each function call when receiver is a |
| 72 | // primitive value. See ECMA-262, Annex C. |
| 73 | a = []; |
| 74 | [1, 2].filter(function() { a.push(this) }, ""); |
| 75 | assertTrue(a[0] !== a[1]); |
| 76 | |
| 77 | // Do not create a new object otherwise. |
| 78 | a = []; |
| 79 | [1, 2].filter(function() { a.push(this) }, {}); |
| 80 | assertEquals(a[0], a[1]); |
| 81 | |
| 82 | // In strict mode primitive values should not be coerced to an object. |
| 83 | a = []; |
| 84 | [1, 2].filter(function() { 'use strict'; a.push(this); }, ""); |
| 85 | assertEquals("", a[0]); |
| 86 | assertEquals(a[0], a[1]); |
| 87 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 88 | })(); |
| 89 | |
| 90 | |
| 91 | // |
| 92 | // Array.prototype.forEach |
| 93 | // |
| 94 | (function() { |
| 95 | // Simple use. |
| 96 | var a = [0,1]; |
| 97 | var count = 0; |
| 98 | a.forEach(function(n) { count++; }); |
| 99 | assertEquals(2, count); |
| 100 | |
| 101 | // Use specified object as this object when calling the function. |
| 102 | var o = { value: 42 } |
| 103 | var result = []; |
| 104 | a.forEach(function(n) { result.push(this.value); }, o); |
| 105 | assertArrayEquals([42,42], result); |
| 106 | |
| 107 | // Modify original array. |
| 108 | a = [0,1]; |
| 109 | count = 0; |
| 110 | a.forEach(function(n, index, array) { array[index] = n + 1; count++; }); |
| 111 | assertEquals(2, count); |
| 112 | assertArrayEquals([1,2], a); |
| 113 | |
| 114 | // Only loop through initial part of array eventhough elements are |
| 115 | // added. |
| 116 | a = [1,1]; |
| 117 | count = 0; |
| 118 | a.forEach(function(n, index, array) { array.push(n+1); count++; }); |
| 119 | assertEquals(2, count); |
| 120 | assertArrayEquals([1,1,2,2], a); |
| 121 | |
| 122 | // Respect holes. |
| 123 | a = new Array(20); |
| 124 | count = 0; |
| 125 | a[15] = 2; |
| 126 | a.forEach(function(n) { count++; }); |
| 127 | assertEquals(1, count); |
| 128 | |
Emily Bernier | 958fae7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 129 | // Create a new object in each function call when receiver is a |
| 130 | // primitive value. See ECMA-262, Annex C. |
| 131 | a = []; |
| 132 | [1, 2].forEach(function() { a.push(this) }, ""); |
| 133 | assertTrue(a[0] !== a[1]); |
| 134 | |
| 135 | // Do not create a new object otherwise. |
| 136 | a = []; |
| 137 | [1, 2].forEach(function() { a.push(this) }, {}); |
| 138 | assertEquals(a[0], a[1]); |
| 139 | |
| 140 | // In strict mode primitive values should not be coerced to an object. |
| 141 | a = []; |
| 142 | [1, 2].forEach(function() { 'use strict'; a.push(this); }, ""); |
| 143 | assertEquals("", a[0]); |
| 144 | assertEquals(a[0], a[1]); |
| 145 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 146 | })(); |
| 147 | |
| 148 | |
| 149 | // |
| 150 | // Array.prototype.every |
| 151 | // |
| 152 | (function() { |
| 153 | // Simple use. |
| 154 | var a = [0,1]; |
| 155 | assertFalse(a.every(function(n) { return n == 0 })); |
| 156 | a = [0,0]; |
| 157 | assertTrue(a.every(function(n) { return n == 0 })); |
| 158 | assertTrue([].every(function(n) { return n == 0})); |
| 159 | |
| 160 | // Use specified object as this object when calling the function. |
| 161 | var o = { value: 42 } |
| 162 | a = [0]; |
| 163 | assertFalse(a.every(function(n) { return this.value == n; }, o)); |
| 164 | a = [42]; |
| 165 | assertTrue(a.every(function(n) { return this.value == n; }, o)); |
| 166 | |
| 167 | // Modify original array. |
| 168 | a = [0,1]; |
| 169 | assertFalse(a.every(function(n, index, array) { array[index] = n + 1; return n == 1;})); |
| 170 | assertArrayEquals([1,1], a); |
Ben Murdoch | 589d697 | 2011-11-30 16:04:58 +0000 | [diff] [blame] | 171 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 172 | // Only loop through initial part of array eventhough elements are |
| 173 | // added. |
| 174 | a = [1,1]; |
| 175 | assertTrue(a.every(function(n, index, array) { array.push(n + 1); return n == 1;})); |
| 176 | assertArrayEquals([1,1,2,2], a); |
| 177 | |
| 178 | // Respect holes. |
| 179 | a = new Array(20); |
| 180 | var count = 0; |
| 181 | a[2] = 2; |
| 182 | a[15] = 2; |
| 183 | assertTrue(a.every(function(n) { count++; return n == 2; })); |
| 184 | assertEquals(2, count); |
| 185 | |
Emily Bernier | 958fae7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 186 | // Create a new object in each function call when receiver is a |
| 187 | // primitive value. See ECMA-262, Annex C. |
| 188 | a = []; |
| 189 | [1, 2].every(function() { a.push(this); return true; }, ""); |
| 190 | assertTrue(a[0] !== a[1]); |
| 191 | |
| 192 | // Do not create a new object otherwise. |
| 193 | a = []; |
| 194 | [1, 2].every(function() { a.push(this); return true; }, {}); |
| 195 | assertEquals(a[0], a[1]); |
| 196 | |
| 197 | // In strict mode primitive values should not be coerced to an object. |
| 198 | a = []; |
| 199 | [1, 2].every(function() { 'use strict'; a.push(this); return true; }, ""); |
| 200 | assertEquals("", a[0]); |
| 201 | assertEquals(a[0], a[1]); |
| 202 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 203 | })(); |
| 204 | |
| 205 | // |
| 206 | // Array.prototype.map |
| 207 | // |
| 208 | (function() { |
| 209 | var a = [0,1,2,3,4]; |
Ben Murdoch | 589d697 | 2011-11-30 16:04:58 +0000 | [diff] [blame] | 210 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 211 | // Simple use. |
| 212 | var result = [1,2,3,4,5]; |
| 213 | assertArrayEquals(result, a.map(function(n) { return n + 1; })); |
| 214 | assertEquals(a, a); |
Ben Murdoch | 589d697 | 2011-11-30 16:04:58 +0000 | [diff] [blame] | 215 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 216 | // Use specified object as this object when calling the function. |
| 217 | var o = { delta: 42 } |
| 218 | result = [42,43,44,45,46]; |
| 219 | assertArrayEquals(result, a.map(function(n) { return this.delta + n; }, o)); |
Ben Murdoch | 589d697 | 2011-11-30 16:04:58 +0000 | [diff] [blame] | 220 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 221 | // Modify original array. |
| 222 | a = [0,1,2,3,4]; |
| 223 | result = [1,2,3,4,5]; |
| 224 | assertArrayEquals(result, a.map(function(n, index, array) { array[index] = n + 1; return n + 1;})); |
| 225 | assertArrayEquals(result, a); |
Ben Murdoch | 589d697 | 2011-11-30 16:04:58 +0000 | [diff] [blame] | 226 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 227 | // Only loop through initial part of array eventhough elements are |
| 228 | // added. |
| 229 | a = [0,1,2,3,4]; |
| 230 | result = [1,2,3,4,5]; |
| 231 | assertArrayEquals(result, a.map(function(n, index, array) { array.push(n); return n + 1;})); |
| 232 | assertArrayEquals([0,1,2,3,4,0,1,2,3,4], a); |
| 233 | |
| 234 | // Respect holes. |
| 235 | a = new Array(20); |
| 236 | a[15] = 2; |
| 237 | a = a.map(function(n) { return 2*n; }); |
| 238 | for (var i in a) assertEquals(4, a[i]); |
| 239 | |
Emily Bernier | 958fae7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 240 | // Create a new object in each function call when receiver is a |
| 241 | // primitive value. See ECMA-262, Annex C. |
| 242 | a = []; |
| 243 | [1, 2].map(function() { a.push(this) }, ""); |
| 244 | assertTrue(a[0] !== a[1]); |
| 245 | |
| 246 | // Do not create a new object otherwise. |
| 247 | a = []; |
| 248 | [1, 2].map(function() { a.push(this) }, {}); |
| 249 | assertEquals(a[0], a[1]); |
| 250 | |
| 251 | // In strict mode primitive values should not be coerced to an object. |
| 252 | a = []; |
| 253 | [1, 2].map(function() { 'use strict'; a.push(this); }, ""); |
| 254 | assertEquals("", a[0]); |
| 255 | assertEquals(a[0], a[1]); |
| 256 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 257 | })(); |
| 258 | |
| 259 | // |
| 260 | // Array.prototype.some |
| 261 | // |
| 262 | (function() { |
| 263 | var a = [0,1,2,3,4]; |
| 264 | |
| 265 | // Simple use. |
| 266 | assertTrue(a.some(function(n) { return n == 3})); |
| 267 | assertFalse(a.some(function(n) { return n == 5})); |
Ben Murdoch | 589d697 | 2011-11-30 16:04:58 +0000 | [diff] [blame] | 268 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 269 | // Use specified object as this object when calling the function. |
| 270 | var o = { element: 42 }; |
| 271 | a = [1,42,3]; |
| 272 | assertTrue(a.some(function(n) { return this.element == n; }, o)); |
| 273 | a = [1]; |
| 274 | assertFalse(a.some(function(n) { return this.element == n; }, o)); |
| 275 | |
| 276 | // Modify original array. |
| 277 | a = [0,1,2,3]; |
| 278 | assertTrue(a.some(function(n, index, array) { array[index] = n + 1; return n == 2; })); |
| 279 | assertArrayEquals([1,2,3,3], a); |
| 280 | |
| 281 | // Only loop through initial part when elements are added. |
| 282 | a = [0,1,2]; |
| 283 | assertFalse(a.some(function(n, index, array) { array.push(42); return n == 42; })); |
| 284 | assertArrayEquals([0,1,2,42,42,42], a); |
| 285 | |
| 286 | // Respect holes. |
| 287 | a = new Array(20); |
| 288 | var count = 0; |
| 289 | a[2] = 42; |
| 290 | a[10] = 2; |
| 291 | a[15] = 42; |
| 292 | assertTrue(a.some(function(n) { count++; return n == 2; })); |
| 293 | assertEquals(2, count); |
| 294 | |
Emily Bernier | 958fae7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 295 | // Create a new object in each function call when receiver is a |
| 296 | // primitive value. See ECMA-262, Annex C. |
| 297 | a = []; |
| 298 | [1, 2].some(function() { a.push(this) }, ""); |
| 299 | assertTrue(a[0] !== a[1]); |
| 300 | |
| 301 | // Do not create a new object otherwise. |
| 302 | a = []; |
| 303 | [1, 2].some(function() { a.push(this) }, {}); |
| 304 | assertEquals(a[0], a[1]); |
| 305 | |
| 306 | // In strict mode primitive values should not be coerced to an object. |
| 307 | a = []; |
| 308 | [1, 2].some(function() { 'use strict'; a.push(this); }, ""); |
| 309 | assertEquals("", a[0]); |
| 310 | assertEquals(a[0], a[1]); |
| 311 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 312 | })(); |