fgets_variation3.phpt 13 KB

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