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
|
'use strict';
/*
* This file tests the FSM test framework.
*/
load('jstests/concurrency/fsm_libs/fsm.js');
(function() {
var getWeightedRandomChoice = fsm._getWeightedRandomChoice;
var doc = {a: 0.25, b: 0.5, c: 0.25};
// NOTE: getWeightedRandomChoice calls assert internally, so it will print stack traces
// when assert.throws executes
assert.throws(function() {
getWeightedRandomChoice(doc, -1);
}, [], 'should reject negative values');
assert.throws(function() {
getWeightedRandomChoice(doc, 1);
}, [], 'should reject values == 1');
assert.throws(function() {
getWeightedRandomChoice(doc, 2);
}, [], 'should reject values > 1');
assert.throws(function() {
getWeightedRandomChoice({}, 0.0);
}, [], 'cannot choose from zero states');
assert.throws(function() {
getWeightedRandomChoice({}, 0.5);
}, [], 'cannot choose from zero states');
assert.throws(function() {
getWeightedRandomChoice({}, 0.99);
}, [], 'cannot choose from zero states');
assert.eq('a', getWeightedRandomChoice(doc, 0.00), '0');
assert.eq('a', getWeightedRandomChoice(doc, 0.24), '1');
assert.eq('b', getWeightedRandomChoice(doc, 0.25), '2');
assert.eq('b', getWeightedRandomChoice(doc, 0.50), '3');
assert.eq('b', getWeightedRandomChoice(doc, 0.74), '4');
assert.eq('c', getWeightedRandomChoice(doc, 0.75), '5');
assert.eq('c', getWeightedRandomChoice(doc, 0.99), '6');
})();
|