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