[BUGFIX] canBeInterpretedAsInteger fatals if given an object
[Packages/TYPO3.CMS.git] / tests / 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
25 require_once('fixture/t3lib_utility_math_fixture_classWithStringRepresentation.php');
26
27 /**
28 * Testcase for class t3lib_utility_Math
29 *
30 * @author Susanne Moog <typo3@susanne-moog.de>
31 *
32 * @package TYPO3
33 * @subpackage t3lib
34 */
35
36 class t3lib_utility_MathTest extends tx_phpunit_testcase {
37
38 //////////////////////////////////
39 // Tests concerning forceIntegerInRange
40 //////////////////////////////////
41 /**
42 * Data provider for forceIntegerInRangeForcesIntegerIntoBoundaries
43 *
44 * @return array expected values, arithmetic expression
45 */
46 public function forceIntegerInRangeForcesIntegerIntoDefaultBoundariesDataProvider() {
47 return array(
48 'negativeValue' => array(0, -10),
49 'normalValue' => array(30, 30),
50 'veryHighValue' => array(2000000000, 3000000001),
51 'zeroValue' => array(0, 0),
52 'anotherNormalValue' => array(12309, 12309)
53 );
54 }
55
56 /**
57 * @test
58 * @dataProvider forceIntegerInRangeForcesIntegerIntoDefaultBoundariesDataProvider
59 */
60 public function forceIntegerInRangeForcesIntegerIntoDefaultBoundaries($expected, $value) {
61 $this->assertEquals($expected, t3lib_utility_Math::forceIntegerInRange($value, 0));
62 }
63
64 /**
65 * @test
66 */
67 public function forceIntegerInRangeSetsDefaultValueIfZeroValueIsGiven() {
68 $this->assertEquals(42, t3lib_utility_Math::forceIntegerInRange('', 0, 2000000000, 42));
69 }
70
71 //////////////////////////////////
72 // Tests concerning convertToPositiveInteger
73 //////////////////////////////////
74 /**
75 * @test
76 */
77 public function convertToPositiveIntegerReturnsZeroForNegativeValues() {
78 $this->assertEquals(0, t3lib_utility_Math::convertToPositiveInteger(-123));
79 }
80
81 /**
82 * @test
83 */
84 public function convertToPositiveIntegerReturnsTheInputValueForPositiveValues() {
85 $this->assertEquals(123, t3lib_utility_Math::convertToPositiveInteger(123));
86 }
87
88 ///////////////////////////////
89 // Tests concerning testInt
90 ///////////////////////////////
91
92 /**
93 * Data provider for canBeInterpretedAsIntegerReturnsTrue
94 *
95 * @return array Data sets
96 */
97 public function functionCanBeInterpretedAsIntegerValidDataProvider() {
98 return array(
99 'int' => array(32425),
100 'negative int' => array(-32425),
101 'largest int' => array(PHP_INT_MAX),
102 'int as string' => array('32425'),
103 'negative int as string' => array('-32425'),
104 'zero' => array(0),
105 'zero as string' => array('0'),
106 );
107 }
108
109 /**
110 * @test
111 * @dataProvider functionCanBeInterpretedAsIntegerValidDataProvider
112 */
113 public function testIntReturnsTrue($int) {
114 $this->assertTrue(t3lib_utility_Math::canBeInterpretedAsInteger($int));
115 }
116
117 /**
118 * Data provider for testIntReturnsFalse
119 *
120 * @return array Data sets
121 */
122 public function functionCanBeInterpretedAsIntegerInvalidDataProvider() {
123 $objectWithNumericalStringRepresentation = new t3lib_utility_math_fixture_classWithStringRepresentation();
124 $objectWithNumericalStringRepresentation->setString('1234');
125 $objectWithNonNumericalStringRepresentation = new t3lib_utility_math_fixture_classWithStringRepresentation();
126 $objectWithNonNumericalStringRepresentation->setString('foo');
127 $objectWithEmptyStringRepresentation = new t3lib_utility_math_fixture_classWithStringRepresentation();
128 $objectWithEmptyStringRepresentation->setString('');
129 return array(
130 'int as string with leading zero' => array('01234'),
131 'positive int as string with plus modifier' => array('+1234'),
132 'negative int as string with leading zero' => array('-01234'),
133 'largest int plus one' => array(PHP_INT_MAX + 1),
134 'string' => array('testInt'),
135 'empty string' => array(''),
136 'int in string' => array('5 times of testInt'),
137 'int as string with space after' => array('5 '),
138 'int as string with space before' => array(' 5'),
139 'int as string with many spaces before' => array(' 5'),
140 'float' => array(3.14159),
141 'float as string' => array('3.14159'),
142 'float as string only a dot' => array('10.'),
143 'float as string trailing zero would evaluate to int 10' => array('10.0'),
144 'float as string trailing zeros would evaluate to int 10' => array('10.00'),
145 'null' => array(NULL),
146 'empty array' => array(array()),
147 'int in array' => array(array(32425)),
148 'int as string in array' => array(array('32425')),
149 'object without string representation' => array(new stdClass()),
150 'object with numerical string representation' => array($objectWithNumericalStringRepresentation),
151 'object without numerical string representation' => array($objectWithNonNumericalStringRepresentation),
152 'object with empty string representation' => array($objectWithEmptyStringRepresentation),
153 );
154 }
155
156 /**
157 * @test
158 * @dataProvider functionCanBeInterpretedAsIntegerInvalidDataProvider
159 */
160 public function canBeInterpretedAsIntegerReturnsFalse($int) {
161 $this->assertFalse(t3lib_utility_Math::canBeInterpretedAsInteger($int));
162 }
163
164 //////////////////////////////////
165 // Tests concerning calculateWithPriorityToAdditionAndSubtraction
166 //////////////////////////////////
167
168 /**
169 * Data provider for calculateWithPriorityToAdditionAndSubtraction
170 *
171 * @return array expected values, arithmetic expression
172 */
173 public function calculateWithPriorityToAdditionAndSubtractionDataProvider() {
174 return array(
175 'add' => array(9, '6 + 3'),
176 'substract with positive result' => array(3, '6 - 3'),
177 'substract with negative result' => array(-3, '3 - 6'),
178 'multiply' => array(6, '2 * 3'),
179 'divide' => array(2.5, '5 / 2'),
180 'modulus' => array(1, '5 % 2'),
181 'power' => array(8, '2 ^ 3'),
182 'three operands with non integer result' => array(6.5, '5 + 3 / 2'),
183 'three operands with power' => array(14, '5 + 3 ^ 2'),
184 'three operads with modulus' => array(4, '5 % 2 + 3'),
185 'four operands' => array(3, '2 + 6 / 2 - 2'),
186 'division by zero when dividing' => array('ERROR: dividing by zero', '2 / 0'),
187 'division by zero with modulus' => array('ERROR: dividing by zero', '2 % 0')
188 );
189 }
190
191 /**
192 * @test
193 * @dataProvider calculateWithPriorityToAdditionAndSubtractionDataProvider
194 */
195 public function calculateWithPriorityToAdditionAndSubtractionCorrectlyCalculatesExpression($expected, $expression) {
196 $this->assertEquals($expected, t3lib_utility_Math::calculateWithPriorityToAdditionAndSubtraction($expression));
197 }
198
199
200 //////////////////////////////////
201 // Tests concerning calcParenthesis
202 //////////////////////////////////
203
204 /**
205 * Data provider for calcParenthesis
206 *
207 * @return array expected values, arithmetic expression
208 */
209 public function calculateWithParenthesesDataProvider() {
210 return array(
211 'starts with parenthesis' => array(18, '(6 + 3) * 2'),
212 'ends with parenthesis' => array(6, '2 * (6 - 3)'),
213 'multiple parentheses' => array(-6, '(3 - 6) * (4 - 2)'),
214 'nested parentheses' => array(22, '2 * (3 + 2 + (3 * 2))'),
215 'parenthesis with division' => array(15, '5 / 2 * (3 * 2)'),
216 );
217 }
218
219 /**
220 * @test
221 * @dataProvider calculateWithParenthesesDataProvider
222 */
223 public function calculateWithParenthesesCorrectlyCalculatesExpression($expected, $expression) {
224 $this->assertEquals($expected, t3lib_utility_Math::calculateWithParentheses($expression));
225 }
226 }
227
228 ?>