UNPKG

9.43 kBJavaScriptView Raw
1var chai = require('chai');
2var expect = chai.expect;
3var PD = require("../index")
4
5// CONFIG
6var repeat = 1000; // How many times to repeat some tests
7
8describe("Test of entropy generation", function() {
9 it('Set entropy to a low level', function() {
10 for(var i=0; i<repeat; i++) {
11 var lowEntropyRandom = PD.prng(1);
12 var numb = lowEntropyRandom * 256;
13 expect(numb).to.equal(Math.round(numb));
14 expect(numb).to.be.above(-1);
15 expect(numb).to.be.below(256);
16 }
17 });
18
19 it('Generates a number between 0 and 1', function() {
20 for(var i=0; i<repeat; i++) {
21 expect(PD.prng()).to.be.above(0);
22 expect(PD.prng()).to.be.below(1);
23 }
24 });
25
26 it('Generates a number between 50 and 60', function() {
27 var rn = PD.runif(repeat, 50, 60);
28 rn.map(function(item) {
29 expect(item).to.be.above(50);
30 expect(item).to.be.below(60);
31 })
32 });
33});
34
35describe("Test of binomial distribution", function() {
36 it('Generates whole numbers between 0 and max', function() {
37 var rn = PD.rbinom(repeat, 6, 0.6);
38 rn.map(function(item) {
39 expect(item).to.equal(Math.round(item));
40 expect(item).to.be.above(-1);
41 expect(item).to.be.below(7);
42 });
43
44 // Try without optional parameters
45 var rn = PD.rbinom(repeat);
46 rn.map(function(item) {
47 expect(item).to.equal(Math.round(item));
48 expect(item).to.be.above(-1);
49 expect(item).to.be.below(2);
50 });
51 });
52});
53
54describe("Test of negative binomial distribution", function() {
55 it('Generates whole numbers', function() {
56 var rn = PD.rnbinom(repeat, 6, 0.7);
57 rn.map(function(item) {
58 expect(item).to.equal(Math.round(item));
59 expect(item).to.be.above(-1);
60 })
61
62 });
63
64 // Test to make sure throwing properly
65 it('Throws errors on bad parameters', function() {
66 expect(function() { PD.rnbinom(1, 2.3 ,0.5) }).to.throw("Size must be a whole number");
67 expect(function() { PD.rnbinom(1, 0, -1) } ).to.throw("Size must be one or greater");
68 expect(function() { PD.rnbinom(1, 3, 0.5, 3) }).to.throw("You must specify probability or mean, not both");
69 expect(function() { PD.rnbinom(1, 6, -1) }).to.throw("Probability values cannot be less than 0");
70 expect(function() { PD.rnbinom(1, 6, 1.1) }).to.throw("Probability values cannot be greater than 1");
71 expect(function() { PD.rnbinom(1, 6) }).to.throw("Probability value is missing or not a number");
72
73 });
74});
75
76describe("Test of normal density function", function() {
77
78 // Test to make sure throwing properly
79 it('Throws errors on bad parameters', function() {
80 expect(function() { PD.dnorm() }).to.throw("A required parameter is missing or not a number");
81 expect(function() { PD.dnorm(1,0,-0.5) }).to.throw("Parameter cannot be less than 0");
82 expect(function() { PD.dnorm(1,"this") }).to.throw("A required parameter is missing or not a number");
83 });
84
85 it('Gives the correct information', function() {
86 expect(PD.dnorm(0,0,0)).to.equal(Infinity);
87 expect(PD.dnorm(1,1,0)).to.equal(Infinity);
88 expect(PD.dnorm(0,1,0)).to.equal(0);
89 expect(Math.round(10e6*PD.dnorm(0))).to.equal(3989423);
90 expect(Math.round(10e6*PD.dnorm(1))).to.equal(2419707);
91 expect(Math.round(10e6*PD.dnorm(-1))).to.equal(2419707);
92 expect(Math.round(10e6*PD.dnorm(1,1))).to.equal(3989423);
93 expect(Math.round(10e6*PD.dnorm(1,1,1))).to.equal(3989423);
94 expect(Math.round(10e6*PD.dnorm(1,1,2))).to.equal(1994711);
95 expect(Math.round(10e6*PD.dnorm(1,-1,2))).to.equal(1209854);
96 expect(Math.round(10e6*PD.dnorm(3,3,1))).to.equal(3989423);
97 });
98});
99
100
101describe("Test of F-distribution", function() {
102 it('Generates non-negative numbers', function() {
103 var rn = PD.rf(repeat, 1, 1);
104 rn.map(function(item) {
105 expect(item).to.be.above(0);
106 })
107
108 });
109
110 // Test to make sure throwing properly
111 it('Throws errors on bad parameters', function() {
112 expect(function() { PD.rf(1, -1 ,3) }).to.throw("Parameter cannot be less than 0");
113 expect(function() { PD.rf(1, 1 ,-3) }).to.throw("Parameter cannot be less than 0");
114 });
115});
116
117describe("Test of sample function", function() {
118
119 // Test to make sure throwing properly
120 it('Throws errors on bad parameters', function() {
121 expect(function() { PD.sample() }).to.throw("Expected an array of length 1 or greater");
122 expect(function() { PD.sample([]) } ).to.throw("Expected an array of length 1 or greater");
123 expect(function() { PD.sample([1,2,3], 4) } ).to.throw("You cannot select 4 items from an array of length 3 without replacement");
124 expect(function() { PD.sample([1,2,3,4], 4, true, [1,1,-1,1]) } ).to.throw("Parameter cannot be less than 0");
125 expect(function() { PD.sample([1,2,3], 4, true, [1,1,1,1]) } ).to.throw("Probabilities for sample must be same length as the array to sample from");
126
127 });
128
129 it('Returns the correct sample', function() {
130 expect(PD.sample([3],1)[0]).to.equal(3);
131 expect(PD.sample([3],1)[1]).to.equal(undefined);
132
133 for(var i=0; i<repeat; i++) {
134 expect(PD.sample([1,2,3,4], 1, true, [0,0,0,0.3])[0]).to.equal(4);
135 }
136 })
137});
138
139
140describe("Test validation functions", function() {
141
142 // Test to make sure throwing properly
143 it('Throws errors on bad parameters', function() {
144
145 // "a"
146 expect(function() { PD._v(undefined, "a") }).to.throw("Expected an array of length 1 or greater");
147 expect(function() { PD._v([], "a") }).to.throw("Expected an array of length 1 or greater");
148 expect(function() { PD._v("fox", "a") }).to.throw("Expected an array of length 1 or greater");
149
150 // "n"
151 expect(function() { PD._v(undefined, "n") }).to.throw("You must specify how many values you want");
152 expect(function() { PD._v("cheese", "n") }).to.throw("The number of values must be numeric");
153 expect(function() { PD._v("7", "n") }).to.throw("The number of values must be numeric");
154 expect(function() { PD._v(2.2, "n") }).to.throw("The number of values must be a whole number");
155 expect(function() { PD._v(-1, "n") }).to.throw("The number of values must be a whole number of 1 or greater");
156 expect(function() { PD._v(1/0, "n") }).to.throw("The number of values cannot be infinite ;-)");
157
158 // "p"
159 expect(function() { PD._v("george", "p") }).to.throw("Probability value is missing or not a number");
160 expect(function() { PD._v(undefined, "p") }).to.throw("Probability value is missing or not a number");
161 expect(function() { PD._v(-0.1, "p") }).to.throw("Probability values cannot be less than 0");
162 expect(function() { PD._v(1/0, "p") }).to.throw("Probability values cannot be greater than 1");
163
164 // "pos"
165 expect(function() { PD._v(undefined, "pos") }).to.throw("A required parameter is missing or not a number");
166 expect(function() { PD._v(" ", "pos") }).to.throw("A required parameter is missing or not a number");
167 expect(function() { PD._v(-0.1, "pos") }).to.throw("Parameter must be greater than 0");
168 expect(function() { PD._v(-10e6, "pos") }).to.throw("Parameter must be greater than 0");
169 expect(function() { PD._v(0, "pos") }).to.throw("Parameter must be greater than 0");
170 expect(function() { PD._v(1/0, "pos") }).to.throw('Sent "infinity" as a parameter');
171
172 // "r"
173 expect(function() { PD._v(undefined, "r") }).to.throw("A required parameter is missing or not a number");
174 expect(function() { PD._v(1/0, "r") }).to.throw('Sent "infinity" as a parameter');
175
176 // "nn"
177 expect(function() { PD._v(undefined, "nn") }).to.throw("A required parameter is missing or not a number");
178 expect(function() { PD._v(-0.2, "nn") }).to.throw("Parameter cannot be less than 0");
179 expect(function() { PD._v(1/0, "nn") }).to.throw('Sent "infinity" as a parameter');
180
181 // "nni"
182 expect(function() { PD._v(undefined, "nni") }).to.throw("A required parameter is missing or not a number");
183 expect(function() { PD._v(23.4, "nni") }).to.throw("Parameter must be a whole number");
184 expect(function() { PD._v(-0.2, "nni") }).to.throw("Parameter must be a whole number");
185 expect(function() { PD._v(1/0, "nni") }).to.throw('Sent "infinity" as a parameter');
186
187 });
188
189 it('Returns the parameter back if correct', function() {
190 expect(PD._v(7, "n")).to.equal(7);
191 expect(PD._v(undefined, "n", 5)).to.equal(5);
192 expect(PD._v(undefined, "p", 0)).to.equal(0);
193 expect(PD._v(undefined, "p", 1)).to.equal(1);
194 expect(PD._v(0, "p")).to.equal(0);
195 expect(PD._v(1, "p")).to.equal(1);
196 expect(PD._v(.1, "p")).to.equal(.1);
197 expect(PD._v(.1, "pos")).to.equal(.1);
198 expect(PD._v(10, "pos")).to.equal(10);
199 expect(PD._v(10e6, "pos")).to.equal(10e6);
200 expect(PD._v(2.33333, "r")).to.equal(2.33333);
201 expect(PD._v(-2.7, "r")).to.equal(-2.7);
202 expect(PD._v(2, "nn")).to.equal(2);
203 expect(PD._v(10e3, "nni")).to.equal(10000);
204
205 })
206});
207
208// TODO: Implement NIST testing or similar