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