1 /**
2 * @file
3 * Test code for mutt_file_stat_compare()
4 *
5 * @authors
6 * Copyright (C) 2019 Richard Russon <rich@flatcap.org>
7 *
8 * @copyright
9 * This program is free software: you can redistribute it and/or modify it under
10 * the terms of the GNU General Public License as published by the Free Software
11 * Foundation, either version 2 of the License, or (at your option) any later
12 * version.
13 *
14 * This program is distributed in the hope that it will be useful, but WITHOUT
15 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
16 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
17 * details.
18 *
19 * You should have received a copy of the GNU General Public License along with
20 * this program. If not, see <http://www.gnu.org/licenses/>.
21 */
22
23 #define TEST_NO_MAIN
24 #include "config.h"
25 #include "acutest.h"
26 #include <sys/stat.h>
27 #include "mutt/lib.h"
28 #include "common.h"
29 #include "test_common.h"
30
test_mutt_file_stat_compare(void)31 void test_mutt_file_stat_compare(void)
32 {
33 // int mutt_file_stat_compare(struct stat *sba, enum MuttStatType sba_type, struct stat *sbb, enum MuttStatType sbb_type);
34
35 // clang-format off
36 static struct TestValue tests[] = {
37 { "%s/file/stat/old", "%s/file/stat/same1", -1 },
38 { "%s/file/stat/same1", "%s/file/stat/same2", 0 },
39 { "%s/file/stat/same2", "%s/file/stat/same1", 0 },
40 { "%s/file/stat/new", "%s/file/stat/same2", 1 },
41 };
42 // clang-format on
43
44 {
45 struct stat stat = { 0 };
46 TEST_CHECK(mutt_file_stat_compare(NULL, 0, &stat, 0) == 0);
47 }
48
49 {
50 struct stat stat = { 0 };
51 TEST_CHECK(mutt_file_stat_compare(&stat, 0, NULL, 0) == 0);
52 }
53
54 int rc;
55 struct stat st1;
56 struct stat st2;
57 char first[256] = { 0 };
58 char second[256] = { 0 };
59 for (size_t i = 0; i < mutt_array_size(tests); i++)
60 {
61 memset(&st1, 0, sizeof(st1));
62 memset(&st2, 0, sizeof(st2));
63 test_gen_path(first, sizeof(first), tests[i].first);
64 test_gen_path(second, sizeof(second), tests[i].second);
65
66 TEST_CASE(first);
67 TEST_CHECK(stat(first, &st1) == 0);
68 TEST_CHECK(stat(second, &st2) == 0);
69
70 rc = mutt_file_stat_compare(&st1, MUTT_STAT_MTIME, &st2, MUTT_STAT_MTIME);
71 if (!TEST_CHECK(rc == tests[i].retval))
72 {
73 TEST_MSG("Expected: %d", tests[i].retval);
74 TEST_MSG("Actual: %d", rc);
75 }
76 }
77 }
78