Completed
Pull Request — master (#10)
by lee
01:18
created

StatisticsTest::testVarianceNotAtLeastTwo()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

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