1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* test Math |
4
|
|
|
* |
5
|
|
|
* @package BlueData |
6
|
|
|
* @subpackage Test |
7
|
|
|
* @author Michał Adamiak <[email protected]> |
8
|
|
|
* @copyright bluetree-service |
9
|
|
|
*/ |
10
|
|
|
namespace Test; |
11
|
|
|
|
12
|
|
|
use BlueData\Calculation\Math; |
13
|
|
|
use PHPUnit\Framework\TestCase; |
14
|
|
|
|
15
|
|
|
class MathTest extends TestCase |
16
|
|
|
{ |
17
|
|
|
public function testGetPercentDifference() |
18
|
|
|
{ |
19
|
|
|
$this->assertEquals(-200, Math::getPercentDifference(10, 30)); |
20
|
|
|
$this->assertEquals(50, Math::getPercentDifference(20, 10)); |
21
|
|
|
$this->assertEquals(Math::getPercentDifference(10, 0), 0); |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
public function testNumberToPercent() |
25
|
|
|
{ |
26
|
|
|
$this->assertEquals(50, Math::numberToPercent(10, 20)); |
27
|
|
|
$this->assertEquals(200, Math::numberToPercent(20, 10)); |
28
|
|
|
$this->assertEquals(Math::numberToPercent(10, 0), 0); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
public function testPercent() |
32
|
|
|
{ |
33
|
|
|
$this->assertEquals(10, Math::percent(10, 100)); |
34
|
|
|
$this->assertEquals(10, Math::percent(100, 10)); |
35
|
|
|
$this->assertEquals(Math::percent(10, 0), 0); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
public function testEnd() |
39
|
|
|
{ |
40
|
|
|
$currentTime = 1498136062; |
41
|
|
|
$startTime = 1498136000; |
42
|
|
|
|
43
|
|
|
$this->assertEquals(1498136682, Math::end(1000, 100, $startTime, $currentTime)); |
44
|
|
|
$this->assertEquals(0, Math::end(1000, 0, $startTime, $currentTime)); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @param array $data |
49
|
|
|
* @param int|float $result |
50
|
|
|
* @dataProvider data |
51
|
|
|
*/ |
52
|
|
|
public function testMedian(array $data, $result) |
53
|
|
|
{ |
54
|
|
|
$this->assertEquals($result['median'], Math::median($data)); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @param array $data |
59
|
|
|
* @param int|float $result |
60
|
|
|
* @dataProvider data |
61
|
|
|
*/ |
62
|
|
|
public function testAverage(array $data, $result) |
63
|
|
|
{ |
64
|
|
|
$this->assertEquals($result['avg'], Math::average($data)); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
public function data() |
68
|
|
|
{ |
69
|
|
|
return [ |
70
|
|
|
[ |
71
|
|
|
[1,1,1,1,2,2,3,1,100,5,3,7,2,3,62,8,9,45,3,2,45,6,7,8,99,6,64,3,2,22,1,1], |
72
|
|
|
[ |
73
|
|
|
'avg' => 16.40625, |
74
|
|
|
'median' => 3, |
75
|
|
|
] |
76
|
|
|
], |
77
|
|
|
[ |
78
|
|
|
[1,2,2,3,1,100,5,3,7,2,3,62,8,9,45,3,2,45,6,7,8,99,6,64,3,2,22,1,1], |
79
|
|
|
[ |
80
|
|
|
'avg' => 18, |
81
|
|
|
'median' => 5, |
82
|
|
|
] |
83
|
|
|
] |
84
|
|
|
]; |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|