1
0
mirror of https://github.com/weechat/weechat.git synced 2026-06-27 21:36:37 +02:00

api: add function file_compare (issue #2250)

This commit is contained in:
Sébastien Helleu
2025-05-07 20:42:46 +02:00
parent 9a661aecd0
commit 51d24fd2da
11 changed files with 391 additions and 7 deletions
+72 -6
View File
@@ -227,16 +227,15 @@ TEST(CoreDir, FileGetContentCopy)
LONGS_EQUAL(0, dir_file_copy ("", ""));
LONGS_EQUAL(0, dir_file_copy ("/tmp/does/not/exist.xyz", "/tmp/test.txt"));
path1 = string_eval_path_home ("${weechat_data_dir}/test_file.txt",
path1 = string_eval_path_home ("${weechat_data_dir}/test_content_file1.txt",
NULL, NULL, NULL);
path2 = string_eval_path_home ("${weechat_data_dir}/test_file2.txt",
path2 = string_eval_path_home ("${weechat_data_dir}/test_content_file2.txt",
NULL, NULL, NULL);
/* small file */
length = strlen (content_small);
f = fopen (path1, "wb");
CHECK(f);
LONGS_EQUAL(length, fwrite (content_small, 1, length, f));
fwrite (content_small, 1, length, f);
fclose (f);
LONGS_EQUAL(1, dir_file_copy (path1, path2));
content_read1 = dir_file_get_content (path1);
@@ -259,8 +258,7 @@ TEST(CoreDir, FileGetContentCopy)
}
content[26 * 5001] = '\0';
f = fopen (path1, "wb");
CHECK(f);
LONGS_EQUAL(length, fwrite (content, 1, length, f));
fwrite (content, 1, length, f);
fclose (f);
LONGS_EQUAL(1, dir_file_copy (path1, path2));
content_read1 = dir_file_get_content (path1);
@@ -303,3 +301,71 @@ TEST(CoreDir, FileCompress)
{
/* TODO: write tests */
}
/*
* Tests functions:
* dir_file_compare
*/
TEST(CoreDir, FileCompare)
{
char *path1, *path2, *content;
const char *content_small = "line 1\nline 2\nend";
int length, i;
FILE *f;
/* file not found */
LONGS_EQUAL(2, dir_file_compare (NULL, NULL));
LONGS_EQUAL(2, dir_file_compare ("", ""));
LONGS_EQUAL(2, dir_file_compare ("/tmp/does/not/exist1.xyz", "/tmp/does/not/exist2.xyz"));
path1 = string_eval_path_home ("${weechat_data_dir}/test_compare_file1.txt",
NULL, NULL, NULL);
path2 = string_eval_path_home ("${weechat_data_dir}/test_compare_file2.txt",
NULL, NULL, NULL);
/* small file */
length = strlen (content_small);
f = fopen (path1, "wb");
fwrite (content_small, 1, length, f);
fclose (f);
f = fopen (path2, "wb");
fwrite (content_small, 1, length, f);
fclose (f);
LONGS_EQUAL(0, dir_file_compare (path1, path2));
f = fopen (path1, "ab");
fwrite ("A", 1, 1, f);
fclose (f);
f = fopen (path2, "ab");
fwrite ("B", 1, 1, f);
fclose (f);
LONGS_EQUAL(1, dir_file_compare (path1, path2));
/* bigger file: 26 lines of 5000 bytes + 1 byte */
length = 26 * 5001;
content = (char *)malloc (length + 1);
CHECK(content);
for (i = 0; i < 26; i++)
{
memset (content + (i * 5001), 'a' + i, 5000);
content[(i * 5001) + 5000] = '\n';
}
content[26 * 5001] = '\0';
f = fopen (path1, "wb");
fwrite (content, 1, length, f);
fclose (f);
f = fopen (path2, "wb");
fwrite (content, 1, length, f);
fclose (f);
LONGS_EQUAL(0, dir_file_compare (path1, path2));
f = fopen (path1, "ab");
fwrite ("A", 1, 1, f);
fclose (f);
f = fopen (path2, "ab");
fwrite ("B", 1, 1, f);
fclose (f);
LONGS_EQUAL(1, dir_file_compare (path1, path2));
unlink (path1);
unlink (path2);
}