1
|
|
|
<?php |
2
|
|
|
namespace Oefenweb\Statistics\Test; |
3
|
|
|
|
4
|
|
|
use Oefenweb\Statistics\Statistics; |
5
|
|
|
use Oefenweb\Statistics\StatisticsError; |
6
|
|
|
use PHPUnit\Framework\TestCase; |
7
|
|
|
|
8
|
|
|
class StatisticsTest extends TestCase |
9
|
|
|
{ |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Tests `sum`. |
13
|
|
|
* |
14
|
|
|
* Integer values. |
15
|
|
|
* |
16
|
|
|
* @return void |
17
|
|
|
*/ |
18
|
|
|
public function testSumIntegers() |
19
|
|
|
{ |
20
|
|
|
$values = [1, 2, 3, 4, 4]; |
21
|
|
|
|
22
|
|
|
$result = Statistics::sum($values); |
23
|
|
|
$expected = 14; |
24
|
|
|
|
25
|
|
|
$this->assertSame($expected, $result); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Tests `sum`. |
30
|
|
|
* |
31
|
|
|
* Float values. |
32
|
|
|
* |
33
|
|
|
* @return void |
34
|
|
|
*/ |
35
|
|
|
public function testSum() |
36
|
|
|
{ |
37
|
|
|
$values = [-1.0, 2.5, 3.25, 5.75]; |
38
|
|
|
|
39
|
|
|
$result = Statistics::sum($values); |
40
|
|
|
$expected = 10.5; |
41
|
|
|
|
42
|
|
|
$this->assertSame($expected, $result); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Tests `sum`. |
47
|
|
|
* |
48
|
|
|
* Mixed values. |
49
|
|
|
* |
50
|
|
|
* @return void |
51
|
|
|
*/ |
52
|
|
|
public function testSumMixed() |
53
|
|
|
{ |
54
|
|
|
$values = [-2, 2.5, 3.25, 5.75, 0]; |
55
|
|
|
|
56
|
|
|
$result = Statistics::sum($values); |
57
|
|
|
$expected = 9.5; |
58
|
|
|
|
59
|
|
|
$this->assertSame($expected, $result); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Tests `min`. |
64
|
|
|
* |
65
|
|
|
* Integer values. |
66
|
|
|
* |
67
|
|
|
* @return void |
68
|
|
|
*/ |
69
|
|
|
public function testMinIntegers() |
70
|
|
|
{ |
71
|
|
|
$values = [1, 2, 3, 4, 4]; |
72
|
|
|
|
73
|
|
|
$result = Statistics::min($values); |
74
|
|
|
$expected = 1; |
75
|
|
|
|
76
|
|
|
$this->assertSame($expected, $result); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Test for `min`. |
81
|
|
|
* |
82
|
|
|
* Float values. |
83
|
|
|
* |
84
|
|
|
* @return void |
85
|
|
|
*/ |
86
|
|
|
public function testMinIntegersFloats() |
87
|
|
|
{ |
88
|
|
|
$values = [-1.0, 2.5, 3.25, 5.75]; |
89
|
|
|
|
90
|
|
|
$result = Statistics::min($values); |
91
|
|
|
$expected = -1.0; |
92
|
|
|
|
93
|
|
|
$this->assertSame($expected, $result); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Tests `max`. |
98
|
|
|
* |
99
|
|
|
* Integer values. |
100
|
|
|
* |
101
|
|
|
* @return void |
102
|
|
|
*/ |
103
|
|
|
public function testMaxIntegers() |
104
|
|
|
{ |
105
|
|
|
$values = [1, 2, 3, 4, 4]; |
106
|
|
|
|
107
|
|
|
$result = Statistics::max($values); |
108
|
|
|
$expected = 4; |
109
|
|
|
|
110
|
|
|
$this->assertSame($expected, $result); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Tests `max`. |
115
|
|
|
* |
116
|
|
|
* Float values. |
117
|
|
|
* |
118
|
|
|
* @return void |
119
|
|
|
*/ |
120
|
|
|
public function testMaxFloats() |
121
|
|
|
{ |
122
|
|
|
$values = [-1.0, 2.5, 3.25, 5.75]; |
123
|
|
|
|
124
|
|
|
$result = Statistics::max($values); |
125
|
|
|
$expected = 5.75; |
126
|
|
|
|
127
|
|
|
$this->assertSame($expected, $result); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* Tests `mean`. |
132
|
|
|
* |
133
|
|
|
* Integer values. |
134
|
|
|
* |
135
|
|
|
* @return void |
136
|
|
|
*/ |
137
|
|
|
public function testMeanIntegers() |
138
|
|
|
{ |
139
|
|
|
$values = [1, 2, 3, 4, 4]; |
140
|
|
|
|
141
|
|
|
$result = Statistics::mean($values); |
142
|
|
|
$expected = 2.8; |
143
|
|
|
|
144
|
|
|
$this->assertSame($expected, $result); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* Tests `mean`. |
149
|
|
|
* |
150
|
|
|
* Float values. |
151
|
|
|
* |
152
|
|
|
* @return void |
153
|
|
|
*/ |
154
|
|
|
public function testMeanFloats() |
155
|
|
|
{ |
156
|
|
|
$values = [-1.0, 2.5, 3.25, 5.75]; |
157
|
|
|
|
158
|
|
|
$result = Statistics::mean($values); |
159
|
|
|
$expected = 2.625; |
160
|
|
|
|
161
|
|
|
$this->assertSame($expected, $result); |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* Tests `mean`. |
166
|
|
|
* |
167
|
|
|
* Mixed values. |
168
|
|
|
* |
169
|
|
|
* @return void |
170
|
|
|
*/ |
171
|
|
|
public function testMeanMixed() |
172
|
|
|
{ |
173
|
|
|
$values = [-2, 2.5, 3.25, 5.75, 0]; |
174
|
|
|
|
175
|
|
|
$result = Statistics::mean($values); |
176
|
|
|
$expected = 1.9; |
177
|
|
|
|
178
|
|
|
$this->assertSame($expected, $result); |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* Tests `frequency`. |
183
|
|
|
* |
184
|
|
|
* Integer values. |
185
|
|
|
* |
186
|
|
|
* @return void |
187
|
|
|
*/ |
188
|
|
|
public function testFrequencyIntegers() |
189
|
|
|
{ |
190
|
|
|
$values = [1, 1, 2, 3, 3, 3, 3, 4]; |
191
|
|
|
|
192
|
|
|
$result = Statistics::frequency($values); |
193
|
|
|
$expected = [ |
194
|
|
|
4 => 1, |
195
|
|
|
2 => 1, |
196
|
|
|
1 => 2, |
197
|
|
|
3 => 4, |
198
|
|
|
]; |
199
|
|
|
|
200
|
|
|
$this->assertEquals($expected, $result); |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
/** |
204
|
|
|
* Tests `frequency`. |
205
|
|
|
* |
206
|
|
|
* Float values. |
207
|
|
|
* |
208
|
|
|
* @return void |
209
|
|
|
*/ |
210
|
|
|
public function testFrequencyFloats() |
211
|
|
|
{ |
212
|
|
|
$values = [1, 3, 6, 6, 6, 6, 7.12, 7.12, 12, 12, 17]; |
213
|
|
|
|
214
|
|
|
$result = Statistics::frequency($values); |
215
|
|
|
$expected = [ |
216
|
|
|
17 => 1, |
217
|
|
|
1 => 1, |
218
|
|
|
3 => 1, |
219
|
|
|
12 => 2, |
220
|
|
|
'7.12' => 2, |
221
|
|
|
6 => 4, |
222
|
|
|
]; |
223
|
|
|
|
224
|
|
|
$this->assertEquals($expected, $result); |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
/** |
228
|
|
|
* Tests `frequency`. |
229
|
|
|
* |
230
|
|
|
* String values. |
231
|
|
|
* |
232
|
|
|
* @return void |
233
|
|
|
*/ |
234
|
|
|
public function testFrequencyStrings() |
235
|
|
|
{ |
236
|
|
|
$values = ['red', 'blue', 'blue', 'red', 'green', 'red', 'red']; |
237
|
|
|
|
238
|
|
|
$result = Statistics::frequency($values); |
239
|
|
|
$expected = [ |
240
|
|
|
'green' => 1, |
241
|
|
|
'blue' => 2, |
242
|
|
|
'red' => 4, |
243
|
|
|
]; |
244
|
|
|
|
245
|
|
|
$this->assertEquals($expected, $result); |
246
|
|
|
} |
247
|
|
|
|
248
|
|
|
/** |
249
|
|
|
* Tests `mode`. |
250
|
|
|
* |
251
|
|
|
* Integer values. |
252
|
|
|
* |
253
|
|
|
* @return void |
254
|
|
|
*/ |
255
|
|
|
public function testModeIntegers() |
256
|
|
|
{ |
257
|
|
|
$values = [3]; |
258
|
|
|
|
259
|
|
|
$result = Statistics::mode($values); |
260
|
|
|
$expected = 3; |
261
|
|
|
|
262
|
|
|
$this->assertSame($expected, $result); |
263
|
|
|
|
264
|
|
|
$values = [1, 1, 2, 3, 3, 3, 3, 4]; |
265
|
|
|
|
266
|
|
|
$result = Statistics::mode($values); |
267
|
|
|
$expected = 3; |
268
|
|
|
|
269
|
|
|
$this->assertSame($expected, $result); |
270
|
|
|
|
271
|
|
|
$values = [1, 3, 6, 6, 6, 6, 7, 7, 12, 12, 17]; |
272
|
|
|
|
273
|
|
|
$result = Statistics::mode($values); |
274
|
|
|
$expected = 6; |
275
|
|
|
|
276
|
|
|
$this->assertSame($expected, $result); |
277
|
|
|
} |
278
|
|
|
|
279
|
|
|
/** |
280
|
|
|
* Tests `mode`. |
281
|
|
|
* |
282
|
|
|
* String values. |
283
|
|
|
* |
284
|
|
|
* @return void |
285
|
|
|
*/ |
286
|
|
|
public function testModeStrings() |
287
|
|
|
{ |
288
|
|
|
$values = ['red', 'blue', 'blue', 'red', 'green', 'red', 'red']; |
289
|
|
|
|
290
|
|
|
$result = Statistics::mode($values); |
291
|
|
|
$expected = 'red'; |
292
|
|
|
|
293
|
|
|
$this->assertSame($expected, $result); |
294
|
|
|
} |
295
|
|
|
|
296
|
|
|
/** |
297
|
|
|
* Tests `mode`. |
298
|
|
|
* |
299
|
|
|
* @return void |
300
|
|
|
* @expectedException \Oefenweb\Statistics\StatisticsError |
301
|
|
|
*/ |
302
|
|
|
public function testModeNotExactlyOne() |
303
|
|
|
{ |
304
|
|
|
$values = [1, 1, 2, 4, 4]; |
305
|
|
|
|
306
|
|
|
Statistics::mode($values); |
307
|
|
|
} |
308
|
|
|
|
309
|
|
|
/** |
310
|
|
|
* Tests `variance`. |
311
|
|
|
* |
312
|
|
|
* Sample (default), integer values. |
313
|
|
|
* |
314
|
|
|
* @return void |
315
|
|
|
*/ |
316
|
|
|
public function testVarianceSampleIntegers() |
317
|
|
|
{ |
318
|
|
|
$values = [2, 4, 4, 4, 5, 5, 7, 9]; |
319
|
|
|
$sample = true; |
320
|
|
|
|
321
|
|
|
$result = Statistics::variance($values, $sample); |
322
|
|
|
$expected = 4.571429; |
323
|
|
|
|
324
|
|
|
$this->assertEquals($expected, $result, '', pow(10, -4)); |
325
|
|
|
} |
326
|
|
|
|
327
|
|
|
/** |
328
|
|
|
* Tests `variance`. |
329
|
|
|
* |
330
|
|
|
* Sample (default), float values. |
331
|
|
|
* |
332
|
|
|
* @return void |
333
|
|
|
*/ |
334
|
|
|
public function testVarianceSampleFloats() |
335
|
|
|
{ |
336
|
|
|
$values = [0.0, 0.25, 0.25, 1.25, 1.5, 1.75, 2.75, 3.25]; |
337
|
|
|
$sample = true; |
338
|
|
|
|
339
|
|
|
$result = Statistics::variance($values, $sample); |
340
|
|
|
$expected = 1.428571; |
341
|
|
|
|
342
|
|
|
$this->assertEquals($expected, $result, '', pow(10, -4)); |
343
|
|
|
} |
344
|
|
|
|
345
|
|
|
/** |
346
|
|
|
* Tests `variance`. |
347
|
|
|
* |
348
|
|
|
* Population, integer values. |
349
|
|
|
* |
350
|
|
|
* @return void |
351
|
|
|
*/ |
352
|
|
View Code Duplication |
public function testVariancePopulationIntegers() |
|
|
|
|
353
|
|
|
{ |
354
|
|
|
$values = [2, 4, 4, 4, 5, 5, 7, 9]; |
355
|
|
|
$sample = false; |
356
|
|
|
|
357
|
|
|
$result = Statistics::variance($values, $sample); |
358
|
|
|
$expected = 4; |
359
|
|
|
|
360
|
|
|
$this->assertSame($expected, $result); |
361
|
|
|
} |
362
|
|
|
|
363
|
|
|
/** |
364
|
|
|
* Tests `variance`. |
365
|
|
|
* |
366
|
|
|
* Population, float values. |
367
|
|
|
* |
368
|
|
|
* @return void |
369
|
|
|
*/ |
370
|
|
|
public function testVariancePopulationFloats() |
371
|
|
|
{ |
372
|
|
|
$values = [0.0, 0.25, 0.25, 1.25, 1.5, 1.75, 2.75, 3.25]; |
373
|
|
|
$sample = false; |
374
|
|
|
|
375
|
|
|
$result = Statistics::variance($values, $sample); |
376
|
|
|
$expected = 1.25; |
377
|
|
|
|
378
|
|
|
$this->assertSame($expected, $result, '', pow(10, -4)); |
379
|
|
|
} |
380
|
|
|
|
381
|
|
|
/** |
382
|
|
|
* Test `variance` |
383
|
|
|
* |
384
|
|
|
* At least two data points. |
385
|
|
|
* |
386
|
|
|
* @return void |
387
|
|
|
*/ |
388
|
|
|
public function testVarianceNotAtLeastTwo() |
389
|
|
|
{ |
390
|
|
|
$this->expectException(StatisticsError::class); |
391
|
|
|
|
392
|
|
|
$values = [1]; |
393
|
|
|
Statistics::variance($values); |
394
|
|
|
} |
395
|
|
|
|
396
|
|
|
/** |
397
|
|
|
* Tests `standardDeviation`. |
398
|
|
|
* |
399
|
|
|
* Sample (default), integers values. |
400
|
|
|
* |
401
|
|
|
* @return void |
402
|
|
|
*/ |
403
|
|
|
public function testStandardDeviationSampleIntegers() |
404
|
|
|
{ |
405
|
|
|
$values = [2, 4, 4, 4, 5, 5, 7, 9]; |
406
|
|
|
$sample = true; |
407
|
|
|
|
408
|
|
|
$result = Statistics::standardDeviation($values, $sample); |
409
|
|
|
$expected = 2.13809; |
410
|
|
|
|
411
|
|
|
$this->assertEquals($expected, $result, '', pow(10, -4)); |
412
|
|
|
} |
413
|
|
|
|
414
|
|
|
/** |
415
|
|
|
* Tests `standardDeviation`. |
416
|
|
|
* |
417
|
|
|
* Sample (default), float values. |
418
|
|
|
* |
419
|
|
|
* @return void |
420
|
|
|
*/ |
421
|
|
View Code Duplication |
public function testStandardDeviationSampleFloats() |
|
|
|
|
422
|
|
|
{ |
423
|
|
|
$values = [1.5, 2.5, 2.5, 2.75, 3.25, 4.75]; |
424
|
|
|
$sample = true; |
425
|
|
|
|
426
|
|
|
$result = Statistics::standardDeviation($values, $sample); |
427
|
|
|
$expected = 1.081087; |
428
|
|
|
|
429
|
|
|
$this->assertEquals($expected, $result, '', pow(10, -4)); |
430
|
|
|
} |
431
|
|
|
|
432
|
|
|
/** |
433
|
|
|
* Tests `standardDeviation`. |
434
|
|
|
* |
435
|
|
|
* Population, integer values. |
436
|
|
|
* |
437
|
|
|
* @return void |
438
|
|
|
*/ |
439
|
|
View Code Duplication |
public function testStandardDeviationPopulationIntegers() |
|
|
|
|
440
|
|
|
{ |
441
|
|
|
$values = [2, 4, 4, 4, 5, 5, 7, 9]; |
442
|
|
|
$sample = false; |
443
|
|
|
|
444
|
|
|
$result = Statistics::standardDeviation($values, $sample); |
445
|
|
|
$expected = 2.0; |
446
|
|
|
|
447
|
|
|
$this->assertSame($expected, $result); |
448
|
|
|
} |
449
|
|
|
|
450
|
|
|
/** |
451
|
|
|
* Tests `standardDeviation`. |
452
|
|
|
* |
453
|
|
|
* Population, floats values. |
454
|
|
|
* |
455
|
|
|
* @return void |
456
|
|
|
*/ |
457
|
|
View Code Duplication |
public function testStandardDeviationPopulationFloats() |
|
|
|
|
458
|
|
|
{ |
459
|
|
|
$values = [1.5, 2.5, 2.5, 2.75, 3.25, 4.75]; |
460
|
|
|
$sample = false; |
461
|
|
|
|
462
|
|
|
$result = Statistics::standardDeviation($values, $sample); |
463
|
|
|
$expected = 0.9868; |
464
|
|
|
|
465
|
|
|
$this->assertEquals($expected, $result, '', pow(10, -4)); |
466
|
|
|
} |
467
|
|
|
|
468
|
|
|
/** |
469
|
|
|
* Tests `range`. |
470
|
|
|
* |
471
|
|
|
* (Unsigned) integer values. |
472
|
|
|
* |
473
|
|
|
* @return void |
474
|
|
|
*/ |
475
|
|
|
public function testRangeIntUnsigned() |
476
|
|
|
{ |
477
|
|
|
$values = [4, 6, 10, 15, 18]; |
478
|
|
|
$result = Statistics::range($values); |
479
|
|
|
$expected = 14; |
480
|
|
|
|
481
|
|
|
$this->assertSame($expected, $result); |
482
|
|
|
} |
483
|
|
|
|
484
|
|
|
/** |
485
|
|
|
* Tests `range`. |
486
|
|
|
* |
487
|
|
|
* (Signed) integer values. |
488
|
|
|
* |
489
|
|
|
* @return void |
490
|
|
|
*/ |
491
|
|
|
public function testRangeIntSigned() |
492
|
|
|
{ |
493
|
|
|
$values = [4, 6, 10, 15, 18, -18]; |
494
|
|
|
$result = Statistics::range($values); |
495
|
|
|
$expected = 36; |
496
|
|
|
|
497
|
|
|
$this->assertSame($expected, $result); |
498
|
|
|
} |
499
|
|
|
|
500
|
|
|
/** |
501
|
|
|
* Tests `range`. |
502
|
|
|
* |
503
|
|
|
* Float values. |
504
|
|
|
* |
505
|
|
|
* @return void |
506
|
|
|
*/ |
507
|
|
|
public function testRangeFloats() |
508
|
|
|
{ |
509
|
|
|
$values = [11, 13, 4.3, 15.5, 14]; |
510
|
|
|
$result = Statistics::range($values); |
511
|
|
|
$expected = 11.2; |
512
|
|
|
|
513
|
|
|
$this->assertSame($expected, $result); |
514
|
|
|
} |
515
|
|
|
} |
516
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.