function CtoolsMathExpressionTestCase::testArithmetic

Test some arithmetic handling.

File

tests/math_expression.test, line 105

Class

CtoolsMathExpressionTestCase
Tests the MathExpression library of ctools.

Code

public function testArithmetic() {
    $math_expr = new ctools_math_expr();
    $this->assertEqual($math_expr->evaluate('2'), 2, 'Check Literal 2');
    $this->assertEqual($math_expr->e('2+1'), $math_expr->evaluate('2+1'), 'Check that e() and evaluate() are equivalent.');
    foreach (range(1, 4) as $n) {
        // Test constant expressions.
        $random_number = mt_rand(0, 20);
        $this->assertEqual($random_number, $math_expr->evaluate((string) $random_number), "Literal {$random_number}");
        // Test simple arithmetic.
        $number_a = mt_rand(-55, 777);
        $number_b = mt_rand(-555, 77);
        $this->assertEqual($number_a + $number_b, $math_expr->evaluate("{$number_a} + {$number_b}"), "Addition: {$number_a} + {$number_b}");
        $this->assertEqual($number_a - $number_b, $math_expr->evaluate("{$number_a} - {$number_b}"), "Subtraction: {$number_a} + {$number_b}");
        $this->assertFloat($number_a * $number_b, $math_expr->evaluate("{$number_a} * {$number_b}"), "Multiplication: {$number_a} * {$number_b} = " . $number_a * $number_b);
        $this->assertFloat($number_a / $number_b, $math_expr->evaluate("{$number_a} / {$number_b}"), "Division: {$number_a} / {$number_b} = " . $number_a / $number_b);
        // Test Associative property.
        $number_c = mt_rand(-99, 77);
        $this->assertEqual($math_expr->evaluate("{$number_a} + ({$number_b} + {$number_c})"), $math_expr->evaluate("({$number_a} + {$number_b}) + {$number_c}"), "Associative: {$number_a} + ({$number_b} + {$number_c})");
        $this->assertEqual($math_expr->evaluate("{$number_a} * ({$number_b} * {$number_c})"), $math_expr->evaluate("({$number_a} * {$number_b}) * {$number_c}"), "Associative: {$number_a} * ({$number_b} * {$number_c})");
        // Test Commutative property.
        $this->assertEqual($math_expr->evaluate("{$number_a} + {$number_b}"), $math_expr->evaluate("{$number_b} + {$number_a}"), "Commutative: {$number_a} + {$number_b}");
        $this->assertEqual($math_expr->evaluate("{$number_a} * {$number_b}"), $math_expr->evaluate("{$number_b} * {$number_a}"), "Commutative: {$number_a} * {$number_b}");
        // Test Distributive property.
        $this->assertEqual($math_expr->evaluate("({$number_a} + {$number_b}) * {$number_c}"), $math_expr->evaluate("({$number_a} * {$number_c} + {$number_b} * {$number_c})"), "Distributive: ({$number_a} + {$number_b}) * {$number_c}");
        // @todo: Doesn't work with zero or negative powers when number is zero or negative, e.g. 0^0, 0^-2, -2^0, -2^-2.
        $random_number = mt_rand(1, 15);
        $random_power = mt_rand(-15, 15);
        $this->assertFloat(pow($random_number, $random_power), $math_expr->evaluate("{$random_number} ^ {$random_power}"), "{$random_number} ^ {$random_power}");
        $this->assertFloat(pow($random_number, $random_power), $math_expr->evaluate("pow({$random_number}, {$random_power})"), "pow({$random_number}, {$random_power})");
    }
}