1 | var chai = require('chai');
|
2 | var expect = chai.expect;
|
3 | var PD = require("../index")
|
4 |
|
5 |
|
6 | var repeat = 1000;
|
7 |
|
8 | describe("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 |
|
35 | describe("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 |
|
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 |
|
54 | describe("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 |
|
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 |
|
76 | describe("Test of normal density function", function() {
|
77 |
|
78 |
|
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 |
|
101 | describe("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 |
|
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 |
|
117 | describe("Test of sample function", function() {
|
118 |
|
119 |
|
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 |
|
140 | describe("Test validation functions", function() {
|
141 |
|
142 |
|
143 | it('Throws errors on bad parameters', function() {
|
144 |
|
145 |
|
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 |
|
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 |
|
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 |
|
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 |
|
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 |
|
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 |
|
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 |
|