js/src/tests/ecma_6/Array/fill.js

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
michael@0 2 /* Any copyright is dedicated to the Public Domain.
michael@0 3 * http://creativecommons.org/licenses/publicdomain/ */
michael@0 4
michael@0 5 //-----------------------------------------------------------------------------
michael@0 6 var BUGNUMBER = 911147;
michael@0 7 var summary = 'Array.prototype.fill';
michael@0 8
michael@0 9 print(BUGNUMBER + ": " + summary);
michael@0 10
michael@0 11 /**************
michael@0 12 * BEGIN TEST *
michael@0 13 **************/
michael@0 14
michael@0 15 assertEq(typeof [].fill, 'function');
michael@0 16 assertEq([].fill.length, 1);
michael@0 17
michael@0 18 // Default values for arguments and absolute values for negative start and end
michael@0 19 // arguments are resolved correctly.
michael@0 20 assertDeepEq([].fill(1), []);
michael@0 21 assertDeepEq([1,1,1].fill(2), [2,2,2]);
michael@0 22 assertDeepEq([1,1,1].fill(2, 1), [1,2,2]);
michael@0 23 assertDeepEq([1,1,1].fill(2, 1, 2), [1,2,1]);
michael@0 24 assertDeepEq([1,1,1].fill(2, -2), [1,2,2]);
michael@0 25 assertDeepEq([1,1,1].fill(2, -2, -1), [1,2,1]);
michael@0 26 assertDeepEq([1,1,1].fill(2, undefined), [2,2,2]);
michael@0 27 assertDeepEq([1,1,1].fill(2, undefined, undefined), [2,2,2]);
michael@0 28 assertDeepEq([1,1,1].fill(2, 1, undefined), [1,2,2]);
michael@0 29 assertDeepEq([1,1,1].fill(2, undefined, 1), [2,1,1]);
michael@0 30 assertDeepEq([1,1,1].fill(2, 2, 1), [1,1,1]);
michael@0 31 assertDeepEq([1,1,1].fill(2, -1, 1), [1,1,1]);
michael@0 32 assertDeepEq([1,1,1].fill(2, -2, 1), [1,1,1]);
michael@0 33 assertDeepEq([1,1,1].fill(2, 1, -2), [1,1,1]);
michael@0 34 assertDeepEq([1,1,1].fill(2, 0.1), [2,2,2]);
michael@0 35 assertDeepEq([1,1,1].fill(2, 0.9), [2,2,2]);
michael@0 36 assertDeepEq([1,1,1].fill(2, 1.1), [1,2,2]);
michael@0 37 assertDeepEq([1,1,1].fill(2, 0.1, 0.9), [1,1,1]);
michael@0 38 assertDeepEq([1,1,1].fill(2, 0.1, 1.9), [2,1,1]);
michael@0 39 assertDeepEq([1,1,1].fill(2, 0.1, 1.9), [2,1,1]);
michael@0 40 assertDeepEq([1,1,1].fill(2, -0), [2,2,2]);
michael@0 41 assertDeepEq([1,1,1].fill(2, 0, -0), [1,1,1]);
michael@0 42 assertDeepEq([1,1,1].fill(2, NaN), [2,2,2]);
michael@0 43 assertDeepEq([1,1,1].fill(2, 0, NaN), [1,1,1]);
michael@0 44 assertDeepEq([1,1,1].fill(2, false), [2,2,2]);
michael@0 45 assertDeepEq([1,1,1].fill(2, true), [1,2,2]);
michael@0 46 assertDeepEq([1,1,1].fill(2, "0"), [2,2,2]);
michael@0 47 assertDeepEq([1,1,1].fill(2, "1"), [1,2,2]);
michael@0 48 assertDeepEq([1,1,1].fill(2, "-2"), [1,2,2]);
michael@0 49 assertDeepEq([1,1,1].fill(2, "-2", "-1"), [1,2,1]);
michael@0 50 assertDeepEq([1,1,1].fill(2, {valueOf: ()=>1}), [1,2,2]);
michael@0 51 assertDeepEq([1,1,1].fill(2, 0, {valueOf: ()=>1}), [2,1,1]);
michael@0 52
michael@0 53 // fill works generically for objects, too.
michael@0 54 assertDeepEq([].fill.call({length: 2}, 2), {0: 2, 1: 2, length: 2});
michael@0 55
michael@0 56 var setterCalled = false;
michael@0 57 var objWithSetter = {set "0"(val) { setterCalled = true}, length: 1};
michael@0 58 [].fill.call(objWithSetter, 2);
michael@0 59 assertEq(setterCalled, true);
michael@0 60
michael@0 61 var setHandlerCallCount = 0;
michael@0 62 var proxy = new Proxy({length: 3}, {set: function(value) {setHandlerCallCount++;}});
michael@0 63 [].fill.call(proxy, 2);
michael@0 64 assertEq(setHandlerCallCount, 3);
michael@0 65
michael@0 66 var valueOfCallCount = 0;
michael@0 67 var typedArray = new Uint8ClampedArray(3);
michael@0 68 [].fill.call(typedArray, {valueOf: function() {valueOfCallCount++; return 2000;}});
michael@0 69 assertEq(valueOfCallCount, 3);
michael@0 70 assertEq(typedArray[0], 0xff);
michael@0 71
michael@0 72 // All remaining cases should throw.
michael@0 73 var objWithGetterOnly = {get "0"() {return 1;}, length: 1};
michael@0 74
michael@0 75 var objWithReadOnlyProp = {length: 1};
michael@0 76 Object.defineProperty(objWithReadOnlyProp, 0, {value: 1, writable: false});
michael@0 77
michael@0 78 var objWithNonconfigurableProp = {length: 1};
michael@0 79 Object.defineProperty(objWithNonconfigurableProp, 0, {value: 1, configurable: false});
michael@0 80
michael@0 81 var frozenObj = {length: 1};
michael@0 82 Object.freeze(frozenObj);
michael@0 83
michael@0 84 var frozenArray = [1, 1, 1];
michael@0 85 Object.freeze(frozenArray);
michael@0 86
michael@0 87 assertThrowsInstanceOf(() => [].fill.call(objWithGetterOnly, 2), TypeError);
michael@0 88 assertThrowsInstanceOf(() => [].fill.call(objWithReadOnlyProp, 2), TypeError);
michael@0 89 assertThrowsInstanceOf(() => [].fill.call(objWithNonconfigurableProp, 2), TypeError);
michael@0 90 assertThrowsInstanceOf(() => [].fill.call(frozenObj, 2), TypeError);
michael@0 91 assertThrowsInstanceOf(() => [].fill.call(frozenArray, 2), TypeError);
michael@0 92 assertThrowsInstanceOf(() => [].fill.call("111", 2), TypeError);
michael@0 93 assertThrowsInstanceOf(() => [].fill.call(null, 2), TypeError);
michael@0 94 assertThrowsInstanceOf(() => [].fill.call(undefined, 2), TypeError);
michael@0 95
michael@0 96 if (typeof reportCompare === "function")
michael@0 97 reportCompare(true, true);

mercurial