math_expression.test 6.6 KB

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