ftruncate_variation7.phpt 11 KB

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