danielebarchiesi@0: 'CTools math expression tests', danielebarchiesi@0: 'description' => 'Test the math expression library of ctools.', danielebarchiesi@0: 'group' => 'Chaos Tools Suite', danielebarchiesi@0: ); danielebarchiesi@0: } danielebarchiesi@0: danielebarchiesi@0: public function setUp() { danielebarchiesi@0: parent::setUp('ctools', 'ctools_plugin_test'); danielebarchiesi@0: } danielebarchiesi@0: danielebarchiesi@0: /** danielebarchiesi@0: * Returns a random double between 0 and 1. danielebarchiesi@0: */ danielebarchiesi@0: protected function rand01() { danielebarchiesi@0: return rand(0, PHP_INT_MAX) / PHP_INT_MAX; danielebarchiesi@0: } danielebarchiesi@0: danielebarchiesi@0: /** danielebarchiesi@0: * A custom assertion with checks the values in a certain range. danielebarchiesi@0: */ danielebarchiesi@0: protected function assertFloat($first, $second, $delta = 0.0000001, $message = '', $group = 'Other') { danielebarchiesi@0: return $this->assert(abs($first - $second) <= $delta, $message ? $message : t('Value @first is equal to value @second.', array('@first' => var_export($first, TRUE), '@second' => var_export($second, TRUE))), $group); danielebarchiesi@0: } danielebarchiesi@0: danielebarchiesi@0: public function testArithmetic() { danielebarchiesi@0: $math_expression = new ctools_math_expr(); danielebarchiesi@0: danielebarchiesi@0: // Test constant expressions. danielebarchiesi@0: $this->assertEqual($math_expression->e('2'), 2); danielebarchiesi@0: $random_number = rand(0, 10); danielebarchiesi@0: $this->assertEqual($random_number, $math_expression->e((string) $random_number)); danielebarchiesi@0: danielebarchiesi@0: // Test simple arithmetic. danielebarchiesi@0: $random_number_a = rand(5, 10); danielebarchiesi@0: $random_number_b = rand(5, 10); danielebarchiesi@0: $this->assertEqual($random_number_a + $random_number_b, $math_expression->e("$random_number_a + $random_number_b")); danielebarchiesi@0: $this->assertEqual($random_number_a - $random_number_b, $math_expression->e("$random_number_a - $random_number_b")); danielebarchiesi@0: $this->assertEqual($random_number_a * $random_number_b, $math_expression->e("$random_number_a * $random_number_b")); danielebarchiesi@0: $this->assertEqual($random_number_a / $random_number_b, $math_expression->e("$random_number_a / $random_number_b")); danielebarchiesi@0: danielebarchiesi@0: // Test Associative property. danielebarchiesi@0: $random_number_c = rand(5, 10); danielebarchiesi@0: $this->assertEqual($math_expression->e("$random_number_a + ($random_number_b + $random_number_c)"), $math_expression->e("($random_number_a + $random_number_b) + $random_number_c")); danielebarchiesi@0: $this->assertEqual($math_expression->e("$random_number_a * ($random_number_b * $random_number_c)"), $math_expression->e("($random_number_a * $random_number_b) * $random_number_c")); danielebarchiesi@0: danielebarchiesi@0: // Test Commutative property. danielebarchiesi@0: $this->assertEqual($math_expression->e("$random_number_a + $random_number_b"), $math_expression->e("$random_number_b + $random_number_a")); danielebarchiesi@0: $this->assertEqual($math_expression->e("$random_number_a * $random_number_b"), $math_expression->e("$random_number_b * $random_number_a")); danielebarchiesi@0: danielebarchiesi@0: // Test Distributive property. danielebarchiesi@0: $this->assertEqual($math_expression->e("($random_number_a + $random_number_b) * $random_number_c"), $math_expression->e("($random_number_a * $random_number_c + $random_number_b * $random_number_c)")); danielebarchiesi@0: danielebarchiesi@0: $this->assertEqual(pow($random_number_a, $random_number_b), $math_expression->e("$random_number_a ^ $random_number_b")); danielebarchiesi@0: } danielebarchiesi@0: danielebarchiesi@0: public function testBuildInFunctions() { danielebarchiesi@0: $math_expression = new ctools_math_expr(); danielebarchiesi@0: danielebarchiesi@0: // @todo: maybe run this code multiple times to test different values. danielebarchiesi@0: $random_double = $this->rand01(); danielebarchiesi@0: $random_int = rand(5, 10); danielebarchiesi@0: $this->assertFloat(sin($random_double), $math_expression->e("sin($random_double)")); danielebarchiesi@0: $this->assertFloat(cos($random_double), $math_expression->e("cos($random_double)")); danielebarchiesi@0: $this->assertFloat(tan($random_double), $math_expression->e("tan($random_double)")); danielebarchiesi@0: $this->assertFloat(exp($random_double), $math_expression->e("exp($random_double)")); danielebarchiesi@0: $this->assertFloat(sqrt($random_double), $math_expression->e("sqrt($random_double)")); danielebarchiesi@0: $this->assertFloat(log($random_double), $math_expression->e("ln($random_double)")); danielebarchiesi@0: $this->assertFloat(round($random_double), $math_expression->e("round($random_double)")); danielebarchiesi@0: $this->assertFloat(abs($random_double + $random_int), $math_expression->e('abs(' . ($random_double + $random_int) . ')')); danielebarchiesi@0: $this->assertEqual(round($random_double + $random_int), $math_expression->e('round(' . ($random_double + $random_int) . ')')); danielebarchiesi@0: $this->assertEqual(ceil($random_double + $random_int), $math_expression->e('ceil(' . ($random_double + $random_int) . ')')); danielebarchiesi@0: $this->assertEqual(floor($random_double + $random_int), $math_expression->e('floor(' . ($random_double + $random_int) . ')')); danielebarchiesi@0: danielebarchiesi@0: // @fixme: you can't run time without an argument. danielebarchiesi@0: $this->assertFloat(time(), $math_expression->e('time(1)')); danielebarchiesi@0: danielebarchiesi@0: $random_double_a = $this->rand01(); danielebarchiesi@0: $random_double_b = $this->rand01(); danielebarchiesi@0: // @fixme: max/min don't work at the moment. danielebarchiesi@0: // $this->assertFloat(max($random_double_a, $random_double_b), $math_expression->e("max($random_double_a, $random_double_b)")); danielebarchiesi@0: // $this->assertFloat(min($random_double_a, $random_double_b), $math_expression->e("min($random_double_a, $random_double_b)")); danielebarchiesi@0: } danielebarchiesi@0: danielebarchiesi@0: public function testVariables() { danielebarchiesi@0: $math_expression = new ctools_math_expr(); danielebarchiesi@0: danielebarchiesi@0: $random_number_a = rand(5, 10); danielebarchiesi@0: $random_number_b = rand(5, 10); danielebarchiesi@0: danielebarchiesi@0: // Store the first random number and use it on calculations. danielebarchiesi@0: $math_expression->e("var = $random_number_a"); danielebarchiesi@0: $this->assertEqual($random_number_a + $random_number_b, $math_expression->e("var + $random_number_b")); danielebarchiesi@0: $this->assertEqual($random_number_a * $random_number_b, $math_expression->e("var * $random_number_b")); danielebarchiesi@0: $this->assertEqual($random_number_a - $random_number_b, $math_expression->e("var - $random_number_b")); danielebarchiesi@0: $this->assertEqual($random_number_a / $random_number_b, $math_expression->e("var / $random_number_b")); danielebarchiesi@0: } danielebarchiesi@0: danielebarchiesi@0: public function testCustomFunctions() { danielebarchiesi@0: $math_expression = new ctools_math_expr(); danielebarchiesi@0: danielebarchiesi@0: $random_number_a = rand(5, 10); danielebarchiesi@0: $random_number_b = rand(5, 10); danielebarchiesi@0: danielebarchiesi@0: // Create a one-argument function. danielebarchiesi@0: $math_expression->e("f(x) = 2 * x"); danielebarchiesi@0: $this->assertEqual($random_number_a * 2, $math_expression->e("f($random_number_a)")); danielebarchiesi@0: $this->assertEqual($random_number_b * 2, $math_expression->e("f($random_number_b)")); danielebarchiesi@0: danielebarchiesi@0: // Create a two-argument function. danielebarchiesi@0: $math_expression->e("g(x, y) = 2 * x + y"); danielebarchiesi@0: $this->assertEqual($random_number_a * 2 + $random_number_b, $math_expression->e("g($random_number_a, $random_number_b)")); danielebarchiesi@0: danielebarchiesi@0: // Use a custom function in another function. danielebarchiesi@0: $this->assertEqual(($random_number_a * 2 + $random_number_b) * 2, $math_expression->e("f(g($random_number_a, $random_number_b))")); danielebarchiesi@0: } danielebarchiesi@0: }