Skip to content
Snippets Groups Projects
Select Git revision
  • 9eed9cea28e841db66c6bab508647832d4c57b37
  • master default protected
  • 1.31
  • 4.38.7
  • 4.38.6
  • 4.38.5
  • 4.38.4
  • 4.38.3
  • 4.38.2
  • 4.38.1
  • 4.38.0
  • 4.37.2
  • 4.37.1
  • 4.37.0
  • 4.36.0
  • 4.35.0
  • 4.34.1
  • 4.34.0
  • 4.33.1
  • 4.33.0
  • 4.32.2
  • 4.32.1
  • 4.32.0
23 results

test.html

Blame
  • random.mjs 1.98 KiB
    /** nodejs doesnt support window.crypt */
    import {expect} from "chai"
    import {getGlobal} from "../../../../application/source/types/global.mjs";
    import * as Crypto from "@peculiar/webcrypto";
    import {random} from "../../../../application/source/math/random.mjs";
    
    const global = getGlobal();
    
    if (!global['crypto']) {
        global['crypto'] = new Crypto.Crypto();
    }
    
    describe('Math', function () {
    
        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);
            });
    
            [
                [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
                })
    
    
            })
    
    
        })
    
    
    })