1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/js/src/tests/js1_6/Array/regress-290592.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,661 @@ 1.4 +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.8 + 1.9 +//----------------------------------------------------------------------------- 1.10 +var BUGNUMBER = 290592; 1.11 +var summary = 'Array extras: forEach, indexOf, filter, map'; 1.12 +var actual = ''; 1.13 +var expect = ''; 1.14 + 1.15 +printBugNumber(BUGNUMBER); 1.16 +printStatus (summary); 1.17 + 1.18 +// Utility functions 1.19 + 1.20 +function identity(v, index, array) 1.21 +{ 1.22 + reportCompare(v, array[index], 'identity: check callback argument consistency'); 1.23 + return v; 1.24 +} 1.25 + 1.26 +function mutate(v, index, array) 1.27 +{ 1.28 + reportCompare(v, array[index], 'mutate: check callback argument consistency'); 1.29 + if (index == 0) 1.30 + { 1.31 + array[1] = 'mutated'; 1.32 + delete array[2]; 1.33 + array.push('not visited'); 1.34 + } 1.35 + return v; 1.36 +} 1.37 + 1.38 +function mutateForEach(v, index, array) 1.39 +{ 1.40 + reportCompare(v, array[index], 'mutateForEach: check callback argument consistency'); 1.41 + if (index == 0) 1.42 + { 1.43 + array[1] = 'mutated'; 1.44 + delete array[2]; 1.45 + array.push('not visited'); 1.46 + } 1.47 + actual += v + ','; 1.48 +} 1.49 + 1.50 +function makeUpperCase(v, index, array) 1.51 +{ 1.52 + reportCompare(v, array[index], 'makeUpperCase: check callback argument consistency'); 1.53 + try 1.54 + { 1.55 + return v.toUpperCase(); 1.56 + } 1.57 + catch(e) 1.58 + { 1.59 + } 1.60 + return v; 1.61 +} 1.62 + 1.63 + 1.64 +function concat(v, index, array) 1.65 +{ 1.66 + reportCompare(v, array[index], 'concat: check callback argument consistency'); 1.67 + actual += v + ','; 1.68 +} 1.69 + 1.70 + 1.71 +function isUpperCase(v, index, array) 1.72 +{ 1.73 + reportCompare(v, array[index], 'isUpperCase: check callback argument consistency'); 1.74 + try 1.75 + { 1.76 + return v == v.toUpperCase(); 1.77 + } 1.78 + catch(e) 1.79 + { 1.80 + } 1.81 + return false; 1.82 +} 1.83 + 1.84 +function isString(v, index, array) 1.85 +{ 1.86 + reportCompare(v, array[index], 'isString: check callback argument consistency'); 1.87 + return typeof v == 'string'; 1.88 +} 1.89 + 1.90 + 1.91 +// callback object. 1.92 +function ArrayCallback(state) 1.93 +{ 1.94 + this.state = state; 1.95 +} 1.96 + 1.97 +ArrayCallback.prototype.makeUpperCase = function (v, index, array) 1.98 +{ 1.99 + reportCompare(v, array[index], 'ArrayCallback.prototype.makeUpperCase: check callback argument consistency'); 1.100 + try 1.101 + { 1.102 + return this.state ? v.toUpperCase() : v.toLowerCase(); 1.103 + } 1.104 + catch(e) 1.105 + { 1.106 + } 1.107 + return v; 1.108 +}; 1.109 + 1.110 +ArrayCallback.prototype.concat = function(v, index, array) 1.111 +{ 1.112 + reportCompare(v, array[index], 'ArrayCallback.prototype.concat: check callback argument consistency'); 1.113 + actual += v + ','; 1.114 +}; 1.115 + 1.116 +ArrayCallback.prototype.isUpperCase = function(v, index, array) 1.117 +{ 1.118 + reportCompare(v, array[index], 'ArrayCallback.prototype.isUpperCase: check callback argument consistency'); 1.119 + try 1.120 + { 1.121 + return this.state ? true : (v == v.toUpperCase()); 1.122 + } 1.123 + catch(e) 1.124 + { 1.125 + } 1.126 + return false; 1.127 +}; 1.128 + 1.129 +ArrayCallback.prototype.isString = function(v, index, array) 1.130 +{ 1.131 + reportCompare(v, array[index], 'ArrayCallback.prototype.isString: check callback argument consistency'); 1.132 + return this.state ? true : (typeof v == 'string'); 1.133 +}; 1.134 + 1.135 +function dumpError(e) 1.136 +{ 1.137 + var s = e.name + ': ' + e.message + 1.138 + ' File: ' + e.fileName + 1.139 + ', Line: ' + e.lineNumber + 1.140 + ', Stack: ' + e.stack; 1.141 + return s; 1.142 +} 1.143 + 1.144 +var obj; 1.145 +var strings = ['hello', 'Array', 'WORLD']; 1.146 +var mixed = [0, '0', 0]; 1.147 +var sparsestrings = new Array(); 1.148 +sparsestrings[2] = 'sparse'; 1.149 + 1.150 +if ('map' in Array.prototype) 1.151 +{ 1.152 +// see http://developer-test.mozilla.org/docs/Core_JavaScript_1.5_Reference:Objects:Array:map 1.153 + 1.154 + // test Array.map 1.155 + 1.156 + // map has 1 required argument 1.157 + expect = 1; 1.158 + actual = Array.prototype.map.length; 1.159 + reportCompare(expect, actual, 'Array.prototype.map.length == 1'); 1.160 + 1.161 + // throw TypeError if no callback function specified 1.162 + expect = 'TypeError'; 1.163 + try 1.164 + { 1.165 + strings.map(); 1.166 + actual = 'no error'; 1.167 + } 1.168 + catch(e) 1.169 + { 1.170 + actual = e.name; 1.171 + } 1.172 + reportCompare(expect, actual, 'Array.map(undefined) throws TypeError'); 1.173 + 1.174 + try 1.175 + { 1.176 + // identity map 1.177 + expect = 'hello,Array,WORLD'; 1.178 + actual = strings.map(identity).toString(); 1.179 + } 1.180 + catch(e) 1.181 + { 1.182 + actual = dumpError(e); 1.183 + } 1.184 + reportCompare(expect, actual, 'Array.map: identity'); 1.185 + 1.186 + 1.187 + try 1.188 + { 1.189 + expect = 'hello,mutated,'; 1.190 + actual = strings.map(mutate).toString(); 1.191 + } 1.192 + catch(e) 1.193 + { 1.194 + actual = dumpError(e); 1.195 + } 1.196 + reportCompare(expect, actual, 'Array.map: mutate'); 1.197 + 1.198 + strings = ['hello', 'Array', 'WORLD']; 1.199 + 1.200 + try 1.201 + { 1.202 + // general map 1.203 + expect = 'HELLO,ARRAY,WORLD'; 1.204 + actual = strings.map(makeUpperCase).toString(); 1.205 + } 1.206 + catch(e) 1.207 + { 1.208 + actual = dumpError(e); 1.209 + } 1.210 + reportCompare(expect, actual, 'Array.map: uppercase'); 1.211 + 1.212 + try 1.213 + { 1.214 + // pass object method as map callback 1.215 + expect = 'HELLO,ARRAY,WORLD'; 1.216 + var obj = new ArrayCallback(true); 1.217 + actual = strings.map(obj.makeUpperCase, obj).toString(); 1.218 + } 1.219 + catch(e) 1.220 + { 1.221 + actual = dumpError(e); 1.222 + } 1.223 + reportCompare(expect, actual, 'Array.map: uppercase with object callback'); 1.224 + 1.225 + try 1.226 + { 1.227 + expect = 'hello,array,world'; 1.228 + obj = new ArrayCallback(false); 1.229 + actual = strings.map(obj.makeUpperCase, obj).toString(); 1.230 + } 1.231 + catch(e) 1.232 + { 1.233 + actual = dumpError(e); 1.234 + } 1.235 + reportCompare(expect, actual, 'Array.map: lowercase with object callback'); 1.236 + 1.237 + try 1.238 + { 1.239 + // map on sparse arrays 1.240 + expect = ',,SPARSE'; 1.241 + actual = sparsestrings.map(makeUpperCase).toString(); 1.242 + } 1.243 + catch(e) 1.244 + { 1.245 + actual = dumpError(e); 1.246 + } 1.247 + reportCompare(expect, actual, 'Array.map: uppercase on sparse array'); 1.248 +} 1.249 + 1.250 +if ('forEach' in Array.prototype) 1.251 +{ 1.252 +// see http://developer-test.mozilla.org/docs/Core_JavaScript_1.5_Reference:Objects:Array:forEach 1.253 + 1.254 + // test Array.forEach 1.255 + 1.256 + // forEach has 1 required argument 1.257 + expect = 1; 1.258 + actual = Array.prototype.forEach.length; 1.259 + reportCompare(expect, actual, 'Array.prototype.forEach.length == 1'); 1.260 + 1.261 + // throw TypeError if no callback function specified 1.262 + expect = 'TypeError'; 1.263 + try 1.264 + { 1.265 + strings.forEach(); 1.266 + actual = 'no error'; 1.267 + } 1.268 + catch(e) 1.269 + { 1.270 + actual = e.name; 1.271 + } 1.272 + reportCompare(expect, actual, 'Array.forEach(undefined) throws TypeError'); 1.273 + 1.274 + try 1.275 + { 1.276 + // general forEach 1.277 + expect = 'hello,Array,WORLD,'; 1.278 + actual = ''; 1.279 + strings.forEach(concat); 1.280 + } 1.281 + catch(e) 1.282 + { 1.283 + actual = dumpError(e); 1.284 + } 1.285 + reportCompare(expect, actual, 'Array.forEach'); 1.286 + 1.287 + try 1.288 + { 1.289 + expect = 'hello,mutated,'; 1.290 + actual = ''; 1.291 + strings.forEach(mutateForEach); 1.292 + } 1.293 + catch(e) 1.294 + { 1.295 + actual = dumpError(e); 1.296 + } 1.297 + reportCompare(expect, actual, 'Array.forEach: mutate'); 1.298 + 1.299 + strings = ['hello', 'Array', 'WORLD']; 1.300 + 1.301 + 1.302 + 1.303 + try 1.304 + { 1.305 + // pass object method as forEach callback 1.306 + expect = 'hello,Array,WORLD,'; 1.307 + actual = ''; 1.308 + obj = new ArrayCallback(true); 1.309 + strings.forEach(obj.concat, obj); 1.310 + } 1.311 + catch(e) 1.312 + { 1.313 + actual = dumpError(e); 1.314 + } 1.315 + reportCompare(expect, actual, 'Array.forEach with object callback 1'); 1.316 + 1.317 + try 1.318 + { 1.319 + expect = 'hello,Array,WORLD,'; 1.320 + actual = ''; 1.321 + obj = new ArrayCallback(false); 1.322 + strings.forEach(obj.concat, obj); 1.323 + } 1.324 + catch(e) 1.325 + { 1.326 + actual = dumpError(e); 1.327 + } 1.328 + reportCompare(expect, actual, 'Array.forEach with object callback 2'); 1.329 + 1.330 + try 1.331 + { 1.332 + // test forEach on sparse arrays 1.333 + // see https://bugzilla.mozilla.org/show_bug.cgi?id=311082 1.334 + expect = 'sparse,'; 1.335 + actual = ''; 1.336 + sparsestrings.forEach(concat); 1.337 + } 1.338 + catch(e) 1.339 + { 1.340 + actual = dumpError(e); 1.341 + } 1.342 + reportCompare(expect, actual, 'Array.forEach on sparse array'); 1.343 +} 1.344 + 1.345 +if ('filter' in Array.prototype) 1.346 +{ 1.347 +// see http://developer-test.mozilla.org/docs/Core_JavaScript_1.5_Reference:Objects:Array:filter 1.348 + 1.349 + // test Array.filter 1.350 + 1.351 + // filter has 1 required argument 1.352 + expect = 1; 1.353 + actual = Array.prototype.filter.length; 1.354 + reportCompare(expect, actual, 'Array.prototype.filter.length == 1'); 1.355 + 1.356 + // throw TypeError if no callback function specified 1.357 + expect = 'TypeError'; 1.358 + try 1.359 + { 1.360 + strings.filter(); 1.361 + actual = 'no error'; 1.362 + } 1.363 + catch(e) 1.364 + { 1.365 + actual = e.name; 1.366 + } 1.367 + reportCompare(expect, actual, 'Array.filter(undefined) throws TypeError'); 1.368 + 1.369 + try 1.370 + { 1.371 + // test general filter 1.372 + expect = 'WORLD'; 1.373 + actual = strings.filter(isUpperCase).toString(); 1.374 + } 1.375 + catch(e) 1.376 + { 1.377 + actual = dumpError(e); 1.378 + } 1.379 + reportCompare(expect, actual, 'Array.filter'); 1.380 + 1.381 + try 1.382 + { 1.383 + expect = 'WORLD'; 1.384 + obj = new ArrayCallback(false); 1.385 + actual = strings.filter(obj.isUpperCase, obj).toString(); 1.386 + } 1.387 + catch(e) 1.388 + { 1.389 + actual = dumpError(e); 1.390 + } 1.391 + reportCompare(expect, actual, 'Array.filter object callback 1'); 1.392 + 1.393 + try 1.394 + { 1.395 + expect = 'hello,Array,WORLD'; 1.396 + obj = new ArrayCallback(true); 1.397 + actual = strings.filter(obj.isUpperCase, obj).toString(); 1.398 + } 1.399 + catch(e) 1.400 + { 1.401 + actual = dumpError(e); 1.402 + } 1.403 + reportCompare(expect, actual, 'Array.filter object callback 2'); 1.404 +} 1.405 + 1.406 +if ('every' in Array.prototype) 1.407 +{ 1.408 +// see http://developer-test.mozilla.org/docs/Core_JavaScript_1.5_Reference:Objects:Array:every 1.409 + 1.410 + // test Array.every 1.411 + 1.412 + // every has 1 required argument 1.413 + 1.414 + expect = 1; 1.415 + actual = Array.prototype.every.length; 1.416 + reportCompare(expect, actual, 'Array.prototype.every.length == 1'); 1.417 + 1.418 + // throw TypeError if no every callback function specified 1.419 + expect = 'TypeError'; 1.420 + try 1.421 + { 1.422 + strings.every(); 1.423 + actual = 'no error'; 1.424 + } 1.425 + catch(e) 1.426 + { 1.427 + actual = e.name; 1.428 + } 1.429 + reportCompare(expect, actual, 'Array.every(undefined) throws TypeError'); 1.430 + 1.431 + // test general every 1.432 + 1.433 + try 1.434 + { 1.435 + expect = true; 1.436 + actual = strings.every(isString); 1.437 + } 1.438 + catch(e) 1.439 + { 1.440 + actual = dumpError(e); 1.441 + } 1.442 + reportCompare(expect, actual, 'strings: every element is a string'); 1.443 + 1.444 + try 1.445 + { 1.446 + expect = false; 1.447 + actual = mixed.every(isString); 1.448 + } 1.449 + catch(e) 1.450 + { 1.451 + actual = dumpError(e); 1.452 + } 1.453 + reportCompare(expect, actual, 'mixed: every element is a string'); 1.454 + 1.455 + try 1.456 + { 1.457 + // see https://bugzilla.mozilla.org/show_bug.cgi?id=311082 1.458 + expect = true; 1.459 + actual = sparsestrings.every(isString); 1.460 + } 1.461 + catch(e) 1.462 + { 1.463 + actual = dumpError(e); 1.464 + } 1.465 + reportCompare(expect, actual, 'sparsestrings: every element is a string'); 1.466 + 1.467 + // pass object method as map callback 1.468 + 1.469 + obj = new ArrayCallback(false); 1.470 + 1.471 + try 1.472 + { 1.473 + expect = true; 1.474 + actual = strings.every(obj.isString, obj); 1.475 + } 1.476 + catch(e) 1.477 + { 1.478 + actual = dumpError(e); 1.479 + } 1.480 + reportCompare(expect, actual, 'strings: every element is a string, via object callback'); 1.481 + 1.482 + try 1.483 + { 1.484 + expect = false; 1.485 + actual = mixed.every(obj.isString, obj); 1.486 + } 1.487 + catch(e) 1.488 + { 1.489 + actual = dumpError(e) ; 1.490 + } 1.491 + reportCompare(expect, actual, 'mixed: every element is a string, via object callback'); 1.492 + 1.493 + try 1.494 + { 1.495 + // see https://bugzilla.mozilla.org/show_bug.cgi?id=311082 1.496 + expect = true; 1.497 + actual = sparsestrings.every(obj.isString, obj); 1.498 + } 1.499 + catch(e) 1.500 + { 1.501 + actual = dumpError(e); 1.502 + } 1.503 + reportCompare(expect, actual, 'sparsestrings: every element is a string, via object callback'); 1.504 + 1.505 +} 1.506 + 1.507 +if ('some' in Array.prototype) 1.508 +{ 1.509 +// see http://developer-test.mozilla.org/docs/Core_JavaScript_1.5_Reference:Objects:Array:some 1.510 + 1.511 + // test Array.some 1.512 + 1.513 + // some has 1 required argument 1.514 + 1.515 + expect = 1; 1.516 + actual = Array.prototype.some.length; 1.517 + reportCompare(expect, actual, 'Array.prototype.some.length == 1'); 1.518 + 1.519 + // throw TypeError if no some callback function specified 1.520 + expect = 'TypeError'; 1.521 + try 1.522 + { 1.523 + strings.some(); 1.524 + actual = 'no error'; 1.525 + } 1.526 + catch(e) 1.527 + { 1.528 + actual = e.name; 1.529 + } 1.530 + reportCompare(expect, actual, 'Array.some(undefined) throws TypeError'); 1.531 + 1.532 + // test general some 1.533 + 1.534 + try 1.535 + { 1.536 + expect = true; 1.537 + actual = strings.some(isString); 1.538 + } 1.539 + catch(e) 1.540 + { 1.541 + actual = dumpError(e); 1.542 + } 1.543 + reportCompare(expect, actual, 'strings: some element is a string'); 1.544 + 1.545 + try 1.546 + { 1.547 + expect = true; 1.548 + actual = mixed.some(isString); 1.549 + } 1.550 + catch(e) 1.551 + { 1.552 + actual = dumpError(e); 1.553 + } 1.554 + reportCompare(expect, actual, 'mixed: some element is a string'); 1.555 + 1.556 + try 1.557 + { 1.558 + expect = true; 1.559 + actual = sparsestrings.some(isString); 1.560 + } 1.561 + catch(e) 1.562 + { 1.563 + actual = dumpError(e); 1.564 + } 1.565 + reportCompare(expect, actual, 'sparsestrings: some element is a string'); 1.566 + 1.567 + // pass object method as map callback 1.568 + 1.569 + obj = new ArrayCallback(false); 1.570 + 1.571 + try 1.572 + { 1.573 + expect = true; 1.574 + actual = strings.some(obj.isString, obj); 1.575 + } 1.576 + catch(e) 1.577 + { 1.578 + actual = dumpError(e); 1.579 + } 1.580 + reportCompare(expect, actual, 'strings: some element is a string, via object callback'); 1.581 + 1.582 + try 1.583 + { 1.584 + expect = true; 1.585 + actual = mixed.some(obj.isString, obj); 1.586 + } 1.587 + catch(e) 1.588 + { 1.589 + actual = dumpError(e); 1.590 + } 1.591 + reportCompare(expect, actual, 'mixed: some element is a string, via object callback'); 1.592 + 1.593 + try 1.594 + { 1.595 + expect = true; 1.596 + actual = sparsestrings.some(obj.isString, obj); 1.597 + } 1.598 + catch(e) 1.599 + { 1.600 + actual = dumpError(e); 1.601 + } 1.602 + reportCompare(expect, actual, 'sparsestrings: some element is a string, via object callback'); 1.603 + 1.604 +} 1.605 + 1.606 +if ('indexOf' in Array.prototype) 1.607 +{ 1.608 +// see http://developer-test.mozilla.org/docs/Core_JavaScript_1.5_Reference:Objects:Array:indexOf 1.609 + 1.610 + // test Array.indexOf 1.611 + 1.612 + // indexOf has 1 required argument 1.613 + 1.614 + expect = 1; 1.615 + actual = Array.prototype.indexOf.length; 1.616 + reportCompare(expect, actual, 'Array.prototype.indexOf.length == 1'); 1.617 + 1.618 + // test general indexOf 1.619 + 1.620 + try 1.621 + { 1.622 + expect = -1; 1.623 + actual = mixed.indexOf('not found'); 1.624 + } 1.625 + catch(e) 1.626 + { 1.627 + actual = dumpError(e); 1.628 + } 1.629 + reportCompare(expect, actual, 'indexOf returns -1 if value not found'); 1.630 + 1.631 + try 1.632 + { 1.633 + expect = 0; 1.634 + actual = mixed.indexOf(0); 1.635 + } 1.636 + catch(e) 1.637 + { 1.638 + actual = dumpError(e); 1.639 + } 1.640 + reportCompare(expect, actual, 'indexOf matches using strict equality'); 1.641 + 1.642 + try 1.643 + { 1.644 + expect = 1; 1.645 + actual = mixed.indexOf('0'); 1.646 + } 1.647 + catch(e) 1.648 + { 1.649 + actual = dumpError(e); 1.650 + } 1.651 + reportCompare(expect, actual, 'indexOf matches using strict equality'); 1.652 + 1.653 + try 1.654 + { 1.655 + expect = 2; 1.656 + actual = mixed.indexOf(0, 1); 1.657 + } 1.658 + catch(e) 1.659 + { 1.660 + actual = dumpError(e); 1.661 + } 1.662 + reportCompare(expect, actual, 'indexOf begins searching at fromIndex'); 1.663 +} 1.664 +