tests: create sparse file for g_file_get_size() tests

This commit is contained in:
Koichiro IWAO 2021-09-06 14:32:48 +09:00
parent 8e4ea49532
commit c24b6d95dd

View File

@ -25,17 +25,63 @@ START_TEST(test_g_file_get_size__returns_file_size)
}
END_TEST
START_TEST(test_g_file_get_size__more_than_4GiB)
START_TEST(test_g_file_get_size__1GiB)
{
unsigned long long size;
const char *file = "./file_1GiB.dat";
unsigned int block_size = 4096;
unsigned int block_count = 262144;
/* C90 5.2.4.2.1 guarantees long long is at least 64 bits */
const long long expected_size =
(long long)block_size * block_count;
long long size;
int system_rv;
system("dd if=/dev/zero of=./file_5GiB.dat bs=4k count=1310720");
char cmd[256];
size = g_file_get_size("./file_5GiB.dat");
ck_assert_int_eq(size, 5368709120);
/* Create a sparse file of the expected size with one block of data
* at the end */
g_snprintf(cmd, sizeof(cmd),
"dd if=/dev/zero of=%s bs=%u seek=%u count=1",
file,
block_size,
block_count -1);
system_rv = system(cmd);
size = g_file_get_size(file);
g_file_delete(file);
ck_assert_int_eq(system_rv, 0);
ck_assert_int_eq(size, expected_size);
}
END_TEST
START_TEST(test_g_file_get_size__just_less_than_2GiB)
{
const char *file = "./file_2GiB.dat";
unsigned int block_size = 4096;
unsigned int block_count = 524287; /* 4096 * 52428__8__ = 2GiB */
/* C90 5.2.4.2.1 guarantees long long is at least 64 bits */
const long long expected_size =
(long long)block_size * block_count;
long long size;
int system_rv;
char cmd[256];
/* Create a sparse file of the expected size with one block of data
* at the end */
g_snprintf(cmd, sizeof(cmd),
"dd if=/dev/zero of=%s bs=%u seek=%u count=1",
file,
block_size,
block_count -1);
system_rv = system(cmd);
size = g_file_get_size(file);
g_file_delete(file);
ck_assert_int_eq(system_rv, 0);
ck_assert_int_eq(size, expected_size);
}
END_TEST
/******************************************************************************/
Suite *
@ -49,7 +95,8 @@ make_suite_test_os_calls(void)
tc_os_calls = tcase_create("oscalls-file");
suite_add_tcase(s, tc_os_calls);
tcase_add_test(tc_os_calls, test_g_file_get_size__returns_file_size);
tcase_add_test(tc_os_calls, test_g_file_get_size__more_than_4GiB);
tcase_add_test(tc_os_calls, test_g_file_get_size__1GiB);
tcase_add_test(tc_os_calls, test_g_file_get_size__just_less_than_2GiB);
return s;
}