2022-10-03 17:57:08 +00:00
|
|
|
import * as bcu from '#pkg'
|
|
|
|
|
2020-04-20 22:50:53 +00:00
|
|
|
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
|
|
|
|
},
|
|
|
|
{
|
2022-01-17 11:30:59 +00:00
|
|
|
min: BigInt(-41536),
|
|
|
|
max: BigInt(213),
|
|
|
|
iterations: 100,
|
|
|
|
error: false,
|
2021-03-25 12:40:04 +00:00
|
|
|
errorMax: false
|
|
|
|
},
|
|
|
|
{
|
2022-01-17 11:30:59 +00:00
|
|
|
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
|
|
|
|
},
|
|
|
|
{
|
2022-01-17 11:30:59 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
]
|
2020-04-20 22:50:53 +00:00
|
|
|
this.timeout(90000)
|
|
|
|
for (const num of numbers) {
|
|
|
|
describe(`randBetween(${num.max}, ${num.min})`, function () {
|
|
|
|
if (!num.error) {
|
2022-01-17 11:30:59 +00:00
|
|
|
it(`[${num.iterations} iterations] should return x such that min <= x <= max`, function () {
|
2020-04-20 22:50:53 +00:00
|
|
|
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)
|
2022-01-17 11:30:59 +00:00
|
|
|
ret = ret && x >= num.min && x <= num.max
|
2020-04-20 22:50:53 +00:00
|
|
|
}
|
|
|
|
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)
|
2020-04-20 22:50:53 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
})
|
|
|
|
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)
|
2020-04-20 22:50:53 +00:00
|
|
|
ret = ret && x >= BigInt(1) && x <= num.max
|
|
|
|
}
|
|
|
|
chai.expect(ret).to.equal(true)
|
|
|
|
})
|
|
|
|
} else {
|
2022-01-17 11:30:59 +00:00
|
|
|
it('should throw RangeError (max <= min)', function () {
|
2022-10-03 17:57:08 +00:00
|
|
|
chai.expect(() => bcu.randBetween(num.max)).to.throw(RangeError)
|
2020-04-20 22:50:53 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
})
|