1--TEST-- 2Test fscanf() function: usage variations - hexa formats with integer values 3--SKIPIF-- 4<?php 5if (PHP_INT_SIZE != 4) { 6 die("skip this test is for 32bit platform only"); 7} 8?> 9--FILE-- 10<?php 11 12/* 13 Prototype: mixed fscanf ( resource $handle, string $format [, mixed &$...] ); 14 Description: Parses input from a file according to a format 15*/ 16 17/* Test fscanf() to scan different integer values using different hexa format types */ 18 19$file_path = dirname(__FILE__); 20 21echo "*** Test fscanf(): different hexa format types with different integer values ***\n"; 22 23// create a file 24$filename = "$file_path/fscanf_variation33.tmp"; 25$file_handle = fopen($filename, "w"); 26if($file_handle == false) 27 exit("Error:failed to open file $filename"); 28 29// different valid integer vlaues 30$valid_ints = array( 31 0, 32 1, 33 -1, 34 -2147483648, // max negative integer value 35 -2147483647, 36 2147483647, // max positive integer value 37 2147483640, 38 0x123B, // integer as hexadecimal 39 0x12ab, 40 0Xfff, 41 0XFA, 42 -0x80000000, // max negative integer as hexadecimal 43 0x7fffffff, // max positive integer as hexadecimal 44 0x7FFFFFFF, // max positive integer as hexadecimal 45 0123, // integer as octal 46 01, // should be quivalent to octal 1 47 -020000000000, // max negative integer as octal 48 017777777777 // max positive integer as octal 49); 50// various hexa formats 51$hexa_formats = array( "%x", 52 "%xx", "%lx", "%Lx", 53 " %x", "%x ", "% x", 54 "\t%x", "\n%x", "%4x", 55 "%30x", "%[0-9A-Fa-f]", "%*x" 56 ); 57 58$counter = 1; 59 60// writing to the file 61foreach($valid_ints as $int_value) { 62 @fprintf($file_handle, $int_value); 63 @fprintf($file_handle, "\n"); 64} 65// closing the file 66fclose($file_handle); 67 68// opening the file for reading 69$file_handle = fopen($filename, "r"); 70if($file_handle == false) { 71 exit("Error:failed to open file $filename"); 72} 73 74$counter = 1; 75// reading the values from file using different hexa formats 76foreach($hexa_formats as $hexa_format) { 77 // rewind the file so that for every foreach iteration the file pointer starts from bof 78 rewind($file_handle); 79 echo "\n-- iteration $counter --\n"; 80 while( !feof($file_handle) ) { 81 var_dump( fscanf($file_handle,$hexa_format) ); 82 } 83 $counter++; 84} 85 86echo "\n*** Done ***"; 87?> 88--CLEAN-- 89<?php 90$file_path = dirname(__FILE__); 91$filename = "$file_path/fscanf_variation33.tmp"; 92unlink($filename); 93?> 94--EXPECTF-- 95*** Test fscanf(): different hexa format types with different integer values *** 96 97-- iteration 1 -- 98array(1) { 99 [0]=> 100 int(0) 101} 102array(1) { 103 [0]=> 104 int(1) 105} 106array(1) { 107 [0]=> 108 int(-1) 109} 110array(1) { 111 [0]=> 112 int(-2147483648) 113} 114array(1) { 115 [0]=> 116 int(-2147483648) 117} 118array(1) { 119 [0]=> 120 int(2147483647) 121} 122array(1) { 123 [0]=> 124 int(2147483647) 125} 126array(1) { 127 [0]=> 128 int(18023) 129} 130array(1) { 131 [0]=> 132 int(18297) 133} 134array(1) { 135 [0]=> 136 int(16533) 137} 138array(1) { 139 [0]=> 140 int(592) 141} 142array(1) { 143 [0]=> 144 int(-2147483648) 145} 146array(1) { 147 [0]=> 148 int(2147483647) 149} 150array(1) { 151 [0]=> 152 int(2147483647) 153} 154array(1) { 155 [0]=> 156 int(131) 157} 158array(1) { 159 [0]=> 160 int(1) 161} 162array(1) { 163 [0]=> 164 int(-2147483648) 165} 166array(1) { 167 [0]=> 168 int(2147483647) 169} 170bool(false) 171 172-- iteration 2 -- 173array(1) { 174 [0]=> 175 int(0) 176} 177array(1) { 178 [0]=> 179 int(1) 180} 181array(1) { 182 [0]=> 183 int(-1) 184} 185array(1) { 186 [0]=> 187 int(-2147483648) 188} 189array(1) { 190 [0]=> 191 int(-2147483648) 192} 193array(1) { 194 [0]=> 195 int(2147483647) 196} 197array(1) { 198 [0]=> 199 int(2147483647) 200} 201array(1) { 202 [0]=> 203 int(18023) 204} 205array(1) { 206 [0]=> 207 int(18297) 208} 209array(1) { 210 [0]=> 211 int(16533) 212} 213array(1) { 214 [0]=> 215 int(592) 216} 217array(1) { 218 [0]=> 219 int(-2147483648) 220} 221array(1) { 222 [0]=> 223 int(2147483647) 224} 225array(1) { 226 [0]=> 227 int(2147483647) 228} 229array(1) { 230 [0]=> 231 int(131) 232} 233array(1) { 234 [0]=> 235 int(1) 236} 237array(1) { 238 [0]=> 239 int(-2147483648) 240} 241array(1) { 242 [0]=> 243 int(2147483647) 244} 245bool(false) 246 247-- iteration 3 -- 248array(1) { 249 [0]=> 250 int(0) 251} 252array(1) { 253 [0]=> 254 int(1) 255} 256array(1) { 257 [0]=> 258 int(-1) 259} 260array(1) { 261 [0]=> 262 int(-2147483648) 263} 264array(1) { 265 [0]=> 266 int(-2147483648) 267} 268array(1) { 269 [0]=> 270 int(2147483647) 271} 272array(1) { 273 [0]=> 274 int(2147483647) 275} 276array(1) { 277 [0]=> 278 int(18023) 279} 280array(1) { 281 [0]=> 282 int(18297) 283} 284array(1) { 285 [0]=> 286 int(16533) 287} 288array(1) { 289 [0]=> 290 int(592) 291} 292array(1) { 293 [0]=> 294 int(-2147483648) 295} 296array(1) { 297 [0]=> 298 int(2147483647) 299} 300array(1) { 301 [0]=> 302 int(2147483647) 303} 304array(1) { 305 [0]=> 306 int(131) 307} 308array(1) { 309 [0]=> 310 int(1) 311} 312array(1) { 313 [0]=> 314 int(-2147483648) 315} 316array(1) { 317 [0]=> 318 int(2147483647) 319} 320bool(false) 321 322-- iteration 4 -- 323array(1) { 324 [0]=> 325 int(0) 326} 327array(1) { 328 [0]=> 329 int(1) 330} 331array(1) { 332 [0]=> 333 int(-1) 334} 335array(1) { 336 [0]=> 337 int(-2147483648) 338} 339array(1) { 340 [0]=> 341 int(-2147483648) 342} 343array(1) { 344 [0]=> 345 int(2147483647) 346} 347array(1) { 348 [0]=> 349 int(2147483647) 350} 351array(1) { 352 [0]=> 353 int(18023) 354} 355array(1) { 356 [0]=> 357 int(18297) 358} 359array(1) { 360 [0]=> 361 int(16533) 362} 363array(1) { 364 [0]=> 365 int(592) 366} 367array(1) { 368 [0]=> 369 int(-2147483648) 370} 371array(1) { 372 [0]=> 373 int(2147483647) 374} 375array(1) { 376 [0]=> 377 int(2147483647) 378} 379array(1) { 380 [0]=> 381 int(131) 382} 383array(1) { 384 [0]=> 385 int(1) 386} 387array(1) { 388 [0]=> 389 int(-2147483648) 390} 391array(1) { 392 [0]=> 393 int(2147483647) 394} 395bool(false) 396 397-- iteration 5 -- 398array(1) { 399 [0]=> 400 int(0) 401} 402array(1) { 403 [0]=> 404 int(1) 405} 406array(1) { 407 [0]=> 408 int(-1) 409} 410array(1) { 411 [0]=> 412 int(-2147483648) 413} 414array(1) { 415 [0]=> 416 int(-2147483648) 417} 418array(1) { 419 [0]=> 420 int(2147483647) 421} 422array(1) { 423 [0]=> 424 int(2147483647) 425} 426array(1) { 427 [0]=> 428 int(18023) 429} 430array(1) { 431 [0]=> 432 int(18297) 433} 434array(1) { 435 [0]=> 436 int(16533) 437} 438array(1) { 439 [0]=> 440 int(592) 441} 442array(1) { 443 [0]=> 444 int(-2147483648) 445} 446array(1) { 447 [0]=> 448 int(2147483647) 449} 450array(1) { 451 [0]=> 452 int(2147483647) 453} 454array(1) { 455 [0]=> 456 int(131) 457} 458array(1) { 459 [0]=> 460 int(1) 461} 462array(1) { 463 [0]=> 464 int(-2147483648) 465} 466array(1) { 467 [0]=> 468 int(2147483647) 469} 470bool(false) 471 472-- iteration 6 -- 473array(1) { 474 [0]=> 475 int(0) 476} 477array(1) { 478 [0]=> 479 int(1) 480} 481array(1) { 482 [0]=> 483 int(-1) 484} 485array(1) { 486 [0]=> 487 int(-2147483648) 488} 489array(1) { 490 [0]=> 491 int(-2147483648) 492} 493array(1) { 494 [0]=> 495 int(2147483647) 496} 497array(1) { 498 [0]=> 499 int(2147483647) 500} 501array(1) { 502 [0]=> 503 int(18023) 504} 505array(1) { 506 [0]=> 507 int(18297) 508} 509array(1) { 510 [0]=> 511 int(16533) 512} 513array(1) { 514 [0]=> 515 int(592) 516} 517array(1) { 518 [0]=> 519 int(-2147483648) 520} 521array(1) { 522 [0]=> 523 int(2147483647) 524} 525array(1) { 526 [0]=> 527 int(2147483647) 528} 529array(1) { 530 [0]=> 531 int(131) 532} 533array(1) { 534 [0]=> 535 int(1) 536} 537array(1) { 538 [0]=> 539 int(-2147483648) 540} 541array(1) { 542 [0]=> 543 int(2147483647) 544} 545bool(false) 546 547-- iteration 7 -- 548 549Warning: fscanf(): Bad scan conversion character " " in %s on line %d 550NULL 551 552Warning: fscanf(): Bad scan conversion character " " in %s on line %d 553NULL 554 555Warning: fscanf(): Bad scan conversion character " " in %s on line %d 556NULL 557 558Warning: fscanf(): Bad scan conversion character " " in %s on line %d 559NULL 560 561Warning: fscanf(): Bad scan conversion character " " in %s on line %d 562NULL 563 564Warning: fscanf(): Bad scan conversion character " " in %s on line %d 565NULL 566 567Warning: fscanf(): Bad scan conversion character " " in %s on line %d 568NULL 569 570Warning: fscanf(): Bad scan conversion character " " in %s on line %d 571NULL 572 573Warning: fscanf(): Bad scan conversion character " " in %s on line %d 574NULL 575 576Warning: fscanf(): Bad scan conversion character " " in %s on line %d 577NULL 578 579Warning: fscanf(): Bad scan conversion character " " in %s on line %d 580NULL 581 582Warning: fscanf(): Bad scan conversion character " " in %s on line %d 583NULL 584 585Warning: fscanf(): Bad scan conversion character " " in %s on line %d 586NULL 587 588Warning: fscanf(): Bad scan conversion character " " in %s on line %d 589NULL 590 591Warning: fscanf(): Bad scan conversion character " " in %s on line %d 592NULL 593 594Warning: fscanf(): Bad scan conversion character " " in %s on line %d 595NULL 596 597Warning: fscanf(): Bad scan conversion character " " in %s on line %d 598NULL 599 600Warning: fscanf(): Bad scan conversion character " " in %s on line %d 601NULL 602bool(false) 603 604-- iteration 8 -- 605array(1) { 606 [0]=> 607 int(0) 608} 609array(1) { 610 [0]=> 611 int(1) 612} 613array(1) { 614 [0]=> 615 int(-1) 616} 617array(1) { 618 [0]=> 619 int(-2147483648) 620} 621array(1) { 622 [0]=> 623 int(-2147483648) 624} 625array(1) { 626 [0]=> 627 int(2147483647) 628} 629array(1) { 630 [0]=> 631 int(2147483647) 632} 633array(1) { 634 [0]=> 635 int(18023) 636} 637array(1) { 638 [0]=> 639 int(18297) 640} 641array(1) { 642 [0]=> 643 int(16533) 644} 645array(1) { 646 [0]=> 647 int(592) 648} 649array(1) { 650 [0]=> 651 int(-2147483648) 652} 653array(1) { 654 [0]=> 655 int(2147483647) 656} 657array(1) { 658 [0]=> 659 int(2147483647) 660} 661array(1) { 662 [0]=> 663 int(131) 664} 665array(1) { 666 [0]=> 667 int(1) 668} 669array(1) { 670 [0]=> 671 int(-2147483648) 672} 673array(1) { 674 [0]=> 675 int(2147483647) 676} 677bool(false) 678 679-- iteration 9 -- 680array(1) { 681 [0]=> 682 int(0) 683} 684array(1) { 685 [0]=> 686 int(1) 687} 688array(1) { 689 [0]=> 690 int(-1) 691} 692array(1) { 693 [0]=> 694 int(-2147483648) 695} 696array(1) { 697 [0]=> 698 int(-2147483648) 699} 700array(1) { 701 [0]=> 702 int(2147483647) 703} 704array(1) { 705 [0]=> 706 int(2147483647) 707} 708array(1) { 709 [0]=> 710 int(18023) 711} 712array(1) { 713 [0]=> 714 int(18297) 715} 716array(1) { 717 [0]=> 718 int(16533) 719} 720array(1) { 721 [0]=> 722 int(592) 723} 724array(1) { 725 [0]=> 726 int(-2147483648) 727} 728array(1) { 729 [0]=> 730 int(2147483647) 731} 732array(1) { 733 [0]=> 734 int(2147483647) 735} 736array(1) { 737 [0]=> 738 int(131) 739} 740array(1) { 741 [0]=> 742 int(1) 743} 744array(1) { 745 [0]=> 746 int(-2147483648) 747} 748array(1) { 749 [0]=> 750 int(2147483647) 751} 752bool(false) 753 754-- iteration 10 -- 755array(1) { 756 [0]=> 757 int(0) 758} 759array(1) { 760 [0]=> 761 int(1) 762} 763array(1) { 764 [0]=> 765 int(-1) 766} 767array(1) { 768 [0]=> 769 int(-532) 770} 771array(1) { 772 [0]=> 773 int(-532) 774} 775array(1) { 776 [0]=> 777 int(8519) 778} 779array(1) { 780 [0]=> 781 int(8519) 782} 783array(1) { 784 [0]=> 785 int(18023) 786} 787array(1) { 788 [0]=> 789 int(18297) 790} 791array(1) { 792 [0]=> 793 int(16533) 794} 795array(1) { 796 [0]=> 797 int(592) 798} 799array(1) { 800 [0]=> 801 int(-532) 802} 803array(1) { 804 [0]=> 805 int(8519) 806} 807array(1) { 808 [0]=> 809 int(8519) 810} 811array(1) { 812 [0]=> 813 int(131) 814} 815array(1) { 816 [0]=> 817 int(1) 818} 819array(1) { 820 [0]=> 821 int(-532) 822} 823array(1) { 824 [0]=> 825 int(8519) 826} 827bool(false) 828 829-- iteration 11 -- 830array(1) { 831 [0]=> 832 int(0) 833} 834array(1) { 835 [0]=> 836 int(1) 837} 838array(1) { 839 [0]=> 840 int(-1) 841} 842array(1) { 843 [0]=> 844 int(-2147483648) 845} 846array(1) { 847 [0]=> 848 int(-2147483648) 849} 850array(1) { 851 [0]=> 852 int(2147483647) 853} 854array(1) { 855 [0]=> 856 int(2147483647) 857} 858array(1) { 859 [0]=> 860 int(18023) 861} 862array(1) { 863 [0]=> 864 int(18297) 865} 866array(1) { 867 [0]=> 868 int(16533) 869} 870array(1) { 871 [0]=> 872 int(592) 873} 874array(1) { 875 [0]=> 876 int(-2147483648) 877} 878array(1) { 879 [0]=> 880 int(2147483647) 881} 882array(1) { 883 [0]=> 884 int(2147483647) 885} 886array(1) { 887 [0]=> 888 int(131) 889} 890array(1) { 891 [0]=> 892 int(1) 893} 894array(1) { 895 [0]=> 896 int(-2147483648) 897} 898array(1) { 899 [0]=> 900 int(2147483647) 901} 902bool(false) 903 904-- iteration 12 -- 905array(1) { 906 [0]=> 907 string(1) "0" 908} 909array(1) { 910 [0]=> 911 string(1) "1" 912} 913array(1) { 914 [0]=> 915 NULL 916} 917array(1) { 918 [0]=> 919 NULL 920} 921array(1) { 922 [0]=> 923 NULL 924} 925array(1) { 926 [0]=> 927 string(10) "2147483647" 928} 929array(1) { 930 [0]=> 931 string(10) "2147483640" 932} 933array(1) { 934 [0]=> 935 string(4) "4667" 936} 937array(1) { 938 [0]=> 939 string(4) "4779" 940} 941array(1) { 942 [0]=> 943 string(4) "4095" 944} 945array(1) { 946 [0]=> 947 string(3) "250" 948} 949array(1) { 950 [0]=> 951 NULL 952} 953array(1) { 954 [0]=> 955 string(10) "2147483647" 956} 957array(1) { 958 [0]=> 959 string(10) "2147483647" 960} 961array(1) { 962 [0]=> 963 string(2) "83" 964} 965array(1) { 966 [0]=> 967 string(1) "1" 968} 969array(1) { 970 [0]=> 971 NULL 972} 973array(1) { 974 [0]=> 975 string(10) "2147483647" 976} 977bool(false) 978 979-- iteration 13 -- 980array(0) { 981} 982array(0) { 983} 984array(0) { 985} 986array(0) { 987} 988array(0) { 989} 990array(0) { 991} 992array(0) { 993} 994array(0) { 995} 996array(0) { 997} 998array(0) { 999} 1000array(0) { 1001} 1002array(0) { 1003} 1004array(0) { 1005} 1006array(0) { 1007} 1008array(0) { 1009} 1010array(0) { 1011} 1012array(0) { 1013} 1014array(0) { 1015} 1016bool(false) 1017 1018*** Done *** 1019