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

97 lines
2.5 KiB
JavaScript

import assert from 'assert'
import math from '../../../../src/defaultInstance.js'
const math2 = math.create({ randomSeed: 'test' })
const randomInt = math2.randomInt
describe('randomInt', function () {
it('should have a function randomInt', function () {
assert.strictEqual(typeof math.randomInt, 'function')
})
it('should pick uniformly distributed integers in [min, max)', function () {
const picked = []
times(10000, function () {
picked.push(randomInt(-15, -5))
})
assertUniformDistributionInt(picked, -15, -5)
})
it('should pick uniformly distributed random array, with elements in [min, max)', function () {
const picked = []
const matrices = []
const size = [2, 3, 4]
times(1000, function () {
matrices.push(randomInt(size, -14.9, -2))
})
// Collect all values in one array
matrices.forEach(function (matrix) {
assert.deepStrictEqual(math.size(matrix), size)
math.forEach(matrix, function (val) {
picked.push(val)
})
})
assert.strictEqual(picked.length, 2 * 3 * 4 * 1000)
assertUniformDistributionInt(picked, -14.9, -2)
})
it('should throw an error if called with invalid arguments', function () {
assert.throws(function () {
randomInt(1, 2, [4, 8])
})
assert.throws(function () {
randomInt(1, 2, 3, 6)
})
})
it('should throw an error in case of wrong number of arguments', function () {
assert.throws(function () { randomInt([2, 3], 10, 100, 12) }, / Too many arguments/)
})
it('should LaTeX randomInt', function () {
const expression = math.parse('randomInt(0,100)')
assert.strictEqual(expression.toTex(), '\\mathrm{randomInt}\\left(0,100\\right)')
})
})
const assertUniformDistributionInt = function (values, min, max) {
const valuesRange = range(Math.floor(min), Math.floor(max))
let count
values.forEach(function (val) {
assert.ok(valuesRange.includes(val))
})
valuesRange.forEach(function (val) {
count = values.filter(function (testVal) { return testVal === val }).length
assertApproxEqual(count / values.length, 1 / valuesRange.length, 0.03)
})
}
const assertApproxEqual = function (testVal, val, tolerance) {
const diff = Math.abs(val - testVal)
if (diff > tolerance) assert.strictEqual(testVal, val)
else assert.ok(diff <= tolerance)
}
function times (n, callback) {
for (let i = 0; i < n; i++) {
callback()
}
}
function range (start, end) {
const array = []
for (let i = start; i < end; i++) {
array.push(i)
}
return array
}