1// Copyright JS Foundation and other contributors, http://js.foundation 2// 3// Licensed under the Apache License, Version 2.0 (the "License"); 4// you may not use this file except in compliance with the License. 5// You may obtain a copy of the License at 6// 7// http://www.apache.org/licenses/LICENSE-2.0 8// 9// Unless required by applicable law or agreed to in writing, software 10// distributed under the License is distributed on an "AS IS" BASIS 11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12// See the License for the specific language governing permissions and 13// limitations under the License. 14 15// helper function - simple implementation 16Array.prototype.equals = function (array) { 17 if (this.length != array.length) 18 return false; 19 20 for (var i = 0; i < this.length; i++) { 21 if (this[i] instanceof Array && array[i] instanceof Array) { 22 if (!this[i].equals(array[i])) 23 return false; 24 } 25 else if (this[i] != array[i]) { 26 return false; 27 } 28 } 29 30 return true; 31} 32 33// check function type 34try { 35 [0].map(new Object()); 36 assert(false); 37} catch(e) { 38 assert(e instanceof TypeError); 39} 40 41// various checks 42assert ([1, 4, 9].map(Math.sqrt).equals([1, 2, 3])); 43 44assert (isNaN([1, 4, "X"].map(Number)[2])); 45 46var func = function(val, idx) { 47 return val + idx; 48}; 49 50assert ([1, 4, 9].map(func).equals([1,5,11])); 51 52assert ([1, "X", 10].map(func).equals([1, "X1", 12])); 53 54var arr = [1,2,3]; 55arr.length = 5; 56assert(arr.map(func).length === arr.length); 57 58var long_array = [0, 1]; 59assert (long_array.map(func).equals([0,2])); 60 61long_array[100] = 1; 62assert (long_array.map(func).equals([0,2,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,101])); 63 64var arr = [1,2]; 65Array.prototype[0] = 3; 66var newArr = arr.map(function(value) { return value; }); 67delete Array.prototype[0]; 68assert(newArr.hasOwnProperty("0")); 69assert(newArr[0] === 1); 70 71// check behavior when unable to get length 72var obj = {}; 73Object.defineProperty(obj, 'length', { 'get' : function () {throw new ReferenceError ("foo"); } }); 74obj.map = Array.prototype.map; 75 76try { 77 obj.map(func); 78 assert(false); 79} catch (e) { 80 assert(e.message === "foo"); 81 assert(e instanceof ReferenceError); 82} 83 84// check behavior when unable to get element 85var obj = {} 86obj.length = 1; 87Object.defineProperty(obj, '0', { 'get' : function () {throw new ReferenceError ("foo"); } }); 88obj.map = Array.prototype.map 89 90try { 91 obj.map(func); 92 assert(false); 93} catch (e) { 94 assert(e.message === "foo"); 95 assert(e instanceof ReferenceError); 96} 97 98// check thisArg 99var thisArg = {add: 10}; 100var func2 = function(value) { 101 return this.add + value; 102} 103assert([1,2].map(func2, thisArg).equals([11, 12])); 104 105// check passed Object 106var array_example = [1,2]; 107Object.defineProperty(array_example, 'const', { 'get' : function () {return "CT";} }); 108var func3 = function(value, idx, thisobj) { 109 return value * idx + thisobj.const; 110} 111assert(array_example.map(func3).equals(["0CT", "2CT"])); 112