MatLabPHPTest   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 331
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 13
lcom 1
cbo 1
dl 0
loc 331
rs 10
c 0
b 0
f 0

13 Methods

Rating   Name   Duplication   Size   Complexity  
A assertMatLabPHP() 0 4 1
B testStringToVector() 0 25 1
A testEye() 0 48 1
B testZeros() 0 43 1
B testLength() 0 43 1
B testSum() 0 41 1
A testPrice2Ret() 0 16 1
A testMean() 0 18 1
A testStd() 0 20 1
A testVariance() 0 12 1
A testCovariance() 0 13 1
A testCorrelation() 0 21 1
A getMatLabObject() 0 7 1
1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 7 and the first side effect is on line 3.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
3
require __DIR__ . '/../src/MatLabPHP.php';
4
5
use MatLabPHP\MatLabPHP;
6
7
class MatLabPHPTest extends \PHPUnit_Framework_TestCase
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
8
{
9
    /**
10
     * Asserts that a variable is of a Stringy instance.
11
     *
12
     * @param mixed $actual
13
     */
14
    public function assertMatLabPHP($actual)
15
    {
16
        $this->assertInstanceOf('MatLabPHP\MatLabPHP', $actual);
17
    }
18
19
    public function testStringToVector()
20
    {
21
        $matLab = $this->getMatLabObject();
22
23
        $result   = $matLab->stringToVector("[3 1 2; 5 4 7; 6 9 7]");
24
        $expected = array(
25
            0 => array(
26
                0 => '3',
27
                1 => '1',
28
                2 => '2'
29
            ),
30
            1 => array(
31
                0 => '5',
32
                1 => '4',
33
                2 => '7'
34
            ),
35
            2 => array(
36
                0 => '6',
37
                1 => '9',
38
                2 => '7'
39
            )
40
        );
41
42
        $this->assertEquals($result, $expected);
43
    }
44
45
    public function testEye()
46
    {
47
        $matLab = $this->getMatLabObject();
48
49
        $result   = $matLab->eye(4);
50
        $expected = array(
51
            1 => array(
52
                '1' => '1',
53
                '2' => '0',
54
                '3' => '0',
55
                '4' => '0'
56
            ),
57
            2 => array(
58
                '1' => '0',
59
                '2' => '1',
60
                '3' => '0',
61
                '4' => '0'
62
            ),
63
            3 => array(
64
                '1' => '0',
65
                '2' => '0',
66
                '3' => '1',
67
                '4' => '0'
68
            ),
69
            4 => array(
70
                '1' => '0',
71
                '2' => '0',
72
                '3' => '0',
73
                '4' => '1'
74
            )
75
        );
76
        $this->assertEquals($result, $expected);
77
78
        $result   = $matLab->eye(2, 3);
79
        $expected = array(
80
            1 => array(
81
                '1' => '1',
82
                '2' => '0',
83
                '3' => '0'
84
            ),
85
            2 => array(
86
                '1' => '0',
87
                '2' => '1',
88
                '3' => '0'
89
            )
90
        );
91
        $this->assertEquals($result, $expected);
92
    }
93
94
    public function testZeros()
95
    {
96
        $matLab = $this->getMatLabObject();
97
98
        $result   = $matLab->zeros(2, 1);
99
        $expected = array(
100
            1 => array(
101
                '1' => '0',
102
            ),
103
            2 => array(
104
                '1' => '0',
105
            )
106
        );
107
        $this->assertEquals($result, $expected);
108
109
        $result   = $matLab->zeros(2);
110
        $expected = array(
111
            1 => array(
112
                '1' => '0',
113
                '2' => '0',
114
            ),
115
            2 => array(
116
                '1' => '0',
117
                '2' => '0',
118
            )
119
        );
120
        $this->assertEquals($result, $expected);
121
122
        $result   = $matLab->zeros(2, 3);
123
        $expected = array(
124
            1 => array(
125
                '1' => '0',
126
                '2' => '0',
127
                '3' => '0'
128
            ),
129
            2 => array(
130
                '1' => '0',
131
                '2' => '0',
132
                '3' => '0'
133
            )
134
        );
135
        $this->assertEquals($result, $expected);
136
    }
137
138
    public function testLength()
139
    {
140
        $matLab = $this->getMatLabObject();
141
142
        $vector = array(
143
                '1' => '0',
144
                '2' => '4',
145
                '3' => '9',
146
                '4' => '10',
147
                '5' => '1',
148
                '6' => '2'
149
            );
150
151
        $result = $matLab->length($vector);
152
        $this->assertEquals($result, 6);
153
154
        $matrix = array(
155
            1 => array(
156
                '1' => '0',
157
                '2' => '0'
158
            ),
159
            2 => array(
160
                '1' => '0',
161
                '2' => '0',
162
                '3' => '0',
163
                '4' => '8'
164
            ),
165
            3 => array(
166
                '1' => '0',
167
                '2' => '0',
168
                '3' => '0'
169
            ),
170
            4 => array(
171
                '1' => '0',
172
                '2' => '0',
173
                '3' => '9',
174
                '4' => '1'
175
            )
176
        );
177
178
        $result = $matLab->length($matrix);
179
        $this->assertEquals($result, 4);
180
    }
181
182
    public function testSum()
183
    {
184
        $matLab = $this->getMatLabObject();
185
186
        $expected = array(
187
            0 => array(
188
                0 => 13
189
            )
190
        );
191
192
        $result = $matLab->sum('5', '8');
193
        $this->assertEquals($result, $expected);
194
195
        $matLab = new MatLabPHP();
196
        $this->assertMatLabPHP($matLab);
197
198
        $expected = array(
199
            0 => array(
200
                0 => 9,
201
                1 => 4,
202
                2 => 6
203
            ),
204
            1 => array(
205
                0 => 12,
206
                1 => 7,
207
                2 => 6
208
            ),
209
            2 => array(
210
                0 => 15,
211
                1 => 3,
212
                2 => 7
213
            )
214
        );
215
216
        $result = $matLab->sum(
217
            '[1 3 2; 4 2 5; 6 1 4]',
218
            '[8 1 4; 8 5 1; 9 2 3]'
219
        );
220
221
        $this->assertEquals($result, $expected);
222
    }
223
224
    public function testPrice2Ret()
225
    {
226
        $matLab = $this->getMatLabObject();
227
228
        $seriesPriceOne = array(10, 12, 13, 9, 11, 9);
229
        $result         = $matLab->price2ret($seriesPriceOne);
230
231
        $expected = array(
232
            10 => null,
233
            12 => 0.18232155679395,
234
            13 => 0.080042707673536,
235
            9 =>  -0.20067069546215,
236
            11 => 0.20067069546215
237
        );
238
        $this->assertEquals($result, $expected);
239
    }
240
241
    public function testMean()
242
    {
243
        $matLabPHP = $this->getMatLabObject();
244
245
        $retFundoX = array(2, 1.5, -2.4, 1.5, 4, 1.2);
246
        $mediaRetX = $matLabPHP->mean($retFundoX);
247
        $this->assertEquals($mediaRetX, 1.3000000000);
248
249
        $mediaRetX = $matLabPHP->avg($retFundoX);
250
        $this->assertEquals($mediaRetX, 1.3000000000);
251
252
        $retFundoY = array(-1.2, 0.2, 1.3, 0, -2, 0.5);
253
        $mediaRetY = $matLabPHP->mean($retFundoY);
254
        $this->assertEquals($mediaRetY, -0.2000000000);
255
256
        $mediaRetY = $matLabPHP->avg($retFundoY);
257
        $this->assertEquals($mediaRetY, -0.2000000000);
258
    }
259
260
    public function testStd()
261
    {
262
        $matLabPHP = $this->getMatLabObject();
263
264
        $retFundoX = array(2, 1.5, -2.4, 1.5, 4, 1.2);
265
266
        $desvPadX  = $matLabPHP->std($retFundoX, true);
267
        $this->assertEquals($desvPadX, 2.0765355763);
268
269
        $desvPadX  = $matLabPHP->stdev($retFundoX, true);
270
        $this->assertEquals($desvPadX, 2.0765355763);
271
272
        $retFundoY = array(-1.2, 0.2, 1.3, 0, -2, 0.5);
273
274
        $desvPadY  = $matLabPHP->std($retFundoY, true);
275
        $this->assertEquals($desvPadY, 1.1983321743);
276
277
        $desvPadY  = $matLabPHP->stdev($retFundoY, true);
278
        $this->assertEquals($desvPadY, 1.1983321743);
279
    }
280
281
    public function testVariance()
282
    {
283
        $matLabPHP = $this->getMatLabObject();
284
285
        $retFundoX = array(2, 1.5, -2.4, 1.5, 4, 1.2);
286
        $result    = $matLabPHP->variance($retFundoX, true);
287
        $this->assertEquals($result, 4.3120000000);
288
289
        $retFundoY = array(-1.2, 0.2, 1.3, 0, -2, 0.5);
290
        $result    = $matLabPHP->variance($retFundoY, true);
291
        $this->assertEquals($result, 1.4360000000);
292
    }
293
294
    public function testCovariance()
295
    {
296
        $matLabPHP = $this->getMatLabObject();
297
298
        $retFundoX = array(2, 1.5, -2.4, 1.5, 4, 1.2);
299
        $retFundoY = array(-1.2, 0.2, 1.3, 0, -2, 0.5);
300
301
        $result    = $matLabPHP->covariance($retFundoX, $retFundoY);
302
        $this->assertEquals($result, -1.8433333333);
303
304
        $result    = $matLabPHP->covar($retFundoX, $retFundoY);
305
        $this->assertEquals($result, -1.8433333333);
306
    }
307
308
    public function testCorrelation()
309
    {
310
        $matLabPHP = $this->getMatLabObject();
311
312
        $retFundoX = array(2, 1.5, -2.4, 1.5, 4, 1.2);
313
        $retFundoY = array(-1.2, 0.2, 1.3, 0, -2, 0.5);
314
315
        $isSample  = false;
316
        $result    = $matLabPHP->correlation($retFundoX, $retFundoY, $isSample);
317
        $this->assertEquals($result, -0.88893197);
318
319
        $result = $matLabPHP->correl($retFundoX, $retFundoY, $isSample);
320
        $this->assertEquals($result, -0.88893197);
321
322
        $isSample  = true;
323
        $result    = $matLabPHP->correlation($retFundoX, $retFundoY, $isSample);
324
        $this->assertEquals($result, -0.74077664);
325
326
        $result = $matLabPHP->correl($retFundoX, $retFundoY, $isSample);
327
        $this->assertEquals($result, -0.74077664);
328
    }
329
330
    public function getMatLabObject()
331
    {
332
        $matLab = new MatLabPHP();
333
        $this->assertMatLabPHP($matLab);
334
335
        return $matLab;
336
    }
337
}
338