Skip to content

Commit 365decc

Browse files
committed
Increase test coverage: Array.prototype.sort
Added new test cases to array-prototype-sort.js to improve branch coverage. Branch coverage: - before: 44/50 - after: 48/48 Also removed an unnecessary condition from the while loop what counts properties with lower index than len. JerryScript-DCO-1.0-Signed-off-by: Csaba Repasi repasics@inf.u-szeged.hu
1 parent f2404ac commit 365decc

File tree

2 files changed

+72
-1
lines changed

2 files changed

+72
-1
lines changed

jerry-core/ecma/builtin-objects/ecma-builtin-array-prototype.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1058,7 +1058,7 @@ ecma_builtin_array_prototype_object_sort (ecma_value_t this_arg, /**< this argum
10581058
ecma_value_t *ecma_value_p = ecma_collection_iterator_init (array_index_props_p);
10591059

10601060
/* Count properties with name that is array index less than len */
1061-
while (ecma_value_p != NULL && ecma_is_value_empty (ret_value))
1061+
while (ecma_value_p != NULL)
10621062
{
10631063
ecma_string_t *property_name_p = ecma_get_string_from_value (*ecma_value_p);
10641064
ecma_value_p = ecma_collection_iterator_next (ecma_value_p);

tests/jerry/array-prototype-sort.js

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,3 +92,74 @@ try {
9292
assert(e.message === "foo");
9393
assert(e instanceof ReferenceError);
9494
}
95+
96+
// Checking behavior when this value is undefined
97+
var obj = { sort : Array.prototype.sort };
98+
99+
try {
100+
obj.sort.call(undefined, function () { });
101+
assert(false);
102+
} catch (e) {
103+
assert(e instanceof TypeError);
104+
}
105+
106+
// Checking behavior when length's valueOf throws exception
107+
var len = { };
108+
Object.defineProperty(len, 'valueOf', { 'get' : function () { throw new ReferenceError ("foo"); } });
109+
var obj = { sort : Array.prototype.sort, length : len };
110+
111+
try {
112+
obj.sort();
113+
assert(false);
114+
} catch (e) {
115+
assert(e.message === 'foo');
116+
assert(e instanceof ReferenceError);
117+
}
118+
119+
// Checking behavior when unable to get elements
120+
var obj = { sort : Array.prototype.sort, length : 2};
121+
Object.defineProperty(obj, '0', { 'get' : function () { throw new ReferenceError ("foo"); } });
122+
Object.defineProperty(obj, '1', { 'get' : function () { throw new ReferenceError ("bar"); } });
123+
124+
try {
125+
obj.sort();
126+
assert(false);
127+
} catch (e) {
128+
assert(e.message === "foo");
129+
assert(e instanceof ReferenceError);
130+
}
131+
132+
// Checking behavior when array is non-extensible while sorting
133+
var arr = [1, 0];
134+
135+
try {
136+
arr.sort(function () { Object.freeze(arr) });
137+
assert(false);
138+
} catch (e) {
139+
assert(e instanceof TypeError);
140+
}
141+
142+
// Checking behavior when unable to delete property
143+
var obj = {sort : Array.prototype.sort, '0' : 2, '1' : 1, length : 4};
144+
Object.defineProperty(obj, '3', function () {});
145+
146+
try {
147+
obj.sort();
148+
assert(false);
149+
}
150+
catch (e) {
151+
assert(e instanceof TypeError);
152+
}
153+
154+
// Checking behavior when unable to get the last element
155+
var arr = [1, 2, ];
156+
Object.defineProperty(arr, '2', { 'get' : function () { throw new ReferenceError ("foo"); } });
157+
158+
try {
159+
arr.sort();
160+
assert(false);
161+
}
162+
catch (e) {
163+
assert(e.message === 'foo');
164+
assert(e instanceof ReferenceError);
165+
}

0 commit comments

Comments
 (0)