Passed
Pull Request — develop (#31)
by Glynn
02:43
created

NumberFunctionTest   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 177
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 84
dl 0
loc 177
rs 10
c 0
b 0
f 0
wmc 16
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([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([['0'], false]);
133
        // Throws InvalidArgumentException.
134
    }
135
136
    public function testCanDivideByAndInto()
137
    {
138
        $divideBy2 = Num\divideBy(2);
139
        $divideInto2 = Num\divideInto(2);
140
141
142
        $a = 10;
143
        $b = 10;
144
145
        $a = $divideBy2($a);
146
        $b = $divideInto2($b);
147
148
        $this->assertEquals(5, $a);
149
        $this->assertEquals(0.2, $b);
150
151
        $this->assertEquals(2.5, $divideBy2($a));
152
        $this->assertEquals(10, $divideInto2($b));
153
    }
154
155
156
157
    public function testCanRemainderByAndInto()
158
    {
159
        $remainderBy2 = Num\remainderBy(2);
160
        $this->assertEquals(0, $remainderBy2(10)); // 10 / 2 = 5
161
        $this->assertEquals(1, $remainderBy2(9)); // 9 / 2 = 4.5
162
163
164
        $remainderInto2 = Num\remainderInto(2);
165
        $this->assertEquals(2, $remainderInto2(10)); // 2 / 10 = 0.2
166
    }
167
168
    public function testCanRoundFloatsAndInts()
169
    {
170
        $twoDecimalPlaces = Num\round(2);
171
        $eightDecimalPlaces = Num\round(8);
172
173
        $this->assertEquals(8.12, $twoDecimalPlaces(8.123456));
174
        $this->assertEquals(8.12345678, $eightDecimalPlaces(8.123456781));
175
        $this->assertEquals(50, $eightDecimalPlaces(50));
176
        $this->assertEquals(1, $twoDecimalPlaces(1));
177
    }
178
179
180
    public function testRoundThrowsNoneNumErrorA()
181
    {
182
        $this->expectException(InvalidArgumentException::class);
183
        $rounder = Num\round(['HELLO', 'NOT A NUMBER']);
184
        // Throws InvalidArgumentException.
185
    }
186
187
188
    public function testRoundThrowsNoneNumErrorB()
189
    {
190
        $this->expectException(InvalidArgumentException::class);
191
        $rounder = Num\round(5);
192
        $rounder('STRINGS');
193
        // Throws InvalidArgumentException.
194
    }
195
}
196