Debian Codegraph @

test/src/fsevents_cf_paths_test.cpp

raw
1 /*
2 * Copyright (c) 2014-2026 Enrico M. Crisostomo
3 *
4 * This program is free software; you can redistribute it and/or modify it under
5 * the terms of the GNU General Public License as published by the Free Software
6 * Foundation; either version 3, or (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful, but WITHOUT
9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
10 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
11 * details.
12 *
13 * You should have received a copy of the GNU General Public License along with
14 * this program. If not, see <http://www.gnu.org/licenses/>.
15 */
16 #include <libfswatch/libfswatch_config.h>
17
18 #include <CoreServices/CoreServices.h>
19 #include <cstdlib>
20 #include <iostream>
21 #include <limits.h>
22 #include <string>
23 #include <vector>
24
25 #include <libfswatch/c++/monitor.hpp>
26
27 #define private public
28 #include <libfswatch/c++/fsevents_monitor.hpp>
29 #undef private
30
31 namespace
32 {
33 bool cf_string_equals(CFStringRef value, const std::string& expected)
34 {
35 char buffer[PATH_MAX];
36 return CFStringGetCString(value, buffer, sizeof(buffer), kCFStringEncodingUTF8)
37 && expected == buffer;
38 }
39 }
40
41 int main()
42 {
43 const std::vector<std::string> expected_paths = {"/tmp", "/var"};
44 CFArrayRef paths = fsw::fsevents_monitor::copy_cf_paths(expected_paths);
45
46 if (!paths)
47 {
48 std::cerr << "Expected path array to be created." << std::endl;
49 return EXIT_FAILURE;
50 }
51
52 if (CFArrayGetCount(paths) != static_cast<CFIndex>(expected_paths.size()))
53 {
54 std::cerr << "Unexpected path count." << std::endl;
55 CFRelease(paths);
56 return EXIT_FAILURE;
57 }
58
59 for (CFIndex i = 0; i < CFArrayGetCount(paths); ++i)
60 {
61 auto path = static_cast<CFStringRef>(CFArrayGetValueAtIndex(paths, i));
62 if (!cf_string_equals(path, expected_paths[i]))
63 {
64 std::cerr << "Unexpected path at index " << i << "." << std::endl;
65 CFRelease(paths);
66 return EXIT_FAILURE;
67 }
68 }
69
70 CFRelease(paths);
71
72 if (fsw::fsevents_monitor::copy_cf_paths({}) != nullptr)
73 {
74 std::cerr << "Empty path input must not create an array." << std::endl;
75 return EXIT_FAILURE;
76 }
77
78 return EXIT_SUCCESS;
79 }