-
Notifications
You must be signed in to change notification settings - Fork 677
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
Array.prototype.reduce() #104
Changes from all commits
76425f9
346b16a
8427cb2
7c2f3b7
35441b7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
// 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); | ||
assert(false); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should add an |
||
catch(e) { | ||
assert(e instanceof TypeError); | ||
} | ||
|
||
// various checks | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should have a check for empty array + no/undefined initialValue to see if that results a TypeError as described in the 5th step. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This check is in lines 28-35 with empty array + no initial value. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ahh... true :) sorry |
||
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"); | ||
|
||
var long_array = [0, 1]; | ||
assert (long_array.reduce(func,10) === 11); | ||
|
||
long_array[10000] = 1; | ||
assert (long_array.reduce(func,10) === 12); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The code following this should be in the
else
case of the condition I think.