17 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
35 'negativeValue' => [0, -10],
36 'normalValue' => [30, 30],
37 'veryHighValue' => [2000000000, PHP_INT_MAX],
38 'zeroValue' => [0, 0],
39 'anotherNormalValue' => [12309, 12309]
49 $this->assertEquals($expected, \
TYPO3\CMS\Core\Utility\MathUtility::forceIntegerInRange($value, 0));
57 $this->assertEquals(42, \
TYPO3\CMS\Core\Utility\MathUtility::forceIntegerInRange(
'', 0, 2000000000, 42));
68 $this->assertEquals(0, \
TYPO3\CMS\Core\Utility\MathUtility::convertToPositiveInteger(-123));
76 $this->assertEquals(123, \
TYPO3\CMS\Core\Utility\MathUtility::convertToPositiveInteger(123));
91 'negative int' => [-32425],
92 'largest int' => [PHP_INT_MAX],
93 'int as string' => [
'32425'],
94 'negative int as string' => [
'-32425'],
96 'zero as string' => [
'0']
106 $this->assertTrue(\
TYPO3\CMS\Core\Utility\MathUtility::canBeInterpretedAsInteger($int));
116 $objectWithNumericalStringRepresentation = new \TYPO3\CMS\Core\Tests\Unit\Utility\Fixtures\MathUtilityTestClassWithStringRepresentationFixture();
117 $objectWithNumericalStringRepresentation->setString(
'1234');
118 $objectWithNonNumericalStringRepresentation = new \TYPO3\CMS\Core\Tests\Unit\Utility\Fixtures\MathUtilityTestClassWithStringRepresentationFixture();
119 $objectWithNonNumericalStringRepresentation->setString(
'foo');
120 $objectWithEmptyStringRepresentation = new \TYPO3\CMS\Core\Tests\Unit\Utility\Fixtures\MathUtilityTestClassWithStringRepresentationFixture();
121 $objectWithEmptyStringRepresentation->setString(
'');
123 'int as string with leading zero' => [
'01234'],
124 'positive int as string with plus modifier' => [
'+1234'],
125 'negative int as string with leading zero' => [
'-01234'],
126 'largest int plus one' => [PHP_INT_MAX + 1],
127 'string' => [
'testInt'],
128 'empty string' => [
''],
129 'int in string' => [
'5 times of testInt'],
130 'int as string with space after' => [
'5 '],
131 'int as string with space before' => [
' 5'],
132 'int as string with many spaces before' => [
' 5'],
133 'float' => [3.14159],
134 'float as string' => [
'3.14159'],
135 'float as string only a dot' => [
'10.'],
136 'float as string trailing zero would evaluate to int 10' => [
'10.0'],
137 'float as string trailing zeros would evaluate to int 10' => [
'10.00'],
139 'empty array' => [[]],
140 'int in array' => [[32425]],
141 'int as string in array' => [[
'32425']],
142 'object without string representation' => [new \stdClass()],
143 'object with numerical string representation' => [$objectWithNumericalStringRepresentation],
144 'object without numerical string representation' => [$objectWithNonNumericalStringRepresentation],
145 'object with empty string representation' => [$objectWithEmptyStringRepresentation]
155 $this->assertFalse(\
TYPO3\CMS\Core\Utility\MathUtility::canBeInterpretedAsInteger($int));
171 'zero as float' => [(float)0],
172 'negative float' => [(
float)-7.5],
173 'negative float as string with exp #1' => [
'-7.5e3'],
174 'negative float as string with exp #2' => [
'-7.5e03'],
175 'negative float as string with exp #3' => [
'-7.5e-3'],
176 'float' => [3.14159],
177 'float as string' => [
'3.14159'],
178 'float as string only a dot' => [
'10.'],
179 'float as string trailing zero' => [
'10.0'],
180 'float as string trailing zeros' => [
'10.00'],
182 return array_merge($intTestcases, $floatTestcases);
191 $this->assertTrue(\
TYPO3\CMS\Core\Utility\MathUtility::canBeInterpretedAsFloat($val));
201 $objectWithNumericalStringRepresentation = new \TYPO3\CMS\Core\Tests\Unit\Utility\Fixtures\MathUtilityTestClassWithStringRepresentationFixture();
202 $objectWithNumericalStringRepresentation->setString(
'1234');
203 $objectWithNonNumericalStringRepresentation = new \TYPO3\CMS\Core\Tests\Unit\Utility\Fixtures\MathUtilityTestClassWithStringRepresentationFixture();
204 $objectWithNonNumericalStringRepresentation->setString(
'foo');
205 $objectWithEmptyStringRepresentation = new \TYPO3\CMS\Core\Tests\Unit\Utility\Fixtures\MathUtilityTestClassWithStringRepresentationFixture();
206 $objectWithEmptyStringRepresentation->setString(
'');
212 'string' => [
'testInt'],
213 'empty string' => [
''],
214 'int in string' => [
'5 times of testInt'],
215 'int as string with space after' => [
'5 '],
216 'int as string with space before' => [
' 5'],
217 'int as string with many spaces before' => [
' 5'],
219 'empty array' => [[]],
220 'int in array' => [[32425]],
221 'int as string in array' => [[
'32425']],
222 'negative float as string with invalid chars in exponent' => [
'-7.5eX3'],
223 'object without string representation' => [new \stdClass()],
224 'object with numerical string representation' => [$objectWithNumericalStringRepresentation],
225 'object without numerical string representation' => [$objectWithNonNumericalStringRepresentation],
226 'object with empty string representation' => [$objectWithEmptyStringRepresentation]
236 $this->assertFalse(\
TYPO3\CMS\Core\Utility\MathUtility::canBeInterpretedAsFloat($int));
250 'add' => [9,
'6 + 3'],
251 'substract with positive result' => [3,
'6 - 3'],
252 'substract with negative result' => [-3,
'3 - 6'],
253 'multiply' => [6,
'2 * 3'],
254 'divide' => [2.5,
'5 / 2'],
255 'modulus' => [1,
'5 % 2'],
256 'power' => [8,
'2 ^ 3'],
257 'three operands with non integer result' => [6.5,
'5 + 3 / 2'],
258 'three operands with power' => [14,
'5 + 3 ^ 2'],
259 'three operads with modulus' => [4,
'5 % 2 + 3'],
260 'four operands' => [3,
'2 + 6 / 2 - 2'],
261 'division by zero when dividing' => [
'ERROR: dividing by zero',
'2 / 0'],
262 'division by zero with modulus' => [
'ERROR: dividing by zero',
'2 % 0']
272 $this->assertEquals($expected, \
TYPO3\CMS\Core\Utility\MathUtility::calculateWithPriorityToAdditionAndSubtraction($expression));
286 'starts with parenthesis' => [18,
'(6 + 3) * 2'],
287 'ends with parenthesis' => [6,
'2 * (6 - 3)'],
288 'multiple parentheses' => [-6,
'(3 - 6) * (4 - 2)'],
289 'nested parentheses' => [22,
'2 * (3 + 2 + (3 * 2))'],
290 'parenthesis with division' => [15,
'5 / 2 * (3 * 2)']
300 $this->assertEquals($expected, \
TYPO3\CMS\Core\Utility\MathUtility::calculateWithParentheses($expression));
311 $this->assertTrue(\
TYPO3\CMS\Core\Utility\MathUtility::isIntegerInRange(1, 1, 2));
319 $this->assertTrue(\
TYPO3\CMS\Core\Utility\MathUtility::isIntegerInRange(2, 1, 2));
327 $this->assertTrue(\
TYPO3\CMS\Core\Utility\MathUtility::isIntegerInRange(10, 1, 100));
335 $this->assertFalse(\
TYPO3\CMS\Core\Utility\MathUtility::isIntegerInRange(10, 1, 2));
344 'negative integer' => [-1],
346 'string' => [
'string'],
348 'object' => [new \stdClass()],
349 'boolean FALSE' => [
false],
360 $this->assertFalse(\
TYPO3\CMS\Core\Utility\MathUtility::isIntegerInRange($inputValue, 0, 10));