1 /* Common f{truncate} tests definitions.
2 Copyright (C) 2016-2022 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <https://www.gnu.org/licenses/>. */
20 #include <sys/types.h>
24 static void do_prepare (void);
25 #define PREPARE(argc, argv) do_prepare ()
26 static int do_test (void);
27 #define TEST_FUNCTION do_test ()
29 #include <test-skeleton.c>
31 static char *temp_filename
;
37 temp_fd
= create_temp_file ("tst-trucate.", &temp_filename
);
40 printf ("cannot create temporary file: %m\n");
46 do { printf ("error: %s (line %d)\n", str, __LINE__); return 1; } while (0)
49 do_test_with_offset (off_t offset
)
54 memset (buf
, 0xcf, sizeof (buf
));
56 if (pwrite (temp_fd
, buf
, sizeof (buf
), offset
) != sizeof (buf
))
57 FAIL ("write failed");
58 if (fstat (temp_fd
, &st
) < 0 || st
.st_size
!= (offset
+ sizeof (buf
)))
59 FAIL ("initial size wrong");
61 if (ftruncate (temp_fd
, offset
+ 800) < 0)
62 FAIL ("size reduction with ftruncate failed");
63 if (fstat (temp_fd
, &st
) < 0 || st
.st_size
!= (offset
+ 800))
64 FAIL ("size after reduction with ftruncate is incorrect");
66 /* The following test covers more than POSIX. POSIX does not require
67 that ftruncate() can increase the file size. But we are testing
69 if (ftruncate (temp_fd
, offset
+ 1200) < 0)
70 FAIL ("size increate with ftruncate failed");
71 if (fstat (temp_fd
, &st
) < 0 || st
.st_size
!= (offset
+ 1200))
72 FAIL ("size after increase is incorrect");
74 if (truncate (temp_filename
, offset
+ 800) < 0)
75 FAIL ("size reduction with truncate failed");
76 if (fstat (temp_fd
, &st
) < 0 || st
.st_size
!= (offset
+ 800))
77 FAIL ("size after reduction with truncate incorrect");
79 /* The following test covers more than POSIX. POSIX does not require
80 that truncate() can increase the file size. But we are testing
82 if (truncate (temp_filename
, (offset
+ 1200)) < 0)
83 FAIL ("size increase with truncate failed");
84 if (fstat (temp_fd
, &st
) < 0 || st
.st_size
!= (offset
+ 1200))
85 FAIL ("size increase with truncate is incorrect");