|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Tests for all number types |
|
5
|
|
|
* |
|
6
|
|
|
* @author Glynn Quelch <[email protected]> |
|
7
|
|
|
* @since 0.1.0 |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
declare(strict_types=1); |
|
11
|
|
|
|
|
12
|
|
|
use PHPUnit\Framework\TestCase; |
|
13
|
|
|
use PinkCrab\FunctionConstructors\Numbers as Num; |
|
14
|
|
|
use PinkCrab\FunctionConstructors\FunctionsLoader; |
|
15
|
|
|
|
|
16
|
|
|
class NumberFunctionTest extends TestCase |
|
17
|
|
|
{ |
|
18
|
|
|
public function setup(): void |
|
19
|
|
|
{ |
|
20
|
|
|
FunctionsLoader::include(); |
|
21
|
|
|
} |
|
22
|
|
|
|
|
23
|
|
|
public function testCanAccumulateInteger(): void |
|
24
|
|
|
{ |
|
25
|
|
|
$acc = Num\accumulatorInt(0); |
|
26
|
|
|
$acc = $acc(11); |
|
27
|
|
|
$this->assertEquals(11, $acc()); |
|
28
|
|
|
$acc = $acc(22); |
|
29
|
|
|
$this->assertEquals(33, $acc()); |
|
30
|
|
|
$acc = $acc(33); |
|
31
|
|
|
$this->assertEquals(66, $acc()); |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
public function testAccumlatorIntThrowsErrorForNoneIntTypes() |
|
35
|
|
|
{ |
|
36
|
|
|
$this->expectException(TypeError::class); |
|
37
|
|
|
$acc = Num\accumulatorInt(0); |
|
38
|
|
|
$acc = $acc('11'); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
public function testCanAccumulate(): void |
|
42
|
|
|
{ |
|
43
|
|
|
$acc = Num\accumulatorFloat(0); |
|
44
|
|
|
$acc = $acc(0.5); |
|
45
|
|
|
$this->assertEquals(0.5, $acc()); |
|
46
|
|
|
$acc = $acc(22.2); |
|
47
|
|
|
$this->assertEquals(22.7, $acc()); |
|
48
|
|
|
$acc = $acc(7.3); |
|
49
|
|
|
$this->assertEquals(30, $acc()); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
public function testAccumlatorFloatThrowsErrorForNoneFloatTypes() |
|
53
|
|
|
{ |
|
54
|
|
|
$this->expectException(TypeError::class); |
|
55
|
|
|
$acc = Num\accumulatorFloat(0); |
|
56
|
|
|
$acc = $acc(array( 1, 2, 3, 4, 5, 6 )); |
|
57
|
|
|
// Throws InvalidArgumentException. |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
public function testCanSum() |
|
61
|
|
|
{ |
|
62
|
|
|
$addsFive = Num\sum(5); |
|
63
|
|
|
$addsTwoAndAHalf = Num\sum(2.5); |
|
64
|
|
|
|
|
65
|
|
|
$a = 0; |
|
66
|
|
|
$b = 1.00; |
|
67
|
|
|
|
|
68
|
|
|
$a = $addsFive($a); |
|
69
|
|
|
$b = $addsTwoAndAHalf($b); |
|
70
|
|
|
|
|
71
|
|
|
$this->assertEquals(5, $a); |
|
72
|
|
|
$this->assertEquals(3.5, $b); |
|
73
|
|
|
|
|
74
|
|
|
$this->assertEquals(10, $addsFive($a)); |
|
75
|
|
|
$this->assertEquals(6, $addsTwoAndAHalf($b)); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
public function testSumThrowsNoneNumError() |
|
79
|
|
|
{ |
|
80
|
|
|
$this->expectException(InvalidArgumentException::class); |
|
81
|
|
|
$acc = Num\sum('0'); |
|
82
|
|
|
// Throws InvalidArgumentException. |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
public function testCanSub() |
|
86
|
|
|
{ |
|
87
|
|
|
$subsFive = Num\subtract(5); |
|
88
|
|
|
$subsTwoAndAHalf = Num\subtract(2.5); |
|
89
|
|
|
|
|
90
|
|
|
$a = 10; |
|
91
|
|
|
$b = 7.5; |
|
92
|
|
|
|
|
93
|
|
|
$a = $subsFive($a); |
|
94
|
|
|
$b = $subsTwoAndAHalf($b); |
|
95
|
|
|
|
|
96
|
|
|
$this->assertEquals(5, $a); |
|
97
|
|
|
$this->assertEquals(5.0, $b); |
|
98
|
|
|
|
|
99
|
|
|
$this->assertEquals(0, $subsFive($a)); |
|
100
|
|
|
$this->assertEquals(2.5, $subsTwoAndAHalf($b)); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
public function testSubThrowsNoneNumError() |
|
104
|
|
|
{ |
|
105
|
|
|
$this->expectException(InvalidArgumentException::class); |
|
106
|
|
|
$acc = Num\subtract('0'); |
|
107
|
|
|
// Throws InvalidArgumentException. |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
public function testCanMultiply() |
|
111
|
|
|
{ |
|
112
|
|
|
$subsFive = Num\multiply(5); |
|
113
|
|
|
$subsTwoAndAHalf = Num\multiply(2.5); |
|
114
|
|
|
|
|
115
|
|
|
$a = 1; |
|
116
|
|
|
$b = 1; |
|
117
|
|
|
|
|
118
|
|
|
$a = $subsFive($a); |
|
119
|
|
|
$b = $subsTwoAndAHalf($b); |
|
120
|
|
|
|
|
121
|
|
|
$this->assertEquals(5, $a); |
|
122
|
|
|
$this->assertEquals(2.5, $b); |
|
123
|
|
|
|
|
124
|
|
|
$this->assertEquals(25, $subsFive($a)); |
|
125
|
|
|
$this->assertEquals(6.25, $subsTwoAndAHalf($b)); |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
|
|
129
|
|
|
public function testMultiplyThrowsNoneNumError() |
|
130
|
|
|
{ |
|
131
|
|
|
$this->expectException(InvalidArgumentException::class); |
|
132
|
|
|
$acc = Num\multiply(array( array( '0' ), false )); |
|
133
|
|
|
// Throws InvalidArgumentException. |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
public function testCanDivideByAndInto() |
|
137
|
|
|
{ |
|
138
|
|
|
$divideBy2 = Num\divideBy(2); |
|
139
|
|
|
$divideInto2 = Num\divideInto(2); |
|
140
|
|
|
|
|
141
|
|
|
$a = 10; |
|
142
|
|
|
$b = 10; |
|
143
|
|
|
|
|
144
|
|
|
$a = $divideBy2($a); |
|
145
|
|
|
$b = $divideInto2($b); |
|
146
|
|
|
|
|
147
|
|
|
$this->assertEquals(5, $a); |
|
148
|
|
|
$this->assertEquals(0.2, $b); |
|
149
|
|
|
|
|
150
|
|
|
$this->assertEquals(2.5, $divideBy2($a)); |
|
151
|
|
|
$this->assertEquals(10, $divideInto2($b)); |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
|
|
155
|
|
|
|
|
156
|
|
|
public function testCanRemainderByAndInto() |
|
157
|
|
|
{ |
|
158
|
|
|
$remainderBy2 = Num\remainderBy(2); |
|
159
|
|
|
$this->assertEquals(0, $remainderBy2(10)); // 10 / 2 = 5 |
|
160
|
|
|
$this->assertEquals(1, $remainderBy2(9)); // 9 / 2 = 4.5 |
|
161
|
|
|
|
|
162
|
|
|
$remainderInto2 = Num\remainderInto(2); |
|
163
|
|
|
$this->assertEquals(2, $remainderInto2(10)); // 2 / 10 = 0.2 |
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
|
|
public function testCanRoundFloatsAndInts() |
|
167
|
|
|
{ |
|
168
|
|
|
$twoDecimalPlaces = Num\round(2); |
|
169
|
|
|
$eightDecimalPlaces = Num\round(8); |
|
170
|
|
|
|
|
171
|
|
|
$this->assertEquals(8.12, $twoDecimalPlaces(8.123456)); |
|
172
|
|
|
$this->assertEquals(8.12345678, $eightDecimalPlaces(8.123456781)); |
|
173
|
|
|
$this->assertEquals(50, $eightDecimalPlaces(50)); |
|
174
|
|
|
$this->assertEquals(1, $twoDecimalPlaces(1)); |
|
175
|
|
|
} |
|
176
|
|
|
|
|
177
|
|
|
|
|
178
|
|
|
public function testRoundThrowsNoneNumErrorA() |
|
179
|
|
|
{ |
|
180
|
|
|
$this->expectException(InvalidArgumentException::class); |
|
181
|
|
|
$rounder = Num\round(array( 'HELLO', 'NOT A NUMBER' )); |
|
182
|
|
|
// Throws InvalidArgumentException. |
|
183
|
|
|
} |
|
184
|
|
|
|
|
185
|
|
|
|
|
186
|
|
|
public function testRoundThrowsNoneNumErrorB() |
|
187
|
|
|
{ |
|
188
|
|
|
$this->expectException(InvalidArgumentException::class); |
|
189
|
|
|
$rounder = Num\round(5); |
|
190
|
|
|
$rounder('STRINGS'); |
|
191
|
|
|
// Throws InvalidArgumentException. |
|
192
|
|
|
} |
|
193
|
|
|
|
|
194
|
|
|
/** @testdox It should be possible to check if a number is a factor of another number */ |
|
195
|
|
|
public function testIsFactorOf(): void |
|
196
|
|
|
{ |
|
197
|
|
|
$factorOf5 = Num\isFactorOf(5); |
|
198
|
|
|
|
|
199
|
|
|
// Is factors of 5 |
|
200
|
|
|
$this->assertTrue($factorOf5(10)); |
|
201
|
|
|
$this->assertTrue($factorOf5(15)); |
|
202
|
|
|
$this->assertTrue($factorOf5(20)); |
|
203
|
|
|
|
|
204
|
|
|
// Is not factors of 5 |
|
205
|
|
|
$this->assertFalse($factorOf5(4)); |
|
206
|
|
|
$this->assertFalse($factorOf5(6)); |
|
207
|
|
|
$this->assertFalse($factorOf5(7)); |
|
208
|
|
|
$this->assertFalse($factorOf5(0)); |
|
209
|
|
|
$this->assertFalse($factorOf5(-1)); |
|
210
|
|
|
} |
|
211
|
|
|
|
|
212
|
|
|
/** @testdox Attempting to use a none number (int or float) as the value for factor, should throw an error */ |
|
213
|
|
|
public function testIsFactorOfThrowsIfBaseNotNumber() |
|
214
|
|
|
{ |
|
215
|
|
|
$this->expectException(InvalidArgumentException::class); |
|
216
|
|
|
Num\isFactorOf('5'); |
|
217
|
|
|
} |
|
218
|
|
|
|
|
219
|
|
|
/** @testdox Attempting to use a none number (int or float) as the value for checked number, should throw an error */ |
|
220
|
|
|
public function testIsFactorOfThrowsIfCompNotNumber() |
|
221
|
|
|
{ |
|
222
|
|
|
$this->expectException(InvalidArgumentException::class); |
|
223
|
|
|
$r = Num\isFactorOf(5); |
|
224
|
|
|
$r('HELLO'); |
|
225
|
|
|
} |
|
226
|
|
|
} |
|
227
|
|
|
|