-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtestSimpleRandomSelector.js
230 lines (213 loc) · 8.48 KB
/
testSimpleRandomSelector.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
var selectorFactory = require('../src/index');
const assert = require('chai').assert;
const expect = require('chai').expect;
var RandomSelector = require('../src/randomSelector');
///const NormalDistribution=require('normal-distribution');
///require('factorial');
describe('Test SimpleRandomSelector constructing', function() {
before(function() {
console.log("BeforeSuite!");
///RandomSelector.prototype.DEBUG = true;
});
beforeEach(function() {
///console.log("BeforeTest!");
///RandomSelector.prototype.DEBUG = true;
});
//Test constructor
describe('#Constructor', function() {
it("New with undefined value: Error", function(){
assert.throws(function(){
selectorFactory.createSimpleSelectorWithoutReplacement();
}, Error, "Error: invalid elements (undefined)!!!");
});
it("New with null value: Error", function(){
assert.throws(function(){
selectorFactory.createSimpleSelectorWithoutReplacement(null);
}, Error, "Error: invalid elements (null)!!!");
});
it("New with number: Error!", function(){
assert.throws(function(){
selectorFactory.createSimpleSelectorWithoutReplacement(1);
}, Error, "Error: invalid elements (not an array)!!!");
});
it("New with valid parameters: no error!", function(){
var selector = selectorFactory.createSimpleSelectorWithoutReplacement([1, 2, 3, 5]);
selector.DEBUG = true;
selector.debug('Constructed!');
});
it("New with valid parameters: correct replacementMode", function(){
var selector = selectorFactory.createSimpleSelectorWithReplacement([1, 2, 3, 5]);
assert.isTrue(selector.hasRelacementMode());
selector = selectorFactory.createSimpleSelectorWithoutReplacement([1, 2, 3, 5]);
assert.isFalse(selector.hasRelacementMode());
});
});
describe("#setReplacementMode()", function(){
var selector = selectorFactory.createSimpleSelectorWithReplacement([1]);
assert.isTrue(selector.hasRelacementMode());
selector.setRelacementMode(false);
assert.isFalse(selector.hasRelacementMode());
selector.setRelacementMode(true);
assert.isTrue(selector.hasRelacementMode());
});
describe('#getElementCount', function() {
it("Return correct count", function() {
var selector = selectorFactory.createSimpleSelectorWithoutReplacement([1, 2, 3, 5]);
assert.equal(4, selector.getElementCount());
assert.deepEqual([1, 2, 3, 5], selector.getElements());
});
});
describe('#getElements', function() {
it("Return correct elements", function(){
var selector = selectorFactory.createSimpleSelectorWithoutReplacement([1, 2, 3, 5]);
assert.deepEqual([1, 2, 3, 5], selector.getElements());
});
});
describe('#Selecting with replacement', function() {
it("Select number array: return an array element", function(){
var elements = [1, 2, 3, 5];
var selector = selectorFactory.createSimpleSelectorWithReplacement(elements);
var selectedElement = selector.select();
expect(elements).to.contains(selectedElement);
});
it("Select string array: return an array element", function(){
var elements = ['hello','world','!'];
var selector = selectorFactory.createSimpleSelectorWithReplacement(elements);
var selectedElement = selector.select();
expect(elements).to.contains(selectedElement);
});
it("Select mix array: return an array element", function(){
var elements = ['hello','world','!', 1, 2, 3, 5, null, undefined];
var selector = selectorFactory.createSimpleSelectorWithReplacement(elements);
selector.DEBUG = false;
for(var i=0;i<1000;i++){
var selectedElement = selector.select();
expect(elements).to.contains(selectedElement);
}
});
it("Select mix array: can return null", function(){
var elements = ['hello','world','!', 1, 2, 3, 5, null, undefined];
var selector = selectorFactory.createSimpleSelectorWithReplacement(elements);
var hasNull = false;
selector.DEBUG = false;
for(var i=0;i<1000;i++){
var selectedElement = selector.select();
if(selectedElement == null)
{
hasNull = true;
break;
}
}
assert.isTrue(hasNull);
});
it("Select mix array: can return undefined", function(){
var elements = ['hello','world','!', 1, 2, 3, 5, null, undefined];
var selector = selectorFactory.createSimpleSelectorWithReplacement(elements);
selector.DEBUG = false;
var hasUndefined = false;
for(var i=0;i<1000;i++){
var selectedElement = selector.select();
if(selectedElement == null)
{
hasUndefined = true;
break;
}
}
assert.isTrue(hasUndefined);
});
it("Select not return null if there is no null element", function(){
var elements = ['hello','world','!', 1, 2, 3, 5];
var selector = selectorFactory.createSimpleSelectorWithReplacement(elements);
selector.DEBUG = false;
for(var i=0;i<100;i++){
var selectedElement = selector.select();
assert.isTrue(selectedElement != null);
assert.isTrue(selectedElement != undefined);
}
});
it("Hypothesis test: coin toss is fair on head!", function(){
var selector = selectorFactory.createSimpleSelectorWithReplacement(['H'/*Head => 0*/
, 'T' /*Tail => 1*/]);
///
selector.DEBUG = false;
var tossCount = 10000;
var headCount = 0;
for(var i=0;i<tossCount;i++)
{
var result = selector.select();
if(result==='H')
{
headCount++;
}
}
var r = headCount/tossCount;///actual probability of obtaining heads in a coin toss)
var zValue = 3.8906;
var p = headCount/tossCount; //true probability of obtaining heads
var E = zValue/(2*Math.sqrt(tossCount));
console.log('Corresponding to 99.99% level of confidence (p-E < r < p+E): ', p-E,'<', r,'<', p+E);
assert.isTrue(r>p-E, "Corresponding to 99.99% level of confidence: p-E < r");
assert.isTrue(r<p+E, "Corresponding to 99.99% level of confidence: r < p+E");
});
it("Hypothesis test: dice rolling is fair on 5!", function(){
var selector = selectorFactory.createSimpleSelectorWithReplacement([1, 2, 3, 4, 5, 6]);
///
selector.DEBUG = false;
var tossCount = 10000;
var fiveCount = 0;
for(var i=0;i<tossCount;i++)
{
var result = selector.select();
if(result===5)
{
fiveCount++;
}
}
var r = fiveCount/tossCount;///actual probability of obtaining five
var zValue = 3.8906;
var p = 1/6; //true probability of obtaining five
var E = zValue/(2*Math.sqrt(tossCount));
console.log('Corresponding to 99.99% level of confidence (p-E < r < p+E): ', p-E,'<', r,'<', p+E);
assert.isTrue(r>p-E, "Corresponding to 99.99% level of confidence: p-E < r");
assert.isTrue(r<p+E, "Corresponding to 99.99% level of confidence: r < p+E");
});
});
describe('#Selecting without replacement', function() {
it("After all element selected, return null", function(){
var selector = selectorFactory.createSimpleSelectorWithoutReplacement([1, 2, 3, 5]);
///selector.DEBUG = true;
assert.isTrue(selector.select() != null);
assert.isTrue(selector.select() != null);
assert.isTrue(selector.select() != null);
assert.isTrue(selector.select() != null);
assert.isTrue(selector.select() == null);
});
it("Select not return null if has at least 1 not null element", function(){
var testCount = 1000;
for(var i=0;i<testCount;i++)
{
var selector = selectorFactory.createSimpleSelectorWithoutReplacement([1, 2, 3, 5]);
assert.isTrue(selector.select() != null);
assert.isTrue(selector.select() != null);
assert.isTrue(selector.select() != null);
assert.isTrue(selector.select() != null);
}
});
it("Select may return null element", function(){
var testCount = 1000;
var hasNull = false;
for(var i=0;i<testCount;i++)
{
var selector = selectorFactory.createSimpleSelectorWithoutReplacement([null, 2, null, 5]);
///selector.DEBUG = true;
var selectedElement = selector.select();
///console.log("Selected: ", selectedElement);
if(selectedElement == null)
{
hasNull = true;
break;
}
}
assert.isTrue(hasNull);
});
});
});