Completed
Push — master ( d07aa1...060586 )
by Mischa ter
19s queued 11s
created

StatisticsTest::assertEqualsWithDeltaPolyfill()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 3
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
        $actual = Statistics::sum($values);
23
        $expected = 14;
24
25
        $this->assertSame($expected, $actual);
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
        $actual = Statistics::sum($values);
40
        $expected = 10.5;
41
42
        $this->assertSame($expected, $actual);
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
        $actual = Statistics::sum($values);
57
        $expected = 9.5;
58
59
        $this->assertSame($expected, $actual);
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
        $actual = Statistics::min($values);
74
        $expected = 1;
75
76
        $this->assertSame($expected, $actual);
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
        $actual = Statistics::min($values);
91
        $expected = -1.0;
92
93
        $this->assertSame($expected, $actual);
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
        $actual = Statistics::max($values);
108
        $expected = 4;
109
110
        $this->assertSame($expected, $actual);
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
        $actual = Statistics::max($values);
125
        $expected = 5.75;
126
127
        $this->assertSame($expected, $actual);
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
        $actual = Statistics::mean($values);
142
        $expected = 2.8;
143
144
        $this->assertSame($expected, $actual);
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
        $actual = Statistics::mean($values);
159
        $expected = 2.625;
160
161
        $this->assertSame($expected, $actual);
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
        $actual = Statistics::mean($values);
176
        $expected = 1.9;
177
178
        $this->assertSame($expected, $actual);
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
        $actual = Statistics::frequency($values);
193
        $expected = [
194
            4 => 1,
195
            2 => 1,
196
            1 => 2,
197
            3 => 4,
198
        ];
199
200
        $this->assertEquals($expected, $actual);
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
        $actual = 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, $actual);
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
        $actual = Statistics::frequency($values);
239
        $expected = [
240
            'green' => 1,
241
            'blue' => 2,
242
            'red' => 4,
243
        ];
244
245
        $this->assertEquals($expected, $actual);
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
        $actual = Statistics::mode($values);
260
        $expected = 3;
261
262
        $this->assertSame($expected, $actual);
263
264
        $values = [1, 1, 2, 3, 3, 3, 3, 4];
265
266
        $actual = Statistics::mode($values);
267
        $expected = 3;
268
269
        $this->assertSame($expected, $actual);
270
271
        $values = [1, 3, 6, 6, 6, 6, 7, 7, 12, 12, 17];
272
273
        $actual = Statistics::mode($values);
274
        $expected = 6;
275
276
        $this->assertSame($expected, $actual);
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
        $actual = Statistics::mode($values);
291
        $expected = 'red';
292
293
        $this->assertSame($expected, $actual);
294
    }
295
296
    /**
297
     * Tests `mode`.
298
     *
299
     * @return void
300
     */
301
    public function testModeNotExactlyOne()
302
    {
303
        $this->expectException(StatisticsError::class);
304
305
        $values = [1, 1, 2, 4, 4];
306
307
        Statistics::mode($values);
308
    }
309
310
    /**
311
     * Tests `variance`.
312
     *
313
     *  Sample (default), integer values.
314
     *
315
     * @return void
316
     */
317 View Code Duplication
    public function testVarianceSampleIntegers()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
318
    {
319
        $values = [2, 4, 4, 4, 5, 5, 7, 9];
320
        $sample = true;
321
322
        $actual = Statistics::variance($values, $sample);
323
        $expected = 4.571429;
324
325
        $this->assertEqualsWithDeltaPolyfill($expected, $actual);
326
    }
327
328
    /**
329
     * Tests `variance`.
330
     *
331
     *  Sample (default), float values.
332
     *
333
     * @return void
334
     */
335 View Code Duplication
    public function testVarianceSampleFloats()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
336
    {
337
        $values = [0.0, 0.25, 0.25, 1.25, 1.5, 1.75, 2.75, 3.25];
338
        $sample = true;
339
340
        $actual = Statistics::variance($values, $sample);
341
        $expected = 1.428571;
342
343
        $this->assertEqualsWithDeltaPolyfill($expected, $actual);
344
    }
345
346
    /**
347
     * Tests `variance`.
348
     *
349
     *  Population, integer values.
350
     *
351
     * @return void
352
     */
353 View Code Duplication
    public function testVariancePopulationIntegers()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
354
    {
355
        $values = [2, 4, 4, 4, 5, 5, 7, 9];
356
        $sample = false;
357
358
        $actual = Statistics::variance($values, $sample);
359
        $expected = 4;
360
361
        $this->assertSame($expected, $actual);
362
    }
363
364
    /**
365
     * Tests `variance`.
366
     *
367
     *  Population, float values.
368
     *
369
     * @return void
370
     */
