Skip to content

Increase branch coverage: Array.prototype functions #2796

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -579,7 +579,7 @@ ecma_builtin_array_prototype_object_reverse (ecma_value_t this_arg, /**< this ar
ECMA_FINALIZE (put_value);
}
/* 6.j */
else if (lower_exist && !upper_exist)
else if (lower_exist)
{
ECMA_TRY_CATCH (del_value, ecma_op_object_delete (obj_p, lower_str_p, true), ret_value);
ECMA_TRY_CATCH (put_value, ecma_op_object_put (obj_p, upper_str_p, lower_value, true), ret_value);
Expand Down Expand Up @@ -1224,8 +1224,9 @@ ecma_builtin_array_prototype_object_splice (const ecma_value_t args[], /**< argu
}
}
/* 13. */
else if (item_count > delete_count)
else
{
JERRY_ASSERT (item_count > delete_count);
/* 13.b */
for (k = len - delete_count; k > start && ecma_is_value_empty (ret_value); k--)
{
Expand Down
14 changes: 14 additions & 0 deletions tests/jerry/array-prototype-concat.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,3 +74,17 @@ try {
assert(e.message === "foo");
assert(e instanceof ReferenceError);
}

/* ES v5.1 15.4.4.4.5.
Checking behavior when unable to get element from a given array */
arr1 = [];
arr2 = [];
arr3 = [];
Object.defineProperty(arr2, '0', { 'get' : function () {throw new ReferenceError ("foo"); } });

try {
arr1.concat(arr2, arr3);
assert(false);
} catch (e) {
assert(e instanceof ReferenceError);
}
13 changes: 13 additions & 0 deletions tests/jerry/array-prototype-join.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,3 +74,16 @@ obj_2[3] = 4;
obj_2.join = Array.prototype.join;

assert (obj_2.join() === "1,2,3");

/* ES v5.1 15.4.4.5.7.
Checking behavior when an element throws error */
try {
var f = function () { throw new TypeError("ooo");};
var arr = [0, 1, 2, 3];
Object.defineProperty(arr, '0', { 'get' : f });
Array.prototype.join.call(arr);
assert(false);
} catch (e) {
assert(e instanceof TypeError);
assert(e.message == "ooo");
}
24 changes: 24 additions & 0 deletions tests/jerry/array-prototype-pop.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,3 +72,27 @@ try {
assert(e.message === "foo");
assert(e instanceof ReferenceError);
}

/* ES v5.1 15.4.4.6.5.c
Checking behavior when unable to delete property */
var obj = {pop : Array.prototype.pop, length : 2};
Object.defineProperty(obj, '1', function () {});

try {
obj.pop();
assert(false);
} catch (e) {
assert(e instanceof TypeError);
}

/* ES v5.1 15.4.4.6.5.d
Checking behavior when array is not modifiable */
var obj = {pop : Array.prototype.pop, length : 2};
Object.freeze(obj);

try {
obj.pop();
assert(false);
} catch (e) {
assert(e instanceof TypeError);
}
12 changes: 12 additions & 0 deletions tests/jerry/array-prototype-push.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,3 +74,15 @@ try {
}
assert(o.length === 1);
assert(o[0] === "z");

/* ES v5.1 15.4.4.7.5.
Checking behavior when array is non-extensible while pushing */
var arr = [];
Object.freeze(arr);

try {
arr.push(1, 2);
assert(false);
} catch (e) {
assert(e instanceof TypeError);
}
74 changes: 74 additions & 0 deletions tests/jerry/array-prototype-reverse.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,77 @@ try {
assert(e.message === "foo");
assert(e instanceof ReferenceError);
}

/* ES v5.1 15.4.4.8.6.e.
Checking behavior when unable to get the last element */
var obj = { reverse : Array.prototype.reverse, length : 4 };
Object.defineProperty(obj, '3', { 'get' : function () {throw new ReferenceError ("foo"); } });

try {
obj.reverse();
assert(false);
} catch (e) {
assert(e.message === "foo");
assert(e instanceof ReferenceError);
}

/* ES v5.1 15.4.4.8.6.h.i.
Checking behavior when first 3 elements are not writable */
try {
var arr = [,,, 3, 4, 5, 6,,,,,,,,,0, 1, 2, 3, 4, 5, 6];
Object.defineProperty(arr, '0', {});
Object.defineProperty(arr, '1', {});
Object.defineProperty(arr, '2', {});
Array.prototype.reverse.call(arr);
assert(false);
} catch (e) {
assert(e instanceof TypeError);
}

/* ES v5.1 15.4.4.8.6.h.ii.
Checking behavior when last 3 elements are not writable */
try {
var arr = [0, 1, 2, 3, 4, 5, 6,,,,,,,,,0, 1, 2, 3,,,];
Object.defineProperty(arr, '19', {});
Object.defineProperty(arr, '20', {});
Object.defineProperty(arr, '21', {});
Array.prototype.reverse.call(arr);
assert(false);
} catch (e) {
assert(e instanceof TypeError);
}

/* ES v5.1 15.4.4.8.6.i.i.
Checking behavior when first elements do not exist and the array is freezed */
try {
var arr = [,,,,,,,,,,,,,,,,0, 1, 2, 3, 4, 5, 6];
arr = Object.freeze(arr);
Array.prototype.reverse.call(arr);
assert(false);
} catch (e) {
assert(e instanceof TypeError);
}

/* ES v5.1 15.4.4.8.6.i.ii.
Checking behavior when unable to get the first 2 elements */
var obj = { reverse : Array.prototype.reverse, length : 4 };
Object.defineProperty(obj, '2', { value : 0 });
Object.defineProperty(obj, '3', { value : 0 });
try {
obj.reverse();
assert(false);
} catch (e) {
assert(e instanceof TypeError);
}

/* ES v5.1 15.4.4.8.6.j.i.
Checking behavior when unable to get the last 2 elements */
var obj = { reverse : Array.prototype.reverse, length : 4 };
Object.defineProperty(obj, '0', { value : 0 });
Object.defineProperty(obj, '1', { value : 0 });
try {
obj.reverse();
assert(false);
} catch (e) {
assert(e instanceof TypeError);
}
58 changes: 58 additions & 0 deletions tests/jerry/array-prototype-shift.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,3 +77,61 @@ try {
assert(e.message === "foo");
assert(e instanceof ReferenceError);
}

/* ES v5.1 15.4.4.9.7.c.
Checking behavior when the array is freezed */
try {
f = function () { throw new ReferenceError("getter"); };
arr = { length : 9 };
Object.defineProperty(arr, '8', { 'get' : f });
Array.prototype.shift.call(arr);
assert(false);
} catch (e) {
assert(e instanceof ReferenceError);
assert(e.message == "getter");
}

/* ES v5.1 15.4.4.9.7.d.ii.
Checking behavior when the array is freezed */
try {
arr = { length : 9 };
Object.defineProperty(arr, '8', { value : 8 });
Object.defineProperty(arr, '7', { value : 7 });
Array.prototype.shift.call(arr);
assert(false);
} catch (e) {
assert(e instanceof TypeError);
}

/* ES v5.1 15.4.4.9.7.e.i.
Checking behavior when the first element is null */
try {
arr = { length : 9 };
Object.defineProperty(arr, '0', { value : null });
Array.prototype.shift.call(arr);
assert(false);
} catch (e) {
assert(e instanceof TypeError);
}

/* ES v5.1 15.4.4.9.8.
Checking behavior when last element is not writable */
try {
arr = { length : 9 };
Object.defineProperty(arr, '8', { writable : false });
Array.prototype.shift.call(arr);
assert(false);
} catch (e) {
assert(e instanceof TypeError);
}

/* ES v5.1 15.4.4.9.9.
Checking behavior when the array is freezed */
try {
arr = { length : 9 };
Object.freeze(arr);
Array.prototype.shift.call(arr);
assert(false);
} catch (e) {
assert(e instanceof TypeError);
}
41 changes: 41 additions & 0 deletions tests/jerry/array-prototype-slice.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,3 +110,44 @@ try {
assert (e.message === "foo");
assert (e instanceof ReferenceError);
}

/* ES v5.1 15.4.4.10.5.
Checking behavior when start value throws exception */
var arg1 = { };
Object.defineProperty(arg1, 'valueOf', { 'get' : function () { throw new ReferenceError ("foo"); } });
var obj = { slice : Array.prototype.slice };

try {
obj.slice(arg1);
assert(false);
} catch (e) {
assert(e.message === 'foo');
assert(e instanceof ReferenceError);
}

/* ES v5.1 15.4.4.10.7.
Checking behavior when end value throws exception */
var arg2 = { };
Object.defineProperty(arg2, 'valueOf', { 'get' : function () { throw new ReferenceError ("foo"); } });
var obj = { slice : Array.prototype.slice };

try {
obj.slice(0, arg2);
assert(false);
} catch (e) {
assert(e.message === 'foo');
assert(e instanceof ReferenceError);
}

/* ES v5.1 15.4.4.10.10.
Checking behavior when unable to get element */
var obj = { length : 3, slice : Array.prototype.slice };
Object.defineProperty(obj, '1', { 'get' : function () { throw new ReferenceError ("foo"); } });

try {
obj.slice(0, 3);
assert (false);
} catch (e) {
assert (e.message === "foo");
assert (e instanceof ReferenceError);
}
92 changes: 92 additions & 0 deletions tests/jerry/array-prototype-sort.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,3 +92,95 @@ try {
assert(e.message === "foo");
assert(e instanceof ReferenceError);
}

// Checking behavior when unable to get elements
var obj = { sort : Array.prototype.sort, length : 2};
Object.defineProperty(obj, '0', { 'get' : function () { throw new ReferenceError ("foo"); } });
Object.defineProperty(obj, '1', { 'get' : function () { throw new ReferenceError ("bar"); } });

try {
obj.sort();
assert(false);
} catch (e) {
assert(e.message === "foo");
assert(e instanceof ReferenceError);
}

// Checking behavior when array is non-extensible while sorting
var arr = [1, 0];

try {
arr.sort(function () { Object.freeze(arr) });
assert(false);
} catch (e) {
assert(e instanceof TypeError);
}

// Checking behavior when unable to delete property
var obj = {sort : Array.prototype.sort, '0' : 2, '1' : 1, length : 4};
Object.defineProperty(obj, '3', function () {});

try {
obj.sort();
assert(false);
} catch (e) {
assert(e instanceof TypeError);
}

// Checking behavior when unable to get the last element
var arr = [1, 2, ];
Object.defineProperty(arr, '2', { 'get' : function () { throw new ReferenceError ("foo"); } });

try {
arr.sort();
assert(false);
} catch (e) {
assert(e.message === 'foo');
assert(e instanceof ReferenceError);
}

// Checking behavior when lhs_value throws exception at comparefn
f = function () { throw new ReferenceError('foo'); };
obj = { 'toString' : f };
arr = [obj, 1];

try {
arr.sort();
assert(false);
} catch (e) {
assert(e.message === 'foo');
assert(e instanceof ReferenceError);
}

// Checking behavior when rhs_value throws exception at comparefn
f = function () { throw new ReferenceError('foo'); };
obj = { 'toString' : f };
arr = [1, obj];

try {
arr.sort();
assert(false);
} catch (e) {
assert(e.message === 'foo');
assert(e instanceof ReferenceError);
}

// Sorting when array elements are the same string
arr = ['foo', 'foo'];
arr.sort();

assert(arr[0] === 'foo');
assert(arr[1] === 'foo');

// Checking behavior when comparefn's call value cannot be converted to number
obj = { };
Object.defineProperty(obj, 'toString', function () { });
f = function () { return obj };
arr = [1, 2];

try {
arr.sort(f);
assert(false);
} catch (e) {
assert(e instanceof TypeError);
}
Loading