mirror of
https://github.com/josdejong/mathjs.git
synced 2026-01-18 14:59:29 +00:00
* 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
115 lines
4.4 KiB
JavaScript
115 lines
4.4 KiB
JavaScript
const assert = require('assert')
|
|
|
|
const math = require('../../src/main')
|
|
const operators = require('../../src/expression/operators')
|
|
const OperatorNode = math.expression.node.OperatorNode
|
|
const AssignmentNode = math.expression.node.AssignmentNode
|
|
const SymbolNode = math.expression.node.SymbolNode
|
|
const ConstantNode = math.expression.node.ConstantNode
|
|
const Node = math.expression.node.Node
|
|
const ParenthesisNode = math.expression.node.ParenthesisNode
|
|
|
|
describe('operators', function () {
|
|
it('should return the precedence of a node', function () {
|
|
const a = new ConstantNode(1)
|
|
const b = new ConstantNode(2)
|
|
|
|
const n1 = new AssignmentNode(new SymbolNode('a'), a)
|
|
const n2 = new OperatorNode('or', 'or', [a, b])
|
|
|
|
assert.strictEqual(operators.getPrecedence(n1, 'keep'), 0)
|
|
assert.strictEqual(operators.getPrecedence(n2, 'keep'), 2)
|
|
})
|
|
|
|
it('should return null if precedence is not defined for a node', function () {
|
|
const n = new Node()
|
|
|
|
assert.strictEqual(operators.getPrecedence(n, 'keep'), null)
|
|
})
|
|
|
|
it('should return the precedence of a ParenthesisNode', function () {
|
|
const c = new ConstantNode(1)
|
|
|
|
const op = new OperatorNode('or', 'or', [c, c])
|
|
|
|
const p = new ParenthesisNode(op)
|
|
|
|
assert.strictEqual(operators.getPrecedence(p, 'all'), operators.getPrecedence(op, 'all'))
|
|
assert.strictEqual(operators.getPrecedence(p, 'auto'), operators.getPrecedence(op, 'all'))
|
|
assert.strictEqual(operators.getPrecedence(p, 'keep'), null)
|
|
})
|
|
|
|
it('should return the associativity of a node', function () {
|
|
const a = new ConstantNode(1)
|
|
|
|
const n1 = new OperatorNode('+', 'add', [a, a])
|
|
const n2 = new OperatorNode('^', 'pow', [a, a])
|
|
const n3 = new OperatorNode('-', 'unaryMinus', [a])
|
|
const n4 = new OperatorNode('!', 'factorial', [a])
|
|
|
|
assert.strictEqual(operators.getAssociativity(n1, 'keep'), 'left')
|
|
assert.strictEqual(operators.getAssociativity(n2, 'keep'), 'right')
|
|
assert.strictEqual(operators.getAssociativity(n3, 'keep'), 'right')
|
|
assert.strictEqual(operators.getAssociativity(n4, 'keep'), 'left')
|
|
})
|
|
|
|
it('should return the associativity of a ParenthesisNode', function () {
|
|
const c = new ConstantNode(1)
|
|
|
|
const op = new OperatorNode('or', 'or', [c, c])
|
|
|
|
const p = new ParenthesisNode(op)
|
|
|
|
assert.strictEqual(operators.getAssociativity(p, 'all'), operators.getAssociativity(op, 'keep'))
|
|
assert.strictEqual(operators.getAssociativity(p, 'auto'), operators.getAssociativity(op, 'keep'))
|
|
assert.strictEqual(operators.getAssociativity(p, 'keep'), null)
|
|
})
|
|
|
|
it('should return null if associativity is not defined for a node', function () {
|
|
const a = new ConstantNode(1)
|
|
|
|
const n1 = new Node()
|
|
const n2 = new AssignmentNode(new SymbolNode('a'), a)
|
|
|
|
assert.strictEqual(operators.getAssociativity(n1, 'keep'), null)
|
|
assert.strictEqual(operators.getAssociativity(n2, 'keep'), null)
|
|
})
|
|
|
|
it('should return if a Node is associative with another Node', function () {
|
|
const a = new ConstantNode(1)
|
|
|
|
const n1 = new OperatorNode('+', 'add', [a, a])
|
|
const n2 = new OperatorNode('-', 'subtract', [a, a])
|
|
|
|
assert.strictEqual(operators.isAssociativeWith(n1, n1, 'keep'), true)
|
|
assert.strictEqual(operators.isAssociativeWith(n1, n2, 'keep'), true)
|
|
assert.strictEqual(operators.isAssociativeWith(n2, n2, 'keep'), false)
|
|
assert.strictEqual(operators.isAssociativeWith(n2, n1, 'keep'), false)
|
|
})
|
|
|
|
it('should return null if the associativity between two Nodes is not defined', function () {
|
|
const a = new ConstantNode(1)
|
|
|
|
const n1 = new Node()
|
|
const n2 = new AssignmentNode(new SymbolNode('a'), a)
|
|
|
|
assert.strictEqual(operators.isAssociativeWith(n1, n1, 'keep'), null)
|
|
assert.strictEqual(operators.isAssociativeWith(n1, n2, 'keep'), null)
|
|
assert.strictEqual(operators.isAssociativeWith(n2, n2, 'keep'), null)
|
|
assert.strictEqual(operators.isAssociativeWith(n2, n1, 'keep'), null)
|
|
})
|
|
|
|
it('should return if a ParenthesisNode is associative with another Node', function () {
|
|
const a = new ConstantNode(1)
|
|
|
|
const add = new OperatorNode('+', 'add', [a, a])
|
|
const sub = new OperatorNode('-', 'subtract', [a, a])
|
|
|
|
const p = new ParenthesisNode(add)
|
|
|
|
assert.strictEqual(operators.isAssociativeWith(p, sub, 'all'), true)
|
|
assert.strictEqual(operators.isAssociativeWith(p, sub, 'auto'), true)
|
|
assert.strictEqual(operators.isAssociativeWith(p, sub, 'keep'), null)
|
|
})
|
|
})
|