acbbed13b9923b21a834ab3a3e5edc2ca33febbe
[Packages/TYPO3.CMS.git] / tests / Unit / t3lib / utility / class.t3lib_utility_mathTest.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 2011 Susanne Moog <typo3@susanne-moog.de>
6 * All rights reserved
7 *
8 * This script is part of the TYPO3 project. The TYPO3 project is
9 * free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * The GNU General Public License can be found at
15 * http://www.gnu.org/copyleft/gpl.html.
16 *
17 * This script is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * This copyright notice MUST APPEAR in all copies of the script!
23 ***************************************************************/
24 require_once 'fixture/t3lib_utility_math_fixture_classWithStringRepresentation.php';
25 /**
26 * Testcase for class t3lib_utility_Math
27 *
28 * @author Susanne Moog <typo3@susanne-moog.de>
29 * @package TYPO3
30 * @subpackage t3lib
31 */
32 class t3lib_utility_MathTest extends tx_phpunit_testcase {
33
34 //////////////////////////////////
35 // Tests concerning forceIntegerInRange
36 //////////////////////////////////
37 /**
38 * Data provider for forceIntegerInRangeForcesIntegerIntoBoundaries
39 *
40 * @return array expected values, arithmetic expression
41 */
42 public function forceIntegerInRangeForcesIntegerIntoDefaultBoundariesDataProvider() {
43 return array(
44 'negativeValue' => array(0, -10),
45 'normalValue' => array(30, 30),
46 'veryHighValue' => array(2000000000, PHP_INT_MAX),
47 'zeroValue' => array(0, 0),
48 'anotherNormalValue' => array(12309, 12309)
49 );
50 }
51
52 /**
53 * @test
54 * @dataProvider forceIntegerInRangeForcesIntegerIntoDefaultBoundariesDataProvider
55 */
56 public function forceIntegerInRangeForcesIntegerIntoDefaultBoundaries($expected, $value) {
57 $this->assertEquals($expected, \TYPO3\CMS\Core\Utility\MathUtility::forceIntegerInRange($value, 0));
58 }
59
60 /**
61 * @test
62 */
63 public function forceIntegerInRangeSetsDefaultValueIfZeroValueIsGiven() {
64 $this->assertEquals(42, \TYPO3\CMS\Core\Utility\MathUtility::forceIntegerInRange('', 0, 2000000000, 42));
65 }
66
67 //////////////////////////////////
68 // Tests concerning convertToPositiveInteger
69 //////////////////////////////////
70 /**
71 * @test
72 */
73 public function convertToPositiveIntegerReturnsZeroForNegativeValues() {
74 $this->assertEquals(0, \TYPO3\CMS\Core\Utility\MathUtility::convertToPositiveInteger(-123));
75 }
76
77 /**
78 * @test
79 */
80 public function convertToPositiveIntegerReturnsTheInputValueForPositiveValues() {
81 $this->assertEquals(123, \TYPO3\CMS\Core\Utility\MathUtility::convertToPositiveInteger(123));
82 }
83
84 ///////////////////////////////
85 // Tests concerning testInt
86 ///////////////////////////////
87 /**
88 * Data provider for canBeInterpretedAsIntegerReturnsTrue
89 *
90 * @return array Data sets
91 */
92 public function functionCanBeInterpretedAsIntegerValidDataProvider() {
93 return array(
94 'int' => array(32425),
95 'negative int' => array(-32425),
96 'largest int' => array(PHP_INT_MAX),
97 'int as string' => array('32425'),
98 'negative int as string' => array('-32425'),
99 'zero' => array(0),
100 'zero as string' => array('0')
101 );
102 }
103
104 /**
105 * @test
106 * @dataProvider functionCanBeInterpretedAsIntegerValidDataProvider
107 */
108 public function testIntReturnsTrue($int) {
109 $this->assertTrue(\TYPO3\CMS\Core\Utility\MathUtility::canBeInterpretedAsInteger($int));
110 }
111
112 /**
113 * Data provider for testIntReturnsFalse
114 *
115 * @return array Data sets
116 */
117 public function functionCanBeInterpretedAsIntegerInvalidDataProvider() {
118 $objectWithNumericalStringRepresentation = new t3lib_utility_math_fixture_classWithStringRepresentation();
119 $objectWithNumericalStringRepresentation->setString('1234');
120 $objectWithNonNumericalStringRepresentation = new t3lib_utility_math_fixture_classWithStringRepresentation();
121 $objectWithNonNumericalStringRepresentation->setString('foo');
122 $objectWithEmptyStringRepresentation = new t3lib_utility_math_fixture_classWithStringRepresentation();
123 $objectWithEmptyStringRepresentation->setString('');
124 return array(
125 'int as string with leading zero' => array('01234'),
126 'positive int as string with plus modifier' => array('+1234'),
127 'negative int as string with leading zero' => array('-01234'),
128 'largest int plus one' => array(PHP_INT_MAX + 1),
129 'string' => array('testInt'),
130 'empty string' => array(''),
131 'int in string' => array('5 times of testInt'),
132 'int as string with space after' => array('5 '),
133 'int as string with space before' => array(' 5'),
134 'int as string with many spaces before' => array(' 5'),
135 'float' => array(3.14159),
136 'float as string' => array('3.14159'),
137 'float as string only a dot' => array('10.'),
138 'float as string trailing zero would evaluate to int 10' => array('10.0'),
139 'float as string trailing zeros would evaluate to int 10' => array('10.00'),
140 'null' => array(NULL),
141 'empty array' => array(array()),
142 'int in array' => array(array(32425)),
143 'int as string in array' => array(array('32425')),
144 'object without string representation' => array(new stdClass()),
145 'object with numerical string representation' => array($objectWithNumericalStringRepresentation),
146 'object without numerical string representation' => array($objectWithNonNumericalStringRepresentation),
147 'object with empty string representation' => array($objectWithEmptyStringRepresentation)
148 );
149 }
150
151 /**
152 * @test
153 * @dataProvider functionCanBeInterpretedAsIntegerInvalidDataProvider
154 */
155 public function canBeInterpretedAsIntegerReturnsFalse($int) {
156 $this->assertFalse(\TYPO3\CMS\Core\Utility\MathUtility::canBeInterpretedAsInteger($int));
157 }
158
159 //////////////////////////////////
160 // Tests concerning calculateWithPriorityToAdditionAndSubtraction
161 //////////////////////////////////
162 /**
163 * Data provider for calculateWithPriorityToAdditionAndSubtraction
164 *
165 * @return array expected values, arithmetic expression
166 */
167 public function calculateWithPriorityToAdditionAndSubtractionDataProvider() {
168 return array(
169 'add' => array(9, '6 + 3'),
170 'substract with positive result' => array(3, '6 - 3'),
171 'substract with negative result' => array(-3, '3 - 6'),
172 'multiply' => array(6, '2 * 3'),
173 'divide' => array(2.5, '5 / 2'),
174 'modulus' => array(1, '5 % 2'),
175 'power' => array(8, '2 ^ 3'),
176 'three operands with non integer result' => array(6.5, '5 + 3 / 2'),
177 'three operands with power' => array(14, '5 + 3 ^ 2'),
178 'three operads with modulus' => array(4, '5 % 2 + 3'),
179 'four operands' => array(3, '2 + 6 / 2 - 2'),
180 'division by zero when dividing' => array('ERROR: dividing by zero', '2 / 0'),
181 'division by zero with modulus' => array('ERROR: dividing by zero', '2 % 0')
182 );
183 }
184
185 /**
186 * @test
187 * @dataProvider calculateWithPriorityToAdditionAndSubtractionDataProvider
188 */
189 public function calculateWithPriorityToAdditionAndSubtractionCorrectlyCalculatesExpression($expected, $expression) {
190 $this->assertEquals($expected, \TYPO3\CMS\Core\Utility\MathUtility::calculateWithPriorityToAdditionAndSubtraction($expression));
191 }
192
193 //////////////////////////////////
194 // Tests concerning calcParenthesis
195 //////////////////////////////////
196 /**
197 * Data provider for calcParenthesis
198 *
199 * @return array expected values, arithmetic expression
200 */
201 public function calculateWithParenthesesDataProvider() {
202 return array(
203 'starts with parenthesis' => array(18, '(6 + 3) * 2'),
204 'ends with parenthesis' => array(6, '2 * (6 - 3)'),
205 'multiple parentheses' => array(-6, '(3 - 6) * (4 - 2)'),
206 'nested parentheses' => array(22, '2 * (3 + 2 + (3 * 2))'),
207 'parenthesis with division' => array(15, '5 / 2 * (3 * 2)')
208 );
209 }
210
211 /**
212 * @test
213 * @dataProvider calculateWithParenthesesDataProvider
214 */
215 public function calculateWithParenthesesCorrectlyCalculatesExpression($expected, $expression) {
216 $this->assertEquals($expected, \TYPO3\CMS\Core\Utility\MathUtility::calculateWithParentheses($expression));
217 }
218
219 //////////////////////////////////
220 // Tests concerning isIntegerInRange
221 //////////////////////////////////
222 /**
223 * @test
224 */
225 public function isIntegerInRangeIncludesLowerBoundary() {
226 $this->assertTrue(\TYPO3\CMS\Core\Utility\MathUtility::isIntegerInRange(1, 1, 2));
227 }
228
229 /**
230 * @test
231 */
232 public function isIntegerInRangeIncludesUpperBoundary() {
233 $this->assertTrue(\TYPO3\CMS\Core\Utility\MathUtility::isIntegerInRange(2, 1, 2));
234 }
235
236 /**
237 * @test
238 */
239 public function isIntegerInRangeAcceptsValueInRange() {
240 $this->assertTrue(\TYPO3\CMS\Core\Utility\MathUtility::isIntegerInRange(10, 1, 100));
241 }
242
243 /**
244 * @test
245 */
246 public function isIntegerInRangeRejectsValueOutsideOfRange() {
247 $this->assertFalse(\TYPO3\CMS\Core\Utility\MathUtility::isIntegerInRange(10, 1, 2));
248 }
249
250 /**
251 * Data provider or isIntegerInRangeRejectsOtherDataTypes
252 */
253 public function isIntegerInRangeRejectsOtherDataTypesDataProvider() {
254 return array(
255 'negative integer' => array(-1),
256 'float' => array(1.5),
257 'string' => array('string'),
258 'array' => array(array()),
259 'object' => array(new stdClass()),
260 'boolean FALSE' => array(FALSE),
261 'NULL' => array(NULL)
262 );
263 }
264
265 /**
266 * @test
267 * @dataProvider isIntegerInRangeRejectsOtherDataTypesDataProvider
268 */
269 public function isIntegerInRangeRejectsOtherDataTypes($inputValue) {
270 $this->assertFalse(\TYPO3\CMS\Core\Utility\MathUtility::isIntegerInRange($invalidValue, 0, 10));
271 }
272
273 }
274
275 ?>