Return to fread_basic.phpt CVS log | Up to [ELWIX - Embedded LightWeight unIX -] / embedaddon / php / ext / standard / tests / file |
1.1 misho 1: --TEST-- 2: Test fread() function : basic functionality 3: --FILE-- 4: <?php 5: /* 6: Prototype: string fread ( resource $handle [, int $length] ); 7: Description: reads up to length bytes from the file pointer referenced by handle. 8: Reading stops when up to length bytes have been read, EOF (end of file) is 9: reached, (for network streams) when a packet becomes available, or (after 10: opening userspace stream) when 8192 bytes have been read whichever comes first. 11: */ 12: 13: // include the file.inc for common functions for test 14: include ("file.inc"); 15: 16: /* Function : function check_size(string $data, int $expect_size) 17: Description : Check the length of the data, and compare the size with $expect_size 18: $data : Text data. 19: $expect_size : Expected data length 20: */ 21: function check_size($data, $expect_size) { 22: 23: $size=strlen($data); 24: if ( $size == $expect_size) 25: echo "OK\n"; 26: else 27: echo "Error: Expected: $expect_size, Actual: $size"; 28: } 29: 30: 31: echo "*** Testing fread() basic operations ***\n"; 32: /* 33: test fread with file opened in "r" and "rb" mode only 34: Content with numeric and strings with it 35: */ 36: $file_modes = array( "r", "rb", "rt", "r+", "r+b", "r+t"); 37: $file_content_types = array("numeric","text","text_with_new_line","alphanumeric"); 38: 39: foreach($file_content_types as $file_content_type) { 40: echo "\n-- Testing fread) with file having data of type ". $file_content_type ." --\n"; 41: /* create files with $file_content_type */ 42: create_files ( dirname(__FILE__), 1, $file_content_type, 0755, 1, "w", "fread_basic"); 43: $filename = dirname(__FILE__)."/fread_basic1.tmp"; // this is name of the file created by create_files() 44: 45: /* open the file using $files_modes and perform fread() on it */ 46: for($inner_loop_counter = 0; 47: $inner_loop_counter < count($file_modes); 48: $inner_loop_counter++) { 49: 50: echo "-- File opened in mode ".$file_modes[$inner_loop_counter]." --\n"; 51: $file_handle = fopen($filename, $file_modes[$inner_loop_counter]); 52: if (!$file_handle) { 53: echo "Error: failed to fopen() file: $filename!"; 54: exit(); 55: } 56: 57: /* read file by giving the acutal length, check the length and content by calculating the 58: hash using md5() function 59: */ 60: /* Reading 1024 bytes from file, expecting 1024 bytes */ ; 61: 62: var_dump(ftell($file_handle)); 63: var_dump( feof($file_handle) ); 64: echo "Reading 1024 bytes from file, expecting 1024 bytes ... "; 65: $data_from_file=fread($file_handle, 1024); 66: check_size($data_from_file,1024); 67: var_dump(ftell($file_handle)); 68: var_dump( feof($file_handle) ); 69: var_dump( md5($data_from_file) ); // calculate the hash and dump it 70: 71: /* read file by giving size more than its size */ 72: var_dump(rewind($file_handle)); 73: var_dump(ftell($file_handle)); 74: var_dump( feof($file_handle) ); 75: /*reading 1030 bytes from file, expecting 1024 bytes */ ; 76: echo "Reading 1030 bytes from file, expecting 1024 bytes ... "; 77: $data_from_file=fread($file_handle, 1030);// request for 6 bytes more than its size 78: check_size($data_from_file,1024); 79: var_dump(ftell($file_handle)); 80: var_dump( feof($file_handle) ); 81: var_dump( md5($data_from_file) ); // calculate the hash and dump it 82: 83: // reading 1000 bytes within the file max size 84: var_dump(rewind($file_handle)); 85: var_dump(ftell($file_handle)); 86: var_dump( feof($file_handle) ); 87: /*reading 1000 bytes from file, expecting 1000 bytes */ ; 88: echo "Reading 1000 bytes from file, expecting 1000 bytes ... "; 89: $data_from_file=fread($file_handle, 1000);// request for 24 bytes less than its size 90: check_size($data_from_file,1000); 91: var_dump(ftell($file_handle)); 92: var_dump( feof($file_handle) ); 93: var_dump( md5($data_from_file) ); // calculate the hash and dump it 94: var_dump(fclose($file_handle)); // now close the file 95: } // end of inner for loop 96: 97: // delete the file created 98: delete_file($filename); // delete file with name 99: } // end of outer foreach loop 100: echo "Done\n"; 101: ?> 102: --EXPECTF-- 103: *** Testing fread() basic operations *** 104: 105: -- Testing fread) with file having data of type numeric -- 106: -- File opened in mode r -- 107: int(0) 108: bool(false) 109: Reading 1024 bytes from file, expecting 1024 bytes ... OK 110: int(1024) 111: bool(false) 112: string(32) "950b7457d1deb6332f2fc5d42f3129d6" 113: bool(true) 114: int(0) 115: bool(false) 116: Reading 1030 bytes from file, expecting 1024 bytes ... OK 117: int(1024) 118: bool(true) 119: string(32) "950b7457d1deb6332f2fc5d42f3129d6" 120: bool(true) 121: int(0) 122: bool(false) 123: Reading 1000 bytes from file, expecting 1000 bytes ... OK 124: int(1000) 125: bool(false) 126: string(32) "4501f99f2b79d0345f26f1394aca58a3" 127: bool(true) 128: -- File opened in mode rb -- 129: int(0) 130: bool(false) 131: Reading 1024 bytes from file, expecting 1024 bytes ... OK 132: int(1024) 133: bool(false) 134: string(32) "950b7457d1deb6332f2fc5d42f3129d6" 135: bool(true) 136: int(0) 137: bool(false) 138: Reading 1030 bytes from file, expecting 1024 bytes ... OK 139: int(1024) 140: bool(true) 141: string(32) "950b7457d1deb6332f2fc5d42f3129d6" 142: bool(true) 143: int(0) 144: bool(false) 145: Reading 1000 bytes from file, expecting 1000 bytes ... OK 146: int(1000) 147: bool(false) 148: string(32) "4501f99f2b79d0345f26f1394aca58a3" 149: bool(true) 150: -- File opened in mode rt -- 151: int(0) 152: bool(false) 153: Reading 1024 bytes from file, expecting 1024 bytes ... OK 154: int(1024) 155: bool(false) 156: string(32) "950b7457d1deb6332f2fc5d42f3129d6" 157: bool(true) 158: int(0) 159: bool(false) 160: Reading 1030 bytes from file, expecting 1024 bytes ... OK 161: int(1024) 162: bool(true) 163: string(32) "950b7457d1deb6332f2fc5d42f3129d6" 164: bool(true) 165: int(0) 166: bool(false) 167: Reading 1000 bytes from file, expecting 1000 bytes ... OK 168: int(1000) 169: bool(false) 170: string(32) "4501f99f2b79d0345f26f1394aca58a3" 171: bool(true) 172: -- File opened in mode r+ -- 173: int(0) 174: bool(false) 175: Reading 1024 bytes from file, expecting 1024 bytes ... OK 176: int(1024) 177: bool(false) 178: string(32) "950b7457d1deb6332f2fc5d42f3129d6" 179: bool(true) 180: int(0) 181: bool(false) 182: Reading 1030 bytes from file, expecting 1024 bytes ... OK 183: int(1024) 184: bool(true) 185: string(32) "950b7457d1deb6332f2fc5d42f3129d6" 186: bool(true) 187: int(0) 188: bool(false) 189: Reading 1000 bytes from file, expecting 1000 bytes ... OK 190: int(1000) 191: bool(false) 192: string(32) "4501f99f2b79d0345f26f1394aca58a3" 193: bool(true) 194: -- File opened in mode r+b -- 195: int(0) 196: bool(false) 197: Reading 1024 bytes from file, expecting 1024 bytes ... OK 198: int(1024) 199: bool(false) 200: string(32) "950b7457d1deb6332f2fc5d42f3129d6" 201: bool(true) 202: int(0) 203: bool(false) 204: Reading 1030 bytes from file, expecting 1024 bytes ... OK 205: int(1024) 206: bool(true) 207: string(32) "950b7457d1deb6332f2fc5d42f3129d6" 208: bool(true) 209: int(0) 210: bool(false) 211: Reading 1000 bytes from file, expecting 1000 bytes ... OK 212: int(1000) 213: bool(false) 214: string(32) "4501f99f2b79d0345f26f1394aca58a3" 215: bool(true) 216: -- File opened in mode r+t -- 217: int(0) 218: bool(false) 219: Reading 1024 bytes from file, expecting 1024 bytes ... OK 220: int(1024) 221: bool(false) 222: string(32) "950b7457d1deb6332f2fc5d42f3129d6" 223: bool(true) 224: int(0) 225: bool(false) 226: Reading 1030 bytes from file, expecting 1024 bytes ... OK 227: int(1024) 228: bool(true) 229: string(32) "950b7457d1deb6332f2fc5d42f3129d6" 230: bool(true) 231: int(0) 232: bool(false) 233: Reading 1000 bytes from file, expecting 1000 bytes ... OK 234: int(1000) 235: bool(false) 236: string(32) "4501f99f2b79d0345f26f1394aca58a3" 237: bool(true) 238: 239: -- Testing fread) with file having data of type text -- 240: -- File opened in mode r -- 241: int(0) 242: bool(false) 243: Reading 1024 bytes from file, expecting 1024 bytes ... OK 244: int(1024) 245: bool(false) 246: string(32) "e486000c4c8452774f746a27658d87fa" 247: bool(true) 248: int(0) 249: bool(false) 250: Reading 1030 bytes from file, expecting 1024 bytes ... OK 251: int(1024) 252: bool(true) 253: string(32) "e486000c4c8452774f746a27658d87fa" 254: bool(true) 255: int(0) 256: bool(false) 257: Reading 1000 bytes from file, expecting 1000 bytes ... OK 258: int(1000) 259: bool(false) 260: string(32) "2ec76a59f8c44b8f8a0f5139f61bb1bd" 261: bool(true) 262: -- File opened in mode rb -- 263: int(0) 264: bool(false) 265: Reading 1024 bytes from file, expecting 1024 bytes ... OK 266: int(1024) 267: bool(false) 268: string(32) "e486000c4c8452774f746a27658d87fa" 269: bool(true) 270: int(0) 271: bool(false) 272: Reading 1030 bytes from file, expecting 1024 bytes ... OK 273: int(1024) 274: bool(true) 275: string(32) "e486000c4c8452774f746a27658d87fa" 276: bool(true) 277: int(0) 278: bool(false) 279: Reading 1000 bytes from file, expecting 1000 bytes ... OK 280: int(1000) 281: bool(false) 282: string(32) "2ec76a59f8c44b8f8a0f5139f61bb1bd" 283: bool(true) 284: -- File opened in mode rt -- 285: int(0) 286: bool(false) 287: Reading 1024 bytes from file, expecting 1024 bytes ... OK 288: int(1024) 289: bool(false) 290: string(32) "e486000c4c8452774f746a27658d87fa" 291: bool(true) 292: int(0) 293: bool(false) 294: Reading 1030 bytes from file, expecting 1024 bytes ... OK 295: int(1024) 296: bool(true) 297: string(32) "e486000c4c8452774f746a27658d87fa" 298: bool(true) 299: int(0) 300: bool(false) 301: Reading 1000 bytes from file, expecting 1000 bytes ... OK 302: int(1000) 303: bool(false) 304: string(32) "2ec76a59f8c44b8f8a0f5139f61bb1bd" 305: bool(true) 306: -- File opened in mode r+ -- 307: int(0) 308: bool(false) 309: Reading 1024 bytes from file, expecting 1024 bytes ... OK 310: int(1024) 311: bool(false) 312: string(32) "e486000c4c8452774f746a27658d87fa" 313: bool(true) 314: int(0) 315: bool(false) 316: Reading 1030 bytes from file, expecting 1024 bytes ... OK 317: int(1024) 318: bool(true) 319: string(32) "e486000c4c8452774f746a27658d87fa" 320: bool(true) 321: int(0) 322: bool(false) 323: Reading 1000 bytes from file, expecting 1000 bytes ... OK 324: int(1000) 325: bool(false) 326: string(32) "2ec76a59f8c44b8f8a0f5139f61bb1bd" 327: bool(true) 328: -- File opened in mode r+b -- 329: int(0) 330: bool(false) 331: Reading 1024 bytes from file, expecting 1024 bytes ... OK 332: int(1024) 333: bool(false) 334: string(32) "e486000c4c8452774f746a27658d87fa" 335: bool(true) 336: int(0) 337: bool(false) 338: Reading 1030 bytes from file, expecting 1024 bytes ... OK 339: int(1024) 340: bool(true) 341: string(32) "e486000c4c8452774f746a27658d87fa" 342: bool(true) 343: int(0) 344: bool(false) 345: Reading 1000 bytes from file, expecting 1000 bytes ... OK 346: int(1000) 347: bool(false) 348: string(32) "2ec76a59f8c44b8f8a0f5139f61bb1bd" 349: bool(true) 350: -- File opened in mode r+t -- 351: int(0) 352: bool(false) 353: Reading 1024 bytes from file, expecting 1024 bytes ... OK 354: int(1024) 355: bool(false) 356: string(32) "e486000c4c8452774f746a27658d87fa" 357: bool(true) 358: int(0) 359: bool(false) 360: Reading 1030 bytes from file, expecting 1024 bytes ... OK 361: int(1024) 362: bool(true) 363: string(32) "e486000c4c8452774f746a27658d87fa" 364: bool(true) 365: int(0) 366: bool(false) 367: Reading 1000 bytes from file, expecting 1000 bytes ... OK 368: int(1000) 369: bool(false) 370: string(32) "2ec76a59f8c44b8f8a0f5139f61bb1bd" 371: bool(true) 372: 373: -- Testing fread) with file having data of type text_with_new_line -- 374: -- File opened in mode r -- 375: int(0) 376: bool(false) 377: Reading 1024 bytes from file, expecting 1024 bytes ... OK 378: int(1024) 379: bool(false) 380: string(32) "b09c8026a64a88d36d4c2f17983964bb" 381: bool(true) 382: int(0) 383: bool(false) 384: Reading 1030 bytes from file, expecting 1024 bytes ... OK 385: int(1024) 386: bool(true) 387: string(32) "b09c8026a64a88d36d4c2f17983964bb" 388: bool(true) 389: int(0) 390: bool(false) 391: Reading 1000 bytes from file, expecting 1000 bytes ... OK 392: int(1000) 393: bool(false) 394: string(32) "a148fa8110bbac875d84fc9d7056c0a1" 395: bool(true) 396: -- File opened in mode rb -- 397: int(0) 398: bool(false) 399: Reading 1024 bytes from file, expecting 1024 bytes ... OK 400: int(1024) 401: bool(false) 402: string(32) "b09c8026a64a88d36d4c2f17983964bb" 403: bool(true) 404: int(0) 405: bool(false) 406: Reading 1030 bytes from file, expecting 1024 bytes ... OK 407: int(1024) 408: bool(true) 409: string(32) "b09c8026a64a88d36d4c2f17983964bb" 410: bool(true) 411: int(0) 412: bool(false) 413: Reading 1000 bytes from file, expecting 1000 bytes ... OK 414: int(1000) 415: bool(false) 416: string(32) "a148fa8110bbac875d84fc9d7056c0a1" 417: bool(true) 418: -- File opened in mode rt -- 419: int(0) 420: bool(false) 421: Reading 1024 bytes from file, expecting 1024 bytes ... OK 422: int(1024) 423: bool(false) 424: string(32) "b09c8026a64a88d36d4c2f17983964bb" 425: bool(true) 426: int(0) 427: bool(false) 428: Reading 1030 bytes from file, expecting 1024 bytes ... OK 429: int(1024) 430: bool(true) 431: string(32) "b09c8026a64a88d36d4c2f17983964bb" 432: bool(true) 433: int(0) 434: bool(false) 435: Reading 1000 bytes from file, expecting 1000 bytes ... OK 436: int(1000) 437: bool(false) 438: string(32) "a148fa8110bbac875d84fc9d7056c0a1" 439: bool(true) 440: -- File opened in mode r+ -- 441: int(0) 442: bool(false) 443: Reading 1024 bytes from file, expecting 1024 bytes ... OK 444: int(1024) 445: bool(false) 446: string(32) "b09c8026a64a88d36d4c2f17983964bb" 447: bool(true) 448: int(0) 449: bool(false) 450: Reading 1030 bytes from file, expecting 1024 bytes ... OK 451: int(1024) 452: bool(true) 453: string(32) "b09c8026a64a88d36d4c2f17983964bb" 454: bool(true) 455: int(0) 456: bool(false) 457: Reading 1000 bytes from file, expecting 1000 bytes ... OK 458: int(1000) 459: bool(false) 460: string(32) "a148fa8110bbac875d84fc9d7056c0a1" 461: bool(true) 462: -- File opened in mode r+b -- 463: int(0) 464: bool(false) 465: Reading 1024 bytes from file, expecting 1024 bytes ... OK 466: int(1024) 467: bool(false) 468: string(32) "b09c8026a64a88d36d4c2f17983964bb" 469: bool(true) 470: int(0) 471: bool(false) 472: Reading 1030 bytes from file, expecting 1024 bytes ... OK 473: int(1024) 474: bool(true) 475: string(32) "b09c8026a64a88d36d4c2f17983964bb" 476: bool(true) 477: int(0) 478: bool(false) 479: Reading 1000 bytes from file, expecting 1000 bytes ... OK 480: int(1000) 481: bool(false) 482: string(32) "a148fa8110bbac875d84fc9d7056c0a1" 483: bool(true) 484: -- File opened in mode r+t -- 485: int(0) 486: bool(false) 487: Reading 1024 bytes from file, expecting 1024 bytes ... OK 488: int(1024) 489: bool(false) 490: string(32) "b09c8026a64a88d36d4c2f17983964bb" 491: bool(true) 492: int(0) 493: bool(false) 494: Reading 1030 bytes from file, expecting 1024 bytes ... OK 495: int(1024) 496: bool(true) 497: string(32) "b09c8026a64a88d36d4c2f17983964bb" 498: bool(true) 499: int(0) 500: bool(false) 501: Reading 1000 bytes from file, expecting 1000 bytes ... OK 502: int(1000) 503: bool(false) 504: string(32) "a148fa8110bbac875d84fc9d7056c0a1" 505: bool(true) 506: 507: -- Testing fread) with file having data of type alphanumeric -- 508: -- File opened in mode r -- 509: int(0) 510: bool(false) 511: Reading 1024 bytes from file, expecting 1024 bytes ... OK 512: int(1024) 513: bool(false) 514: string(32) "3fabd48d8eaa65c14e0d93d6880c560c" 515: bool(true) 516: int(0) 517: bool(false) 518: Reading 1030 bytes from file, expecting 1024 bytes ... OK 519: int(1024) 520: bool(true) 521: string(32) "3fabd48d8eaa65c14e0d93d6880c560c" 522: bool(true) 523: int(0) 524: bool(false) 525: Reading 1000 bytes from file, expecting 1000 bytes ... OK 526: int(1000) 527: bool(false) 528: string(32) "a49d752f980184c7f44568e930f89c72" 529: bool(true) 530: -- File opened in mode rb -- 531: int(0) 532: bool(false) 533: Reading 1024 bytes from file, expecting 1024 bytes ... OK 534: int(1024) 535: bool(false) 536: string(32) "3fabd48d8eaa65c14e0d93d6880c560c" 537: bool(true) 538: int(0) 539: bool(false) 540: Reading 1030 bytes from file, expecting 1024 bytes ... OK 541: int(1024) 542: bool(true) 543: string(32) "3fabd48d8eaa65c14e0d93d6880c560c" 544: bool(true) 545: int(0) 546: bool(false) 547: Reading 1000 bytes from file, expecting 1000 bytes ... OK 548: int(1000) 549: bool(false) 550: string(32) "a49d752f980184c7f44568e930f89c72" 551: bool(true) 552: -- File opened in mode rt -- 553: int(0) 554: bool(false) 555: Reading 1024 bytes from file, expecting 1024 bytes ... OK 556: int(1024) 557: bool(false) 558: string(32) "3fabd48d8eaa65c14e0d93d6880c560c" 559: bool(true) 560: int(0) 561: bool(false) 562: Reading 1030 bytes from file, expecting 1024 bytes ... OK 563: int(1024) 564: bool(true) 565: string(32) "3fabd48d8eaa65c14e0d93d6880c560c" 566: bool(true) 567: int(0) 568: bool(false) 569: Reading 1000 bytes from file, expecting 1000 bytes ... OK 570: int(1000) 571: bool(false) 572: string(32) "a49d752f980184c7f44568e930f89c72" 573: bool(true) 574: -- File opened in mode r+ -- 575: int(0) 576: bool(false) 577: Reading 1024 bytes from file, expecting 1024 bytes ... OK 578: int(1024) 579: bool(false) 580: string(32) "3fabd48d8eaa65c14e0d93d6880c560c" 581: bool(true) 582: int(0) 583: bool(false) 584: Reading 1030 bytes from file, expecting 1024 bytes ... OK 585: int(1024) 586: bool(true) 587: string(32) "3fabd48d8eaa65c14e0d93d6880c560c" 588: bool(true) 589: int(0) 590: bool(false) 591: Reading 1000 bytes from file, expecting 1000 bytes ... OK 592: int(1000) 593: bool(false) 594: string(32) "a49d752f980184c7f44568e930f89c72" 595: bool(true) 596: -- File opened in mode r+b -- 597: int(0) 598: bool(false) 599: Reading 1024 bytes from file, expecting 1024 bytes ... OK 600: int(1024) 601: bool(false) 602: string(32) "3fabd48d8eaa65c14e0d93d6880c560c" 603: bool(true) 604: int(0) 605: bool(false) 606: Reading 1030 bytes from file, expecting 1024 bytes ... OK 607: int(1024) 608: bool(true) 609: string(32) "3fabd48d8eaa65c14e0d93d6880c560c" 610: bool(true) 611: int(0) 612: bool(false) 613: Reading 1000 bytes from file, expecting 1000 bytes ... OK 614: int(1000) 615: bool(false) 616: string(32) "a49d752f980184c7f44568e930f89c72" 617: bool(true) 618: -- File opened in mode r+t -- 619: int(0) 620: bool(false) 621: Reading 1024 bytes from file, expecting 1024 bytes ... OK 622: int(1024) 623: bool(false) 624: string(32) "3fabd48d8eaa65c14e0d93d6880c560c" 625: bool(true) 626: int(0) 627: bool(false) 628: Reading 1030 bytes from file, expecting 1024 bytes ... OK 629: int(1024) 630: bool(true) 631: string(32) "3fabd48d8eaa65c14e0d93d6880c560c" 632: bool(true) 633: int(0) 634: bool(false) 635: Reading 1000 bytes from file, expecting 1000 bytes ... OK 636: int(1000) 637: bool(false) 638: string(32) "a49d752f980184c7f44568e930f89c72" 639: bool(true) 640: Done