Skip to content

Commit 1cdb062

Browse files
committed
Array.prototype.map() tests added.
JerryScript-DCO-1.0-Signed-off-by: Laszlo Vidacs [email protected]
1 parent 8230b66 commit 1cdb062

File tree

1 file changed

+86
-0
lines changed

1 file changed

+86
-0
lines changed

tests/jerry/array_prototype_map.js

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
// Copyright 2015 Samsung Electronics Co., Ltd.
2+
// Copyright 2015 University of Szeged.
3+
//
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
//
8+
// http://www.apache.org/licenses/LICENSE-2.0
9+
//
10+
// Unless required by applicable law or agreed to in writing, software
11+
// distributed under the License is distributed on an "AS IS" BASIS
12+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
// See the License for the specific language governing permissions and
14+
// limitations under the License.
15+
16+
// helper function - simple implementation
17+
Array.prototype.equals = function (array) {
18+
if (this.length != array.length)
19+
return false;
20+
21+
for (var i = 0; i < this.length; i++) {
22+
if (this[i] instanceof Array && array[i] instanceof Array) {
23+
if (!this[i].equals(array[i]))
24+
return false;
25+
}
26+
else if (this[i] != array[i]) {
27+
return false;
28+
}
29+
}
30+
31+
return true;
32+
}
33+
34+
// check function type
35+
try {
36+
[0].map(new Object());
37+
assert(false);
38+
} catch(e) {
39+
assert(e instanceof TypeError);
40+
}
41+
42+
// various checks
43+
assert ([1, 4, 9].map(Math.sqrt).equals([1, 2, 3]));
44+
45+
assert (isNaN([1, 4, "X"].map(Number)[2]));
46+
47+
var func = function(val, idx) {
48+
return val + idx;
49+
};
50+
51+
assert ([1, 4, 9].map(func).equals([1,5,11]));
52+
53+
assert ([1, "X", 10].map(func).equals([1, "X1", 12]));
54+
55+
var long_array = [0, 1];
56+
assert (long_array.map(func).equals([0,2]));
57+
58+
long_array[100] = 1;
59+
assert (long_array.map(func).equals([0,2,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,101]));
60+
61+
// Checking behavior when unable to get length
62+
var obj = {};
63+
Object.defineProperty(obj, 'length', { 'get' : function () {throw new ReferenceError ("foo"); } });
64+
obj.map = Array.prototype.map;
65+
66+
try {
67+
obj.map(func);
68+
assert(false);
69+
} catch (e) {
70+
assert(e.message === "foo");
71+
assert(e instanceof ReferenceError);
72+
}
73+
74+
// Checking behavior when unable to get element
75+
var obj = {}
76+
obj.length = 1;
77+
Object.defineProperty(obj, '0', { 'get' : function () {throw new ReferenceError ("foo"); } });
78+
obj.map = Array.prototype.map
79+
80+
try {
81+
obj.map(func);
82+
assert(false);
83+
} catch (e) {
84+
assert(e.message === "foo");
85+
assert(e instanceof ReferenceError);
86+
}

0 commit comments

Comments
 (0)