Skip to content

Commit

Permalink
Added tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dvd101x committed Aug 2, 2024
1 parent 0200a2f commit 82d447d
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/function/matrix/map.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ export const createMap = /* #__PURE__ */ factory(name, dependencies, ({ typed })
function _getCallback (callback, broadcastedArrays) {
const numberOfArrays = broadcastedArrays.length
if (callback.length >= 2 * numberOfArrays + 1) {
return (x, idx) => callback(...x, idx, broadcastedArrays)
return (x, idx) => callback(...x, idx, ...broadcastedArrays)
}
if (callback.length === numberOfArrays + 1) {
return (x, idx) => callback(...x, idx)
Expand Down
36 changes: 32 additions & 4 deletions test/unit-tests/function/matrix/map.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -150,12 +150,32 @@ describe('map', function () {
})

it('should invoke a typed function with correct number of arguments for two matrices and an index', function () {
const callback = function (a, b, idx) {
return a + b + idx[0]
}
const output = math.map(math.matrix([1, 2, 3]), math.matrix([4, 5, 6]), math.typed('callback', {
'number, number, Array': function (a, b, idx) {
return a + b + idx[0]
}
'number, number, Array': callback
}))
assert.deepStrictEqual(output, math.matrix([5, 8, 11]))
const expected = math.matrix([5, 8, 11])
assert.deepStrictEqual(output, expected)
})

it('should invoke a function with correct number of arguments for two matrices and an index', function () {
const callback = function (a, b, idx) {
return a + b + idx[0]
}
const output = math.map(math.matrix([1, 2, 3]), math.matrix([4, 5, 6]), callback)
const expected = math.matrix([5, 8, 11])
assert.deepStrictEqual(output, expected)
})

it('should invoke a function with correct number of arguments for two matrices, index and original matrices', function () {
const callback = function (a, b, idx, A, B) {
return a + b + A.get(idx) + B.get(idx) + idx[0]
}
const output = math.map(math.matrix([1, 2, 3]), math.matrix([4, 5, 6]), callback)
const expected = math.matrix([10, 15, 20])
assert.deepStrictEqual(output, expected)
})

it('should invoke a typed function with correct number of arguments (2)', function () {
Expand Down Expand Up @@ -209,6 +229,14 @@ describe('map', function () {
/TypeError: Function map cannot apply callback arguments sqrt\(value: function, index: Array, array: Array\) at index \[0]/)
})

it('should throw an error if the last argument of a mullti callback function is not a function', function () {
assert.throws(() => math.map([1], [2], 'not a function'),
/TypeError: Unexpected type of argument in function map/)

assert.throws(() => math.map([1], [2], ['not a function']),
/Last argument must be a callback function/)
})

it('should operate from the parser', function () {
assert.deepStrictEqual(
math.evaluate('map([1,2,3], square)'),
Expand Down

0 comments on commit 82d447d

Please sign in to comment.