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