Branch data Line data Source code
1 : : #include "sute.h"
2 : :
3 : : /**
4 : : * @brief Tests the extract_relative_path function with various path and prefix cases.
5 : : *
6 : : * This function initializes a set of test cases containing different combinations
7 : : * of file paths and prefixes, both absolute and relative. It then calls the
8 : : * extract_relative_path function for each case and compares the result with the
9 : : * expected output. The function asserts correctness and returns a test status.
10 : : *
11 : : * The test cases include:
12 : : * - Absolute paths with different prefixes
13 : : * - Relative paths with different prefixes
14 : : * - Paths in the root directory
15 : : *
16 : : * @return Test status indicating success or failure.
17 : : */
18 : 1 : Return test0023(void)
19 : : {
20 : 1 : INITTEST;
21 : :
22 : : struct {
23 : : const char *path;
24 : : const char *prefix;
25 : : const char *expected_result;
26 : 1 : } test_cases[] = {
27 : : // Absolute paths with different prefixes
28 : : {"/home/user/project/file.txt","/home/user","project/file.txt"},
29 : : {"/home/user/project/file.txt","/","home/user/project/file.txt"},
30 : : {"/home/user/project/file.txt","/home/user/project","file.txt"},
31 : : {"/home/user/project/file.txt","/home/user/project/file.txt","."},
32 : : {"/home/user/project/file.txt","/invalid/prefix","/home/user/project/file.txt"},
33 : : {"/home/user/project","/home/user/project","."},
34 : : {"/home/user/project/","/home/user","project/"},
35 : : {"/home/user/project/","/home/user/project","."},
36 : : {"/home/user/project/","/","home/user/project/"},
37 : :
38 : : // Prefix as a relative path
39 : : {"home/user/project/file.txt","home/user","project/file.txt"},
40 : : {"home/user/project/file.txt","home","user/project/file.txt"},
41 : : {"home/user/project/file.txt","home/user/project","file.txt"},
42 : : {"home/user/project/file.txt","home/user/project/file.txt","."},
43 : : {"home/user/project/file.txt","invalid/prefix","home/user/project/file.txt"},
44 : : {"home/user/project","home/user/project","."},
45 : : {"home/user/project/","home/user","project/"},
46 : : {"home/user/project/","home/user/project","."},
47 : : {"home/user/project/","home","user/project/"},
48 : :
49 : : // Absolute path as a relative path
50 : : {"./file.txt","./","file.txt"},
51 : : {"./project/file.txt","./project","file.txt"},
52 : : {"./project/file.txt",".","project/file.txt"},
53 : : {"../project/file.txt","../project","file.txt"},
54 : : {"../../file.txt","../..","file.txt"},
55 : :
56 : : // Relative paths without "./"
57 : : {"file.txt","file.txt","."},
58 : : {"project/file.txt","project","file.txt"},
59 : : {"project/subdir/file.txt","project/subdir","file.txt"},
60 : : {"../file.txt","..","file.txt"},
61 : : {"../project/file.txt","../project","file.txt"},
62 : : {"../../file.txt","../..","file.txt"},
63 : :
64 : : // Paths in the root directory with prefix "/"
65 : : {"/.package-cache-mutate","/",".package-cache-mutate"},
66 : : {"/..package-cache-mutate","/","..package-cache-mutate"},
67 : : {"/config/settings.json","/","config/settings.json"},
68 : : {"/var/log/system.log","/var","log/system.log"},
69 : : {"/var/log/system.log","/","var/log/system.log"},
70 : : {"/tmp/file.txt","/tmp","file.txt"},
71 : : {"/file.txt","/","file.txt"},
72 : : {NULL,NULL,NULL} // Sentinel value
73 : : };
74 : :
75 : : // Run tests and check results
76 [ + + ]: 37 : for(size_t i = 0; test_cases[i].path != NULL; i++)
77 : : {
78 : 36 : const char *expected = test_cases[i].expected_result;
79 : 36 : const char *result = extract_relative_path(test_cases[i].path,test_cases[i].prefix);
80 : :
81 : : // Compare result with expected value
82 [ + - + - ]: 36 : ASSERT(COMPLETED == strcmp(result,expected));
83 : : }
84 : :
85 [ + - - + : 1 : RETURN_STATUS;
- - - + +
- ]
86 : : }
|