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