aboutsummaryrefslogtreecommitdiff
path: root/NXP/Tests/MathTest.php
blob: 6ab67ee7b0d8ed200459f63e6d682808ad3f6375 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
<?php
/**
 * Author: Alexander "NeonXP" Kiryukhin
 * Date: 14.03.13
 * Time: 3:41
 */
namespace NXP\Tests;

use \NXP\MathExecutor;

class MathTest extends \PHPUnit_Framework_TestCase {
    public function setup()
    {
        require '../MathExecutor.php';
    }
    public function testCalculating()
    {
        $calculator = new MathExecutor();
        for ($i = 1; $i <= 10; $i++) {
            $expression = $this->generateExpression();
            print "Test #$i. Expression: '$expression'\t";

            eval('$result1 = ' . $expression . ';');
            print "PHP result: $result1 \t";
            $result2 = $calculator->execute($expression);
            print "NXP Math Executor result: $result2\n";
            $this->assertEquals($result1, $result2);
        }
    }

    private function generateExpression()
    {
        $operators = [ '+', '-', '*', '/' ];
        $number = true;
        $expression = '';
        $brackets = 0;
        for ($i = 1; $i < rand(1,10)*2; $i++) {
            if ($number) {
                $expression .= rand(1,100)*0.5;
            } else {
                $expression .= $operators[rand(0,3)];
            }
            $number = !$number;
            $rand = rand(1,5);
            if (($rand == 1) && ($number)) {
                $expression .= '(';
                $brackets++;
            } elseif (($rand == 2) && (!$number) && ($brackets > 0)) {
                $expression .= ')';
                $brackets--;
            }
        }
        if ($number) {
            $expression .= rand(1,100)*0.5;
        }
        $expression .=  str_repeat(')', $brackets);

        return $expression;
    }
}