Completed
Pull Request — master (#124)
by Enrico
03:49
created

MulTest::testDoubleOnDoubleResultDataProvider()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 9
nc 1
nop 0
dl 0
loc 12
rs 9.4285
c 0
b 0
f 0
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 MulTest extends \Tests\PHPSA\TestCase
10
{
11
    /**
12
     * Data provider for Div {int} / {int} = {int}
13
     *
14
     * @return array
15
     */
16
    public function testDivIntResultDataProvider()
17
    {
18
        return array(
19
            array(-1, -1, 1),
20
            array(-1, 1, -1),
21
            array(1, 1, 1),
22
            array(true, 1, 1),
23
            array(true, true, 1),
24
            array(1, true, 1),
25
            array(0, 1, 0),
26
            array(25, 25, 25*25),
27
            array(50, 50, 50*50),
28
            array(500, 50, 25000),
29
            array(5000, 50, 250000)
30
        );
31
    }
32
33
    /**
34
     * Tests {int} / {int} = {int}
35
     *
36
     * @dataProvider testDivIntResultDataProvider
37
     */
38
    public function testDivIntToIntWithIntResult($a, $b, $c)
39
    {
40
//        $this->assertInternalType('int', $a);
41
//        $this->assertInternalType('int', $b);
42
        $this->assertInternalType('int', $c);
43
44
        $baseExpression = new Node\Expr\BinaryOp\Mul(
45
            $this->newScalarExpr($a),
46
            $this->newScalarExpr($b)
47
        );
48
        $compiledExpression = $this->compileExpression($baseExpression);
49
50
        $this->assertInstanceOfCompiledExpression($compiledExpression);
51
        $this->assertSame(CompiledExpression::INTEGER, $compiledExpression->getType());
52
        $this->assertSame($c, $compiledExpression->getValue());
53
    }
54
55
    /**
56
     * Data provider for {int} * {double} = {double}
57
     *
58
     * @return array
59
     */
60
    public function testFloatResultDataProvider()
61
    {
62
        return array(
63
            array(-1, -1.25, 1*1.25),
64
            array(-1, 1.25, -(1*1.25)),
65
            array(1, 1.25, 1*1.25),
66
            array(true, 1.25, 1.25),
67
            array(0, 1.25, 0.0),
68
            array(25, 12.5, 25*12.5),
69
            array(25, 6.25, 25*6.25),
70
            array(25, 3.125, 25*3.125),
71
        );
72
    }
73
74
    /**
75
     * Tests {int} * {double} = {double}
76
     *
77
     * @dataProvider testFloatResultDataProvider
78
     */
79
    public function testIntOnDoubleWithDoubleResult($a, $b, $c)
80
    {
81
//        $this->assertInternalType('int', $a);
82
        $this->assertInternalType('double', $b);
83
        $this->assertInternalType('double', $c);
84
85
        $baseExpression = new Node\Expr\BinaryOp\Mul(
86
            $this->newScalarExpr($a),
87
            $this->newScalarExpr($b)
88
        );
89
        $compiledExpression = $this->compileExpression($baseExpression);
90
91
        $this->assertInstanceOfCompiledExpression($compiledExpression);
92
        $this->assertSame(CompiledExpression::DOUBLE, $compiledExpression->getType());
93
        $this->assertSame($c, $compiledExpression->getValue());
94
    }
95
96
    /**
97
     * Data provider for {double} * {double} = {double}
98
     *
99
     * @return array
100
     */
101
    public function testDoubleOnDoubleResultDataProvider()
102
    {
103
        return array(
104
            array(-1.25, -1, 1.25),
105
            array(1.25, -1, -1.25),
106
            array(1.25, 1, 1.25),
107
            array(1.25, true, 1.25),
108
            array(12.5, 25, 312.5),
109
            array(6.25, 25, 156.25),
110
            array(3.125, 25, 78.125),
111
        );
112
    }
113
114
    /**
115
     * Tests {double} * {int} = {double}
116
     *
117
     * @dataProvider testDoubleOnDoubleResultDataProvider
118
     */
119
    public function testDoubleOnIntWithDoubleResult($a, $b, $c)
120
    {
121
        $this->assertInternalType('double', $a);
122
//        $this->assertInternalType('int', $b);
123
        $this->assertInternalType('double', $c);
124
125
        $baseExpression = new Node\Expr\BinaryOp\Mul(
126
            $this->newScalarExpr($a),
127
            $this->newScalarExpr($b)
128
        );
129
        $compiledExpression = $this->compileExpression($baseExpression);
130
131
        $this->assertInstanceOfCompiledExpression($compiledExpression);
132
        $this->assertSame(CompiledExpression::DOUBLE, $compiledExpression->getType());
133
        $this->assertSame($c, $compiledExpression->getValue());
134
    }
135
136
    /**
137
     * Tests {double} * {double} = {double}
138
     *
139
     * @dataProvider testDoubleOnDoubleResultDataProvider
140
     */
141
    public function testDoubleOnDoubleWithDoubleResult($a, $b, $c)
142
    {
143
        $b = (float) $b;
144
145
        $this->assertInternalType('double', $a);
146
        $this->assertInternalType('double', $b);
147
        $this->assertInternalType('double', $c);
148
149
        $baseExpression = new Node\Expr\BinaryOp\Mul(
150
            $this->newScalarExpr($a),
151
            $this->newScalarExpr($b)
152
        );
153
        $compiledExpression = $this->compileExpression($baseExpression);
154
155
        $this->assertInstanceOfCompiledExpression($compiledExpression);
156
        $this->assertSame(CompiledExpression::DOUBLE, $compiledExpression->getType());
157
        $this->assertSame((float) $c, $compiledExpression->getValue());
158
    }
159
160
    /**
161
     * Tests {left-expr::UNKNOWN} * {right-expr}
162
     */
163
    public function testFirstUnexpectedTypes()
164
    {
165
        $baseExpression = new Node\Expr\BinaryOp\Mul(
166
            $this->newFakeScalarExpr(),
167
            $this->newScalarExpr(1)
168
        );
169
        $compiledExpression = $this->compileExpression($baseExpression);
170
171
        $this->assertInstanceOfCompiledExpression($compiledExpression);
172
        $this->assertSame(CompiledExpression::UNKNOWN, $compiledExpression->getType());
173
        $this->assertSame(null, $compiledExpression->getValue());
174
    }
175
176
    /**
177
     * Tests {left-expr} * {right-expr::UNKNOWN}
178
     */
179
    public function testSecondUnexpectedTypes()
180
    {
181
        $baseExpression = new Node\Expr\BinaryOp\Mul(
182
            $this->newScalarExpr(1),
183
            $this->newFakeScalarExpr()
184
        );
185
        $compiledExpression = $this->compileExpression($baseExpression);
186
187
        $this->assertInstanceOfCompiledExpression($compiledExpression);
188
        $this->assertSame(CompiledExpression::UNKNOWN, $compiledExpression->getType());
189
        $this->assertSame(null, $compiledExpression->getValue());
190
    }
191
}
192