TYPO3 CMS  TYPO3_6-2
MathUtilityTest.php
Go to the documentation of this file.
1 <?php
3 
23 
25  // Tests concerning forceIntegerInRange
27 
33  return array(
34  'negativeValue' => array(0, -10),
35  'normalValue' => array(30, 30),
36  'veryHighValue' => array(2000000000, PHP_INT_MAX),
37  'zeroValue' => array(0, 0),
38  'anotherNormalValue' => array(12309, 12309)
39  );
40  }
41 
46  public function forceIntegerInRangeForcesIntegerIntoDefaultBoundaries($expected, $value) {
47  $this->assertEquals($expected, \TYPO3\CMS\Core\Utility\MathUtility::forceIntegerInRange($value, 0));
48  }
49 
54  $this->assertEquals(42, \TYPO3\CMS\Core\Utility\MathUtility::forceIntegerInRange('', 0, 2000000000, 42));
55  }
56 
58  // Tests concerning convertToPositiveInteger
60 
64  $this->assertEquals(0, \TYPO3\CMS\Core\Utility\MathUtility::convertToPositiveInteger(-123));
65  }
66 
71  $this->assertEquals(123, \TYPO3\CMS\Core\Utility\MathUtility::convertToPositiveInteger(123));
72  }
73 
75  // Tests concerning canBeInterpretedAsInteger
77 
83  return array(
84  'int' => array(32425),
85  'negative int' => array(-32425),
86  'largest int' => array(PHP_INT_MAX),
87  'int as string' => array('32425'),
88  'negative int as string' => array('-32425'),
89  'zero' => array(0),
90  'zero as string' => array('0')
91  );
92  }
93 
98  public function canBeInterpretedAsIntegerReturnsTrue($int) {
99  $this->assertTrue(\TYPO3\CMS\Core\Utility\MathUtility::canBeInterpretedAsInteger($int));
100  }
101 
108  $objectWithNumericalStringRepresentation = new \TYPO3\CMS\Core\Tests\Unit\Utility\Fixtures\MathUtilityTestClassWithStringRepresentationFixture();
109  $objectWithNumericalStringRepresentation->setString('1234');
110  $objectWithNonNumericalStringRepresentation = new \TYPO3\CMS\Core\Tests\Unit\Utility\Fixtures\MathUtilityTestClassWithStringRepresentationFixture();
111  $objectWithNonNumericalStringRepresentation->setString('foo');
112  $objectWithEmptyStringRepresentation = new \TYPO3\CMS\Core\Tests\Unit\Utility\Fixtures\MathUtilityTestClassWithStringRepresentationFixture();
113  $objectWithEmptyStringRepresentation->setString('');
114  return array(
115  'int as string with leading zero' => array('01234'),
116  'positive int as string with plus modifier' => array('+1234'),
117  'negative int as string with leading zero' => array('-01234'),
118  'largest int plus one' => array(PHP_INT_MAX + 1),
119  'string' => array('testInt'),
120  'empty string' => array(''),
121  'int in string' => array('5 times of testInt'),
122  'int as string with space after' => array('5 '),
123  'int as string with space before' => array(' 5'),
124  'int as string with many spaces before' => array(' 5'),
125  'float' => array(3.14159),
126  'float as string' => array('3.14159'),
127  'float as string only a dot' => array('10.'),
128  'float as string trailing zero would evaluate to int 10' => array('10.0'),
129  'float as string trailing zeros would evaluate to int 10' => array('10.00'),
130  'null' => array(NULL),
131  'empty array' => array(array()),
132  'int in array' => array(array(32425)),
133  'int as string in array' => array(array('32425')),
134  'object without string representation' => array(new \stdClass()),
135  'object with numerical string representation' => array($objectWithNumericalStringRepresentation),
136  'object without numerical string representation' => array($objectWithNonNumericalStringRepresentation),
137  'object with empty string representation' => array($objectWithEmptyStringRepresentation)
138  );
139  }
140 
146  $this->assertFalse(\TYPO3\CMS\Core\Utility\MathUtility::canBeInterpretedAsInteger($int));
147  }
148 
150  // Tests concerning canBeInterpretedAsFloat
152 
158  // testcases for Integer apply for float as well
160  $floatTestcases = array(
161  'zero as float' => array((float) 0),
162  'negative float' => array((float) -7.5),
163  'negative float as string with exp #1' => array('-7.5e3'),
164  'negative float as string with exp #2' => array('-7.5e03'),
165  'negative float as string with exp #3' => array('-7.5e-3'),
166  'float' => array(3.14159),
167  'float as string' => array('3.14159'),
168  'float as string only a dot' => array('10.'),
169  'float as string trailing zero' => array('10.0'),
170  'float as string trailing zeros' => array('10.00'),
171  );
172  return array_merge($intTestcases, $floatTestcases);
173  }
174 
179  public function canBeInterpretedAsFloatReturnsTrue($val) {
180  $this->assertTrue(\TYPO3\CMS\Core\Utility\MathUtility::canBeInterpretedAsFloat($val));
181  }
182 
189  $objectWithNumericalStringRepresentation = new \TYPO3\CMS\Core\Tests\Unit\Utility\Fixtures\MathUtilityTestClassWithStringRepresentationFixture();
190  $objectWithNumericalStringRepresentation->setString('1234');
191  $objectWithNonNumericalStringRepresentation = new \TYPO3\CMS\Core\Tests\Unit\Utility\Fixtures\MathUtilityTestClassWithStringRepresentationFixture();
192  $objectWithNonNumericalStringRepresentation->setString('foo');
193  $objectWithEmptyStringRepresentation = new \TYPO3\CMS\Core\Tests\Unit\Utility\Fixtures\MathUtilityTestClassWithStringRepresentationFixture();
194  $objectWithEmptyStringRepresentation->setString('');
195  return array(
196  // 'int as string with leading zero' => array('01234'),
197  // 'positive int as string with plus modifier' => array('+1234'),
198  // 'negative int as string with leading zero' => array('-01234'),
199  // 'largest int plus one' => array(PHP_INT_MAX + 1),
200  'string' => array('testInt'),
201  'empty string' => array(''),
202  'int in string' => array('5 times of testInt'),
203  'int as string with space after' => array('5 '),
204  'int as string with space before' => array(' 5'),
205  'int as string with many spaces before' => array(' 5'),
206  'null' => array(NULL),
207  'empty array' => array(array()),
208  'int in array' => array(array(32425)),
209  'int as string in array' => array(array('32425')),
210  'negative float as string with invalid chars in exponent' => array('-7.5eX3'),
211  'object without string representation' => array(new \stdClass()),
212  'object with numerical string representation' => array($objectWithNumericalStringRepresentation),
213  'object without numerical string representation' => array($objectWithNonNumericalStringRepresentation),
214  'object with empty string representation' => array($objectWithEmptyStringRepresentation)
215  );
216  }
217 
222  public function canBeInterpretedAsFloatReturnsFalse($int) {
223  $this->assertFalse(\TYPO3\CMS\Core\Utility\MathUtility::canBeInterpretedAsFloat($int));
224  }
225 
227  // Tests concerning calculateWithPriorityToAdditionAndSubtraction
229 
235  return array(
236  'add' => array(9, '6 + 3'),
237  'substract with positive result' => array(3, '6 - 3'),
238  'substract with negative result' => array(-3, '3 - 6'),
239  'multiply' => array(6, '2 * 3'),
240  'divide' => array(2.5, '5 / 2'),
241  'modulus' => array(1, '5 % 2'),
242  'power' => array(8, '2 ^ 3'),
243  'three operands with non integer result' => array(6.5, '5 + 3 / 2'),
244  'three operands with power' => array(14, '5 + 3 ^ 2'),
245  'three operads with modulus' => array(4, '5 % 2 + 3'),
246  'four operands' => array(3, '2 + 6 / 2 - 2'),
247  'division by zero when dividing' => array('ERROR: dividing by zero', '2 / 0'),
248  'division by zero with modulus' => array('ERROR: dividing by zero', '2 % 0')
249  );
250  }
251 
257  $this->assertEquals($expected, \TYPO3\CMS\Core\Utility\MathUtility::calculateWithPriorityToAdditionAndSubtraction($expression));
258  }
259 
261  // Tests concerning calcParenthesis
263 
269  return array(
270  'starts with parenthesis' => array(18, '(6 + 3) * 2'),
271  'ends with parenthesis' => array(6, '2 * (6 - 3)'),
272  'multiple parentheses' => array(-6, '(3 - 6) * (4 - 2)'),
273  'nested parentheses' => array(22, '2 * (3 + 2 + (3 * 2))'),
274  'parenthesis with division' => array(15, '5 / 2 * (3 * 2)')
275  );
276  }
277 
282  public function calculateWithParenthesesCorrectlyCalculatesExpression($expected, $expression) {
283  $this->assertEquals($expected, \TYPO3\CMS\Core\Utility\MathUtility::calculateWithParentheses($expression));
284  }
285 
287  // Tests concerning isIntegerInRange
289 
293  $this->assertTrue(\TYPO3\CMS\Core\Utility\MathUtility::isIntegerInRange(1, 1, 2));
294  }
295 
300  $this->assertTrue(\TYPO3\CMS\Core\Utility\MathUtility::isIntegerInRange(2, 1, 2));
301  }
302 
307  $this->assertTrue(\TYPO3\CMS\Core\Utility\MathUtility::isIntegerInRange(10, 1, 100));
308  }
309 
314  $this->assertFalse(\TYPO3\CMS\Core\Utility\MathUtility::isIntegerInRange(10, 1, 2));
315  }
316 
321  return array(
322  'negative integer' => array(-1),
323  'float' => array(1.5),
324  'string' => array('string'),
325  'array' => array(array()),
326  'object' => array(new \stdClass()),
327  'boolean FALSE' => array(FALSE),
328  'NULL' => array(NULL)
329  );
330  }
331 
336  public function isIntegerInRangeRejectsOtherDataTypes($inputValue) {
337  $this->assertFalse(\TYPO3\CMS\Core\Utility\MathUtility::isIntegerInRange($inputValue, 0, 10));
338  }
339 
340 }
calculateWithPriorityToAdditionAndSubtractionCorrectlyCalculatesExpression($expected, $expression)
static forceIntegerInRange($theInt, $min, $max=2000000000, $defaultValue=0)
Definition: MathUtility.php:32
calculateWithParenthesesCorrectlyCalculatesExpression($expected, $expression)
static calculateWithPriorityToAdditionAndSubtraction($string)
static calculateWithParentheses($string)
static isIntegerInRange($value, $minimum, $maximum)
static convertToPositiveInteger($theInt)
Definition: MathUtility.php:55
forceIntegerInRangeForcesIntegerIntoDefaultBoundaries($expected, $value)