Completed
Pull Request — master (#178)
by Cláudio
03:52 queued 21s
created

AbstractDivMod::divFloatResultDataProvider()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

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