Skip to content
Snippets Groups Projects
Select Git revision
  • a48311555fd82fb1ea0a322f36c8a404802987a4
  • main default protected
  • drip-server-timing
  • compress-middleware
  • v2.11.0
  • v2.10.0
  • v2.9.2
  • v2.9.1
  • v2.9.0
  • v2.8.0
  • v2.7.0
  • v2.6.0
  • v2.5.6
  • v2.5.5
  • v2.5.4
  • v2.5.3
  • v2.5.2
  • v2.5.1
  • v2.5.0
  • v2.4.2
  • v2.4.1
  • v2.4.0
  • v2.3.0
  • v2.2.2
24 results

handlers_test.go

Blame
  • random.mjs 2.43 KiB
    /** 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
                    })
    
    
                })
    
            })
    
    
        })
    
    
    })