js/src/tests/ecma_6/Array/find_findindex.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/js/src/tests/ecma_6/Array/find_findindex.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,206 @@
     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 = 885553;
    1.11 +var summary = 'Array.prototype.find and Array.prototype.findIndex';
    1.12 +
    1.13 +print(BUGNUMBER + ": " + summary);
    1.14 +
    1.15 +/**************
    1.16 + * BEGIN TEST *
    1.17 + **************/
    1.18 +
    1.19 +function isString(v, index, array)
    1.20 +{
    1.21 +  assertEq(array[index], v);
    1.22 +  return typeof v == 'string';
    1.23 +}
    1.24 +
    1.25 +function dumpError(e)
    1.26 +{
    1.27 +  var s = e.name + ': ' + e.message +
    1.28 +    ' File: ' + e.fileName +
    1.29 +    ', Line: ' + e.lineNumber +
    1.30 +    ', Stack: ' + e.stack;
    1.31 +  return s;
    1.32 +}
    1.33 +
    1.34 +var expect;
    1.35 +var actual;
    1.36 +var obj;
    1.37 +
    1.38 +var strings = ['hello', 'Array', 'WORLD'];
    1.39 +var mixed   = [0, '1', 2];
    1.40 +var sparsestrings = new Array();
    1.41 +sparsestrings[2] = 'sparse';
    1.42 +var arraylike = {0:0, 1:'string', 2:2, length:3};
    1.43 +// array for which JSObject::isIndexed() holds.
    1.44 +var indexedArray = [];
    1.45 +Object.defineProperty(indexedArray, 42, { get: function() { return 42; } });
    1.46 +Object.defineProperty(indexedArray, 142, { get: function() { return 'string'; } });
    1.47 +
    1.48 +// find and findIndex have 1 required argument
    1.49 +
    1.50 +expect = 1;
    1.51 +actual = Array.prototype.find.length;
    1.52 +reportCompare(expect, actual, 'Array.prototype.find.length == 1');
    1.53 +actual = Array.prototype.findIndex.length;
    1.54 +reportCompare(expect, actual, 'Array.prototype.findIndex.length == 1');
    1.55 +
    1.56 +// throw TypeError if no predicate specified
    1.57 +expect = 'TypeError';
    1.58 +try
    1.59 +{
    1.60 +  strings.find();
    1.61 +  actual = 'no error';
    1.62 +}
    1.63 +catch(e)
    1.64 +{
    1.65 +  actual = e.name;
    1.66 +}
    1.67 +reportCompare(expect, actual, 'Array.find(undefined) throws TypeError');
    1.68 +try
    1.69 +{
    1.70 +  strings.findIndex();
    1.71 +  actual = 'no error';
    1.72 +}
    1.73 +catch(e)
    1.74 +{
    1.75 +  actual = e.name;
    1.76 +}
    1.77 +reportCompare(expect, actual, 'Array.findIndex(undefined) throws TypeError');
    1.78 +
    1.79 +// Length gets treated as integer, not uint32
    1.80 +obj = { length: -4294967295, 0: 42 };
    1.81 +expected = undefined;
    1.82 +actual = Array.prototype.find.call(obj, () => true);
    1.83 +reportCompare(expected, actual, 'find correctly treats "length" as an integer');
    1.84 +expected = -1
    1.85 +actual = Array.prototype.findIndex.call(obj, () => true);
    1.86 +reportCompare(expected, actual, 'findIndex correctly treats "length" as an integer');
    1.87 +
    1.88 +// test find and findIndex results
    1.89 +try
    1.90 +{
    1.91 +  expect = 'hello';
    1.92 +  actual = strings.find(isString);
    1.93 +}
    1.94 +catch(e)
    1.95 +{
    1.96 +  actual = dumpError(e);
    1.97 +}
    1.98 +reportCompare(expect, actual, 'strings: find finds first string element');
    1.99 +
   1.100 +try
   1.101 +{
   1.102 +  expect = 0;
   1.103 +  actual = strings.findIndex(isString);
   1.104 +}
   1.105 +catch(e)
   1.106 +{
   1.107 +  actual = dumpError(e);
   1.108 +}
   1.109 +reportCompare(expect, actual, 'strings: findIndex finds first string element');
   1.110 +
   1.111 +try
   1.112 +{
   1.113 +  expect = '1';
   1.114 +  actual = mixed.find(isString);
   1.115 +}
   1.116 +catch(e)
   1.117 +{
   1.118 +  actual = dumpError(e);
   1.119 +}
   1.120 +reportCompare(expect, actual, 'mixed: find finds first string element');
   1.121 +
   1.122 +try
   1.123 +{
   1.124 +  expect = 1;
   1.125 +  actual = mixed.findIndex(isString);
   1.126 +}
   1.127 +catch(e)
   1.128 +{
   1.129 +  actual = dumpError(e);
   1.130 +}
   1.131 +reportCompare(expect, actual, 'mixed: findIndex finds first string element');
   1.132 +
   1.133 +try
   1.134 +{
   1.135 +  expect = 'sparse';
   1.136 +  actual = sparsestrings.find(isString);
   1.137 +}
   1.138 +catch(e)
   1.139 +{
   1.140 +  actual = dumpError(e);
   1.141 +}
   1.142 +reportCompare(expect, actual, 'sparsestrings: find finds first string element');
   1.143 +
   1.144 +try
   1.145 +{
   1.146 +  expect = 2;
   1.147 +  actual = sparsestrings.findIndex(isString);
   1.148 +}
   1.149 +catch(e)
   1.150 +{
   1.151 +  actual = dumpError(e);
   1.152 +}
   1.153 +reportCompare(expect, actual, 'sparsestrings: findIndex finds first string element');
   1.154 +
   1.155 +try
   1.156 +{
   1.157 +  expect = 'string';
   1.158 +  actual = [].find.call(arraylike, isString);
   1.159 +}
   1.160 +catch(e)
   1.161 +{
   1.162 +  actual = dumpError(e);
   1.163 +}
   1.164 +reportCompare(expect, actual, 'arraylike: find finds first string element');
   1.165 +
   1.166 +try
   1.167 +{
   1.168 +  expect = 1;
   1.169 +  actual = [].findIndex.call(arraylike, isString);
   1.170 +}
   1.171 +catch(e)
   1.172 +{
   1.173 +  actual = dumpError(e);
   1.174 +}
   1.175 +reportCompare(expect, actual, 'arraylike: findIndex finds first string element');
   1.176 +
   1.177 +try
   1.178 +{
   1.179 +  expect = 1;
   1.180 +  actual = 0;
   1.181 +  Array.prototype.find.call({get 0(){ actual++ }, length: 1}, ()=>true);
   1.182 +}
   1.183 +catch(e)
   1.184 +{
   1.185 +  actual = dumpError(e);
   1.186 +}
   1.187 +reportCompare(expect, actual, 'arraylike with getter: getter only called once');
   1.188 +
   1.189 +try
   1.190 +{
   1.191 +  expect = 'string';
   1.192 +  actual = [].find.call(indexedArray, isString);
   1.193 +}
   1.194 +catch(e)
   1.195 +{
   1.196 +  actual = dumpError(e);
   1.197 +}
   1.198 +reportCompare(expect, actual, 'indexedArray: find finds first string element');
   1.199 +
   1.200 +try
   1.201 +{
   1.202 +  expect = 142;
   1.203 +  actual = [].findIndex.call(indexedArray, isString);
   1.204 +}
   1.205 +catch(e)
   1.206 +{
   1.207 +  actual = dumpError(e);
   1.208 +}
   1.209 +reportCompare(expect, actual, 'indexedArray: findIndex finds first string element');

mercurial