mathjs/test/utils/function.test.js
greenkeeper[bot] c5971b371a Update standard to the latest version 🚀 (#1226)
* chore(package): update standard to version 12.0.0

* update to new lint version with --fix

I believe this mainly adds whitespace to `{}`'s.

* Replace assert.equal with assert.strictEqual

This breaks a lot of tests which I will endevour to fix in the next
commits.

* Fix most errors due to assert.strictEquals

Some instances of `strictEquals` are replaced by `deepEquals`.
`toString` has been used to make some string comparisions explicit.
Tests will still fail untill #1236 and #1237 are fixed.

* Fix assertion erros due to -0

With node 10, assert.strictEqual no longer considers `0 === -0`.
I missed these first time round as I was using node 8.

* Put toString correct side of bracket

I was converting the constructor to a string rather
than the result of the computation. Oops.

* Fixed #1236: quantileSeq has inconsistant return

* Update package-lock

* Fixed #1237: norm sometimes returning a complex number instead of number

* Fix cli tests

* More changes for standardjs, and fixes in unit tests
2018-09-08 16:33:58 +02:00

99 lines
2.8 KiB
JavaScript

const assert = require('assert')
const functionUtils = require('../../src/utils/function')
describe('util.function', function () {
describe('memoize', function () {
it('should memoize a function with one argument', function () {
const f = function (x) { return x * x }
const m = functionUtils.memoize(f)
assert.strictEqual(m(2), 4)
assert.strictEqual(m(3), 9)
})
it('should memoize a function with two arguments', function () {
const f = function (x, y) { return x * y }
const m = functionUtils.memoize(f)
assert.strictEqual(m(2, 3), 6)
// hash should differ
assert.strictEqual(m(1, 23), 23)
assert.strictEqual(m(12, 3), 36)
})
it('should memoize a function with objects as arguments', function () {
const f = function (obj) { return obj.x * obj.y }
const m = functionUtils.memoize(f)
assert.strictEqual(m({ x: 2, y: 3 }), 6)
assert.deepStrictEqual(Object.keys(m.cache), ['[{"x":2,"y":3}]'])
assert.strictEqual(m.cache['[{"x":2,"y":3}]'], 6)
})
it('should memoize a function with a custom hashIt function', function () {
const f = function (obj) { return obj.id }
const hashIt = function (args) {
return 'id:' + args[0].id
}
const m = functionUtils.memoize(f, hashIt)
assert.strictEqual(m({ id: 2 }), 2)
assert.deepStrictEqual(Object.keys(m.cache), ['id:2'])
assert.strictEqual(m.cache['id:2'], 2)
})
it('should really return the cached result', function () {
let a = 2
const f = function (x) { return a } // trick: no pure function
const m = functionUtils.memoize(f)
assert.strictEqual(m(4), 2)
a = 3
assert.strictEqual(m(4), 2)
})
})
describe('maxArgumentCount', function () {
it('should calculate the max argument count of a typed function', function () {
const a = function () {}
a.signatures = {
'number, number': function () {},
'number': function () {}
}
assert.strictEqual(functionUtils.maxArgumentCount(a), 2)
const b = function () {}
b.signatures = {
'number': function () {},
'number, number': function () {}
}
assert.strictEqual(functionUtils.maxArgumentCount(b), 2)
const c = function () {}
c.signatures = {
'number': function () {},
'BigNumber': function () {}
}
assert.strictEqual(functionUtils.maxArgumentCount(c), 1)
const d = function () {}
d.signatures = {
'number,number': function () {},
'number': function () {},
'number,any,number': function () {}
}
assert.strictEqual(functionUtils.maxArgumentCount(d), 3)
})
it('should return -1 for regular functions', function () {
assert.strictEqual(functionUtils.maxArgumentCount(function () {}), -1)
})
})
})