-
Notifications
You must be signed in to change notification settings - Fork 6
/
helpers.test.ts
83 lines (76 loc) · 2.89 KB
/
helpers.test.ts
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import "mocha";
import {expect} from "chai";
import {constraintWithUsage, rebalanceDist} from "./helpers";
export const test = () =>
describe("helpers", function () {
describe("constraintWithUsage()", function () {
it("Should throw on empty constraint", function () {
expect(() =>
constraintWithUsage(new Set(), new Map([["a", 1]])),
).to.throw(Error, "Empty constraint");
});
it("Should output zero if empty usage", function () {
expect([
...constraintWithUsage(
new Set(["a", "b", "c"]),
new Map(),
).entries(),
]).to.have.deep.members([
["a", 0],
["b", 0],
["c", 0],
]);
});
it("Should add usage probabilities", function () {
expect([
...constraintWithUsage(
new Set(["a", "b", "c"]),
new Map([
["a", 0.5],
["c", 0.5],
]),
).entries(),
]).to.have.deep.members([
["a", 0.5],
["b", 0],
["c", 0.5],
]);
});
it("Should add usage probabilities with smoothing", function () {
expect([
...constraintWithUsage(
new Set(["a", "b", "c"]),
new Map([
["a", 0.5],
["c", 0.5],
]),
0.1,
).entries(),
]).to.have.deep.members([
["a", 0.9 * 0.5 + 0.1 / 3],
["b", 0.1 / 3],
["c", 0.9 * 0.5 + 0.1 / 3],
]);
});
});
describe("rebalanceDist()", function () {
it("Should rebalance distribution", function () {
expect(rebalanceDist([0.6, 0.2, 0.2])).to.have.members([
0.3, 0.35, 0.35,
]);
});
it("Should tend toward uniform distribution", function () {
let arr = [0.6, 0.2, 0.2];
for (let i = 0; i < 10; ++i) {
arr = rebalanceDist(arr);
}
expect(arr).to.have.lengthOf(3);
expect(arr[0]).to.be.closeTo(1 / 3, 0.0001);
expect(arr[1]).to.be.closeTo(1 / 3, 0.0001);
expect(arr[2]).to.be.closeTo(1 / 3, 0.0001);
});
it("Should not change uniform distribution", function () {
expect(rebalanceDist([0.5, 0.5])).to.have.members([0.5, 0.5]);
});
});
});