Skip to content

Commit 0345a38

Browse files
committed
p_fallocate: add a test for our implementation
1 parent 7ab7bf4 commit 0345a38

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed

tests/core/posix.c

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,11 @@ void test_core_posix__initialize(void)
2727
#endif
2828
}
2929

30+
void test_core_posix__cleanup(void)
31+
{
32+
p_unlink("fallocate_test");
33+
}
34+
3035
static bool supports_ipv6(void)
3136
{
3237
#ifdef GIT_WIN32
@@ -190,3 +195,39 @@ void test_core_posix__p_regcomp_compile_userdiff_regexps(void)
190195
cl_must_pass(error);
191196
}
192197
}
198+
199+
void test_core_posix__fallocate(void)
200+
{
201+
int fd;
202+
struct stat st;
203+
204+
/* fallocate a new file succeeds */
205+
cl_must_pass(fd = p_open("fallocate_test", O_RDWR|O_CREAT, 0666));
206+
cl_must_pass(p_fallocate(fd, 0, 42));
207+
cl_must_pass(p_fstat(fd, &st));
208+
cl_assert_equal_i(42, st.st_size);
209+
p_close(fd);
210+
211+
/* fallocate an existing file succeeds */
212+
cl_must_pass(fd = p_open("fallocate_test", O_RDWR, 0666));
213+
cl_must_pass(p_fallocate(fd, 90, 9));
214+
cl_must_pass(p_fstat(fd, &st));
215+
cl_assert_equal_i(99, st.st_size);
216+
p_close(fd);
217+
218+
/* fallocate doesn't shrink */
219+
cl_must_pass(fd = p_open("fallocate_test", O_RDWR, 0666));
220+
cl_must_pass(p_fallocate(fd, 0, 14));
221+
cl_must_pass(p_fstat(fd, &st));
222+
cl_assert_equal_i(99, st.st_size);
223+
p_close(fd);
224+
225+
/* fallocate doesn't move the cursor */
226+
cl_must_pass(fd = p_open("fallocate_test", O_RDWR, 0666));
227+
cl_must_pass(p_fallocate(fd, 0, 100));
228+
cl_assert_equal_i(0, p_lseek(fd, 0, SEEK_CUR));
229+
cl_must_pass(p_lseek(fd, 42, SEEK_SET));
230+
cl_must_pass(p_fallocate(fd, 0, 200));
231+
cl_assert_equal_i(42, p_lseek(fd, 0, SEEK_CUR));
232+
p_close(fd);
233+
}

0 commit comments

Comments
 (0)