1 /* Common f{truncate} tests definitions.
2 Copyright (C) 2016-2024 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 #include <support/check.h>
26 static void do_prepare (void);
27 #define PREPARE(argc, argv) do_prepare ()
28 static int do_test (void);
29 #define TEST_FUNCTION do_test ()
31 #include <test-skeleton.c>
33 static char *temp_filename
;
39 temp_fd
= create_temp_file ("tst-trucate.", &temp_filename
);
42 printf ("cannot create temporary file: %m\n");
48 do_test_with_offset (off_t offset
)
53 memset (buf
, 0xcf, sizeof (buf
));
55 if (pwrite (temp_fd
, buf
, sizeof (buf
), offset
) != sizeof (buf
))
56 FAIL_RET ("write failed");
57 if (fstat (temp_fd
, &st
) < 0 || st
.st_size
!= (offset
+ sizeof (buf
)))
58 FAIL_RET ("initial size wrong");
60 if (ftruncate (temp_fd
, offset
+ 800) < 0)
61 FAIL_RET ("size reduction with ftruncate failed");
62 if (fstat (temp_fd
, &st
) < 0 || st
.st_size
!= (offset
+ 800))
63 FAIL_RET ("size after reduction with ftruncate is incorrect");
65 /* The following test covers more than POSIX. POSIX does not require
66 that ftruncate() can increase the file size. But we are testing
68 if (ftruncate (temp_fd
, offset
+ 1200) < 0)
69 FAIL_RET ("size increate with ftruncate failed");
70 if (fstat (temp_fd
, &st
) < 0 || st
.st_size
!= (offset
+ 1200))
71 FAIL_RET ("size after increase is incorrect");
73 if (truncate (temp_filename
, offset
+ 800) < 0)
74 FAIL_RET ("size reduction with truncate failed");
75 if (fstat (temp_fd
, &st
) < 0 || st
.st_size
!= (offset
+ 800))
76 FAIL_RET ("size after reduction with truncate incorrect");
78 /* The following test covers more than POSIX. POSIX does not require
79 that truncate() can increase the file size. But we are testing
81 if (truncate (temp_filename
, (offset
+ 1200)) < 0)
82 FAIL_RET ("size increase with truncate failed");
83 if (fstat (temp_fd
, &st
) < 0 || st
.st_size
!= (offset
+ 1200))
84 FAIL_RET ("size increase with truncate is incorrect");