Jos de Jong 6f00715754
Specify import require paths (continuation of #1941) (#1962)
* Add `.js` extension to source file imports

* Specify package `exports` in `package.json`

Specify package type as `commonjs` (It's good to be specific)

* Move all compiled scripts into `lib` directory

Remove ./number.js (You can use the compiled ones in `./lib/*`)

Tell node that the `esm` directory is type `module` and enable tree shaking.

Remove unused files from packages `files` property

* Allow importing of package.json

* Make library ESM first

* - Fix merge conflicts
- Refactor `bundleAny` into `defaultInstance.js` and `browserBundle.cjs`
- Refactor unit tests to be able to run with plain nodejs (no transpiling)
- Fix browser examples

* Fix browser and browserstack tests

* Fix running unit tests on Node 10 (which has no support for modules)

* Fix node.js examples (those are still commonjs)

* Remove the need for `browserBundle.cjs`

* Generate minified bundle only

* [Security] Bump node-fetch from 2.6.0 to 2.6.1 (#1963)

Bumps [node-fetch](https://github.com/bitinn/node-fetch) from 2.6.0 to 2.6.1. **This update includes a security fix.**
- [Release notes](https://github.com/bitinn/node-fetch/releases)
- [Changelog](https://github.com/node-fetch/node-fetch/blob/master/docs/CHANGELOG.md)
- [Commits](https://github.com/bitinn/node-fetch/compare/v2.6.0...v2.6.1)

Signed-off-by: dependabot-preview[bot] <support@dependabot.com>

Co-authored-by: dependabot-preview[bot] <27856297+dependabot-preview[bot]@users.noreply.github.com>

* Cleanup console.log

* Add integration tests to test the entry points (commonjs/esm, full/number only)

* Create backward compatibility error messages in the files moved/removed since v8

* Describe breaking changes in HISTORY.md

* Bump karma from 5.2.1 to 5.2.2 (#1965)

Bumps [karma](https://github.com/karma-runner/karma) from 5.2.1 to 5.2.2.
- [Release notes](https://github.com/karma-runner/karma/releases)
- [Changelog](https://github.com/karma-runner/karma/blob/master/CHANGELOG.md)
- [Commits](https://github.com/karma-runner/karma/compare/v5.2.1...v5.2.2)

Signed-off-by: dependabot-preview[bot] <support@dependabot.com>

Co-authored-by: dependabot-preview[bot] <27856297+dependabot-preview[bot]@users.noreply.github.com>

Co-authored-by: Lee Langley-Rees <lee@greenimp.co.uk>
Co-authored-by: dependabot-preview[bot] <27856297+dependabot-preview[bot]@users.noreply.github.com>
2020-09-20 18:01:29 +02:00

175 lines
7.6 KiB
JavaScript

// test mod
import assert from 'assert'
import approx from '../../../../tools/approx.js'
import math from '../../../../src/defaultInstance.js'
const bignumber = math.bignumber
const matrix = math.matrix
const sparse = math.sparse
const mod = math.mod
describe('mod', function () {
it('should calculate the modulus of booleans correctly', function () {
assert.strictEqual(mod(true, true), 0)
assert.strictEqual(mod(false, true), 0)
assert.strictEqual(mod(true, false), 1)
assert.strictEqual(mod(false, false), 0)
})
it('should calculate the modulus of two numbers', function () {
assert.strictEqual(mod(1, 1), 0)
assert.strictEqual(mod(0, 1), 0)
assert.strictEqual(mod(1, 0), 1)
assert.strictEqual(mod(0, 0), 0)
assert.strictEqual(mod(7, 0), 7)
approx.equal(mod(7, 2), 1)
approx.equal(mod(9, 3), 0)
approx.equal(mod(10, 4), 2)
approx.equal(mod(-10, 4), 2)
approx.equal(mod(8.2, 3), 2.2)
approx.equal(mod(4, 1.5), 1)
approx.equal(mod(0, 3), 0)
})
it('should throw an error if the modulus is negative', function () {
assert.throws(function () { mod(10, -4) })
})
it('should throw an error if used with wrong number of arguments', function () {
assert.throws(function () { mod(1) }, /TypeError: Too few arguments/)
assert.throws(function () { mod(1, 2, 3) }, /TypeError: Too many arguments/)
})
it('should throw an error if used with wrong type of arguments', function () {
assert.throws(function () { mod(1, new Date()) }, /TypeError: Unexpected type of argument/)
assert.throws(function () { mod(1, null) }, /TypeError: Unexpected type of argument/)
assert.throws(function () { mod(new Date(), bignumber(2)) }, /TypeError: Unexpected type of argument/)
})
it('should calculate the modulus of bignumbers', function () {
assert.deepStrictEqual(mod(bignumber(7), bignumber(2)), bignumber(1))
assert.deepStrictEqual(mod(bignumber(7), bignumber(0)), bignumber(7))
assert.deepStrictEqual(mod(bignumber(0), bignumber(3)), bignumber(0))
assert.deepStrictEqual(mod(bignumber(7), bignumber(2)), bignumber(1))
assert.deepStrictEqual(mod(bignumber(8), bignumber(3)).valueOf(), bignumber(2).valueOf())
})
it.skip('should calculate the modulus of bignumbers for fractions', function () {
assert.deepStrictEqual(mod(bignumber(7).div(3), bignumber(1).div(3)), bignumber(0))
})
it.skip('should calculate the modulus of bignumbers for negative values', function () {
assert.deepStrictEqual(mod(bignumber(-10), bignumber(4)), bignumber(2))
})
it('should calculate the modulus of mixed numbers and bignumbers', function () {
assert.deepStrictEqual(mod(bignumber(7), 2), bignumber(1))
assert.deepStrictEqual(mod(bignumber(7), 0), bignumber(7))
assert.deepStrictEqual(mod(8, bignumber(3)), bignumber(2))
assert.deepStrictEqual(mod(7, bignumber(0)), bignumber(7))
assert.deepStrictEqual(mod(bignumber(0), 3), bignumber(0))
assert.deepStrictEqual(mod(bignumber(7), 0), bignumber(7))
assert.throws(function () { mod(7 / 3, bignumber(2)) }, /TypeError: Cannot implicitly convert a number with >15 significant digits to BigNumber/)
assert.throws(function () { mod(bignumber(7).div(3), 1 / 3) }, /TypeError: Cannot implicitly convert a number with >15 significant digits to BigNumber/)
})
it('should calculate the modulus of mixed booleans and bignumbers', function () {
assert.deepStrictEqual(mod(bignumber(7), true), bignumber(0))
assert.deepStrictEqual(mod(bignumber(7), false), bignumber(7))
assert.deepStrictEqual(mod(true, bignumber(3)), bignumber(1))
assert.deepStrictEqual(mod(false, bignumber(3)), bignumber(0))
})
it('should throw an error if used on complex numbers', function () {
assert.throws(function () { mod(math.complex(1, 2), 3) }, TypeError)
assert.throws(function () { mod(3, math.complex(1, 2)) }, TypeError)
assert.throws(function () { mod(bignumber(3), math.complex(1, 2)) }, TypeError)
})
it('should convert string to number', function () {
assert.strictEqual(mod('8', '3'), 2)
assert.strictEqual(mod('8', 3), 2)
assert.strictEqual(mod(8, '3'), 2)
assert.throws(function () { mod(5, 'a') }, /Cannot convert "a" to a number/)
})
it('should calculate modulus of two fractions', function () {
const b = math.fraction(8)
const a = mod(b, math.fraction(3))
assert.strictEqual(a.toString(), '2')
assert.strictEqual(b.toString(), '8')
assert(a instanceof math.Fraction)
assert.strictEqual(mod(math.fraction(4.55), math.fraction(0.05)).toString(), '0')
})
it('should calculate modulus of mixed fractions and numbers', function () {
assert.deepStrictEqual(mod(8, math.fraction(3)), math.fraction(2))
assert.deepStrictEqual(mod(math.fraction(8), 3), math.fraction(2))
})
describe('Array', function () {
it('should perform element-wise modulus on array and scalar', function () {
approx.deepEqual(mod([[-4, -3, 0, -1], [0, 1, 2, 3]], 3), [[2, 0, 0, 2], [0, 1, 2, 0]])
approx.deepEqual(mod(3, [[4, 3], [2, 1]]), [[3, 0], [1, 0]])
})
it('should perform element-wise modulus on array and array', function () {
approx.deepEqual(mod([[-40, -31], [11, -23]], [[3, 7], [1, 3]]), [[2, 4], [0, 1]])
})
it('should perform element-wise modulus on array and dense matrix', function () {
approx.deepEqual(mod([[-40, -31], [11, -23]], matrix([[3, 7], [1, 3]])), matrix([[2, 4], [0, 1]]))
})
it('should perform element-wise modulus on array and sparse matrix', function () {
approx.deepEqual(mod([[-40, -31], [11, -23]], sparse([[3, 7], [1, 3]])), matrix([[2, 4], [0, 1]]))
})
})
describe('DenseMatrix', function () {
it('should perform element-wise modulus on dense matrix and scalar', function () {
approx.deepEqual(mod(matrix([[-4, -3, 0, -1], [0, 1, 2, 3]]), 3), matrix([[2, 0, 0, 2], [0, 1, 2, 0]]))
approx.deepEqual(mod(3, matrix([[4, 3], [2, 1]])), matrix([[3, 0], [1, 0]]))
})
it('should perform element-wise modulus on dense matrix and array', function () {
approx.deepEqual(mod(matrix([[-40, -31], [11, -23]]), [[3, 7], [1, 3]]), matrix([[2, 4], [0, 1]]))
})
it('should perform element-wise modulus on dense matrix and dense matrix', function () {
approx.deepEqual(mod(matrix([[-40, -31], [11, -23]]), matrix([[3, 7], [1, 3]])), matrix([[2, 4], [0, 1]]))
})
it('should perform element-wise modulus on dense matrix and sparse matrix', function () {
approx.deepEqual(mod(matrix([[-40, -31], [11, -23]]), sparse([[3, 7], [1, 3]])), matrix([[2, 4], [0, 1]]))
})
})
describe('SparseMatrix', function () {
it('should perform element-wise modulus on sparse matrix and scalar', function () {
approx.deepEqual(mod(sparse([[-4, -3, 0, -1], [0, 1, 2, 3]]), 3), sparse([[2, 0, 0, 2], [0, 1, 2, 0]]))
approx.deepEqual(mod(3, sparse([[4, 3], [2, 1]])), matrix([[3, 0], [1, 0]]))
})
it('should perform element-wise modulus on sparse matrix and array', function () {
approx.deepEqual(mod(sparse([[-40, -31], [11, -23]]), [[3, 7], [1, 3]]), sparse([[2, 4], [0, 1]]))
})
it('should perform element-wise modulus on sparse matrix and dense matrix', function () {
approx.deepEqual(mod(sparse([[-40, -31], [11, -23]]), matrix([[3, 7], [1, 3]])), sparse([[2, 4], [0, 1]]))
})
it('should perform element-wise modulus on sparse matrix and sparse matrix', function () {
approx.deepEqual(mod(sparse([[-40, -31], [11, -23]]), sparse([[3, 7], [1, 3]])), sparse([[2, 4], [0, 1]]))
})
})
it('should LaTeX mod', function () {
const expression = math.parse('mod(11,2)')
assert.strictEqual(expression.toTex(), '\\left(11\\mod2\\right)')
})
})