|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Tests\PHPSA\Compiler\Expression\Operators\Arithmetical; |
|
4
|
|
|
|
|
5
|
|
|
use PhpParser\Node; |
|
6
|
|
|
use PHPSA\CompiledExpression; |
|
7
|
|
|
use PHPSA\Compiler\Expression; |
|
8
|
|
|
|
|
9
|
|
|
class DivTest extends AbstractDivMod |
|
10
|
|
|
{ |
|
11
|
|
|
/** |
|
12
|
|
|
* @param $a |
|
13
|
|
|
* @param $b |
|
14
|
|
|
* @return float |
|
15
|
|
|
*/ |
|
16
|
|
|
protected function process($a, $b) |
|
17
|
|
|
{ |
|
18
|
|
|
return $a / $b; |
|
19
|
|
|
} |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @param Node\Scalar $a |
|
23
|
|
|
* @param Node\Scalar $b |
|
24
|
|
|
* @return Node\Expr\BinaryOp\Div |
|
25
|
|
|
*/ |
|
26
|
|
|
protected function buildExpression($a, $b) |
|
27
|
|
|
{ |
|
28
|
|
|
return new Node\Expr\BinaryOp\Div($a, $b); |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
protected function getAssertType() |
|
32
|
|
|
{ |
|
33
|
|
|
return CompiledExpression::DOUBLE; |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* Data provider for Div {int} / {int} = {int} |
|
38
|
|
|
* |
|
39
|
|
|
* @return array |
|
40
|
|
|
*/ |
|
41
|
|
|
public function divIntResultDataProvider() |
|
42
|
|
|
{ |
|
43
|
|
|
return array( |
|
44
|
|
|
array(-1, -1), |
|
45
|
|
|
array(-1, 1), |
|
46
|
|
|
array(1, 1), |
|
47
|
|
|
array(true, 1), |
|
48
|
|
|
array(true, true), |
|
49
|
|
|
array(1, true), |
|
50
|
|
|
array(0, 1), |
|
51
|
|
|
array(25, 25), |
|
52
|
|
|
array(50, 50), |
|
53
|
|
|
array(500, 50), |
|
54
|
|
|
array(5000, 50) |
|
55
|
|
|
); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* Tests {int} $operator {int} = {int} |
|
60
|
|
|
* |
|
61
|
|
|
* @dataProvider divIntResultDataProvider |
|
62
|
|
|
*/ |
|
63
|
|
|
public function testDivIntToIntWithIntResult($a, $b) |
|
64
|
|
|
{ |
|
65
|
|
|
$compiledExpression = $this->compileExpression( |
|
66
|
|
|
$this->buildExpression( |
|
67
|
|
|
$this->newScalarExpr($a), |
|
|
|
|
|
|
68
|
|
|
$this->newScalarExpr($b) |
|
|
|
|
|
|
69
|
|
|
) |
|
70
|
|
|
); |
|
71
|
|
|
|
|
72
|
|
|
$this->assertInstanceOfCompiledExpression($compiledExpression); |
|
73
|
|
|
$this->assertSame(CompiledExpression::INTEGER, $compiledExpression->getType()); |
|
74
|
|
|
$this->assertSame($this->process($a, $b), $compiledExpression->getValue()); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* Data provider for Div {int} / {double} = {double} |
|
79
|
|
|
* |
|
80
|
|
|
* @return array |
|
81
|
|
|
*/ |
|
82
|
|
|
public function divFloatResultDataProvider() |
|
83
|
|
|
{ |
|
84
|
|
|
return array( |
|
85
|
|
|
array(-1, -1.25, 0.8), |
|
86
|
|
|
array(-1, 1.25, -0.8), |
|
87
|
|
|
array(1, 1.25, 0.8), |
|
88
|
|
|
array(true, 1.25, 0.8), |
|
89
|
|
|
array(0, 1.25, 0.0), |
|
90
|
|
|
array(25, 12.5, 2.0), |
|
91
|
|
|
array(25, 6.25, 4.0), |
|
92
|
|
|
array(25, 3.125, 8.0), |
|
93
|
|
|
); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* Tests {int} $operator {double} = {double} |
|
98
|
|
|
* |
|
99
|
|
|
* @dataProvider divFloatResultDataProvider |
|
100
|
|
|
*/ |
|
101
|
|
|
public function testDivIntToDoubleWithDoubleResult($a, $b, $c) |
|
102
|
|
|
{ |
|
103
|
|
|
// $this->assertInternalType('int', $a); |
|
104
|
|
|
$this->assertInternalType('double', $b); |
|
105
|
|
|
$this->assertInternalType('double', $c); |
|
106
|
|
|
|
|
107
|
|
|
$compiledExpression = $this->compileExpression( |
|
108
|
|
|
$this->buildExpression( |
|
109
|
|
|
$this->newScalarExpr($a), |
|
|
|
|
|
|
110
|
|
|
$this->newScalarExpr($b) |
|
|
|
|
|
|
111
|
|
|
) |
|
112
|
|
|
); |
|
113
|
|
|
|
|
114
|
|
|
$this->assertInstanceOfCompiledExpression($compiledExpression); |
|
115
|
|
|
$this->assertSame(CompiledExpression::DOUBLE, $compiledExpression->getType()); |
|
116
|
|
|
$this->assertSame($this->process($a, $b), $compiledExpression->getValue()); |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
/** |
|
120
|
|
|
* Data provider for Div {double} / {double} = {double} |
|
121
|
|
|
* |
|
122
|
|
|
* @return array |
|
123
|
|
|
*/ |
|
124
|
|
|
public function divDoubleToDoubleResultDataProvider() |
|
125
|
|
|
{ |
|
126
|
|
|
return array( |
|
127
|
|
|
array(-1.25, -1, 1.25), |
|
128
|
|
|
array(1.25, -1, -1.25), |
|
129
|
|
|
array(1.25, 1, 1.25), |
|
130
|
|
|
array(1.25, true, 1.25), |
|
131
|
|
|
array(0.0, 1, 0.0), |
|
132
|
|
|
array(12.5, 25, 1/2), |
|
133
|
|
|
array(6.25, 25, 1/4), |
|
134
|
|
|
array(3.125, 25, 1/8), |
|
135
|
|
|
); |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
/** |
|
139
|
|
|
* Tests {double} $operator {int} = {double} |
|
140
|
|
|
* |
|
141
|
|
|
* @dataProvider divDoubleToDoubleResultDataProvider |
|
142
|
|
|
*/ |
|
143
|
|
|
public function testDivDoubleToIntWithDoubleResult($a, $b, $c) |
|
144
|
|
|
{ |
|
145
|
|
|
$this->assertInternalType('double', $a); |
|
146
|
|
|
// $this->assertInternalType('int', $b); |
|
147
|
|
|
$this->assertInternalType('double', $c); |
|
148
|
|
|
|
|
149
|
|
|
$compiledExpression = $this->compileExpression( |
|
150
|
|
|
$this->buildExpression( |
|
151
|
|
|
$this->newScalarExpr($a), |
|
|
|
|
|
|
152
|
|
|
$this->newScalarExpr($b) |
|
|
|
|
|
|
153
|
|
|
) |
|
154
|
|
|
); |
|
155
|
|
|
$this->assertInstanceOfCompiledExpression($compiledExpression); |
|
156
|
|
|
$this->assertSame(CompiledExpression::DOUBLE, $compiledExpression->getType()); |
|
157
|
|
|
$this->assertSame($this->process($a, $b), $compiledExpression->getValue()); |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
/** |
|
161
|
|
|
* Tests {double} $operator {double} = {double} |
|
162
|
|
|
* |
|
163
|
|
|
* @dataProvider divDoubleToDoubleResultDataProvider |
|
164
|
|
|
*/ |
|
165
|
|
|
public function testDivDoubleToDoubleWithDoubleResult($a, $b, $c) |
|
166
|
|
|
{ |
|
167
|
|
|
$b = (float) $b; |
|
168
|
|
|
|
|
169
|
|
|
$this->assertInternalType('double', $a); |
|
170
|
|
|
$this->assertInternalType('double', $b); |
|
171
|
|
|
$this->assertInternalType('double', $c); |
|
172
|
|
|
|
|
173
|
|
|
$compiledExpression = $this->compileExpression( |
|
174
|
|
|
$this->buildExpression( |
|
175
|
|
|
$this->newScalarExpr($a), |
|
|
|
|
|
|
176
|
|
|
$this->newScalarExpr($b) |
|
|
|
|
|
|
177
|
|
|
) |
|
178
|
|
|
); |
|
179
|
|
|
$this->assertInstanceOfCompiledExpression($compiledExpression); |
|
180
|
|
|
$this->assertSame(CompiledExpression::DOUBLE, $compiledExpression->getType()); |
|
181
|
|
|
$this->assertSame($this->process($a, $b), $compiledExpression->getValue()); |
|
182
|
|
|
} |
|
183
|
|
|
} |
|
184
|
|
|
|
This check looks at variables that are passed out again to other methods.
If the outgoing method call has stricter type requirements than the method itself, an issue is raised.
An additional type check may prevent trouble.