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