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

163 lines
4.6 KiB
JavaScript

import assert from 'assert'
import math from '../../../../src/defaultInstance.js'
const math2 = math.create({ randomSeed: 'test' })
const random = math2.random
const Matrix = math2.Matrix
describe('random', function () {
it('should have a function random', function () {
assert.strictEqual(typeof math.random, 'function')
})
it('should pick uniformly distributed numbers in [0, 1]', function () {
const picked = []
times(1000, function () {
picked.push(random())
})
assertUniformDistribution(picked, 0, 1)
})
it('should pick uniformly distributed numbers in [min, max]', function () {
const picked = []
times(1000, function () {
picked.push(random(-10, 10))
})
assertUniformDistribution(picked, -10, 10)
})
it('should pick uniformly distributed random array, with elements in [0, 1]', function () {
const picked = []
const matrices = []
const size = [2, 3, 4]
times(100, function () {
matrices.push(random(size))
})
// Collect all values in one array
matrices.forEach(function (matrix) {
assert(Array.isArray(matrix))
assert.deepStrictEqual(math.size(matrix), size)
math.forEach(matrix, function (val) {
picked.push(val)
})
})
assert.strictEqual(picked.length, 2 * 3 * 4 * 100)
assertUniformDistribution(picked, 0, 1)
})
it('should pick uniformly distributed random array, with elements in [0, max]', function () {
const picked = []
const matrices = []
const size = [2, 3, 4]
times(100, function () {
matrices.push(random(size, 8))
})
// Collect all values in one array
matrices.forEach(function (matrix) {
assert(Array.isArray(matrix))
assert.deepStrictEqual(math.size(matrix), size)
math.forEach(matrix, function (val) {
picked.push(val)
})
})
assert.strictEqual(picked.length, 2 * 3 * 4 * 100)
assertUniformDistribution(picked, 0, 8)
})
it('should pick uniformly distributed random matrix, with elements in [0, 1]', function () {
const picked = []
const matrices = []
const size = math2.matrix([2, 3, 4])
times(100, function () {
matrices.push(random(size))
})
// Collect all values in one array
matrices.forEach(function (matrix) {
assert(matrix instanceof Matrix)
assert.deepStrictEqual(matrix.size(), size.valueOf())
matrix.forEach(function (val) {
picked.push(val)
})
})
assert.strictEqual(picked.length, 2 * 3 * 4 * 100)
assertUniformDistribution(picked, 0, 1)
})
it('should pick uniformly distributed random array, with elements in [min, max]', function () {
const picked = []
const matrices = []
const size = [2, 3, 4]
times(100, function () {
matrices.push(random(size, -103, 8))
})
// 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 * 100)
assertUniformDistribution(picked, -103, 8)
})
it('should throw an error if called with invalid arguments', function () {
assert.throws(function () { random(1, 2, [4, 8]) })
assert.throws(function () { random(1, 2, 3, 6) })
assert.throws(function () { random('str', 10) })
assert.throws(function () { random(math2.bignumber(-10), 10) })
})
it('should throw an error in case of wrong number of arguments', function () {
assert.throws(function () { random([2, 3], 10, 100, 12) }, /Too many arguments/)
})
it('should LaTeX random', function () {
const expression = math.parse('random(0,1)')
assert.strictEqual(expression.toTex(), '\\mathrm{random}\\left(0,1\\right)')
})
})
function assertUniformDistribution (values, min, max) {
const interval = (max - min) / 10
let count
let i
count = values.filter(function (val) { return val < min }).length
assert.strictEqual(count, 0)
count = values.filter(function (val) { return val > max }).length
assert.strictEqual(count, 0)
for (i = 0; i < 10; i++) {
count = values.filter(function (val) {
return val >= (min + i * interval) && val < (min + (i + 1) * interval)
}).length
assertApproxEqual(count / values.length, 0.1, 0.02)
}
}
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()
}
}