fgets_variation3.phpt 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500
  1. --TEST--
  2. Test fgets() function : usage variations - read with/without length
  3. --FILE--
  4. <?php
  5. /*
  6. Prototype: string fgets ( resource $handle [, int $length] );
  7. Description: Gets a line from file pointer
  8. */
  9. // include the file.inc for common test funcitons
  10. include ("file.inc");
  11. $file_modes = array("w+", "w+b", "w+t",
  12. "a+", "a+b", "a+t",
  13. "x+", "x+b", "x+t");
  14. $file_content_types = array("numeric", "text", "text_with_new_line", "alphanumeric");
  15. echo "*** Testing fgets() : usage variations ***\n";
  16. $filename = dirname(__FILE__)."/fgets_variation3.tmp";
  17. foreach($file_modes as $file_mode) {
  18. echo "\n-- Testing fgets() with file opened using mode $file_mode --\n";
  19. foreach($file_content_types as $file_content_type) {
  20. echo "-- File content type : $file_content_type --\n";
  21. /* create files with $file_content_type */
  22. $file_handle = fopen($filename, $file_mode);
  23. $data = fill_file($file_handle, $file_content_type, 50);
  24. if ( !$file_handle ) {
  25. echo "Error: failed to open file $filename!";
  26. exit();
  27. }
  28. echo "-- fgets() with default length, file pointer at 0 --\n";
  29. // get the file pointer to beginning of the file
  30. rewind($file_handle);
  31. var_dump( ftell($file_handle) );
  32. var_dump( fgets($file_handle) ); // with default length
  33. var_dump( ftell($file_handle) ); // ensure the file pointer position
  34. var_dump( feof($file_handle) ); // enusre if eof set
  35. echo "-- fgets() with length = 23, Expected: 22 chars, file pointer at 0 --\n";
  36. // get the file pointer to beginning of the file
  37. rewind($file_handle);
  38. var_dump( ftell($file_handle) );
  39. var_dump( fgets($file_handle, 23) ); // expected: 22 chars
  40. var_dump( ftell($file_handle) ); // ensure the file pointer position
  41. var_dump( feof($file_handle) ); // enusre if eof set
  42. //close file
  43. fclose($file_handle);
  44. // delete file
  45. delete_file($filename);
  46. } // file_content_type loop
  47. } // file_mode loop
  48. echo "Done\n";
  49. ?>
  50. --EXPECTF--
  51. *** Testing fgets() : usage variations ***
  52. -- Testing fgets() with file opened using mode w+ --
  53. -- File content type : numeric --
  54. -- fgets() with default length, file pointer at 0 --
  55. int(0)
  56. string(50) "22222222222222222222222222222222222222222222222222"
  57. int(50)
  58. bool(true)
  59. -- fgets() with length = 23, Expected: 22 chars, file pointer at 0 --
  60. int(0)
  61. string(22) "2222222222222222222222"
  62. int(22)
  63. bool(false)
  64. -- File content type : text --
  65. -- fgets() with default length, file pointer at 0 --
  66. int(0)
  67. string(50) "text text text text text text text text text text "
  68. int(50)
  69. bool(true)
  70. -- fgets() with length = 23, Expected: 22 chars, file pointer at 0 --
  71. int(0)
  72. string(22) "text text text text te"
  73. int(22)
  74. bool(false)
  75. -- File content type : text_with_new_line --
  76. -- fgets() with default length, file pointer at 0 --
  77. int(0)
  78. string(5) "line
  79. "
  80. int(5)
  81. bool(false)
  82. -- fgets() with length = 23, Expected: 22 chars, file pointer at 0 --
  83. int(0)
  84. string(5) "line
  85. "
  86. int(5)
  87. bool(false)
  88. -- File content type : alphanumeric --
  89. -- fgets() with default length, file pointer at 0 --
  90. int(0)
  91. string(50) "ab12 ab12 ab12 ab12 ab12 ab12 ab12 ab12 ab12 ab12 "
  92. int(50)
  93. bool(true)
  94. -- fgets() with length = 23, Expected: 22 chars, file pointer at 0 --
  95. int(0)
  96. string(22) "ab12 ab12 ab12 ab12 ab"
  97. int(22)
  98. bool(false)
  99. -- Testing fgets() with file opened using mode w+b --
  100. -- File content type : numeric --
  101. -- fgets() with default length, file pointer at 0 --
  102. int(0)
  103. string(50) "22222222222222222222222222222222222222222222222222"
  104. int(50)
  105. bool(true)
  106. -- fgets() with length = 23, Expected: 22 chars, file pointer at 0 --
  107. int(0)
  108. string(22) "2222222222222222222222"
  109. int(22)
  110. bool(false)
  111. -- File content type : text --
  112. -- fgets() with default length, file pointer at 0 --
  113. int(0)
  114. string(50) "text text text text text text text text text text "
  115. int(50)
  116. bool(true)
  117. -- fgets() with length = 23, Expected: 22 chars, file pointer at 0 --
  118. int(0)
  119. string(22) "text text text text te"
  120. int(22)
  121. bool(false)
  122. -- File content type : text_with_new_line --
  123. -- fgets() with default length, file pointer at 0 --
  124. int(0)
  125. string(5) "line
  126. "
  127. int(5)
  128. bool(false)
  129. -- fgets() with length = 23, Expected: 22 chars, file pointer at 0 --
  130. int(0)
  131. string(5) "line
  132. "
  133. int(5)
  134. bool(false)
  135. -- File content type : alphanumeric --
  136. -- fgets() with default length, file pointer at 0 --
  137. int(0)
  138. string(50) "ab12 ab12 ab12 ab12 ab12 ab12 ab12 ab12 ab12 ab12 "
  139. int(50)
  140. bool(true)
  141. -- fgets() with length = 23, Expected: 22 chars, file pointer at 0 --
  142. int(0)
  143. string(22) "ab12 ab12 ab12 ab12 ab"
  144. int(22)
  145. bool(false)
  146. -- Testing fgets() with file opened using mode w+t --
  147. -- File content type : numeric --
  148. -- fgets() with default length, file pointer at 0 --
  149. int(0)
  150. string(50) "22222222222222222222222222222222222222222222222222"
  151. int(50)
  152. bool(true)
  153. -- fgets() with length = 23, Expected: 22 chars, file pointer at 0 --
  154. int(0)
  155. string(22) "2222222222222222222222"
  156. int(22)
  157. bool(false)
  158. -- File content type : text --
  159. -- fgets() with default length, file pointer at 0 --
  160. int(0)
  161. string(50) "text text text text text text text text text text "
  162. int(50)
  163. bool(true)
  164. -- fgets() with length = 23, Expected: 22 chars, file pointer at 0 --
  165. int(0)
  166. string(22) "text text text text te"
  167. int(22)
  168. bool(false)
  169. -- File content type : text_with_new_line --
  170. -- fgets() with default length, file pointer at 0 --
  171. int(0)
  172. string(5) "line
  173. "
  174. int(5)
  175. bool(false)
  176. -- fgets() with length = 23, Expected: 22 chars, file pointer at 0 --
  177. int(0)
  178. string(5) "line
  179. "
  180. int(5)
  181. bool(false)
  182. -- File content type : alphanumeric --
  183. -- fgets() with default length, file pointer at 0 --
  184. int(0)
  185. string(50) "ab12 ab12 ab12 ab12 ab12 ab12 ab12 ab12 ab12 ab12 "
  186. int(50)
  187. bool(true)
  188. -- fgets() with length = 23, Expected: 22 chars, file pointer at 0 --
  189. int(0)
  190. string(22) "ab12 ab12 ab12 ab12 ab"
  191. int(22)
  192. bool(false)
  193. -- Testing fgets() with file opened using mode a+ --
  194. -- File content type : numeric --
  195. -- fgets() with default length, file pointer at 0 --
  196. int(0)
  197. string(50) "22222222222222222222222222222222222222222222222222"
  198. int(50)
  199. bool(true)
  200. -- fgets() with length = 23, Expected: 22 chars, file pointer at 0 --
  201. int(0)
  202. string(22) "2222222222222222222222"
  203. int(22)
  204. bool(false)
  205. -- File content type : text --
  206. -- fgets() with default length, file pointer at 0 --
  207. int(0)
  208. string(50) "text text text text text text text text text text "
  209. int(50)
  210. bool(true)
  211. -- fgets() with length = 23, Expected: 22 chars, file pointer at 0 --
  212. int(0)
  213. string(22) "text text text text te"
  214. int(22)
  215. bool(false)
  216. -- File content type : text_with_new_line --
  217. -- fgets() with default length, file pointer at 0 --
  218. int(0)
  219. string(5) "line
  220. "
  221. int(5)
  222. bool(false)
  223. -- fgets() with length = 23, Expected: 22 chars, file pointer at 0 --
  224. int(0)
  225. string(5) "line
  226. "
  227. int(5)
  228. bool(false)
  229. -- File content type : alphanumeric --
  230. -- fgets() with default length, file pointer at 0 --
  231. int(0)
  232. string(50) "ab12 ab12 ab12 ab12 ab12 ab12 ab12 ab12 ab12 ab12 "
  233. int(50)
  234. bool(true)
  235. -- fgets() with length = 23, Expected: 22 chars, file pointer at 0 --
  236. int(0)
  237. string(22) "ab12 ab12 ab12 ab12 ab"
  238. int(22)
  239. bool(false)
  240. -- Testing fgets() with file opened using mode a+b --
  241. -- File content type : numeric --
  242. -- fgets() with default length, file pointer at 0 --
  243. int(0)
  244. string(50) "22222222222222222222222222222222222222222222222222"
  245. int(50)
  246. bool(true)
  247. -- fgets() with length = 23, Expected: 22 chars, file pointer at 0 --
  248. int(0)
  249. string(22) "2222222222222222222222"
  250. int(22)
  251. bool(false)
  252. -- File content type : text --
  253. -- fgets() with default length, file pointer at 0 --
  254. int(0)
  255. string(50) "text text text text text text text text text text "
  256. int(50)
  257. bool(true)
  258. -- fgets() with length = 23, Expected: 22 chars, file pointer at 0 --
  259. int(0)
  260. string(22) "text text text text te"
  261. int(22)
  262. bool(false)
  263. -- File content type : text_with_new_line --
  264. -- fgets() with default length, file pointer at 0 --
  265. int(0)
  266. string(5) "line
  267. "
  268. int(5)
  269. bool(false)
  270. -- fgets() with length = 23, Expected: 22 chars, file pointer at 0 --
  271. int(0)
  272. string(5) "line
  273. "
  274. int(5)
  275. bool(false)
  276. -- File content type : alphanumeric --
  277. -- fgets() with default length, file pointer at 0 --
  278. int(0)
  279. string(50) "ab12 ab12 ab12 ab12 ab12 ab12 ab12 ab12 ab12 ab12 "
  280. int(50)
  281. bool(true)
  282. -- fgets() with length = 23, Expected: 22 chars, file pointer at 0 --
  283. int(0)
  284. string(22) "ab12 ab12 ab12 ab12 ab"
  285. int(22)
  286. bool(false)
  287. -- Testing fgets() with file opened using mode a+t --
  288. -- File content type : numeric --
  289. -- fgets() with default length, file pointer at 0 --
  290. int(0)
  291. string(50) "22222222222222222222222222222222222222222222222222"
  292. int(50)
  293. bool(true)
  294. -- fgets() with length = 23, Expected: 22 chars, file pointer at 0 --
  295. int(0)
  296. string(22) "2222222222222222222222"
  297. int(22)
  298. bool(false)
  299. -- File content type : text --
  300. -- fgets() with default length, file pointer at 0 --
  301. int(0)
  302. string(50) "text text text text text text text text text text "
  303. int(50)
  304. bool(true)
  305. -- fgets() with length = 23, Expected: 22 chars, file pointer at 0 --
  306. int(0)
  307. string(22) "text text text text te"
  308. int(22)
  309. bool(false)
  310. -- File content type : text_with_new_line --
  311. -- fgets() with default length, file pointer at 0 --
  312. int(0)
  313. string(5) "line
  314. "
  315. int(5)
  316. bool(false)
  317. -- fgets() with length = 23, Expected: 22 chars, file pointer at 0 --
  318. int(0)
  319. string(5) "line
  320. "
  321. int(5)
  322. bool(false)
  323. -- File content type : alphanumeric --
  324. -- fgets() with default length, file pointer at 0 --
  325. int(0)
  326. string(50) "ab12 ab12 ab12 ab12 ab12 ab12 ab12 ab12 ab12 ab12 "
  327. int(50)
  328. bool(true)
  329. -- fgets() with length = 23, Expected: 22 chars, file pointer at 0 --
  330. int(0)
  331. string(22) "ab12 ab12 ab12 ab12 ab"
  332. int(22)
  333. bool(false)
  334. -- Testing fgets() with file opened using mode x+ --
  335. -- File content type : numeric --
  336. -- fgets() with default length, file pointer at 0 --
  337. int(0)
  338. string(50) "22222222222222222222222222222222222222222222222222"
  339. int(50)
  340. bool(true)
  341. -- fgets() with length = 23, Expected: 22 chars, file pointer at 0 --
  342. int(0)
  343. string(22) "2222222222222222222222"
  344. int(22)
  345. bool(false)
  346. -- File content type : text --
  347. -- fgets() with default length, file pointer at 0 --
  348. int(0)
  349. string(50) "text text text text text text text text text text "
  350. int(50)
  351. bool(true)
  352. -- fgets() with length = 23, Expected: 22 chars, file pointer at 0 --
  353. int(0)
  354. string(22) "text text text text te"
  355. int(22)
  356. bool(false)
  357. -- File content type : text_with_new_line --
  358. -- fgets() with default length, file pointer at 0 --
  359. int(0)
  360. string(5) "line
  361. "
  362. int(5)
  363. bool(false)
  364. -- fgets() with length = 23, Expected: 22 chars, file pointer at 0 --
  365. int(0)
  366. string(5) "line
  367. "
  368. int(5)
  369. bool(false)
  370. -- File content type : alphanumeric --
  371. -- fgets() with default length, file pointer at 0 --
  372. int(0)
  373. string(50) "ab12 ab12 ab12 ab12 ab12 ab12 ab12 ab12 ab12 ab12 "
  374. int(50)
  375. bool(true)
  376. -- fgets() with length = 23, Expected: 22 chars, file pointer at 0 --
  377. int(0)
  378. string(22) "ab12 ab12 ab12 ab12 ab"
  379. int(22)
  380. bool(false)
  381. -- Testing fgets() with file opened using mode x+b --
  382. -- File content type : numeric --
  383. -- fgets() with default length, file pointer at 0 --
  384. int(0)
  385. string(50) "22222222222222222222222222222222222222222222222222"
  386. int(50)
  387. bool(true)
  388. -- fgets() with length = 23, Expected: 22 chars, file pointer at 0 --
  389. int(0)
  390. string(22) "2222222222222222222222"
  391. int(22)
  392. bool(false)
  393. -- File content type : text --
  394. -- fgets() with default length, file pointer at 0 --
  395. int(0)
  396. string(50) "text text text text text text text text text text "
  397. int(50)
  398. bool(true)
  399. -- fgets() with length = 23, Expected: 22 chars, file pointer at 0 --
  400. int(0)
  401. string(22) "text text text text te"
  402. int(22)
  403. bool(false)
  404. -- File content type : text_with_new_line --
  405. -- fgets() with default length, file pointer at 0 --
  406. int(0)
  407. string(5) "line
  408. "
  409. int(5)
  410. bool(false)
  411. -- fgets() with length = 23, Expected: 22 chars, file pointer at 0 --
  412. int(0)
  413. string(5) "line
  414. "
  415. int(5)
  416. bool(false)
  417. -- File content type : alphanumeric --
  418. -- fgets() with default length, file pointer at 0 --
  419. int(0)
  420. string(50) "ab12 ab12 ab12 ab12 ab12 ab12 ab12 ab12 ab12 ab12 "
  421. int(50)
  422. bool(true)
  423. -- fgets() with length = 23, Expected: 22 chars, file pointer at 0 --
  424. int(0)
  425. string(22) "ab12 ab12 ab12 ab12 ab"
  426. int(22)
  427. bool(false)
  428. -- Testing fgets() with file opened using mode x+t --
  429. -- File content type : numeric --
  430. -- fgets() with default length, file pointer at 0 --
  431. int(0)
  432. string(50) "22222222222222222222222222222222222222222222222222"
  433. int(50)
  434. bool(true)
  435. -- fgets() with length = 23, Expected: 22 chars, file pointer at 0 --
  436. int(0)
  437. string(22) "2222222222222222222222"
  438. int(22)
  439. bool(false)
  440. -- File content type : text --
  441. -- fgets() with default length, file pointer at 0 --
  442. int(0)
  443. string(50) "text text text text text text text text text text "
  444. int(50)
  445. bool(true)
  446. -- fgets() with length = 23, Expected: 22 chars, file pointer at 0 --
  447. int(0)
  448. string(22) "text text text text te"
  449. int(22)
  450. bool(false)
  451. -- File content type : text_with_new_line --
  452. -- fgets() with default length, file pointer at 0 --
  453. int(0)
  454. string(5) "line
  455. "
  456. int(5)
  457. bool(false)
  458. -- fgets() with length = 23, Expected: 22 chars, file pointer at 0 --
  459. int(0)
  460. string(5) "line
  461. "
  462. int(5)
  463. bool(false)
  464. -- File content type : alphanumeric --
  465. -- fgets() with default length, file pointer at 0 --
  466. int(0)
  467. string(50) "ab12 ab12 ab12 ab12 ab12 ab12 ab12 ab12 ab12 ab12 "
  468. int(50)
  469. bool(true)
  470. -- fgets() with length = 23, Expected: 22 chars, file pointer at 0 --
  471. int(0)
  472. string(22) "ab12 ab12 ab12 ab12 ab"
  473. int(22)
  474. bool(false)
  475. Done