[FEATURE] MathUtility: Add canBeInterpretedAsFloat
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Tests / Unit / Utility / MathUtilityTest.php
1 <?php
2 namespace TYPO3\CMS\Core\Tests\Unit\Utility;
3
4 /***************************************************************
5 * Copyright notice
6 *
7 * (c) 2011-2013 Susanne Moog <typo3@susanne-moog.de>
8 * All rights reserved
9 *
10 * This script is part of the TYPO3 project. The TYPO3 project is
11 * free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * The GNU General Public License can be found at
17 * http://www.gnu.org/copyleft/gpl.html.
18 *
19 * This script is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
23 *
24 * This copyright notice MUST APPEAR in all copies of the script!
25 ***************************************************************/
26
27 require_once 'Fixtures/MathUtilityTestClassWithStringRepresentationFixture.php';
28
29 /**
30 * Testcase for class \TYPO3\CMS\Core\Utility\MathUtility
31 *
32 * @author Susanne Moog <typo3@susanne-moog.de>
33 */
34 class MathUtilityTest extends \TYPO3\CMS\Core\Tests\UnitTestCase {
35
36 //////////////////////////////////
37 // Tests concerning forceIntegerInRange
38 //////////////////////////////////
39 /**
40 * Data provider for forceIntegerInRangeForcesIntegerIntoBoundaries
41 *
42 * @return array expected values, arithmetic expression
43 */
44 public function forceIntegerInRangeForcesIntegerIntoDefaultBoundariesDataProvider() {
45 return array(
46 'negativeValue' => array(0, -10),
47 'normalValue' => array(30, 30),
48 'veryHighValue' => array(2000000000, PHP_INT_MAX),
49 'zeroValue' => array(0, 0),
50 'anotherNormalValue' => array(12309, 12309)
51 );
52 }
53
54 /**
55 * @test
56 * @dataProvider forceIntegerInRangeForcesIntegerIntoDefaultBoundariesDataProvider
57 */
58 public function forceIntegerInRangeForcesIntegerIntoDefaultBoundaries($expected, $value) {
59 $this->assertEquals($expected, \TYPO3\CMS\Core\Utility\MathUtility::forceIntegerInRange($value, 0));
60 }
61
62 /**
63 * @test
64 */
65 public function forceIntegerInRangeSetsDefaultValueIfZeroValueIsGiven() {
66 $this->assertEquals(42, \TYPO3\CMS\Core\Utility\MathUtility::forceIntegerInRange('', 0, 2000000000, 42));
67 }
68
69 //////////////////////////////////
70 // Tests concerning convertToPositiveInteger
71 //////////////////////////////////
72 /**
73 * @test
74 */
75 public function convertToPositiveIntegerReturnsZeroForNegativeValues() {
76 $this->assertEquals(0, \TYPO3\CMS\Core\Utility\MathUtility::convertToPositiveInteger(-123));
77 }
78
79 /**
80 * @test
81 */
82 public function convertToPositiveIntegerReturnsTheInputValueForPositiveValues() {
83 $this->assertEquals(123, \TYPO3\CMS\Core\Utility\MathUtility::convertToPositiveInteger(123));
84 }
85
86 ///////////////////////////////
87 // Tests concerning canBeInterpretedAsInteger
88 ///////////////////////////////
89 /**
90 * Data provider for canBeInterpretedAsIntegerReturnsTrue
91 *
92 * @return array Data sets
93 */
94 public function functionCanBeInterpretedAsIntegerValidDataProvider() {
95 return array(
96 'int' => array(32425),
97 'negative int' => array(-32425),
98 'largest int' => array(PHP_INT_MAX),
99 'int as string' => array('32425'),
100 'negative int as string' => array('-32425'),
101 'zero' => array(0),
102 'zero as string' => array('0')
103 );
104 }
105
106 /**
107 * @test
108 * @dataProvider functionCanBeInterpretedAsIntegerValidDataProvider
109 */
110 public function canBeInterpretedAsIntegerReturnsTrue($int) {
111 $this->assertTrue(\TYPO3\CMS\Core\Utility\MathUtility::canBeInterpretedAsInteger($int));
112 }
113
114 /**
115 * Data provider for canBeInterpretedAsIntegerReturnsFalse
116 *
117 * @return array Data sets
118 */
119 public function functionCanBeInterpretedAsIntegerInvalidDataProvider() {
120 $objectWithNumericalStringRepresentation = new \TYPO3\CMS\Core\Tests\Unit\Utility\Fixtures\MathUtilityTestClassWithStringRepresentationFixture();
121 $objectWithNumericalStringRepresentation->setString('1234');
122 $objectWithNonNumericalStringRepresentation = new \TYPO3\CMS\Core\Tests\Unit\Utility\Fixtures\MathUtilityTestClassWithStringRepresentationFixture();
123 $objectWithNonNumericalStringRepresentation->setString('foo');
124 $objectWithEmptyStringRepresentation = new \TYPO3\CMS\Core\Tests\Unit\Utility\Fixtures\MathUtilityTestClassWithStringRepresentationFixture();
125 $objectWithEmptyStringRepresentation->setString('');
126 return array(
127 'int as string with leading zero' => array('01234'),
128 'positive int as string with plus modifier' => array('+1234'),
129 'negative int as string with leading zero' => array('-01234'),
130 'largest int plus one' => array(PHP_INT_MAX + 1),
131 'string' => array('testInt'),
132 'empty string' => array(''),
133 'int in string' => array('5 times of testInt'),
134 'int as string with space after' => array('5 '),
135 'int as string with space before' => array(' 5'),
136 'int as string with many spaces before' => array(' 5'),
137 'float' => array(3.14159),
138 'float as string' => array('3.14159'),
139 'float as string only a dot' => array('10.'),
140 'float as string trailing zero would evaluate to int 10' => array('10.0'),
141 'float as string trailing zeros would evaluate to int 10' => array('10.00'),
142 'null' => array(NULL),
143 'empty array' => array(array()),
144 'int in array' => array(array(32425)),
145 'int as string in array' => array(array('32425')),
146 'object without string representation' => array(new \stdClass()),
147 'object with numerical string representation' => array($objectWithNumericalStringRepresentation),
148 'object without numerical string representation' => array($objectWithNonNumericalStringRepresentation),
149 'object with empty string representation' => array($objectWithEmptyStringRepresentation)
150 );
151 }
152
153 /**
154 * @test
155 * @dataProvider functionCanBeInterpretedAsIntegerInvalidDataProvider
156 */
157 public function canBeInterpretedAsIntegerReturnsFalse($int) {
158 $this->assertFalse(\TYPO3\CMS\Core\Utility\MathUtility::canBeInterpretedAsInteger($int));
159 }
160
161 ///////////////////////////////
162 // Tests concerning canBeInterpretedAsFloat
163 ///////////////////////////////
164 /**
165 * Data provider for canBeInterpretedAsFloatReturnsTrue
166 *
167 * @return array Data sets
168 */
169 public function functionCanBeInterpretedAsFloatValidDataProvider() {
170 // testcases for Integer apply for float as well
171 $intTestcases = $this->functionCanBeInterpretedAsIntegerValidDataProvider();
172 $floatTestcases = array(
173 'zero as float' => array((float) 0),
174 'negative float' => array((float) -7.5),
175 'negative float as string with exp #1' => array('-7.5e3'),
176 'negative float as string with exp #2' => array('-7.5e03'),
177 'negative float as string with exp #3' => array('-7.5e-3'),
178 'float' => array(3.14159),
179 'float as string' => array('3.14159'),
180 'float as string only a dot' => array('10.'),
181 'float as string trailing zero' => array('10.0'),
182 'float as string trailing zeros' => array('10.00'),
183 );
184 return array_merge($intTestcases, $floatTestcases);
185 }
186
187 /**
188 * @test
189 * @dataProvider functionCanBeInterpretedAsFloatValidDataProvider
190 */
191 public function canBeInterpretedAsFloatReturnsTrue($val) {
192 $this->assertTrue(\TYPO3\CMS\Core\Utility\MathUtility::canBeInterpretedAsFloat($val));
193 }
194
195 /**
196 * Data provider for canBeInterpretedAsFloatReturnsFalse
197 *
198 * @return array Data sets
199 */
200 public function functionCanBeInterpretedAsFloatInvalidDataProvider() {
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('');
207 return array(
208 // 'int as string with leading zero' => array('01234'),
209 // 'positive int as string with plus modifier' => array('+1234'),
210 // 'negative int as string with leading zero' => array('-01234'),
211 // 'largest int plus one' => array(PHP_INT_MAX + 1),
212 'string' => array('testInt'),
213 'empty string' => array(''),
214 'int in string' => array('5 times of testInt'),
215 'int as string with space after' => array('5 '),
216 'int as string with space before' => array(' 5'),
217 'int as string with many spaces before' => array(' 5'),
218 'null' => array(NULL),
219 'empty array' => array(array()),
220 'int in array' => array(array(32425)),
221 'int as string in array' => array(array('32425')),
222 'negative float as string with invalid chars in exponent' => array('-7.5eX3'),
223 'object without string representation' => array(new \stdClass()),
224 'object with numerical string representation' => array($objectWithNumericalStringRepresentation),
225 'object without numerical string representation' => array($objectWithNonNumericalStringRepresentation),
226 'object with empty string representation' => array($objectWithEmptyStringRepresentation)
227 );
228 }
229
230 /**
231 * @test
232 * @dataProvider functionCanBeInterpretedAsFloatInvalidDataProvider
233 */
234 public function canBeInterpretedAsFloatReturnsFalse($int) {
235 $this->assertFalse(\TYPO3\CMS\Core\Utility\MathUtility::canBeInterpretedAsFloat($int));
236 }
237
238 //////////////////////////////////
239 // Tests concerning calculateWithPriorityToAdditionAndSubtraction
240 //////////////////////////////////
241 /**
242 * Data provider for calculateWithPriorityToAdditionAndSubtraction
243 *
244 * @return array expected values, arithmetic expression
245 */
246 public function calculateWithPriorityToAdditionAndSubtractionDataProvider() {
247 return array(
248 'add' => array(9, '6 + 3'),
249 'substract with positive result' => array(3, '6 - 3'),
250 'substract with negative result' => array(-3, '3 - 6'),
251 'multiply' => array(6, '2 * 3'),
252 'divide' => array(2.5, '5 / 2'),
253 'modulus' => array(1, '5 % 2'),
254 'power' => array(8, '2 ^ 3'),
255 'three operands with non integer result' => array(6.5, '5 + 3 / 2'),
256 'three operands with power' => array(14, '5 + 3 ^ 2'),
257 'three operads with modulus' => array(4, '5 % 2 + 3'),
258 'four operands' => array(3, '2 + 6 / 2 - 2'),
259 'division by zero when dividing' => array('ERROR: dividing by zero', '2 / 0'),
260 'division by zero with modulus' => array('ERROR: dividing by zero', '2 % 0')
261 );
262 }
263
264 /**
265 * @test
266 * @dataProvider calculateWithPriorityToAdditionAndSubtractionDataProvider
267 */
268 public function calculateWithPriorityToAdditionAndSubtractionCorrectlyCalculatesExpression($expected, $expression) {
269 $this->assertEquals($expected, \TYPO3\CMS\Core\Utility\MathUtility::calculateWithPriorityToAdditionAndSubtraction($expression));
270 }
271
272 //////////////////////////////////
273 // Tests concerning calcParenthesis
274 //////////////////////////////////
275 /**
276 * Data provider for calcParenthesis
277 *
278 * @return array expected values, arithmetic expression
279 */
280 public function calculateWithParenthesesDataProvider() {
281 return array(
282 'starts with parenthesis' => array(18, '(6 + 3) * 2'),
283 'ends with parenthesis' => array(6, '2 * (6 - 3)'),
284 'multiple parentheses' => array(-6, '(3 - 6) * (4 - 2)'),
285 'nested parentheses' => array(22, '2 * (3 + 2 + (3 * 2))'),
286 'parenthesis with division' => array(15, '5 / 2 * (3 * 2)')
287 );
288 }
289
290 /**
291 * @test
292 * @dataProvider calculateWithParenthesesDataProvider
293 */
294 public function calculateWithParenthesesCorrectlyCalculatesExpression($expected, $expression) {
295 $this->assertEquals($expected, \TYPO3\CMS\Core\Utility\MathUtility::calculateWithParentheses($expression));
296 }
297
298 //////////////////////////////////
299 // Tests concerning isIntegerInRange
300 //////////////////////////////////
301 /**
302 * @test
303 */
304 public function isIntegerInRangeIncludesLowerBoundary() {
305 $this->assertTrue(\TYPO3\CMS\Core\Utility\MathUtility::isIntegerInRange(1, 1, 2));
306 }
307
308 /**
309 * @test
310 */
311 public function isIntegerInRangeIncludesUpperBoundary() {
312 $this->assertTrue(\TYPO3\CMS\Core\Utility\MathUtility::isIntegerInRange(2, 1, 2));
313 }
314
315 /**
316 * @test
317 */
318 public function isIntegerInRangeAcceptsValueInRange() {
319 $this->assertTrue(\TYPO3\CMS\Core\Utility\MathUtility::isIntegerInRange(10, 1, 100));
320 }
321
322 /**
323 * @test
324 */
325 public function isIntegerInRangeRejectsValueOutsideOfRange() {
326 $this->assertFalse(\TYPO3\CMS\Core\Utility\MathUtility::isIntegerInRange(10, 1, 2));
327 }
328
329 /**
330 * Data provider or isIntegerInRangeRejectsOtherDataTypes
331 */
332 public function isIntegerInRangeRejectsOtherDataTypesDataProvider() {
333 return array(
334 'negative integer' => array(-1),
335 'float' => array(1.5),
336 'string' => array('string'),
337 'array' => array(array()),
338 'object' => array(new \stdClass()),
339 'boolean FALSE' => array(FALSE),
340 'NULL' => array(NULL)
341 );
342 }
343
344 /**
345 * @test
346 * @dataProvider isIntegerInRangeRejectsOtherDataTypesDataProvider
347 */
348 public function isIntegerInRangeRejectsOtherDataTypes($inputValue) {
349 $this->assertFalse(\TYPO3\CMS\Core\Utility\MathUtility::isIntegerInRange($inputValue, 0, 10));
350 }
351
352 }
353
354 ?>