1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of the Cubiche package. |
5
|
|
|
* |
6
|
|
|
* Copyright (c) Cubiche |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
namespace Cubiche\Domain\System\Tests\Units; |
12
|
|
|
|
13
|
|
|
use Cubiche\Domain\System\Real; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* RealTests class. |
17
|
|
|
* |
18
|
|
|
* @author Ivannis Suárez Jerez <[email protected]> |
19
|
|
|
*/ |
20
|
|
|
class RealTests extends RealTestCase |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* {@inheritdoc} |
24
|
|
|
*/ |
25
|
|
|
protected function positiveInfiniteNativeNumber() |
26
|
|
|
{ |
27
|
|
|
return INF; |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* {@inheritdoc} |
32
|
|
|
*/ |
33
|
|
|
protected function negativeInfiniteNativeNumber() |
34
|
|
|
{ |
35
|
|
|
return -INF; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* {@inheritdoc} |
40
|
|
|
*/ |
41
|
|
|
protected function randomNativeNumber() |
42
|
|
|
{ |
43
|
|
|
return (rand(1, 10) / 10) + 1; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* {@inheritdoc} |
48
|
|
|
*/ |
49
|
|
|
protected function uniqueNativeNumber() |
50
|
|
|
{ |
51
|
|
|
return 20.32; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* {@inheritdoc} |
56
|
|
|
*/ |
57
|
|
|
protected function negativeNativeNumber() |
58
|
|
|
{ |
59
|
|
|
return (rand(-1, -10) / 10) - 1; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* {@inheritdoc} |
64
|
|
|
*/ |
65
|
|
|
protected function invalidNativeNumber() |
66
|
|
|
{ |
67
|
|
|
return NAN; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* {@inheritdoc} |
72
|
|
|
*/ |
73
|
|
|
protected function fromNative($value) |
74
|
|
|
{ |
75
|
|
|
return Real::fromNative($value); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/* |
79
|
|
|
* Test add. |
80
|
|
|
*/ |
81
|
|
View Code Duplication |
public function testAdd() |
|
|
|
|
82
|
|
|
{ |
83
|
|
|
parent::testAdd(); |
84
|
|
|
|
85
|
|
|
$this |
86
|
|
|
->given( |
87
|
|
|
$a = $this->fromNative($this->randomNativeNumber()), |
88
|
|
|
$b = $this->fromNative($this->randomNativeNumber()) |
89
|
|
|
) |
90
|
|
|
->when($c = $a->add($b->toInteger())) |
91
|
|
|
->then |
92
|
|
|
->object($c) |
93
|
|
|
->isInstanceOf(Real::class) |
94
|
|
|
; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/* |
98
|
|
|
* Test sub. |
99
|
|
|
*/ |
100
|
|
View Code Duplication |
public function testSub() |
|
|
|
|
101
|
|
|
{ |
102
|
|
|
parent::testSub(); |
103
|
|
|
|
104
|
|
|
$this |
105
|
|
|
->given( |
106
|
|
|
$a = $this->fromNative($this->randomNativeNumber()), |
107
|
|
|
$b = $this->fromNative($this->randomNativeNumber()) |
108
|
|
|
) |
109
|
|
|
->when($c = $a->sub($b->toInteger())) |
110
|
|
|
->then |
111
|
|
|
->object($c) |
112
|
|
|
->isInstanceOf(Real::class) |
113
|
|
|
; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/* |
117
|
|
|
* Test mult. |
118
|
|
|
*/ |
119
|
|
View Code Duplication |
public function testMult() |
|
|
|
|
120
|
|
|
{ |
121
|
|
|
parent::testMult(); |
122
|
|
|
|
123
|
|
|
$this |
124
|
|
|
->given( |
125
|
|
|
$a = $this->fromNative($this->randomNativeNumber()), |
126
|
|
|
$b = $this->fromNative($this->randomNativeNumber()) |
127
|
|
|
) |
128
|
|
|
->when($c = $a->mult($b->toInteger())) |
129
|
|
|
->then |
130
|
|
|
->object($c) |
131
|
|
|
->isInstanceOf(Real::class) |
132
|
|
|
; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/* |
136
|
|
|
* Test div. |
137
|
|
|
*/ |
138
|
|
View Code Duplication |
public function testDiv() |
|
|
|
|
139
|
|
|
{ |
140
|
|
|
parent::testDiv(); |
141
|
|
|
|
142
|
|
|
$this |
143
|
|
|
->given( |
144
|
|
|
$a = $this->fromNative($this->randomNativeNumber()), |
145
|
|
|
$b = $this->fromNative($this->randomNativeNumber()) |
146
|
|
|
) |
147
|
|
|
->when($c = $a->div($b->toInteger())) |
148
|
|
|
->then |
149
|
|
|
->object($c) |
150
|
|
|
->isInstanceOf(Real::class) |
151
|
|
|
; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/* |
155
|
|
|
* Test sqrt. |
156
|
|
|
*/ |
157
|
|
|
public function testSqrt() |
158
|
|
|
{ |
159
|
|
|
$this |
160
|
|
|
->given( |
161
|
|
|
$native = $this->randomNativeNumber(), |
162
|
|
|
$invalidScale = -1.34, |
163
|
|
|
$a = $this->fromNative($native) |
164
|
|
|
) |
165
|
|
|
->when( |
166
|
|
|
$b = $a->sqrt(), |
167
|
|
|
$c = $a->sqrt(2) |
168
|
|
|
) |
169
|
|
|
->then |
170
|
|
|
->object($b) |
171
|
|
|
->isInstanceOf(Real::class) |
172
|
|
|
->variable($b->toNative()) |
173
|
|
|
->isEqualTo(\sqrt($native)) |
174
|
|
|
->variable($c->toNative()) |
175
|
|
|
->isEqualTo(\bcsqrt($native, 2)) |
176
|
|
|
->exception(function () use ($a, $invalidScale) { |
177
|
|
|
$a->sqrt($invalidScale); |
178
|
|
|
})->isInstanceOf(\InvalidArgumentException::class) |
179
|
|
|
; |
180
|
|
|
} |
181
|
|
|
} |
182
|
|
|
|
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.