/** nodejs doesnt support window.crypt */ import {expect} from "chai" describe('Math', function () { let random = () => {} before(function (done) { let promises = [] if (!globalThis['crypto']) { promises.push(import("@peculiar/webcrypto").then(m => { globalThis['crypto'] = new m.Crypto(); return true; })) } promises.push(import("../../../source/math/random.mjs").then(m => { random = m.random; return true; })) Promise.all(promises).then(() => { done() }); }); describe('.random()', function () { it('a greater b should throw error ', function () { expect(() => random(10, 2)).to.throw(Error); }); it(' should return a number ', function () { let r = random(); expect(r).that.is.a('number'); }); it(' should return a number ', function () { expect(() => random(5, 6)).to.throw(Error); }); it(' should throw Error we cannot generate numbers larger than 53 bits.', function () { expect(() => random(-999999999, 99999999999999999999)).to.throw(Error); }); it(' should throw Error we cannot generate numbers larger than 53 bits.', function () { [ [1, 100], [500, 600], [100, 1000] ].forEach(function (data) { let a = data.shift() let b = data.shift() let r = random(a, b); it(r + ' should return value between ' + a + ' ' + b, function () { expect(r >= a).to.be.true; expect(r <= b).to.be.true; }); }); [ [1, 100], [500, 600], [100, 1000] ].forEach(function (data) { let a = data.shift() let b = data.shift() let sum = 0; const rounds = 500 for (let i = 0, max = rounds; i < max; i++) { sum += random(a, b); } let avg = sum / rounds; it(avg + ' should between ' + a + ' ' + b, function () { expect(avg > a).to.be.true expect(avg < b).to.be.true }) }) }) }) })