2 /***************************************************************
5 * (c) 2011 Susanne Moog <typo3@susanne-moog.de>
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.
14 * The GNU General Public License can be found at
15 * http://www.gnu.org/copyleft/gpl.html.
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.
22 * This copyright notice MUST APPEAR in all copies of the script!
23 ***************************************************************/
25 require_once('fixture/t3lib_utility_math_fixture_classWithStringRepresentation.php');
28 * Testcase for class t3lib_utility_Math
30 * @author Susanne Moog <typo3@susanne-moog.de>
36 class t3lib_utility_MathTest
extends tx_phpunit_testcase
{
38 //////////////////////////////////
39 // Tests concerning forceIntegerInRange
40 //////////////////////////////////
42 * Data provider for forceIntegerInRangeForcesIntegerIntoBoundaries
44 * @return array expected values, arithmetic expression
46 public function forceIntegerInRangeForcesIntegerIntoDefaultBoundariesDataProvider() {
48 'negativeValue' => array(0, -10),
49 'normalValue' => array(30, 30),
50 'veryHighValue' => array(2000000000, PHP_INT_MAX
),
51 'zeroValue' => array(0, 0),
52 'anotherNormalValue' => array(12309, 12309)
58 * @dataProvider forceIntegerInRangeForcesIntegerIntoDefaultBoundariesDataProvider
60 public function forceIntegerInRangeForcesIntegerIntoDefaultBoundaries($expected, $value) {
61 $this->assertEquals($expected, t3lib_utility_Math
::forceIntegerInRange($value, 0));
67 public function forceIntegerInRangeSetsDefaultValueIfZeroValueIsGiven() {
68 $this->assertEquals(42, t3lib_utility_Math
::forceIntegerInRange('', 0, 2000000000, 42));
71 //////////////////////////////////
72 // Tests concerning convertToPositiveInteger
73 //////////////////////////////////
77 public function convertToPositiveIntegerReturnsZeroForNegativeValues() {
78 $this->assertEquals(0, t3lib_utility_Math
::convertToPositiveInteger(-123));
84 public function convertToPositiveIntegerReturnsTheInputValueForPositiveValues() {
85 $this->assertEquals(123, t3lib_utility_Math
::convertToPositiveInteger(123));
88 ///////////////////////////////
89 // Tests concerning testInt
90 ///////////////////////////////
93 * Data provider for canBeInterpretedAsIntegerReturnsTrue
95 * @return array Data sets
97 public function functionCanBeInterpretedAsIntegerValidDataProvider() {
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'),
105 'zero as string' => array('0'),
111 * @dataProvider functionCanBeInterpretedAsIntegerValidDataProvider
113 public function testIntReturnsTrue($int) {
114 $this->assertTrue(t3lib_utility_Math
::canBeInterpretedAsInteger($int));
118 * Data provider for testIntReturnsFalse
120 * @return array Data sets
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('');
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),
158 * @dataProvider functionCanBeInterpretedAsIntegerInvalidDataProvider
160 public function canBeInterpretedAsIntegerReturnsFalse($int) {
161 $this->assertFalse(t3lib_utility_Math
::canBeInterpretedAsInteger($int));
164 //////////////////////////////////
165 // Tests concerning calculateWithPriorityToAdditionAndSubtraction
166 //////////////////////////////////
169 * Data provider for calculateWithPriorityToAdditionAndSubtraction
171 * @return array expected values, arithmetic expression
173 public function calculateWithPriorityToAdditionAndSubtractionDataProvider() {
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')
193 * @dataProvider calculateWithPriorityToAdditionAndSubtractionDataProvider
195 public function calculateWithPriorityToAdditionAndSubtractionCorrectlyCalculatesExpression($expected, $expression) {
196 $this->assertEquals($expected, t3lib_utility_Math
::calculateWithPriorityToAdditionAndSubtraction($expression));
200 //////////////////////////////////
201 // Tests concerning calcParenthesis
202 //////////////////////////////////
205 * Data provider for calcParenthesis
207 * @return array expected values, arithmetic expression
209 public function calculateWithParenthesesDataProvider() {
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)'),
221 * @dataProvider calculateWithParenthesesDataProvider
223 public function calculateWithParenthesesCorrectlyCalculatesExpression($expected, $expression) {
224 $this->assertEquals($expected, t3lib_utility_Math
::calculateWithParentheses($expression));