-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.js
50 lines (44 loc) · 1.23 KB
/
test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
const randomRange = require("./");
const { promisify } = require("util");
const crypto = require("crypto");
const randomBytes = promisify(crypto.randomBytes);
const assert = require("assert");
const test = async () => {
for (let i = 0; i < 100; i++) {
const num = await randomRange(0, 10);
assert.ok(num >= 0);
assert.ok(num < 10);
}
for (let i = 0; i < 100; i++) {
const num = await randomRange(0, 1000000);
assert.ok(num >= 0);
assert.ok(num < 1000000);
}
for (let i = 0; i < 100; i++) {
const num = await randomRange(-10, 5);
assert.ok(num >= -10);
assert.ok(num < 5);
}
{
const maxVal = Math.pow(2, 6 * 8) - 1;
const bufs = [maxVal - 2, maxVal - 1, maxVal - 0, maxVal - 3].map(n => {
const b = Buffer.allocUnsafe(6);
b.writeUIntBE(n, 0, 6);
return b;
});
let i = 0;
const getBytes = async _ => {
buf = bufs[i];
i = i + 1;
return buf;
};
// randomRange should request just 1 byte and reject all numbers in [253, 255] interval
const num = await randomRange(0, 5, getBytes);
// stops requesting bytes when 252 is picked
assert.equal(i, 4);
// console.log(num);
assert.ok(num >= 0);
assert.ok(num < 5);
}
};
test();