michael@0: /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: //----------------------------------------------------------------------------- michael@0: var BUGNUMBER = 885553; michael@0: var summary = 'Array.prototype.find and Array.prototype.findIndex'; michael@0: michael@0: print(BUGNUMBER + ": " + summary); michael@0: michael@0: /************** michael@0: * BEGIN TEST * michael@0: **************/ michael@0: michael@0: function isString(v, index, array) michael@0: { michael@0: assertEq(array[index], v); michael@0: return typeof v == 'string'; michael@0: } michael@0: michael@0: function dumpError(e) michael@0: { michael@0: var s = e.name + ': ' + e.message + michael@0: ' File: ' + e.fileName + michael@0: ', Line: ' + e.lineNumber + michael@0: ', Stack: ' + e.stack; michael@0: return s; michael@0: } michael@0: michael@0: var expect; michael@0: var actual; michael@0: var obj; michael@0: michael@0: var strings = ['hello', 'Array', 'WORLD']; michael@0: var mixed = [0, '1', 2]; michael@0: var sparsestrings = new Array(); michael@0: sparsestrings[2] = 'sparse'; michael@0: var arraylike = {0:0, 1:'string', 2:2, length:3}; michael@0: // array for which JSObject::isIndexed() holds. michael@0: var indexedArray = []; michael@0: Object.defineProperty(indexedArray, 42, { get: function() { return 42; } }); michael@0: Object.defineProperty(indexedArray, 142, { get: function() { return 'string'; } }); michael@0: michael@0: // find and findIndex have 1 required argument michael@0: michael@0: expect = 1; michael@0: actual = Array.prototype.find.length; michael@0: reportCompare(expect, actual, 'Array.prototype.find.length == 1'); michael@0: actual = Array.prototype.findIndex.length; michael@0: reportCompare(expect, actual, 'Array.prototype.findIndex.length == 1'); michael@0: michael@0: // throw TypeError if no predicate specified michael@0: expect = 'TypeError'; michael@0: try michael@0: { michael@0: strings.find(); michael@0: actual = 'no error'; michael@0: } michael@0: catch(e) michael@0: { michael@0: actual = e.name; michael@0: } michael@0: reportCompare(expect, actual, 'Array.find(undefined) throws TypeError'); michael@0: try michael@0: { michael@0: strings.findIndex(); michael@0: actual = 'no error'; michael@0: } michael@0: catch(e) michael@0: { michael@0: actual = e.name; michael@0: } michael@0: reportCompare(expect, actual, 'Array.findIndex(undefined) throws TypeError'); michael@0: michael@0: // Length gets treated as integer, not uint32 michael@0: obj = { length: -4294967295, 0: 42 }; michael@0: expected = undefined; michael@0: actual = Array.prototype.find.call(obj, () => true); michael@0: reportCompare(expected, actual, 'find correctly treats "length" as an integer'); michael@0: expected = -1 michael@0: actual = Array.prototype.findIndex.call(obj, () => true); michael@0: reportCompare(expected, actual, 'findIndex correctly treats "length" as an integer'); michael@0: michael@0: // test find and findIndex results michael@0: try michael@0: { michael@0: expect = 'hello'; michael@0: actual = strings.find(isString); michael@0: } michael@0: catch(e) michael@0: { michael@0: actual = dumpError(e); michael@0: } michael@0: reportCompare(expect, actual, 'strings: find finds first string element'); michael@0: michael@0: try michael@0: { michael@0: expect = 0; michael@0: actual = strings.findIndex(isString); michael@0: } michael@0: catch(e) michael@0: { michael@0: actual = dumpError(e); michael@0: } michael@0: reportCompare(expect, actual, 'strings: findIndex finds first string element'); michael@0: michael@0: try michael@0: { michael@0: expect = '1'; michael@0: actual = mixed.find(isString); michael@0: } michael@0: catch(e) michael@0: { michael@0: actual = dumpError(e); michael@0: } michael@0: reportCompare(expect, actual, 'mixed: find finds first string element'); michael@0: michael@0: try michael@0: { michael@0: expect = 1; michael@0: actual = mixed.findIndex(isString); michael@0: } michael@0: catch(e) michael@0: { michael@0: actual = dumpError(e); michael@0: } michael@0: reportCompare(expect, actual, 'mixed: findIndex finds first string element'); michael@0: michael@0: try michael@0: { michael@0: expect = 'sparse'; michael@0: actual = sparsestrings.find(isString); michael@0: } michael@0: catch(e) michael@0: { michael@0: actual = dumpError(e); michael@0: } michael@0: reportCompare(expect, actual, 'sparsestrings: find finds first string element'); michael@0: michael@0: try michael@0: { michael@0: expect = 2; michael@0: actual = sparsestrings.findIndex(isString); michael@0: } michael@0: catch(e) michael@0: { michael@0: actual = dumpError(e); michael@0: } michael@0: reportCompare(expect, actual, 'sparsestrings: findIndex finds first string element'); michael@0: michael@0: try michael@0: { michael@0: expect = 'string'; michael@0: actual = [].find.call(arraylike, isString); michael@0: } michael@0: catch(e) michael@0: { michael@0: actual = dumpError(e); michael@0: } michael@0: reportCompare(expect, actual, 'arraylike: find finds first string element'); michael@0: michael@0: try michael@0: { michael@0: expect = 1; michael@0: actual = [].findIndex.call(arraylike, isString); michael@0: } michael@0: catch(e) michael@0: { michael@0: actual = dumpError(e); michael@0: } michael@0: reportCompare(expect, actual, 'arraylike: findIndex finds first string element'); michael@0: michael@0: try michael@0: { michael@0: expect = 1; michael@0: actual = 0; michael@0: Array.prototype.find.call({get 0(){ actual++ }, length: 1}, ()=>true); michael@0: } michael@0: catch(e) michael@0: { michael@0: actual = dumpError(e); michael@0: } michael@0: reportCompare(expect, actual, 'arraylike with getter: getter only called once'); michael@0: michael@0: try michael@0: { michael@0: expect = 'string'; michael@0: actual = [].find.call(indexedArray, isString); michael@0: } michael@0: catch(e) michael@0: { michael@0: actual = dumpError(e); michael@0: } michael@0: reportCompare(expect, actual, 'indexedArray: find finds first string element'); michael@0: michael@0: try michael@0: { michael@0: expect = 142; michael@0: actual = [].findIndex.call(indexedArray, isString); michael@0: } michael@0: catch(e) michael@0: { michael@0: actual = dumpError(e); michael@0: } michael@0: reportCompare(expect, actual, 'indexedArray: findIndex finds first string element');