371
    public function testVariancePopulationFloats()
372
    {
373
        $values = [0.0, 0.25, 0.25, 1.25, 1.5, 1.75, 2.75, 3.25];
374
        $sample = false;
375
376
        $actual = Statistics::variance($values, $sample);
377
        $expected = 1.25;
378
379
        $this->assertSame($expected, $actual, '', pow(10, -4));
380
    }
381
382
    /**
383
     * Test `variance`
384
     *
385
     * At least two data points.
386
     *
387
     * @return void
388
     */
389
    public function testVarianceNotAtLeastTwo()
390
    {
391
        $this->expectException(StatisticsError::class);
392
393
        $values = [1];
394
        Statistics::variance($values);
395
    }
396
397
    /**
398
     * Tests `standardDeviation`.
399
     *
400
     *  Sample (default), integers values.
401
     *
402
     * @return void
403
     */
404 View Code Duplication
    public function testStandardDeviationSampleIntegers()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
405
    {
406
        $values = [2, 4, 4, 4, 5, 5, 7, 9];
407
        $sample = true;
408
409
        $actual = Statistics::standardDeviation($values, $sample);
410
        $expected = 2.13809;
411
412
        $this->assertEqualsWithDeltaPolyfill($expected, $actual);
413
    }
414
415
    /**
416
     * Tests `standardDeviation`.
417
     *
418
     *  Sample (default), float values.
419
     *
420
     * @return void
421
     */
422 View Code Duplication
    public function testStandardDeviationSampleFloats()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
423
    {
424
        $values = [1.5, 2.5, 2.5, 2.75, 3.25, 4.75];
425
        $sample = true;
426
427
        $actual = Statistics::standardDeviation($values, $sample);
428
        $expected = 1.081087;
429
430
        $this->assertEqualsWithDeltaPolyfill($expected, $actual);
431
    }
432
433
    /**
434
     * Tests `standardDeviation`.
435
     *
436
     *  Population, integer values.
437
     *
438
     * @return void
439
     */
440 View Code Duplication
    public function testStandardDeviationPopulationIntegers()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
441
    {
442
        $values = [2, 4, 4, 4, 5, 5, 7, 9];
443
        $sample = false;
444
445
        $actual = Statistics::standardDeviation($values, $sample);
446
        $expected = 2.0;
447
448
        $this->assertSame($expected, $actual);
449
    }
450
451
    /**
452
     * Tests `standardDeviation`.
453
     *
454
     *  Population, floats values.
455
     *
456
     * @return void
457
     */
458 View Code Duplication
    public function testStandardDeviationPopulationFloats()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
459
    {
460
        $values = [1.5, 2.5, 2.5, 2.75, 3.25, 4.75];
461
        $sample = false;
462
463
        $actual = Statistics::standardDeviation($values, $sample);
464
        $expected = 0.9868;
465
466
        $this->assertEqualsWithDeltaPolyfill($expected, $actual);
467
    }
468
469
    /**
470
     * Tests `range`.
471
     *
472
     *  (Unsigned) integer values.
473
     *
474
     * @return void
475
     */
476
    public function testRangeIntUnsigned()
477
    {
478
        $values = [4, 6, 10, 15, 18];
479
        $actual = Statistics::range($values);
480
        $expected = 14;
481
482
        $this->assertSame($expected, $actual);
483
    }
484
485
    /**
486
     * Tests `range`.
487
     *
488
     *  (Signed) integer values.
489
     *
490
     * @return void
491
     */
492
    public function testRangeIntSigned()
493
    {
494
        $values = [4, 6, 10, 15, 18, -18];
495
        $actual = Statistics::range($values);
496
        $expected = 36;
497
498
        $this->assertSame($expected, $actual);
499
    }
500
501
    /**
502
     * Tests `range`.
503
     *
504
     *  Float values.
505
     *
506
     * @return void
507
     */
508
    public function testRangeFloats()
509
    {
510
        $values = [11, 13, 4.3, 15.5, 14];
511
        $actual = Statistics::range($values);
512
        $expected = 11.2;
513
514
        $this->assertSame($expected, $actual);
515
    }
516
517
    /**
518
     * Polyfill for `assertEqualsWithDelta`.
519
     *
520
     * @param mixed $expected
521
     * @param mixed $actual
522
     * @param float $delta
523
     */
524
    protected function assertEqualsWithDeltaPolyfill($expected, $actual, float $delta = 0.0001)
525
    {
526
        if (method_exists($this, 'assertEqualsWithDelta')) {
527
            $this->assertEqualsWithDelta($expected, $actual, $delta);
528
        } else {
529
            $this->assertEquals($expected, $actual, '', $delta);
530
        }
531
    }
532
}
533