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

162 lines
7.0 KiB
JavaScript

// test bitXor
import assert from 'assert'
import math from '../../../../src/defaultInstance.js'
const matrix = math.matrix
const sparse = math.sparse
const bignumber = math.bignumber
const bitXor = math.bitXor
describe('bitXor', function () {
it('should xor two numbers', function () {
assert.strictEqual(bitXor(53, 131), 182)
assert.strictEqual(bitXor(2, 3), 1)
assert.strictEqual(bitXor(-2, 3), -3)
assert.strictEqual(bitXor(2, -3), -1)
assert.strictEqual(bitXor(-5, -3), 6)
})
it('should xor booleans', function () {
assert.strictEqual(bitXor(true, true), 0)
assert.strictEqual(bitXor(true, false), 1)
assert.strictEqual(bitXor(false, true), 1)
assert.strictEqual(bitXor(false, false), 0)
})
it('should xor mixed numbers and booleans', function () {
assert.strictEqual(bitXor(0, true), 1)
assert.strictEqual(bitXor(0, false), 0)
assert.strictEqual(bitXor(true, 0), 1)
assert.strictEqual(bitXor(false, 0), 0)
assert.strictEqual(bitXor(true, 1), 0)
assert.strictEqual(bitXor(false, 1), 1)
})
it('should bitwise xor bignumbers', function () {
assert.deepStrictEqual(bitXor(bignumber(1), bignumber(2)), bignumber(3))
assert.deepStrictEqual(bitXor(bignumber('-1.0e+31'), bignumber('-1.0e+32')), bignumber('92546795970570634164073698164736'))
assert.deepStrictEqual(bitXor(bignumber('1.0e+31'), bignumber('1.0e+32')), bignumber('92546795970570634164077993132032'))
assert.deepStrictEqual(bitXor(bignumber('-1.0e+31'), bignumber('1.0e+32')), bignumber('-92546795970570634164077993132032'))
assert.deepStrictEqual(bitXor(bignumber('1.0e+31'), bignumber('-1.0e+32')), bignumber('-92546795970570634164073698164736'))
})
it('should bitwise xor mixed numbers and bignumbers', function () {
assert.deepStrictEqual(bitXor(bignumber(1), 2), bignumber(3))
assert.deepStrictEqual(bitXor(1, bignumber(2)), bignumber(3))
assert.deepStrictEqual(bitXor(bignumber(7), 9), bignumber(14))
assert.deepStrictEqual(bitXor(7, bignumber(9)), bignumber(14))
})
it('should bitwise xor mixed booleans and bignumbers', function () {
assert.deepStrictEqual(bitXor(bignumber(1), true), bignumber(0))
assert.deepStrictEqual(bitXor(bignumber(1), false), bignumber(1))
assert.deepStrictEqual(bitXor(true, bignumber(3)), bignumber(2))
assert.deepStrictEqual(bitXor(false, bignumber(3)), bignumber(3))
})
it('should throw an error if used with a unit', function () {
assert.throws(function () { bitXor(math.unit('5cm'), 2) }, /TypeError: Unexpected type of argument/)
assert.throws(function () { bitXor(2, math.unit('5cm')) }, /TypeError: Unexpected type of argument/)
assert.throws(function () { bitXor(math.unit('2cm'), math.unit('5cm')) }, /TypeError: Unexpected type of argument/)
})
it('should throw an error if the parameters are not integers', function () {
assert.throws(function () {
bitXor(1.1, 1)
}, /Integers expected in function bitXor/)
assert.throws(function () {
bitXor(1, 1.1)
}, /Integers expected in function bitXor/)
assert.throws(function () {
bitXor(1.1, 1.1)
}, /Integers expected in function bitXor/)
assert.throws(function () {
bitXor(bignumber(1.1), 1)
}, /Integers expected in function bitXor/)
assert.throws(function () {
bitXor(1, bignumber(1.1))
}, /Integers expected in function bitXor/)
assert.throws(function () {
bitXor(bignumber(1.1), bignumber(1))
}, /Integers expected in function bitXor/)
assert.throws(function () {
bitXor(bignumber(1), bignumber(1.1))
}, /Integers expected in function bitXor/)
})
describe('Array', function () {
it('should bitwise xor array - scalar', function () {
assert.deepStrictEqual(bitXor(12, [3, 9]), [15, 5])
assert.deepStrictEqual(bitXor([3, 9], 12), [15, 5])
})
it('should bitwise xor array - array', function () {
assert.deepStrictEqual(bitXor([[1, 2], [3, 4]], [[5, 6], [7, 8]]), [[4, 4], [4, 12]])
})
it('should bitwise xor array - dense matrix', function () {
assert.deepStrictEqual(bitXor([[1, 2], [3, 4]], matrix([[5, 6], [7, 8]])), matrix([[4, 4], [4, 12]]))
})
it('should bitwise xor array - sparse matrix', function () {
assert.deepStrictEqual(bitXor([[1, 2], [3, 4]], sparse([[5, 6], [7, 8]])), matrix([[4, 4], [4, 12]]))
})
})
describe('DenseMatrix', function () {
it('should bitwise xor dense matrix - scalar', function () {
assert.deepStrictEqual(bitXor(12, matrix([3, 9])), matrix([15, 5]))
assert.deepStrictEqual(bitXor(matrix([3, 9]), 12), matrix([15, 5]))
})
it('should bitwise xor dense matrix - array', function () {
assert.deepStrictEqual(bitXor(matrix([[1, 2], [3, 4]]), [[5, 6], [7, 8]]), matrix([[4, 4], [4, 12]]))
})
it('should bitwise xor dense matrix - dense matrix', function () {
assert.deepStrictEqual(bitXor(matrix([[1, 2], [3, 4]]), matrix([[5, 6], [7, 8]])), matrix([[4, 4], [4, 12]]))
})
it('should bitwise xor dense matrix - sparse matrix', function () {
assert.deepStrictEqual(bitXor(matrix([[1, 2], [3, 4]]), sparse([[5, 6], [7, 8]])), matrix([[4, 4], [4, 12]]))
})
})
describe('SparseMatrix', function () {
it('should bitwise xor sparse matrix - scalar', function () {
assert.deepStrictEqual(bitXor(12, sparse([[3, 9], [9, 3]])), matrix([[15, 5], [5, 15]]))
assert.deepStrictEqual(bitXor(sparse([[3, 9], [9, 3]]), 12), matrix([[15, 5], [5, 15]]))
})
it('should bitwise xor sparse matrix - array', function () {
assert.deepStrictEqual(bitXor(sparse([[1, 2], [3, 4]]), [[5, 6], [7, 8]]), matrix([[4, 4], [4, 12]]))
})
it('should bitwise xor sparse matrix - dense matrix', function () {
assert.deepStrictEqual(bitXor(sparse([[1, 2], [3, 4]]), matrix([[5, 6], [7, 8]])), matrix([[4, 4], [4, 12]]))
})
it('should bitwise xor sparse matrix - sparse matrix', function () {
assert.deepStrictEqual(bitXor(sparse([[1, 2], [3, 4]]), sparse([[5, 6], [7, 8]])), matrix([[4, 4], [4, 12]]))
})
})
it('should throw an error in case of invalid number of arguments', function () {
assert.throws(function () { bitXor(1) }, /TypeError: Too few arguments/)
assert.throws(function () { bitXor(1, 2, 3) }, /TypeError: Too many arguments/)
})
it('should throw an error in case of invalid type of arguments', function () {
assert.throws(function () { bitXor(2, null) }, /TypeError: Unexpected type of argument/)
assert.throws(function () { bitXor(new Date(), true) }, /TypeError: Unexpected type of argument/)
assert.throws(function () { bitXor(true, new Date()) }, /TypeError: Unexpected type of argument/)
assert.throws(function () { bitXor(true, undefined) }, /TypeError: Unexpected type of argument/)
assert.throws(function () { bitXor(undefined, true) }, /TypeError: Unexpected type of argument/)
})
it('should LaTeX bitXor', function () {
const expression = math.parse('bitXor(2,3)')
assert.strictEqual(expression.toTex(), '\\left(2\\underline{|}3\\right)')
})
})