-
Notifications
You must be signed in to change notification settings - Fork 4
/
boolSpec.js
165 lines (134 loc) · 5.3 KB
/
boolSpec.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
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
(function (Random) {
describe("bool distribution", function () {
describe("when passed no arguments", function () {
it("returns true if the least bit is 1", function () {
var distribution = Random.bool();
var actual = distribution(function () {
return 1;
});
expect(actual).toBe(true);
});
it("returns false if the least bit is 0", function () {
var distribution = Random.bool();
var actual = distribution(function () {
return 2;
});
expect(actual).toBe(false);
});
});
describe("when passed one argument", function () {
[0, -1, -0.5].forEach(function (value) {
describe("when passed " + value, function () {
it("always returns false", function () {
var distribution = Random.bool(value);
for (var i = 0; i < 10; ++i) {
expect(distribution()).toBe(false);
}
});
});
});
[1, 2, 1.5].forEach(function (value) {
describe("when passed " + value, function () {
it("always returns true", function () {
var distribution = Random.bool(value);
for (var i = 0; i < 10; ++i) {
expect(distribution()).toBe(true);
}
});
});
});
describe("when passed a number that only requires 32 bits of randomness", function () {
it("returns false if the engine passes in a value >= than the percentage * " + 0x100000000, function () {
var nextValue = 0;
var percentage = 0.125;
var distribution = Random.bool(percentage);
var actual = distribution(function () {
return percentage * 0x100000000;
});
expect(actual).toBe(false);
});
it("returns true if the engine passes in a value < than the percentage * " + 0x100000000, function () {
var nextValue = 0;
var percentage = 0.125;
var distribution = Random.bool(percentage);
var actual = distribution(function () {
return (percentage * 0x100000000) - 1;
});
expect(actual).toBe(true);
});
});
describe("when passed a number that requires more than 32 bits of randomness", function () {
it("returns false if uint53 passes in a value >= than the percentage * " + 0x20000000000000, function () {
var nextValue = 0;
var percentage = 0.12345678901234567890;
spyOn(Random, "uint53").andReturn(Math.ceil(percentage * 0x20000000000000));
var distribution = Random.bool(percentage);
var actual = distribution(function () {
return 0;
});
expect(actual).toBe(false);
});
it("returns true if uint53 passes in a value < than the percentage * " + 0x20000000000000, function () {
var nextValue = 0;
var percentage = 0.12345678901234567890;
spyOn(Random, "uint53").andReturn(Math.floor(percentage * 0x20000000000000) - 1);
var distribution = Random.bool(percentage);
var actual = distribution(function () {
return 0;
});
expect(actual).toBe(true);
});
});
});
describe("when passed two arguments", function () {
[0, -1].forEach(function (numerator) {
describe("when passed " + numerator + " for the numerator", function () {
it("always returns false", function () {
var distribution = Random.bool(numerator, 10);
for (var i = 0; i < 10; ++i) {
expect(distribution()).toBe(false);
}
});
});
});
[0, 1].forEach(function (addition) {
describe("when passed a numerator that is the denominator + " + addition, function () {
it("always returns true", function () {
var distribution = Random.bool(10 + addition, 10);
for (var i = 0; i < 10; ++i) {
expect(distribution()).toBe(true);
}
});
});
});
it("uses the integer distribution and returns true if the numerator is < than the result", function () {
var numerator = 3;
var denominator = 10;
spyOn(Random, "integer").andCallFake(function (min, max) {
expect(min).toBe(0);
expect(max).toBe(denominator - 1);
return function () {
return 2;
};
});
var distribution = Random.bool(3, 10);
var actual = distribution();
expect(actual).toBe(true);
});
it("uses the integer distribution and returns false if the numerator is >= than the result", function () {
var numerator = 3;
var denominator = 10;
spyOn(Random, "integer").andCallFake(function (min, max) {
expect(min).toBe(0);
expect(max).toBe(denominator - 1);
return function () {
return 3;
};
});
var distribution = Random.bool(3, 10);
var actual = distribution();
expect(actual).toBe(false);
});
});
});
}(typeof module !== "undefined" ? require("../lib/random") : Random));