51efb5f2dfbe60c96c19c62bca48d1954d87db5f
[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 testInt
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 testIntReturnsTrue($int) {
111 $this->assertTrue(\TYPO3\CMS\Core\Utility\MathUtility::canBeInterpretedAsInteger($int));
112 }
113
114 /**
115 * Data provider for testIntReturnsFalse
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 calculateWithPriorityToAdditionAndSubtraction
163 //////////////////////////////////
164 /**
165 * Data provider for calculateWithPriorityToAdditionAndSubtraction
166 *
167 * @return array expected values, arithmetic expression
168 */
169 public function calculateWithPriorityToAdditionAndSubtractionDataProvider() {
170 return array(
171 'add' => array(9, '6 + 3'),
172 'substract with positive result' => array(3, '6 - 3'),
173 'substract with negative result' => array(-3, '3 - 6'),
174 'multiply' => array(6, '2 * 3'),
175 'divide' => array(2.5, '5 / 2'),
176 'modulus' => array(1, '5 % 2'),
177 'power' => array(8, '2 ^ 3'),
178 'three operands with non integer result' => array(6.5, '5 + 3 / 2'),
179 'three operands with power' => array(14, '5 + 3 ^ 2'),
180 'three operads with modulus' => array(4, '5 % 2 + 3'),
181 'four operands' => array(3, '2 + 6 / 2 - 2'),
182 'division by zero when dividing' => array('ERROR: dividing by zero', '2 / 0'),
183 'division by zero with modulus' => array('ERROR: dividing by zero', '2 % 0')
184 );
185 }
186
187 /**
188 * @test
189 * @dataProvider calculateWithPriorityToAdditionAndSubtractionDataProvider
190 */
191 public function calculateWithPriorityToAdditionAndSubtractionCorrectlyCalculatesExpression($expected, $expression) {
192 $this->assertEquals($expected, \TYPO3\CMS\Core\Utility\MathUtility::calculateWithPriorityToAdditionAndSubtraction($expression));
193 }
194
195 //////////////////////////////////
196 // Tests concerning calcParenthesis
197 //////////////////////////////////
198 /**
199 * Data provider for calcParenthesis
200 *
201 * @return array expected values, arithmetic expression
202 */
203 public function calculateWithParenthesesDataProvider() {
204 return array(
205 'starts with parenthesis' => array(18, '(6 + 3) * 2'),
206 'ends with parenthesis' => array(6, '2 * (6 - 3)'),
207 'multiple parentheses' => array(-6, '(3 - 6) * (4 - 2)'),
208 'nested parentheses' => array(22, '2 * (3 + 2 + (3 * 2))'),
209 'parenthesis with division' => array(15, '5 / 2 * (3 * 2)')
210 );
211 }
212
213 /**
214 * @test
215 * @dataProvider calculateWithParenthesesDataProvider
216 */
217 public function calculateWithParenthesesCorrectlyCalculatesExpression($expected, $expression) {
218 $this->assertEquals($expected, \TYPO3\CMS\Core\Utility\MathUtility::calculateWithParentheses($expression));
219 }
220
221 //////////////////////////////////
222 // Tests concerning isIntegerInRange
223 //////////////////////////////////
224 /**
225 * @test
226 */
227 public function isIntegerInRangeIncludesLowerBoundary() {
228 $this->assertTrue(\TYPO3\CMS\Core\Utility\MathUtility::isIntegerInRange(1, 1, 2));
229 }
230
231 /**
232 * @test
233 */
234 public function isIntegerInRangeIncludesUpperBoundary() {
235 $this->assertTrue(\TYPO3\CMS\Core\Utility\MathUtility::isIntegerInRange(2, 1, 2));
236 }
237
238 /**
239 * @test
240 */
241 public function isIntegerInRangeAcceptsValueInRange() {
242 $this->assertTrue(\TYPO3\CMS\Core\Utility\MathUtility::isIntegerInRange(10, 1, 100));
243 }
244
245 /**
246 * @test
247 */
248 public function isIntegerInRangeRejectsValueOutsideOfRange() {
249 $this->assertFalse(\TYPO3\CMS\Core\Utility\MathUtility::isIntegerInRange(10, 1, 2));
250 }
251
252 /**
253 * Data provider or isIntegerInRangeRejectsOtherDataTypes
254 */
255 public function isIntegerInRangeRejectsOtherDataTypesDataProvider() {
256 return array(
257 'negative integer' => array(-1),
258 'float' => array(1.5),
259 'string' => array('string'),
260 'array' => array(array()),
261 'object' => array(new \stdClass()),
262 'boolean FALSE' => array(FALSE),
263 'NULL' => array(NULL)
264 );
265 }
266
267 /**
268 * @test
269 * @dataProvider isIntegerInRangeRejectsOtherDataTypesDataProvider
270 */
271 public function isIntegerInRangeRejectsOtherDataTypes($inputValue) {
272 $this->assertFalse(\TYPO3\CMS\Core\Utility\MathUtility::isIntegerInRange($inputValue, 0, 10));
273 }
274
275 }
276
277 ?>