Branch data Line data Source code
1 : : #include "sute.h"
2 : :
3 : : /**
4 : : * @brief Helper function to test remove_trailing_slash and compare with expected output.
5 : : *
6 : : * This function copies the input string, applies remove_trailing_slash(), and
7 : : * compares the result with the expected output.
8 : : *
9 : : * @param input The original path to be processed.
10 : : * @param expected The expected output after removing trailing slashes.
11 : : */
12 : 17 : static Return test_remove_trailing_slash(
13 : : const char *input,
14 : : const char *expected)
15 : : {
16 : : /* The status that will be returned before exiting */
17 : : /* By default, assumes the function ran without errors */
18 : 17 : Return status = SUCCESS;
19 : :
20 : 17 : create(char,temp_buffer);
21 : 17 : size_t len = strlen(input) + 1; // +1 for null terminator
22 : :
23 [ + - - + ]: 17 : run(resize(temp_buffer,len));
24 : :
25 [ + - ]: 17 : if(SUCCESS & status)
26 : : {
27 : 17 : char *buffer_data = data(char,temp_buffer);
28 : :
29 [ + - - + ]: 17 : if((SUCCESS & status) && buffer_data == NULL)
30 : : {
31 : 0 : status = FAILURE;
32 : : }
33 : :
34 [ + - ]: 17 : if(SUCCESS & status)
35 : : {
36 : 17 : memcpy(buffer_data,input,len);
37 : :
38 : 17 : remove_trailing_slash(buffer_data);
39 : :
40 : 17 : const char *buffer_view = cdata(char,temp_buffer);
41 : :
42 [ + - - + ]: 17 : if((SUCCESS & status) && buffer_view == NULL)
43 : : {
44 : 0 : status = FAILURE;
45 : : }
46 : :
47 [ + - - + ]: 17 : if((SUCCESS & status) && strcmp(buffer_view,expected) != 0)
48 : : {
49 : 0 : status = FAILURE;
50 : : }
51 : : }
52 : : }
53 : :
54 : 17 : del(temp_buffer);
55 : :
56 [ - + - - : 17 : deliver(status);
- + + - ]
57 : : }
58 : :
59 : : /**
60 : : * @brief Runs a series of test cases for remove_trailing_slash().
61 : : *
62 : : * Unit Testing of precizer. This function tests various scenarios including:
63 : : * - Removing trailing slashes from paths
64 : : * - Ensuring single '/' paths remain unchanged
65 : : * - Handling edge cases with multiple slashes
66 : : */
67 : 1 : Return test0022(void)
68 : : {
69 : 1 : INITTEST;
70 : :
71 : : static const struct {
72 : : const char *input;
73 : : const char *expected;
74 : : } test_cases[] = {
75 : : // Basic cases
76 : : {"path/","path"},
77 : : {"folder////","folder"},
78 : : {"/home/user////","/home/user"},
79 : : {"/var/log//","/var/log"},
80 : : {"/usr/local/bin/","/usr/local/bin"},
81 : : {"///home///","///home"},
82 : :
83 : : // Root path cases
84 : : {"/","/"},
85 : : {"////","/"},
86 : :
87 : : // No trailing slashes
88 : : {"path","path"},
89 : : {"/usr/local/bin","/usr/local/bin"},
90 : : {"no/slash/here","no/slash/here"},
91 : :
92 : : // Empty string
93 : : {"",""},
94 : :
95 : : // Edge cases
96 : : {"//double//slash//","//double//slash"},
97 : : {"////multiple///leading/slashes////","////multiple///leading/slashes"},
98 : :
99 : : // Single character paths
100 : : {"a/","a"},
101 : : {"b////","b"},
102 : : {"c","c"},
103 : : {NULL,NULL} // Sentinel value
104 : : };
105 : :
106 [ + + ]: 18 : for(size_t i = 0; test_cases[i].input != NULL; i++)
107 : : {
108 : 17 : ASSERT(SUCCESS & test_remove_trailing_slash(test_cases[i].input,test_cases[i].expected));
109 : : }
110 : :
111 : 1 : RETURN_STATUS;
112 : : }
|