WordPress.org

Make WordPress Core

Ticket #28559: absint.php.2.patch

File absint.php.2.patch, 13.8 KB (added by pbearne, 8 years ago)

patch with 64bit update

  • tests/phpunit/tests/functions/absint.php

     
     1<?php
     2/*
     3 * Test absint().
     4 *
     5 * @group functions.php
     6 *
     7*/
     8
     9/**
     10 * Converts value to nonnegative integer.
     11 *
     12 * @since 2.5.0
     13 *
     14 * @param mixed $maybeint Data you wish to have converted to a nonnegative integer
     15 * @return int An nonnegative integer
     16 */
     17// function absint( $maybeint ) {
     18//      return abs( intval( $maybeint ) );
     19// }
     20
     21
     22
     23
     24
     25class Tests_Absint extends WP_UnitTestCase {
     26        function setUp() {
     27                parent::setUp();
     28        }
     29        /*
     30        * helper funtion to switch tests for 64 bit systems
     31        */
     32        private function is_64_bit(){
     33                if ( 8 === PHP_INT_SIZE ){
     34                        return true;
     35                }
     36                return false;
     37        }
     38
     39        /**
     40         * testing numbers
     41         * note the 4.9999999999999999 becomes 5 not 4
     42         */
     43        function test_absint() {
     44                $this->assertEquals( absint( 1 ), 1, '1' );
     45                $this->assertEquals( absint( -1 ), 1, '-1' );
     46                $this->assertEquals( absint( 999 ), 999, '999' );
     47                $this->assertEquals( absint( -999 ), 999, '-999');
     48                $this->assertEquals( absint( 0 ), 0, '0');
     49                $this->assertEquals( absint( 4.0000000000000000001 ), 4, '4.0000000000000000001' );
     50                $this->assertEquals( absint( 4.4999999999999999999 ), 4, '4.4999999999999999999' );
     51                $this->assertEquals( absint( 4.5 ), 4, '4.5' );
     52                $this->assertEquals( absint( 4.5000000000000000001 ), 4, '4.5000000000000000001' );
     53                $this->assertEquals( absint( 4.51 ), 4, '4.51' );
     54                $this->assertEquals( absint( 4.6 ), 4, '4.6' );
     55                $this->assertEquals( absint( 4.7 ), 4, '4.7' );
     56                $this->assertEquals( absint( 4.8 ), 4, '4.8' );
     57                $this->assertEquals( absint( 4.9 ), 4, '4.9' ); // double(4.9)
     58                $this->assertEquals( absint( 4.99 ), 4, '4.99' );
     59                $this->assertEquals( absint( 4.999 ), 4, '4.999' );
     60                $this->assertEquals( absint( 4.9999 ), 4, '4.9999' );
     61                $this->assertEquals( absint( 4.99999 ), 4, '4.99999' );
     62                $this->assertEquals( absint( 4.999999 ), 4, '4.999999' );
     63                $this->assertEquals( absint( 4.9999999 ), 4, '4.9999999' );
     64                $this->assertEquals( absint( 4.99999999 ), 4, '4.99999999' );
     65                $this->assertEquals( absint( 4.999999999 ), 4, '4.999999999' );
     66                $this->assertEquals( absint( 4.9999999999 ), 4, '4.9999999999' );
     67                $this->assertEquals( absint( 4.99999999999 ), 4, '4.99999999999' );
     68                $this->assertEquals( absint( 4.999999999999 ), 4, '4.999999999999' );
     69                $this->assertEquals( absint( 4.9999999999999 ), 4, '4.999999999999' );// double(4.999999999999)
     70                $this->assertEquals( absint( 4.99999999999999 ), 4, '4.99999999999999' ); // double(5)
     71                $this->assertEquals( absint( 4.999999999999999 ), 4, '4.999999999999999' );
     72
     73                 // why does it swap to 4 where does the round up not round down come from
     74                $this->assertEquals( absint( 4.9999999999999999 ), 5, '4.9999999999999999' );
     75                $this->assertEquals( absint( 4.99999999999999999 ), 5, '4.99999999999999999' );
     76                $this->assertEquals( absint( 4.999999999999999999 ), 5, '4.999999999999999999' );
     77                $this->assertEquals( absint( 4.9999999999999999999 ), 5, '4.9999999999999999999' );
     78
     79                $this->assertEquals( absint( 4.11111111111111111111 ), 4, '4.11111111111111111111' );
     80                $this->assertEquals( absint( 4.555555555555555555555 ), 4, '4.555555555555555555555' );
     81
     82
     83                $this->assertEquals( absint( 4.99999999999999 ), 4,             '4.99999999999999' );
     84                $this->assertEquals( absint( 49.9999999999999 ), 49,            '49.9999999999999' );
     85                $this->assertEquals( absint( 499.999999999999 ), 499,           '499.999999999999' );
     86                $this->assertEquals( absint( 4999.99999999999 ), 4999,          '4999.99999999999' );
     87                $this->assertEquals( absint( 49999.9999999999 ), 49999,         '49999.9999999999' );
     88                $this->assertEquals( absint( 499999.999999999 ), 499999,        '499999.999999999' );
     89                $this->assertEquals( absint( 4999999.99999999 ), 4999999,       '4999999.99999999' );
     90                $this->assertEquals( absint( 49999999.9999999 ), 49999999,      '49999999.9999999' );
     91                $this->assertEquals( absint( 499999999.999999 ), 499999999,     '499999999.999999' );
     92
     93                $this->assertEquals( absint( 4.9999999999999999999 ), 5,                '4.9999999999999999999' );
     94                $this->assertEquals( absint( 49.999999999999999999 ), 50,               '49.999999999999999999' );
     95                $this->assertEquals( absint( 499.99999999999999999 ), 500,              '499.99999999999999999' );
     96                $this->assertEquals( absint( 4999.9999999999999999 ), 5000,             '4999.9999999999999999' );
     97                $this->assertEquals( absint( 49999.999999999999999 ), 50000,    '49999.999999999999999' );
     98                $this->assertEquals( absint( 499999.99999999999999 ), 500000,   '499999.99999999999999' );
     99                $this->assertEquals( absint( 4999999.9999999999999 ), 5000000,  '4999999.9999999999999' );
     100                $this->assertEquals( absint( 49999999.999999999999 ), 50000000,         '49999999.999999999999' );
     101                $this->assertEquals( absint( 499999999.99999999999 ), 500000000,        '499999999.99999999999' );
     102
     103
     104                if ( $this->is_64_bit() ){
     105                        $this->assertEquals( absint( 4999999999.9999999999 ), 5000000000,                       '4999999999.9999999999' );
     106                        $this->assertEquals( absint( 49999999999.999999999 ), 50000000000,              '49999999999.999999999' );
     107                        $this->assertEquals( absint( 499999999999.99999999 ), 500000000000,             '499999999999.99999999' );
     108                        $this->assertEquals( absint( 4999999999999.9999999 ), 5000000000000,            '4999999999999.9999999' );
     109                        $this->assertEquals( absint( 49999999999999.999999 ), 50000000000000,           '49999999999999.999999' );
     110                        $this->assertEquals( absint( 499999999999999.99999 ), 500000000000000,          '499999999999999.99999' );
     111                        $this->assertEquals( absint( 4999999999999999.9999 ), 5000000000000000,                 '4999999999999999.9999' );
     112                        $this->assertEquals( absint( 49999999999999999.999 ), 50000000000000000,        '49999999999999999.999' );
     113                        $this->assertEquals( absint( 499999999999999999.99 ), 500000000000000000,       '499999999999999999.99' );
     114                        $this->assertEquals( absint( 4999999999999999999.9 ), 5000000000000000000,      '4999999999999999999.9' );
     115                        $this->assertEquals( absint( 49999999999999999999 ),  5340232221128654848,      '49999999999999999999' );
     116                } else {
     117                        $this->assertEquals( absint( 4999999999.9999999999 ), 705032704,        '4999999999.9999999999' );
     118                        $this->assertEquals( absint( 49999999999.999999999 ), 1539607552,       '49999999999.999999999' );
     119                        $this->assertEquals( absint( 499999999999.99999999 ), 1783793664,       '499999999999.99999999' );
     120                        $this->assertEquals( absint( 4999999999999.9999999 ), 658067456,        '4999999999999.9999999' );
     121                        $this->assertEquals( absint( 49999999999999.999999 ), 2009260032,       '49999999999999.999999' );
     122                        $this->assertEquals( absint( 499999999999999.99999 ), 1382236160,       '499999999999999.99999' );
     123                        $this->assertEquals( absint( 4999999999999999.9999 ), 937459712,        '4999999999999999.9999' );
     124                        $this->assertEquals( absint( 49999999999999999.999 ), 784662528,        '49999999999999999.999' );
     125                        $this->assertEquals( absint( 499999999999999999.99 ), 743309312,        '499999999999999999.99' );
     126                }
     127
     128
     129                $this->assertEquals( absint( 5 ), 5, '5' );
     130                $this->assertEquals( absint( 5.0000000000000000001 ), 5, '5.0000000000000000001' );
     131                $this->assertEquals( absint( -4.0000000000000000001 ), 4, '-4.0000000000000000001' );
     132                $this->assertEquals( absint( -4.4999999999999999999 ), 4, '-4.4999999999999999999' );
     133                $this->assertEquals( absint( -4.5 ), 4, '-4.5' );
     134                $this->assertEquals( absint( -4.5000000000000000001 ), 4, '-4.5000000000000000001' );
     135                $this->assertEquals( absint( -4.51 ), 4, '-4.51' );
     136                $this->assertEquals( absint( -4.9999999999999999999 ), 5, '-4.9999999999999999999' );
     137                $this->assertEquals( absint( -5 ), 5, '-5' );
     138                $this->assertEquals( absint( -5.0000000000000000001 ), 5, '-5.0000000000000000001' );
     139
     140        }
     141
     142        /**
     143         *  test passing in strings
     144         */
     145        function test_absint_strings() {
     146
     147                $this->assertEquals( absint( '1' ), 1, '1' );
     148                $this->assertEquals( absint( '-1' ), 1, '-1');
     149                $this->assertEquals( absint( '1_string' ), 1, '1_string' );
     150                $this->assertEquals( absint( '-1_string' ), 1, '1_string' );
     151                $this->assertEquals( absint( '1_this_a_very_extra_long_string_that_so_long_that_might_brack_or_might_not_but_we_need_to_test' ), 1, '1_very_long_string' );
     152                $this->assertEquals( absint( '1 string with spaces' ), 1, '1 string with spaces' );
     153                $this->assertEquals( absint( '1_1' ), 1, '1_1' );
     154                $this->assertEquals( absint( '1 1 1 ' ), 1, '1 1 1 ' );
     155                $this->assertEquals( absint( '1 . 1 1 ' ), 1, '1 . 1 1 ' );
     156
     157                $number = '1';
     158                $this->assertEquals( absint( $number ), 1, '1$number' );
     159                $this->assertEquals( absint( "1$number" ), 11, '1$number' );
     160                $this->assertEquals( absint( "1".$number ), 11, '"1".$number' );
     161                $this->assertEquals( absint( '1'.$number ), 11, "'1'.$number" );
     162
     163                $number = 1;
     164                $this->assertEquals( absint( $number ), 1, '1$number' );
     165                $this->assertEquals( absint( "1$number" ), 11, '1$number' );
     166                $this->assertEquals( absint( "1".$number ), 11, '"1".$number' );
     167                $this->assertEquals( absint( '1'.$number ), 11, "'1'.$number" );
     168
     169
     170
     171                $this->assertEquals( absint( '999_string' ), 999, '999_string' );
     172                $this->assertEquals( absint( '0' ), 0, '0' );
     173                $this->assertEquals( absint( 'zero' ), 0, 'zero' );
     174                $this->assertEquals( absint( 'string_1' ), 0, 'string_1' );
     175                $this->assertEquals( absint( 'one' ), 0 ,'one' );
     176                $this->assertEquals( absint( 'two' ), 0 ,'two' );
     177                $this->assertEquals( absint( '-one' ), 0 ,'-one' );
     178                $this->assertEquals( absint( '-two' ), 0 ,'-two' );
     179                $this->assertEquals( absint( 'misus one' ), 0 ,'misus one' );
     180                $this->assertEquals( absint( 'misus two' ), 0 ,'misus two' );
     181
     182                // 1 to the power of 10
     183                $this->assertEquals( absint( '1e10' ), 1 ,'(string)1e10' );
     184                if ( $this->is_64_bit() ){
     185                        $this->assertEquals( absint( 1e10 ), 10000000000 ,'1e10 - 64' );
     186                        $this->assertEquals( absint( 1e20 ), 7766279631452241920,'1e20 - 64' );
     187
     188                } else{
     189                        $this->assertEquals( absint( 1e10 ), 1410065408 ,'1e10 - 32' );
     190                }
     191
     192                $this->assertEquals( absint( 0x1A ), 26 ,'0x1A' );
     193                $this->assertEquals( absint( '0x1A' ), 0 ,'(string)0x1A' );
     194
     195
     196        }
     197        /*
     198        *  PHP_INT_MAX
     199        */
     200        function test_absint_PHP_INT_MAX() {
     201
     202                $this->assertEquals( absint( PHP_INT_MAX ), PHP_INT_MAX, ' PHP_INT_MAX ');
     203                $this->assertEquals( absint( -PHP_INT_MAX ), PHP_INT_MAX, ' -PHP_INT_MAX ' );
     204
     205                $this->assertEquals( absint( PHP_INT_MAX-1 ), PHP_INT_MAX-1, ' PHP_INT_MAX-1 ');
     206
     207        }
     208
     209
     210        /**
     211         * this fails in strange way as we have hit MAX Init
     212         */
     213        function test_absint_over_max_int() {
     214                if ( $this->is_64_bit() ){
     215                        $this->assertEquals( absint( PHP_INT_MAX +1 ),     9.2233720368547758E+18, 'PHP_INT_MAX+1-64bit' );
     216                        $this->assertEquals( absint( PHP_INT_MAX +11 ),   9.2233720368547758E+18, 'PHP_INT_MAX+11-64bit' );
     217                        $this->assertEquals( absint( PHP_INT_MAX +99 ),   9.2233720368547758E+18, 'PHP_INT_MAX+99-64bit' );
     218                        $this->assertEquals( absint( PHP_INT_MAX +999 ), 9.2233720368547758E+18, 'PHP_INT_MAX+999-64bit' );
     219                }else{
     220                        $this->assertEquals( absint( PHP_INT_MAX +1 ),     2147483648, 'PHP_INT_MAX+1-32bit' );
     221                        $this->assertEquals( absint( PHP_INT_MAX +11 ),   2147483638, 'PHP_INT_MAX+11-32bit' );
     222                        $this->assertEquals( absint( PHP_INT_MAX +99 ),   2147483550, 'PHP_INT_MAX+99-32bit' );
     223                        $this->assertEquals( absint( PHP_INT_MAX +999 ), 2147482650, 'PHP_INT_MAX+999-32bit' );
     224                }
     225
     226        }
     227
     228        /**
     229        *  this is what you should get but we have hit max int
     230        * @ticket 28559
     231        */
     232        function test_absint_that_fail_max_int(){
     233
     234                if ( $this->is_64_bit() ){
     235                        $this->assertEquals( absint( PHP_INT_MAX +1 ), 0, 'PHP_INT_MAX+1-64bit' );// Failed asserting that false matches expected 2147483648.
     236                        $this->assertEquals( absint( PHP_INT_MAX +11 ), 0, 'PHP_INT_MAX+11-64bit' );// Failed asserting that false matches expected 2147483638.
     237                        $this->assertEquals( absint( PHP_INT_MAX +99 ), 0, 'PHP_INT_MAX+99-64bit' );// Failed asserting that false matches expected 2147483550.
     238                        $this->assertEquals( absint( PHP_INT_MAX +999 ), 0, 'PHP_INT_MAX+999-64bit' );// Failed asserting that false matches expected 2147482650.
     239                }else{
     240                        $this->assertEquals( absint( PHP_INT_MAX +1 ), 0, 'PHP_INT_MAX+1-32bit' ); // Failed asserting that false matches expected 2147483648.
     241                        $this->assertEquals( absint( PHP_INT_MAX +11 ), 0, 'PHP_INT_MAX+11-32bit' );  // Failed asserting that false matches expected 2147483638.
     242                        $this->assertEquals( absint( PHP_INT_MAX +99 ), 0, 'PHP_INT_MAX+99-32bit' );  // Failed asserting that false matches expected 2147483550.
     243                        $this->assertEquals( absint( PHP_INT_MAX +999 ), 0, 'PHP_INT_MAX+999-32bit' );  // Failed asserting that false matches expected 2147482650.
     244                }
     245        }
     246
     247
     248        /**
     249         *  this is what you should get but we have hit max int
     250         * @ticket 28559
     251         */
     252        function test_absint_to_the_power_that_fails(){
     253
     254                if ( $this->is_64_bit() ){
     255                        $this->assertEquals( absint( 1e20 ), 100000000000000000000 ,'1e20' );
     256                } else{
     257                        $this->assertEquals( absint( 1e10 ), 10000000000 ,'1e10' );  //Failed asserting that 10000000000 matches expected 1410065408.
     258
     259                }
     260
     261        }
     262        /**
     263         * this is what you should get but we have hit max int
     264         * @ticket 28559
     265         */
     266        function test_absint_49999999999999999999(){
     267                if ( $this->is_64_bit() ){
     268                        $this->assertEquals( absint( 49999999999999999999 ),  50000000000000000000,     '49999999999999999999' );
     269                } else {
     270                        $this->assertEquals( absint( 4999999999.9999999999 ), 5000000000,                       '4999999999.9999999999' );
     271                }
     272        }
     273
     274        /**
     275         *  at .9e16 absint returns 5 not 4
     276         *  @ticket 28559
     277         */
     278        public function  test_absint_dot_16()
     279        {
     280                $this->assertEquals( absint( 4.999999999999999 ), 4, '4.999999999999999' );
     281
     282                 // why does it swap to 4 where does the round up not round down come from
     283                $this->assertEquals( absint( 4.9999999999999999 ), 4, '4.9999999999999999' );
     284        }
     285
     286
     287}
     288