|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Tests\PHPSA\Expression\Operators\Arithmetical; |
|
4
|
|
|
|
|
5
|
|
|
use PhpParser\Node; |
|
6
|
|
|
use PHPSA\CompiledExpression; |
|
7
|
|
|
use PHPSA\Compiler\Expression; |
|
8
|
|
|
|
|
9
|
|
|
class PowTest extends \Tests\PHPSA\TestCase |
|
10
|
|
|
{ |
|
11
|
|
|
/** |
|
12
|
|
|
* Data provider for {expr} ** {expr} == {int} |
|
13
|
|
|
* |
|
14
|
|
|
* @return array |
|
15
|
|
|
*/ |
|
16
|
|
|
public function testPowResultIntDataProvider() |
|
17
|
|
|
{ |
|
18
|
|
|
return array( |
|
19
|
|
|
array(2, 2, 4), |
|
20
|
|
|
array(true, 2, 1), |
|
21
|
|
|
array(3, true, 3), |
|
22
|
|
|
array(true, true, 1), |
|
23
|
|
|
array(2, 0, 1), |
|
24
|
|
|
array(false, 3, 0), |
|
25
|
|
|
array(2, false, 1), |
|
26
|
|
|
array(false, false, 1), |
|
27
|
|
|
array(0, 0, 1), |
|
28
|
|
|
array(0, 3, 0), |
|
29
|
|
|
array(true, false, 1), |
|
30
|
|
|
); |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* Tests {expr} ** {expr} = {int} |
|
35
|
|
|
* |
|
36
|
|
|
* @dataProvider testPowResultIntDataProvider |
|
37
|
|
|
*/ |
|
38
|
|
|
public function testPowResultInt($a, $b, $c) |
|
39
|
|
|
{ |
|
40
|
|
|
|
|
41
|
|
|
$baseExpression = new Node\Expr\BinaryOp\Pow( |
|
42
|
|
|
$this->newScalarExpr($a), |
|
43
|
|
|
$this->newScalarExpr($b) |
|
44
|
|
|
); |
|
45
|
|
|
$compiledExpression = $this->compileExpression($baseExpression); |
|
46
|
|
|
|
|
47
|
|
|
$this->assertInstanceOfCompiledExpression($compiledExpression); |
|
48
|
|
|
$this->assertSame(CompiledExpression::INTEGER, $compiledExpression->getType()); |
|
49
|
|
|
$this->assertSame($c, $compiledExpression->getValue()); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* Data provider for {expr} ** {expr} == {double} |
|
54
|
|
|
* |
|
55
|
|
|
* @return array |
|
56
|
|
|
*/ |
|
57
|
|
|
public function testPowResultDoubleDataProvider() |
|
58
|
|
|
{ |
|
59
|
|
|
return array( |
|
60
|
|
|
array(2, -2, 0.25), |
|
61
|
|
|
array(1.5, 2, 2.25), |
|
62
|
|
|
array(1, 1.5, 1.0), |
|
63
|
|
|
array(100, 2.5, 100000.0), |
|
64
|
|
|
array(true, 1.5, 1.0), |
|
65
|
|
|
array(false, 1.5, 0.0), |
|
66
|
|
|
array(1.5, false, 1.0), |
|
67
|
|
|
array(1.5, true, 1.5), |
|
68
|
|
|
); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* Tests {expr} ** {expr} = {double} |
|
73
|
|
|
* |
|
74
|
|
|
* @dataProvider testPowResultDoubleDataProvider |
|
75
|
|
|
*/ |
|
76
|
|
|
public function testPowResultDouble($a, $b, $c) |
|
77
|
|
|
{ |
|
78
|
|
|
|
|
79
|
|
|
$baseExpression = new Node\Expr\BinaryOp\Pow( |
|
80
|
|
|
$this->newScalarExpr($a), |
|
81
|
|
|
$this->newScalarExpr($b) |
|
82
|
|
|
); |
|
83
|
|
|
$compiledExpression = $this->compileExpression($baseExpression); |
|
84
|
|
|
|
|
85
|
|
|
$this->assertInstanceOfCompiledExpression($compiledExpression); |
|
86
|
|
|
$this->assertSame(CompiledExpression::DOUBLE, $compiledExpression->getType()); |
|
87
|
|
|
$this->assertSame($c, $compiledExpression->getValue()); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* Tests {left-expr::UNKNOWN} ** {right-expr} |
|
92
|
|
|
*/ |
|
93
|
|
|
public function testFirstUnexpectedType() |
|
94
|
|
|
{ |
|
95
|
|
|
$baseExpression = new Node\Expr\BinaryOp\Pow( |
|
96
|
|
|
$this->newFakeScalarExpr(), |
|
97
|
|
|
$this->newScalarExpr(1) |
|
98
|
|
|
); |
|
99
|
|
|
$compiledExpression = $this->compileExpression($baseExpression); |
|
100
|
|
|
|
|
101
|
|
|
$this->assertInstanceOfCompiledExpression($compiledExpression); |
|
102
|
|
|
$this->assertSame(CompiledExpression::UNKNOWN, $compiledExpression->getType()); |
|
103
|
|
|
$this->assertSame(null, $compiledExpression->getValue()); |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* Tests {left-expr} ** {right-expr::UNKNOWN} |
|
108
|
|
|
*/ |
|
109
|
|
|
public function testSecondUnexpectedType() |
|
110
|
|
|
{ |
|
111
|
|
|
$baseExpression = new Node\Expr\BinaryOp\Pow( |
|
112
|
|
|
$this->newScalarExpr(1), |
|
113
|
|
|
$this->newFakeScalarExpr() |
|
114
|
|
|
); |
|
115
|
|
|
$compiledExpression = $this->compileExpression($baseExpression); |
|
116
|
|
|
|
|
117
|
|
|
$this->assertInstanceOfCompiledExpression($compiledExpression); |
|
118
|
|
|
$this->assertSame(CompiledExpression::UNKNOWN, $compiledExpression->getType()); |
|
119
|
|
|
$this->assertSame(null, $compiledExpression->getValue()); |
|
120
|
|
|
} |
|
121
|
|
|
} |
|
122
|
|
|
|