1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace SKien\Test\PNServer; |
5
|
|
|
|
6
|
|
|
use PHPUnit\Framework\TestCase; |
7
|
|
|
use SKien\PNServer\Utils\Math; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* @author Stefanius <[email protected]> |
11
|
|
|
* @copyright MIT License - see the LICENSE file for details |
12
|
|
|
*/ |
13
|
|
|
class UtilsMathTest extends TestCase |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* @dataProvider cmpProvider |
17
|
|
|
*/ |
18
|
|
|
public function test_cmp(\GMP $a, \GMP $b, int $expected) : void |
19
|
|
|
{ |
20
|
|
|
$this->assertEquals($expected, Math::cmp($a, $b)); |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
public function cmpProvider() : array |
24
|
|
|
{ |
25
|
|
|
return [ |
26
|
|
|
[gmp_init(2), gmp_init(2), 0], |
27
|
|
|
[gmp_init(3), gmp_init(2), 1], |
28
|
|
|
[gmp_init(2), gmp_init(3), -1], |
29
|
|
|
[gmp_init(5), gmp_init(0), 1], |
30
|
|
|
[gmp_init(0), gmp_init(3), -1], |
31
|
|
|
[gmp_init(-2), gmp_init(2), -2], |
32
|
|
|
[gmp_init(2), gmp_init(-2), 2], |
33
|
|
|
[gmp_init(-5), gmp_init(2), -2], |
34
|
|
|
[gmp_init(5), gmp_init(-2), 2] |
35
|
|
|
]; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @dataProvider equalsProvider |
40
|
|
|
*/ |
41
|
|
|
public function test_equals(\GMP $a, \GMP $b, bool $expected) : void |
42
|
|
|
{ |
43
|
|
|
$this->assertEquals($expected, Math::equals($a, $b)); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
public function equalsProvider() : array |
47
|
|
|
{ |
48
|
|
|
return [ |
49
|
|
|
'2 == 2' => [gmp_init(2), gmp_init(2), true], |
50
|
|
|
'2 == 3' => [gmp_init(2), gmp_init(3), false], |
51
|
|
|
'2 == -2' => [gmp_init(2), gmp_init(-2), false] |
52
|
|
|
]; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
public function test_mod() : void |
56
|
|
|
{ |
57
|
|
|
$this->assertEquals((int) Math::mod(gmp_init(7), gmp_init(2)), 1); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
public function test_add() : void |
61
|
|
|
{ |
62
|
|
|
$this->assertEquals((int) Math::add(gmp_init(2), gmp_init(3)), 5); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
public function test_sub() : void |
66
|
|
|
{ |
67
|
|
|
$this->assertEquals((int) Math::sub(gmp_init(2), gmp_init(3)), -1); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @dataProvider mulProvider |
72
|
|
|
*/ |
73
|
|
|
public function test_mul(\GMP $a, \GMP $b, int $expected) : void |
74
|
|
|
{ |
75
|
|
|
$this->assertEquals($expected, (int) Math::mul($a, $b)); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
public function mulProvider() : array |
79
|
|
|
{ |
80
|
|
|
return [ |
81
|
|
|
[gmp_init(5), gmp_init(11), 55], |
82
|
|
|
[gmp_init(-5), gmp_init(-3), 15], |
83
|
|
|
[gmp_init(4), gmp_init(-15), -60], |
84
|
|
|
[gmp_init(-4), gmp_init(15), -60] |
85
|
|
|
]; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @dataProvider powProvider |
90
|
|
|
*/ |
91
|
|
|
public function test_pow(\GMP $a, int $b, int $expected) : void |
92
|
|
|
{ |
93
|
|
|
$this->assertEquals($expected, (int) Math::pow($a, $b)); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
public function powProvider() : array |
97
|
|
|
{ |
98
|
|
|
return [ |
99
|
|
|
[gmp_init(5), 2, 25], |
100
|
|
|
[gmp_init(-5), 2, 25], |
101
|
|
|
[gmp_init(2), 3, 8], |
102
|
|
|
[gmp_init(-2), 3, -8] |
103
|
|
|
]; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
public function test_bitwiseAnd() : void |
107
|
|
|
{ |
108
|
|
|
$this->assertEquals((int) Math::bitwiseAnd(gmp_init('110011', 2), gmp_init('011110', 2)), (int)gmp_init('010010', 2)); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
public function test_bitwiseXor() : void |
112
|
|
|
{ |
113
|
|
|
$this->assertEquals((int) Math::bitwiseXor(gmp_init('110011', 2), gmp_init('011110', 2)), (int)gmp_init('101101', 2)); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
public function test_rightShift() : void |
117
|
|
|
{ |
118
|
|
|
$this->assertEquals((int) Math::rightShift(gmp_init('110011', 2), 3), (int)gmp_init('110', 2)); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
public function test_toString() : void |
122
|
|
|
{ |
123
|
|
|
$this->assertEquals(Math::toString(gmp_init('100', 16)), '256'); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
public function test_baseConvert() : void |
127
|
|
|
{ |
128
|
|
|
$this->assertEquals(Math::baseConvert('0x1B', 16, 2), '11011'); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
public function test_inverseMod() : void |
132
|
|
|
{ |
133
|
|
|
$this->assertEquals(9, (int) Math::inverseMod(gmp_init(5), gmp_init(11))); |
134
|
|
|
$this->assertEquals(11, (int) Math::inverseMod(gmp_init(-4), gmp_init(15))); |
135
|
|
|
$this->assertFalse(Math::inverseMod(gmp_init(5), gmp_init(10))); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
public function test_modSub() : void |
139
|
|
|
{ |
140
|
|
|
$this->assertEquals((int) Math::modSub(gmp_init(12), gmp_init(4), gmp_init(5)), 3); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
public function test_modMul() : void |
144
|
|
|
{ |
145
|
|
|
$this->assertEquals((int) Math::modMul(gmp_init(3), gmp_init(4), gmp_init(5)), 2); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
public function test_modDiv() : void |
149
|
|
|
{ |
150
|
|
|
$this->assertEquals((int) Math::modDiv(gmp_init(12), gmp_init(2), gmp_init(5)), 36); |
151
|
|
|
} |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
|