forked from mfornos/sampling
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest.js
100 lines (74 loc) · 1.57 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
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
var test = require('tape'),
sample = require('./');
test('Always 3', function (t) {
var s = sample([0.0, 0.0, 0.0, 1.0]);
t.plan(10);
for (i = 0; i < 10; i++) {
t.equal(s.next(), 3);
}
});
test('Always 137', function (t) {
var s = sample([0.0, 0.0, 1.0], [10, 11, 137]);
t.plan(10);
for (i = 0; i < 10; i++) {
t.equal(s.next(), 137);
}
});
test('First outcome 0.5', function (t) {
var s = sample([0.5, 0.25, 0.25], [10, 11, 12]),
c1 = 0,
c2 = 0;
t.plan(2);
for (i = 0; i < 1000; i++) {
if(s.next() == 10)
c1++;
else
c2++;
}
t.equals(c1 + c2, 1000);
t.ok(c1 > 440 && c1 < 550, 'Half approx.');
});
test('Multiple outcomes', function (t) {
var s = sample([0.5, 0.25, 0.25], [10, 11, 12]),
outcomes = [],
c1 = 0;
t.plan(2);
outcomes = s.next(1000);
for (i = 0; i < outcomes.length; i++) {
if(outcomes[i] == 10) c1++;
}
t.equals(outcomes.length, 1000);
t.ok(c1 > 440 && c1 < 550, 'Half approx.');
});
test('String outcome', function (t) {
var s = sample([0.85, 0.15], ['A', 'B']),
ok = false;
t.plan(1);
for (i = 0; i < 1000; i++) {
if(s.next() == 'A') {
ok = true;
break;
}
}
t.ok(ok, 'A is found');
});
test('Probability cannot be zero', function (t) {
var e;
t.plan(1);
try {
sample([0.0, 0.0, 0.0]);
} catch (ex) {
e = ex;
}
t.ok(e, e);
});
test('Probability must be positive', function (t) {
var e;
t.plan(1);
try {
sample([0.0, -10.0, 0.0]);
} catch (ex) {
e = ex;
}
t.ok(e, e);
});