-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathselect_from_weighted_test.js
41 lines (31 loc) · 1.38 KB
/
select_from_weighted_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
// Build lib: wasm-pack build --target nodejs -- --features js
// Run tests: node pick_test.js
const { select_from_weighted } = require('./pkg/nois');
let count = 0;
function test(f) {
count += 1;
try {
const res = f();
console.log(`Test ${count}:`, res);
} catch (e) {
console.error(`Test ${count}:`, e);
}
}
const randomness = "2267ba7356c01a58e405d4194a31bddc3fd3eb1f0a86758f7a609ba8a47420ba"
test(() => select_from_weighted(randomness, [["red", 15]]));
test(() => select_from_weighted(randomness, [["red", 15], ["blue", 15]]));
test(() => select_from_weighted(randomness, [["left", 99], ["right", 1]]));
test(() => select_from_weighted(randomness, [["left", 1], ["right", 99]]));
// Cannot pick from empty
test(() => select_from_weighted(randomness, []));
// Cannot weight is 0
test(() => select_from_weighted(randomness, [["red", 0]]));
// Wrong weight type
test(() => select_from_weighted(randomness, [["red", null]]));
test(() => select_from_weighted(randomness, [["red", "42"]]));
test(() => select_from_weighted(randomness, [["red", Number.NaN]]));
test(() => select_from_weighted(randomness, [["red", Number.MAX_SAFE_INTEGER]])); // Exceeds u32
// Sum of weights exceeds uint32
test(() => select_from_weighted(randomness, [["red", 4294967295], ["blue", 1]]));
// Element with 3 components
test(() => select_from_weighted(randomness, [["left", 1], ["right", 99, "troll"]]));