Skip to content

Commit 0904c3d

Browse files
committed
Increase test coverage: Array.prototype.sort
Added new test cases to array-prototype-sort.js to improve branch coverage. Branch coverage: sort: - before: 44/50 - after: 48/48 sort compare helper: - before: 18/24 - after: 24/24 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 [email protected]
1 parent f2404ac commit 0904c3d

File tree

2 files changed

+121
-1
lines changed

2 files changed

+121
-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: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,3 +92,123 @@ 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+
}
166+
167+
// Checking behavior when lhs_value throws exception at comparefn
168+
f = function () { throw new ReferenceError('foo'); };
169+
obj = { 'toString' : f };
170+
arr = [obj, 1];
171+
172+
try {
173+
arr.sort();
174+
assert(false);
175+
}
176+
catch (e) {
177+
assert(e.message === 'foo');
178+
assert(e instanceof ReferenceError);
179+
}
180+
181+
// Checking behavior when rhs_value throws exception at comparefn
182+
f = function () { throw new ReferenceError('foo'); };
183+
obj = { 'toString' : f };
184+
arr = [1, obj];
185+
186+
try {
187+
arr.sort();
188+
assert(false);
189+
}
190+
catch (e) {
191+
assert(e.message === 'foo');
192+
assert(e instanceof ReferenceError);
193+
}
194+
195+
// Sorting when array elements are the same string
196+
arr = ['foo', 'foo'];
197+
arr.sort();
198+
199+
assert(arr[0] === 'foo');
200+
assert(arr[1] === 'foo');
201+
202+
// Checking behavior when comparefn's call value cannot be converted to number
203+
obj = { };
204+
Object.defineProperty(obj, 'toString', function () { });
205+
f = function () { return obj };
206+
arr = [1, 2];
207+
208+
try {
209+
arr.sort(f);
210+
assert(false);
211+
}
212+
catch (e) {
213+
assert(e instanceof TypeError);
214+
}

0 commit comments

Comments
 (0)