annotate sites/all/modules/ctools/tests/math_expression.test @ 9:830c812b520f

added smtp module
author root <root@paio.local>
date Mon, 28 Oct 2013 15:34:27 +0000
parents ff03f76ab3fe
children
rev   line source
danielebarchiesi@0 1 <?php
danielebarchiesi@0 2
danielebarchiesi@0 3 /**
danielebarchiesi@0 4 * @file
danielebarchiesi@0 5 * Contains \CtoolsMathExpressionTestCase.
danielebarchiesi@0 6 */
danielebarchiesi@0 7
danielebarchiesi@0 8 /**
danielebarchiesi@0 9 * Tests the MathExpression library of ctools.
danielebarchiesi@0 10 */
danielebarchiesi@0 11 class CtoolsMathExpressionTestCase extends DrupalWebTestCase {
danielebarchiesi@0 12 public static function getInfo() {
danielebarchiesi@0 13 return array(
danielebarchiesi@0 14 'name' => 'CTools math expression tests',
danielebarchiesi@0 15 'description' => 'Test the math expression library of ctools.',
danielebarchiesi@0 16 'group' => 'Chaos Tools Suite',
danielebarchiesi@0 17 );
danielebarchiesi@0 18 }
danielebarchiesi@0 19
danielebarchiesi@0 20 public function setUp() {
danielebarchiesi@0 21 parent::setUp('ctools', 'ctools_plugin_test');
danielebarchiesi@0 22 }
danielebarchiesi@0 23
danielebarchiesi@0 24 /**
danielebarchiesi@0 25 * Returns a random double between 0 and 1.
danielebarchiesi@0 26 */
danielebarchiesi@0 27 protected function rand01() {
danielebarchiesi@0 28 return rand(0, PHP_INT_MAX) / PHP_INT_MAX;
danielebarchiesi@0 29 }
danielebarchiesi@0 30
danielebarchiesi@0 31 /**
danielebarchiesi@0 32 * A custom assertion with checks the values in a certain range.
danielebarchiesi@0 33 */
danielebarchiesi@0 34 protected function assertFloat($first, $second, $delta = 0.0000001, $message = '', $group = 'Other') {
danielebarchiesi@0 35 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 36 }
danielebarchiesi@0 37
danielebarchiesi@0 38 public function testArithmetic() {
danielebarchiesi@0 39 $math_expression = new ctools_math_expr();
danielebarchiesi@0 40
danielebarchiesi@0 41 // Test constant expressions.
danielebarchiesi@0 42 $this->assertEqual($math_expression->e('2'), 2);
danielebarchiesi@0 43 $random_number = rand(0, 10);
danielebarchiesi@0 44 $this->assertEqual($random_number, $math_expression->e((string) $random_number));
danielebarchiesi@0 45
danielebarchiesi@0 46 // Test simple arithmetic.
danielebarchiesi@0 47 $random_number_a = rand(5, 10);
danielebarchiesi@0 48 $random_number_b = rand(5, 10);
danielebarchiesi@0 49 $this->assertEqual($random_number_a + $random_number_b, $math_expression->e("$random_number_a + $random_number_b"));
danielebarchiesi@0 50 $this->assertEqual($random_number_a - $random_number_b, $math_expression->e("$random_number_a - $random_number_b"));
danielebarchiesi@0 51 $this->assertEqual($random_number_a * $random_number_b, $math_expression->e("$random_number_a * $random_number_b"));
danielebarchiesi@0 52 $this->assertEqual($random_number_a / $random_number_b, $math_expression->e("$random_number_a / $random_number_b"));
danielebarchiesi@0 53
danielebarchiesi@0 54 // Test Associative property.
danielebarchiesi@0 55 $random_number_c = rand(5, 10);
danielebarchiesi@0 56 $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 57 $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 58
danielebarchiesi@0 59 // Test Commutative property.
danielebarchiesi@0 60 $this->assertEqual($math_expression->e("$random_number_a + $random_number_b"), $math_expression->e("$random_number_b + $random_number_a"));
danielebarchiesi@0 61 $this->assertEqual($math_expression->e("$random_number_a * $random_number_b"), $math_expression->e("$random_number_b * $random_number_a"));
danielebarchiesi@0 62
danielebarchiesi@0 63 // Test Distributive property.
danielebarchiesi@0 64 $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 65
danielebarchiesi@0 66 $this->assertEqual(pow($random_number_a, $random_number_b), $math_expression->e("$random_number_a ^ $random_number_b"));
danielebarchiesi@0 67 }
danielebarchiesi@0 68
danielebarchiesi@0 69 public function testBuildInFunctions() {
danielebarchiesi@0 70 $math_expression = new ctools_math_expr();
danielebarchiesi@0 71
danielebarchiesi@0 72 // @todo: maybe run this code multiple times to test different values.
danielebarchiesi@0 73 $random_double = $this->rand01();
danielebarchiesi@0 74 $random_int = rand(5, 10);
danielebarchiesi@0 75 $this->assertFloat(sin($random_double), $math_expression->e("sin($random_double)"));
danielebarchiesi@0 76 $this->assertFloat(cos($random_double), $math_expression->e("cos($random_double)"));
danielebarchiesi@0 77 $this->assertFloat(tan($random_double), $math_expression->e("tan($random_double)"));
danielebarchiesi@0 78 $this->assertFloat(exp($random_double), $math_expression->e("exp($random_double)"));
danielebarchiesi@0 79 $this->assertFloat(sqrt($random_double), $math_expression->e("sqrt($random_double)"));
danielebarchiesi@0 80 $this->assertFloat(log($random_double), $math_expression->e("ln($random_double)"));
danielebarchiesi@0 81 $this->assertFloat(round($random_double), $math_expression->e("round($random_double)"));
danielebarchiesi@0 82 $this->assertFloat(abs($random_double + $random_int), $math_expression->e('abs(' . ($random_double + $random_int) . ')'));
danielebarchiesi@0 83 $this->assertEqual(round($random_double + $random_int), $math_expression->e('round(' . ($random_double + $random_int) . ')'));
danielebarchiesi@0 84 $this->assertEqual(ceil($random_double + $random_int), $math_expression->e('ceil(' . ($random_double + $random_int) . ')'));
danielebarchiesi@0 85 $this->assertEqual(floor($random_double + $random_int), $math_expression->e('floor(' . ($random_double + $random_int) . ')'));
danielebarchiesi@0 86
danielebarchiesi@0 87 // @fixme: you can't run time without an argument.
danielebarchiesi@0 88 $this->assertFloat(time(), $math_expression->e('time(1)'));
danielebarchiesi@0 89
danielebarchiesi@0 90 $random_double_a = $this->rand01();
danielebarchiesi@0 91 $random_double_b = $this->rand01();
danielebarchiesi@0 92 // @fixme: max/min don't work at the moment.
danielebarchiesi@0 93 // $this->assertFloat(max($random_double_a, $random_double_b), $math_expression->e("max($random_double_a, $random_double_b)"));
danielebarchiesi@0 94 // $this->assertFloat(min($random_double_a, $random_double_b), $math_expression->e("min($random_double_a, $random_double_b)"));
danielebarchiesi@0 95 }
danielebarchiesi@0 96
danielebarchiesi@0 97 public function testVariables() {
danielebarchiesi@0 98 $math_expression = new ctools_math_expr();
danielebarchiesi@0 99
danielebarchiesi@0 100 $random_number_a = rand(5, 10);
danielebarchiesi@0 101 $random_number_b = rand(5, 10);
danielebarchiesi@0 102
danielebarchiesi@0 103 // Store the first random number and use it on calculations.
danielebarchiesi@0 104 $math_expression->e("var = $random_number_a");
danielebarchiesi@0 105 $this->assertEqual($random_number_a + $random_number_b, $math_expression->e("var + $random_number_b"));
danielebarchiesi@0 106 $this->assertEqual($random_number_a * $random_number_b, $math_expression->e("var * $random_number_b"));
danielebarchiesi@0 107 $this->assertEqual($random_number_a - $random_number_b, $math_expression->e("var - $random_number_b"));
danielebarchiesi@0 108 $this->assertEqual($random_number_a / $random_number_b, $math_expression->e("var / $random_number_b"));
danielebarchiesi@0 109 }
danielebarchiesi@0 110
danielebarchiesi@0 111 public function testCustomFunctions() {
danielebarchiesi@0 112 $math_expression = new ctools_math_expr();
danielebarchiesi@0 113
danielebarchiesi@0 114 $random_number_a = rand(5, 10);
danielebarchiesi@0 115 $random_number_b = rand(5, 10);
danielebarchiesi@0 116
danielebarchiesi@0 117 // Create a one-argument function.
danielebarchiesi@0 118 $math_expression->e("f(x) = 2 * x");
danielebarchiesi@0 119 $this->assertEqual($random_number_a * 2, $math_expression->e("f($random_number_a)"));
danielebarchiesi@0 120 $this->assertEqual($random_number_b * 2, $math_expression->e("f($random_number_b)"));
danielebarchiesi@0 121
danielebarchiesi@0 122 // Create a two-argument function.
danielebarchiesi@0 123 $math_expression->e("g(x, y) = 2 * x + y");
danielebarchiesi@0 124 $this->assertEqual($random_number_a * 2 + $random_number_b, $math_expression->e("g($random_number_a, $random_number_b)"));
danielebarchiesi@0 125
danielebarchiesi@0 126 // Use a custom function in another function.
danielebarchiesi@0 127 $this->assertEqual(($random_number_a * 2 + $random_number_b) * 2, $math_expression->e("f(g($random_number_a, $random_number_b))"));
danielebarchiesi@0 128 }
danielebarchiesi@0 129 }