math_expression.test 6.3 KB

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