p_fallocate: add a test for our implementation
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
diff --git a/tests/core/posix.c b/tests/core/posix.c
index 1724620..529ee37 100644
--- a/tests/core/posix.c
+++ b/tests/core/posix.c
@@ -27,6 +27,11 @@ void test_core_posix__initialize(void)
#endif
}
+void test_core_posix__cleanup(void)
+{
+ p_unlink("fallocate_test");
+}
+
static bool supports_ipv6(void)
{
#ifdef GIT_WIN32
@@ -190,3 +195,39 @@ void test_core_posix__p_regcomp_compile_userdiff_regexps(void)
cl_must_pass(error);
}
}
+
+void test_core_posix__fallocate(void)
+{
+ int fd;
+ struct stat st;
+
+ /* fallocate a new file succeeds */
+ cl_must_pass(fd = p_open("fallocate_test", O_RDWR|O_CREAT, 0666));
+ cl_must_pass(p_fallocate(fd, 0, 42));
+ cl_must_pass(p_fstat(fd, &st));
+ cl_assert_equal_i(42, st.st_size);
+ p_close(fd);
+
+ /* fallocate an existing file succeeds */
+ cl_must_pass(fd = p_open("fallocate_test", O_RDWR, 0666));
+ cl_must_pass(p_fallocate(fd, 90, 9));
+ cl_must_pass(p_fstat(fd, &st));
+ cl_assert_equal_i(99, st.st_size);
+ p_close(fd);
+
+ /* fallocate doesn't shrink */
+ cl_must_pass(fd = p_open("fallocate_test", O_RDWR, 0666));
+ cl_must_pass(p_fallocate(fd, 0, 14));
+ cl_must_pass(p_fstat(fd, &st));
+ cl_assert_equal_i(99, st.st_size);
+ p_close(fd);
+
+ /* fallocate doesn't move the cursor */
+ cl_must_pass(fd = p_open("fallocate_test", O_RDWR, 0666));
+ cl_must_pass(p_fallocate(fd, 0, 100));
+ cl_assert_equal_i(0, p_lseek(fd, 0, SEEK_CUR));
+ cl_must_pass(p_lseek(fd, 42, SEEK_SET));
+ cl_must_pass(p_fallocate(fd, 0, 200));
+ cl_assert_equal_i(42, p_lseek(fd, 0, SEEK_CUR));
+ p_close(fd);
+}