Annotation of embedaddon/php/ext/standard/tests/strings/printf.phpt, revision 1.1.1.1

1.1       misho       1: --TEST--
                      2: Test printf() function (32bit)
                      3: --INI--
                      4: precision=14
                      5: --SKIPIF--
                      6: <?php
                      7: if (PHP_INT_MAX > 2147483647) {
                      8:         die("skip 32bit test only");
                      9: }
                     10: ?>
                     11: --FILE--
                     12: <?php
                     13: 
                     14: /* Various input arrays for different format types */
                     15: 
                     16: $float_variation  = array( "%f", "%-f", "%+f", "%7.2f", "%-7.2f", "%07.2f", "%-07.2f", "%'#7.2f" );
                     17: $float_numbers    = array( 0, 1, -1, 0.32, -0.32, 3.4. -3.4, 2.54, -2.54, 1.2345678e99, -1.2345678e99 );
                     18: 
                     19: $int_variation    = array( "%d", "%-d", "%+d", "%7.2d", "%-7.2d", "%07.2d", "%-07.2d", "%'#7.2d" );
                     20: $int_numbers      = array( 0, 1, -1, 2.7, -2.7, 23333333, -23333333, "1234" );
                     21: 
                     22: $char_variation   = array( 'a', "a", 67, -67, 99 );
                     23: 
                     24: $string_variation = array( "%5s", "%-5s", "%05s", "%'#5s" );
                     25: $strings          = array( NULL, "abc", 'aaa' );
                     26: 
                     27: /* Checking warning messages */
                     28: 
                     29: /* Zero argument */
                     30: echo "\n*** Output for zero argument ***\n";
                     31: printf();
                     32: 
                     33: /* Number of arguments not matching as specified in format field */
                     34: echo "\n*** Output for insufficient number of arguments ***\n";
                     35: $string = "dingy%sflem%dwombat";
                     36: $nbr = 5;
                     37: $name = "voudras";
                     38: printf("%d $string %s", $nbr, $name);
                     39: 
                     40: 
                     41: /* Scalar argument */
                     42: echo "\n*** Output for scalar argument ***\n";
                     43: printf(3);
                     44: 
                     45: /* NULL argument */
                     46: echo "\n*** Output for NULL as argument ***\n";
                     47: printf(NULL);
                     48: 
                     49: 
                     50: /* Float type variations */
                     51: 
                     52: $counter = 1;
                     53: echo "\n\n*** Output for float type ***\n";
                     54: echo "\n Input Float numbers variation array is:\n";
                     55: print_r($float_numbers);
                     56: 
                     57: foreach( $float_variation as $float_var )
                     58: {
                     59:  echo "\n\nFloat Iteration $counter";
                     60:  foreach( $float_numbers as $float_num )
                     61:  {
                     62:   echo "\n";
                     63:   printf( $float_var, $float_num );
                     64:  }
                     65:  $counter++;
                     66: }
                     67: 
                     68: 
                     69: /* Integer type variations */
                     70: 
                     71: $counter = 1;
                     72: echo "\n\n*** Output for integer type ***\n";
                     73: echo "\n Input Integer numbers variation array is:\n";
                     74: print_r($int_numbers);
                     75: 
                     76: foreach( $int_variation as $int_var )
                     77: {
                     78:  echo "\n\nInteger Iteration $counter";
                     79:  foreach( $int_numbers as $int_num )
                     80:  {
                     81:   echo "\n";
                     82:   printf( $int_var, $int_num );
                     83:  }
                     84:  $counter++;
                     85: }
                     86: 
                     87: 
                     88: /* Binary type variations */
                     89: 
                     90: echo "\n\n*** Output for binary type ***\n";
                     91: echo "\n Input  numbers variation array is:\n";
                     92: print_r($int_numbers);
                     93: 
                     94:  foreach( $int_numbers as $bin_num )
                     95:  {
                     96:   echo "\n";
                     97:   printf( "%b", $bin_num );
                     98:  }
                     99: 
                    100: 
                    101: /* Chararter type variations */
                    102: echo "\n\n*** Output for char type ***\n";
                    103: echo "\n Input Characters variation array is:\n";
                    104: print_r($char_variation);
                    105: 
                    106: foreach( $char_variation as $char )
                    107: {
                    108:  echo "\n";
                    109:  printf( "%c", $char );
                    110: }
                    111: 
                    112: /* Scientific type variations */
                    113: echo "\n\n*** Output for scientific type ***\n";
                    114: echo "\n Input numbers variation array is:\n";
                    115: print_r($int_numbers);
                    116: 
                    117: foreach( $int_numbers as $num )
                    118: {
                    119:  echo "\n";
                    120:  printf( "%e", $num );
                    121: }
                    122: 
                    123: /* Unsigned Integer type variation */
                    124: echo "\n\n*** Output for unsigned integer type ***\n";
                    125: echo "\n Input Integer numbers variation array is:\n";
                    126: print_r($int_numbers);
                    127: 
                    128: foreach( $int_numbers as $unsig_num )
                    129: {
                    130:  echo "\n";
                    131:  printf( "%u", $unsig_num );
                    132: }
                    133: 
                    134: /* Octal type variations */
                    135: echo "\n\n*** Output for octal type ***\n";
                    136: echo "\n Input numbers variation array is:\n";
                    137: print_r($int_numbers);
                    138: 
                    139: foreach( $int_numbers as $octal_num )
                    140: {
                    141:  echo "\n";
                    142:  printf( "%o", $octal_num );
                    143: }
                    144: 
                    145: /* Hexadecimal type variations */
                    146: echo "\n\n*** Output for hexadecimal type ***\n";
                    147: echo "\n Input numbers variation array is:\n";
                    148: print_r($int_numbers);
                    149: 
                    150: foreach( $int_numbers as $hexa_num )
                    151: {
                    152:  echo "\n";
                    153:  printf( "%x", $hexa_num );
                    154: }
                    155: 
                    156: /* String type variations */
                    157: echo "\n\n*** Output for string type ***\n";
                    158: echo "\n Input Strings format variation array is:\n";
                    159: print_r($string_variation);
                    160: echo "\n Input strings variation array is:\n";
                    161: print_r($strings);
                    162: 
                    163: foreach( $string_variation as $string_var )
                    164: {
                    165:  foreach( $strings as $str )
                    166:  {
                    167:   echo "\n";
                    168:   printf( $string_var, $str );
                    169:  }
                    170: }
                    171: 
                    172: 
                    173: /* variations of %g type */
                    174: $format_g = array("%g", "%.0g", "%+g", "%-g", "%-1.2g", "%+1.2g", "%G", "%.0G", "%+G", "%-G", "%-1.2G", "%+1.2G");
                    175: 
                    176: echo "\n\n*** Output for '%g' type ***\n";
                    177: echo "\n Input format variation array is:\n";
                    178: print_r($format_g);
                    179: 
                    180: foreach( $format_g as $formatg )
                    181: {
                    182:  printf("\n$formatg",123456);
                    183:  printf("\n$formatg",-123456);
                    184: }
                    185: 
                    186: 
                    187: /* Some more typical cases */
                    188: 
                    189: $tempnum = 12345;
                    190: $tempstring = "abcdefghjklmnpqrstuvwxyz";
                    191: 
                    192: echo"\n\n*** Output for '%%%.2f' as the format parameter ***\n";
                    193: printf("%%%.2f",1.23456789e10);
                    194: 
                    195: echo"\n\n*** Output for '%%' as the format parameter ***\n";
                    196: printf("%%",1.23456789e10);
                    197: 
                    198: echo"\n\n*** Output for precision value more than maximum ***\n";
                    199: printf("%.988f",1.23456789e10);   
                    200: 
                    201: echo"\n\n*** Output for invalid width(-15) specifier ***\n";
                    202: printf("%030.-15s", $tempstring);  
                    203: 
                    204: echo"\n\n*** Output for '%F' as the format parameter ***\n";
                    205: printf("%F",1.23456789e10);
                    206: 
                    207: echo"\n\n*** Output for '%X' as the format parameter ***\n";
                    208: printf("%X",12);
                    209: 
                    210: echo"\n\n*** Output  with no format parameter ***\n";
                    211: printf($tempnum);
                    212: 
                    213: echo"\n\n*** Output for multiple format parameters ***\n";
                    214: printf("%d  %s  %d\n", $tempnum, $tempstring, $tempnum); 
                    215: 
                    216: echo"\n\n*** Output for excess of mixed type arguments  ***\n";
                    217: printf("%s", $tempstring, $tempstring, $tempstring);      
                    218: 
                    219: echo"\n\n*** Output for string format parameter and integer type argument ***\n";
                    220: printf("%s", $tempnum);          
                    221: 
                    222: echo"\n\n*** Output for integer format parameter and string type argument ***\n";
                    223: printf("%d", $tempstring);    
                    224: 
                    225: 
                    226: ?>
                    227: --EXPECTF--
                    228: *** Output for zero argument ***
                    229: 
                    230: Warning: printf() expects at least %d parameter, %d given in %s on line %d
                    231: 
                    232: *** Output for insufficient number of arguments ***
                    233: 
                    234: Warning: printf(): Too few arguments in %s on line %d
                    235: 
                    236: *** Output for scalar argument ***
                    237: 3
                    238: *** Output for NULL as argument ***
                    239: 
                    240: 
                    241: *** Output for float type ***
                    242: 
                    243:  Input Float numbers variation array is:
                    244: Array
                    245: (
                    246:     [0] => 0
                    247:     [1] => 1
                    248:     [2] => -1
                    249:     [3] => 0.32
                    250:     [4] => -0.32
                    251:     [5] => 3.4-3.4
                    252:     [6] => 2.54
                    253:     [7] => -2.54
                    254:     [8] => 1.2345678E+99
                    255:     [9] => -1.2345678E+99
                    256: )
                    257: 
                    258: 
                    259: Float Iteration 1
                    260: 0.000000
                    261: 1.000000
                    262: -1.000000
                    263: 0.320000
                    264: -0.320000
                    265: 3.400000
                    266: 2.540000
                    267: -2.540000
                    268: 1234567%d.000000
                    269: -1234567%d.000000
                    270: 
                    271: Float Iteration 2
                    272: 0.000000
                    273: 1.000000
                    274: -1.000000
                    275: 0.320000
                    276: -0.320000
                    277: 3.400000
                    278: 2.540000
                    279: -2.540000
                    280: 1234567%d.000000
                    281: -1234567%d.000000
                    282: 
                    283: Float Iteration 3
                    284: +0.000000
                    285: +1.000000
                    286: -1.000000
                    287: +0.320000
                    288: -0.320000
                    289: +3.400000
                    290: +2.540000
                    291: -2.540000
                    292: +1234567%d.000000
                    293: -1234567%d.000000
                    294: 
                    295: Float Iteration 4
                    296:    0.00
                    297:    1.00
                    298:   -1.00
                    299:    0.32
                    300:   -0.32
                    301:    3.40
                    302:    2.54
                    303:   -2.54
                    304: 1234567%d.00
                    305: -1234567%d.00
                    306: 
                    307: Float Iteration 5
                    308: 0.00   
                    309: 1.00   
                    310: -1.00  
                    311: 0.32   
                    312: -0.32  
                    313: 3.40   
                    314: 2.54   
                    315: -2.54  
                    316: 1234567%d.00
                    317: -1234567%d.00
                    318: 
                    319: Float Iteration 6
                    320: 0000.00
                    321: 0001.00
                    322: -001.00
                    323: 0000.32
                    324: -000.32
                    325: 0003.40
                    326: 0002.54
                    327: -002.54
                    328: 1234567%d.00
                    329: -1234567%d.00
                    330: 
                    331: Float Iteration 7
                    332: 0.00000
                    333: 1.00000
                    334: -1.0000
                    335: 0.32000
                    336: -0.3200
                    337: 3.40000
                    338: 2.54000
                    339: -2.5400
                    340: 1234567%d.00
                    341: -1234567%d.00
                    342: 
                    343: Float Iteration 8
                    344: ###0.00
                    345: ###1.00
                    346: ##-1.00
                    347: ###0.32
                    348: ##-0.32
                    349: ###3.40
                    350: ###2.54
                    351: ##-2.54
                    352: 1234567%d.00
                    353: -1234567%d.00
                    354: 
                    355: *** Output for integer type ***
                    356: 
                    357:  Input Integer numbers variation array is:
                    358: Array
                    359: (
                    360:     [0] => 0
                    361:     [1] => 1
                    362:     [2] => -1
                    363:     [3] => 2.7
                    364:     [4] => -2.7
                    365:     [5] => 23333333
                    366:     [6] => -23333333
                    367:     [7] => 1234
                    368: )
                    369: 
                    370: 
                    371: Integer Iteration 1
                    372: 0
                    373: 1
                    374: -1
                    375: 2
                    376: -2
                    377: 23333333
                    378: -23333333
                    379: 1234
                    380: 
                    381: Integer Iteration 2
                    382: 0
                    383: 1
                    384: -1
                    385: 2
                    386: -2
                    387: 23333333
                    388: -23333333
                    389: 1234
                    390: 
                    391: Integer Iteration 3
                    392: +0
                    393: +1
                    394: -1
                    395: +2
                    396: -2
                    397: +23333333
                    398: -23333333
                    399: +1234
                    400: 
                    401: Integer Iteration 4
                    402:       0
                    403:       1
                    404:      -1
                    405:       2
                    406:      -2
                    407: 23333333
                    408: -23333333
                    409:    1234
                    410: 
                    411: Integer Iteration 5
                    412: 0      
                    413: 1      
                    414: -1     
                    415: 2      
                    416: -2     
                    417: 23333333
                    418: -23333333
                    419: 1234   
                    420: 
                    421: Integer Iteration 6
                    422: 0000000
                    423: 0000001
                    424: -000001
                    425: 0000002
                    426: -000002
                    427: 23333333
                    428: -23333333
                    429: 0001234
                    430: 
                    431: Integer Iteration 7
                    432: 0      
                    433: 1      
                    434: -1     
                    435: 2      
                    436: -2     
                    437: 23333333
                    438: -23333333
                    439: 1234   
                    440: 
                    441: Integer Iteration 8
                    442: ######0
                    443: ######1
                    444: #####-1
                    445: ######2
                    446: #####-2
                    447: 23333333
                    448: -23333333
                    449: ###1234
                    450: 
                    451: *** Output for binary type ***
                    452: 
                    453:  Input  numbers variation array is:
                    454: Array
                    455: (
                    456:     [0] => 0
                    457:     [1] => 1
                    458:     [2] => -1
                    459:     [3] => 2.7
                    460:     [4] => -2.7
                    461:     [5] => 23333333
                    462:     [6] => -23333333
                    463:     [7] => 1234
                    464: )
                    465: 
                    466: 0
                    467: 1
                    468: 11111111111111111111111111111111
                    469: 10
                    470: 11111111111111111111111111111110
                    471: 1011001000000100111010101
                    472: 11111110100110111111011000101011
                    473: 10011010010
                    474: 
                    475: *** Output for char type ***
                    476: 
                    477:  Input Characters variation array is:
                    478: Array
                    479: (
                    480:     [0] => a
                    481:     [1] => a
                    482:     [2] => 67
                    483:     [3] => -67
                    484:     [4] => 99
                    485: )
                    486: 
                    487: 
                    488: 
                    489: C
                    490: ½
                    491: c
                    492: 
                    493: *** Output for scientific type ***
                    494: 
                    495:  Input numbers variation array is:
                    496: Array
                    497: (
                    498:     [0] => 0
                    499:     [1] => 1
                    500:     [2] => -1
                    501:     [3] => 2.7
                    502:     [4] => -2.7
                    503:     [5] => 23333333
                    504:     [6] => -23333333
                    505:     [7] => 1234
                    506: )
                    507: 
                    508: 0.000000e+0
                    509: 1.000000e+0
                    510: -1.000000e+0
                    511: 2.700000e+0
                    512: -2.700000e+0
                    513: 2.333333e+7
                    514: -2.333333e+7
                    515: 1.234000e+3
                    516: 
                    517: *** Output for unsigned integer type ***
                    518: 
                    519:  Input Integer numbers variation array is:
                    520: Array
                    521: (
                    522:     [0] => 0
                    523:     [1] => 1
                    524:     [2] => -1
                    525:     [3] => 2.7
                    526:     [4] => -2.7
                    527:     [5] => 23333333
                    528:     [6] => -23333333
                    529:     [7] => 1234
                    530: )
                    531: 
                    532: 0
                    533: 1
                    534: 4294967295
                    535: 2
                    536: 4294967294
                    537: 23333333
                    538: 4271633963
                    539: 1234
                    540: 
                    541: *** Output for octal type ***
                    542: 
                    543:  Input numbers variation array is:
                    544: Array
                    545: (
                    546:     [0] => 0
                    547:     [1] => 1
                    548:     [2] => -1
                    549:     [3] => 2.7
                    550:     [4] => -2.7
                    551:     [5] => 23333333
                    552:     [6] => -23333333
                    553:     [7] => 1234
                    554: )
                    555: 
                    556: 0
                    557: 1
                    558: 37777777777
                    559: 2
                    560: 37777777776
                    561: 131004725
                    562: 37646773053
                    563: 2322
                    564: 
                    565: *** Output for hexadecimal type ***
                    566: 
                    567:  Input numbers variation array is:
                    568: Array
                    569: (
                    570:     [0] => 0
                    571:     [1] => 1
                    572:     [2] => -1
                    573:     [3] => 2.7
                    574:     [4] => -2.7
                    575:     [5] => 23333333
                    576:     [6] => -23333333
                    577:     [7] => 1234
                    578: )
                    579: 
                    580: 0
                    581: 1
                    582: ffffffff
                    583: 2
                    584: fffffffe
                    585: 16409d5
                    586: fe9bf62b
                    587: 4d2
                    588: 
                    589: *** Output for string type ***
                    590: 
                    591:  Input Strings format variation array is:
                    592: Array
                    593: (
                    594:     [0] => %5s
                    595:     [1] => %-5s
                    596:     [2] => %05s
                    597:     [3] => %'#5s
                    598: )
                    599: 
                    600:  Input strings variation array is:
                    601: Array
                    602: (
                    603:     [0] => 
                    604:     [1] => abc
                    605:     [2] => aaa
                    606: )
                    607: 
                    608:      
                    609:   abc
                    610:   aaa
                    611:      
                    612: abc  
                    613: aaa  
                    614: 00000
                    615: 00abc
                    616: 00aaa
                    617: #####
                    618: ##abc
                    619: ##aaa
                    620: 
                    621: *** Output for '%g' type ***
                    622: 
                    623:  Input format variation array is:
                    624: Array
                    625: (
                    626:     [0] => %g
                    627:     [1] => %.0g
                    628:     [2] => %+g
                    629:     [3] => %-g
                    630:     [4] => %-1.2g
                    631:     [5] => %+1.2g
                    632:     [6] => %G
                    633:     [7] => %.0G
                    634:     [8] => %+G
                    635:     [9] => %-G
                    636:     [10] => %-1.2G
                    637:     [11] => %+1.2G
                    638: )
                    639: 
                    640: 123456
                    641: -123456
                    642: 1.0e+5
                    643: -1.0e+5
                    644: +123456
                    645: -123456
                    646: 123456
                    647: -123456
                    648: 1.2e+5
                    649: -1.2e+5
                    650: +1.2e+5
                    651: -1.2e+5
                    652: 123456
                    653: -123456
                    654: 1.0E+5
                    655: -1.0E+5
                    656: +123456
                    657: -123456
                    658: 123456
                    659: -123456
                    660: 1.2E+5
                    661: -1.2E+5
                    662: +1.2E+5
                    663: -1.2E+5
                    664: 
                    665: *** Output for '%%%.2f' as the format parameter ***
                    666: %12345678900.00
                    667: 
                    668: *** Output for '%%' as the format parameter ***
                    669: %
                    670: 
                    671: *** Output for precision value more than maximum ***
                    672: 
                    673: Notice: printf(): Requested precision of 988 digits was truncated to PHP maximum of 53 digits in %s on line %d
                    674: 12345678900.00000000000000000000000000000000000000000000000000000
                    675: 
                    676: *** Output for invalid width(-15) specifier ***
                    677: 15s
                    678: 
                    679: *** Output for '%F' as the format parameter ***
                    680: 12345678900.000000
                    681: 
                    682: *** Output for '%X' as the format parameter ***
                    683: C
                    684: 
                    685: *** Output  with no format parameter ***
                    686: 12345
                    687: 
                    688: *** Output for multiple format parameters ***
                    689: 12345  abcdefghjklmnpqrstuvwxyz  12345
                    690: 
                    691: 
                    692: *** Output for excess of mixed type arguments  ***
                    693: abcdefghjklmnpqrstuvwxyz
                    694: 
                    695: *** Output for string format parameter and integer type argument ***
                    696: 12345
                    697: 
                    698: *** Output for integer format parameter and string type argument ***
                    699: 0

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>