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