Array.prototype.reduce() tests, minor fixes.

JerryScript-DCO-1.0-Signed-off-by: Laszlo Vidacs lvidacs.u-szeged@partner.samsung.com
This commit is contained in:
Laszlo Vidacs 2015-05-27 11:05:08 +02:00 committed by Peter Gal
parent 71059fe346
commit f22f3c50e7
2 changed files with 60 additions and 1 deletions

View File

@ -2560,13 +2560,17 @@ ecma_builtin_array_prototype_object_reduce (ecma_value_t this_arg, /**< this arg
current_index = ecma_make_number_value (num_p);
ecma_value_t prev_value = ecma_get_completion_value_value (accumulator);
ecma_value_t call_args[] = {prev_value, current_value, current_index, obj_this};
ecma_value_t undefined_value = ecma_make_simple_value (ECMA_SIMPLE_VALUE_UNDEFINED);
ECMA_TRY_CATCH (call_value,
ecma_op_function_call (func_object_p, ECMA_SIMPLE_VALUE_UNDEFINED, call_args, 4),
ecma_op_function_call (func_object_p, undefined_value, call_args, 4),
ret_value);
ecma_free_completion_value (accumulator);
accumulator = ecma_copy_completion_value (call_value);
ECMA_FINALIZE (call_value);
ecma_free_value (undefined_value, false);
ECMA_FINALIZE (current_value);
}
ecma_deref_ecma_string (index_str_p);

View File

@ -0,0 +1,55 @@
// Copyright 2015 Samsung Electronics Co., Ltd.
// Copyright 2015 University of Szeged.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
var func = function(a, b) {
return a + b;
}
// check function type
try {
[0].reduce(new Object());
assert(false);
} catch(e) {
assert(e instanceof TypeError);
}
// check for init value
try {
[].reduce(func);
}
catch(e) {
assert(e instanceof TypeError);
}
// various checks
assert ([].reduce(func, 1) === 1);
assert ([0].reduce(func) === 0);
assert ([0, 1].reduce(func) === 1);
assert ([0, 1].reduce(func, 1) === 2);
assert ([0, 1, 2, 3].reduce(func, 1) === 7);
assert (["A","B"].reduce(func) === "AB");
assert (["A","B"].reduce(func, "Init:") === "Init:AB");
assert ([0, 1].reduce(func, 3.2) === 4.2);
assert ([0, "x", 1].reduce(func) === "0x1");
assert ([0, "x", 1].reduce(func, 3.2) === "3.2x1");