bigint-crypto-utils/test/randBetween.ts

91 lines
2.3 KiB
TypeScript
Raw Normal View History

2022-10-03 17:57:08 +00:00
import * as bcu from '#pkg'
describe('randBetween', function () {
2021-03-25 12:40:04 +00:00
const numbers = [
{
min: BigInt(1),
max: BigInt(2) ** BigInt(234),
iterations: 100,
error: false,
errorMax: false
},
{
min: BigInt('122461641436345153'),
max: BigInt(2) ** BigInt(234),
iterations: 100,
error: false,
errorMax: false
},
{
min: BigInt(-41536),
max: BigInt(213),
iterations: 100,
error: false,
2021-03-25 12:40:04 +00:00
errorMax: false
},
{
min: BigInt('-41446134643671357134674615124613467356734646146125'),
max: BigInt('-125246'),
iterations: 100,
error: false,
errorMax: true
},
{
min: BigInt(146347),
max: BigInt(232),
2021-03-25 12:40:04 +00:00
iterations: 1,
error: true,
errorMax: false
},
{
min: BigInt(2),
2021-03-25 12:40:04 +00:00
max: BigInt(2),
iterations: 1,
error: true,
errorMax: false
},
{
min: BigInt(1),
max: BigInt(-1),
iterations: 1,
error: true,
errorMax: true
}
]
this.timeout(90000)
for (const num of numbers) {
describe(`randBetween(${num.max}, ${num.min})`, function () {
if (!num.error) {
it(`[${num.iterations} iterations] should return x such that min <= x <= max`, function () {
let ret = true
for (let i = 0; i < num.iterations; i++) {
2022-10-03 17:57:08 +00:00
const x = bcu.randBetween(num.max, num.min)
ret = ret && x >= num.min && x <= num.max
}
chai.expect(ret).to.equal(true)
})
} else {
2020-04-21 00:41:32 +00:00
it('should throw RangeError (max <=0 || min <0 || min>=max)', function () {
2022-10-03 17:57:08 +00:00
chai.expect(() => bcu.randBetween(num.max, num.min)).to.throw(RangeError)
})
}
})
describe(`randBetween(${num.max})`, function () {
if (!num.errorMax) {
it(`[${num.iterations} iterations] should return x such that 1 <= x <= max`, function () {
let ret = true
for (let i = 0; i < num.iterations; i++) {
2022-10-03 17:57:08 +00:00
const x = bcu.randBetween(num.max)
ret = ret && x >= BigInt(1) && x <= num.max
}
chai.expect(ret).to.equal(true)
})
} else {
it('should throw RangeError (max <= min)', function () {
2022-10-03 17:57:08 +00:00
chai.expect(() => bcu.randBetween(num.max)).to.throw(RangeError)
})
}
})
}
})