Make WordPress Core

Ticket #13317: 13317-test_int.php

File 13317-test_int.php, 8.7 KB (added by hakre, 15 years ago)

Testcases

Line 
1<pre>
2<?php 
3/*
4 * intval and casting integers tests
5 *
6 * Last Updated: 13. May 2010
7 * 
8 * @author hakre <http://hakre.wordpress.com/>
9 */
10
11$values = array(
12        array('value' => 42                     , 'intval' => 42),
13        array('value' => -42                    , 'intval' => -42),
14        array('value' => 4.2                    , 'intval' => 4  ),
15        array('value' => '42'                   , 'intval' => 42 ),
16        array('value' => '+42'                  , 'intval' => 42 ),
17        array('value' => '-42'                  , 'intval' => -42),
18        array('value' => 042                    , 'intval' => 34        , 'notation' => '042' ), # 34
19        array('value' => '042'                  , 'intval' => 42 ),
20        array('value' => 1e10                   , 'intval' => 1410065408, 'notation' => '1e10'), # 10000000000
21        array('value' => '1e10'                 , 'intval' => 1  ),
22        array('value' => 0x1A                   , 'intval' => 26        , 'notation' => '0x1A'), # 26
23        array('value' => 42000000               , 'intval' => 42000000),
24        array('value' => 2147483647             , 'intval' => 2147483647),
25        array('value' => 2147483648             , 'intval' => -2147483648),
26        array('value' => 420000000000000000000  , 'intval' => 0         , 'notation' => '420000000000000000000'), # 4.2E+20
27        array('value' => '420000000000000000000', 'intval' => 2147483647),
28        array('value' => array()                , 'intval' => 0  ),
29        array('value' => array('foo', 'bar')    , 'intval' => 1  ),
30);
31
32$tests  = array_keys($values);
33$labels = array(0 => 'pass.', 1 => 'failed.', 2 => 'different.', 3 => 'error.' , 4 => 'skipped.');
34
35printf ("PHP %s %s/%s - %s\n\n", PHP_VERSION, PHP_OS, PHP_SAPI, str_replace(' +0000', ' UTC ', gmdate('r')));
36
37print "\n* TEST #1: Comparison of results intval() and casting to integer:\n\n";
38print "#   INPUT                     EXPECTED      INTVAL()      CAST        RESULT\n";
39print "----------------------------------------------------------------------------\n";
40foreach ($tests as $test) {
41        flush();
42        $value    = $values[$test];     
43        $input    = $value['value'];
44        $notation = test_varnotation($value);
45        $expected = $value['intval'];
46        $intval   = intval($input);
47        $castval  = (int) $input;
48        $result   = 1 - (int) ($expected == $intval && $intval === $castval);
49        $label    = $labels[$result];
50        printf('#%-2s %-23s : %-11s / %-11s / %-11s  %s', $test, $notation, $expected, $intval, $castval, $label);
51       
52        print "\n";
53}
54print "\n";
55flush();
56
57print "\n* TEST #2: Comparison of results intval() and 0 + \$var:\n\n";
58print "#   INPUT                     EXPECTED      INTVAL()      0+\$VAR      RESULT\n";
59print "----------------------------------------------------------------------------\n";
60foreach ($tests as $test) {
61        flush();
62        $value    = $values[$test];     
63        $input    = $value['value'];
64        $notation = test_varnotation($value);
65        $expected = $value['intval'];
66        $intval   = intval($input);
67        if (is_array($input)) {
68                $castval  = $labels[3];
69                $label    = $labels[4]; 
70        } else {       
71                $castval  = 0 + $input;
72                $result   = 1 - (int) ($expected == $intval && $intval === $castval);
73                $label    = $labels[$result * 2];
74        }
75        printf('#%-2s %-23s : %-11s / %-11s / %-11s  %s', $test, $notation, $expected, $intval, $castval, $label);
76       
77        print "\n";
78}
79print "\n";
80flush();
81
82print "\n* TEST #3: abs((int)\$val) vs. (int) abs(\$val) \n\n";
83print "#   INPUT                     ABS((INT)\$val)   (INT)ABS(\$val)      RESULT\n";
84print "----------------------------------------------------------------------------\n";
85foreach ($tests as $test) {
86        flush();
87        $value    = $values[$test];     
88        $input    = $value['value'];   
89        $notation = test_varnotation($value);
90        $absint   = abs((int) $input);
91        $intabs   = (int) abs($input);
92        $result   = 1 - (int) ($absint === $intabs);
93        $label    = $labels[$result * 2];
94        printf('#%-2s %-23s : %-14s / %-11s        %s', $test, $notation, $absint, $intabs, $label);   
95        print "\n";
96}
97print "\n";
98flush();
99
100print "\n* TEST #4: abs((int)\$val) vs. absint(\$val) \n\n";
101print "#   INPUT                     ABS((INT)\$val)   ABSINT(\$val)       RESULT\n";
102print "----------------------------------------------------------------------------\n";
103foreach ($tests as $test) {
104        flush();
105        $value    = $values[$test];     
106        $input    = $value['value'];
107        $notation = test_varnotation($value);
108        $absint   = abs((int) $input);
109        $intabs   = absint($input);
110        $result   = 1 - (int) ($absint === $intabs);
111        $label    = $labels[$result * 2];
112        printf('#%-2s %-23s : %-14s / %-10s         %s', $test, $notation, $absint, $intabs, $label);   
113        print "\n";
114}
115print "\n";
116flush();
117
118ini_set("max_execution_time", "300");
119$iterations = 100000;
120
121print "\n* TEST #5: Speed comparison (int)\$val  vs. intval(\$val) \n\n";
122print "#   INPUT                     (INT)\$val        INTVAL(\$val)       RATIO\n";
123print "----------------------------------------------------------------------------\n";
124foreach ($tests as $test) {
125        flush();
126        $value    = $values[$test];     
127        $input    = $value['value'];
128        $notation = test_varnotation($value);
129       
130        $start = microtime(true);
131        for ($i = 0; $i < $iterations; $i++) {
132                $calc = (int) $input;
133        }
134        $stop = microtime(true);
135        $timea = $stop - $start;
136
137        $start = microtime(true);
138        for ($i = 0; $i < $iterations; $i++) {
139                $calc = intval($input);
140        }
141        $stop = microtime(true);
142        $timeb = $stop - $start;
143       
144       
145        $ratio = ($timeb / $timea) * 100;
146        printf('#%-2s %-23s : %-12f   / %-12f         %3d%%', $test, $notation, $timea, $timeb, $ratio);       
147        print "\n";
148}
149printf("(%s iterations each)\n\n", number_format($iterations));
150flush();
151
152print "\n* TEST #6: Speed comparison settype() vs. intval() \n\n";
153print "#   INPUT                     SETTYPE()         INTVAL(\$val)       RATIO\n";
154print "----------------------------------------------------------------------------\n";
155foreach ($tests as $test) {
156        flush();
157        $value    = $values[$test];     
158        $input    = $value['value'];
159        $notation = test_varnotation($value);
160       
161        $start = microtime(true);
162        for ($i = 0; $i < $iterations; $i++) {
163                $calc = $input;
164                $r    = settype( $calc, 'int' );
165        }
166        $stop = microtime(true);
167        $timea = $stop - $start;
168
169        $start = microtime(true);
170        for ($i = 0; $i < $iterations; $i++) {
171                $calc = $input;
172                $calc = intval($input);
173        }
174        $stop = microtime(true);
175        $timeb = $stop - $start;
176       
177       
178        $ratio = ($timeb / $timea) * 100;
179        printf('#%-2s %-23s : %-12f   / %-12f         %3d%%', $test, $notation, $timea, $timeb, $ratio);       
180        print "\n";
181}
182printf("(%s iterations each)\n\n", number_format($iterations));
183flush();
184
185print "\n* TEST #7: Speed comparison abs((int)\$val) vs. absint(\$val) \n\n";
186print "#   INPUT                     ABS((INT)\$val)   ABSINT(\$val)       RATIO\n";
187print "----------------------------------------------------------------------------\n";
188foreach ($tests as $test) {
189        flush();
190        $value    = $values[$test];     
191        $input    = $value['value'];
192        $notation = test_varnotation($value);
193       
194        $start = microtime(true);
195        for ($i = 0; $i < $iterations; $i++) {
196                $calc = abs((int) $input);
197        }
198        $stop = microtime(true);
199        $timea = $stop - $start;
200
201        $start = microtime(true);
202        for ($i = 0; $i < $iterations; $i++) {
203                $calc = absint($input);
204        }
205        $stop = microtime(true);
206        $timeb = $stop - $start;
207       
208       
209        $ratio = ($timeb / $timea) * 100;
210        printf('#%-2s %-23s : %-12f   / %-12f         %3d%%', $test, $notation, $timea, $timeb, $ratio);       
211        print "\n";
212}
213printf("(%s iterations each)\n\n", number_format($iterations));
214flush(); 
215
216print "\n* TEST #8: Speed comparison (int)\$val vs. 0 + \$val \n\n";
217print "#   INPUT                     (INT)\$val        0+\$val             RATIO\n";
218print "----------------------------------------------------------------------------\n";
219foreach ($tests as $test) {
220        flush();
221        $value    = $values[$test];     
222        $input    = $value['value'];
223        $notation = test_varnotation($value);
224       
225        if (is_array($input)) {
226                printf('#%-2s %-23s : %s', $test, $notation, $labels[4]);
227                print "\n";
228                continue;
229        }
230       
231        $start = microtime(true);
232        for ($i = 0; $i < $iterations; $i++) {
233                $calc = (int) $input;
234        }
235        $stop = microtime(true);
236        $timea = $stop - $start;
237
238        $start = microtime(true);
239        for ($i = 0; $i < $iterations; $i++) {
240                $calc = 0 + $input;
241        }
242        $stop = microtime(true);
243        $timeb = $stop - $start;       
244       
245        $ratio = ($timeb / $timea) * 100;
246        printf('#%-2s %-23s : %-12f   / %-12f         %3d%%', $test, $notation, $timea, $timeb, $ratio);       
247        print "\n";
248}
249printf("(%s iterations each)\n\n", number_format($iterations));
250flush(); 
251
252print "</pre>\n";
253return;
254
255function test_varnotation(array $test) {
256        if (isset($test['notation'])) {
257                return $test['notation'];
258        }
259        $input = $test['value'];
260       
261        $notation = preg_replace('(\n|\s+)', ' ' , var_export($input, true));
262        $notation = str_replace(array('array ( ',' 0 => ','  1 => ',', )'), array('array(', '',' ',')'), $notation);
263        return $notation;
264}
265
266function absint( $maybeint ) {
267        return abs( intval( $maybeint ) );
268}
269
270# This is the last line in the fil ?>