join_test.c (3394B)
1 #include <cwalk.h> 2 #include <memory.h> 3 #include <stdio.h> 4 #include <stdlib.h> 5 #include <string.h> 6 7 int join_multiple(void) 8 { 9 char buffer[FILENAME_MAX]; 10 const char *paths[3]; 11 size_t length; 12 13 cwk_path_set_style(CWK_STYLE_UNIX); 14 15 paths[0] = "hello/there"; 16 paths[1] = "../world"; 17 paths[2] = NULL; 18 19 length = cwk_path_join_multiple(paths, buffer, sizeof(buffer)); 20 21 if (length != 11) { 22 return EXIT_FAILURE; 23 } 24 25 if (strcmp(buffer, "hello/world") != 0) { 26 return EXIT_FAILURE; 27 } 28 29 return EXIT_SUCCESS; 30 } 31 32 int join_relative_back_after_root(void) 33 { 34 char buffer[FILENAME_MAX]; 35 size_t length; 36 37 cwk_path_set_style(CWK_STYLE_WINDOWS); 38 length = cwk_path_join("this\\", "C:\\..\\..\\is\\a\\test\\", buffer, 39 sizeof(buffer)); 40 41 if (length != 9) { 42 return EXIT_FAILURE; 43 } 44 45 if (strcmp(buffer, "is\\a\\test") != 0) { 46 return EXIT_FAILURE; 47 } 48 49 return EXIT_SUCCESS; 50 } 51 52 int join_back_after_root(void) 53 { 54 char buffer[FILENAME_MAX]; 55 size_t length; 56 57 cwk_path_set_style(CWK_STYLE_WINDOWS); 58 length = cwk_path_join("C:\\this\\path", "C:\\..\\is\\a\\test\\", buffer, 59 sizeof(buffer)); 60 61 if (length != 22) { 62 return EXIT_FAILURE; 63 } 64 65 if (strcmp(buffer, "C:\\this\\path\\is\\a\\test") != 0) { 66 return EXIT_FAILURE; 67 } 68 69 return EXIT_SUCCESS; 70 } 71 72 int join_with_two_roots(void) 73 { 74 char buffer[FILENAME_MAX]; 75 size_t length; 76 77 cwk_path_set_style(CWK_STYLE_WINDOWS); 78 length = cwk_path_join("C:\\this\\path", "C:\\is\\a\\test\\", buffer, 79 sizeof(buffer)); 80 81 if (length != 25) { 82 return EXIT_FAILURE; 83 } 84 85 if (strcmp(buffer, "C:\\this\\path\\C:\\is\\a\\test") != 0) { 86 return EXIT_FAILURE; 87 } 88 89 return EXIT_SUCCESS; 90 } 91 92 int join_two_unc(void) 93 { 94 char buffer[FILENAME_MAX]; 95 size_t length; 96 97 cwk_path_set_style(CWK_STYLE_WINDOWS); 98 length = cwk_path_join("\\\\server\\unc\\path", "\\\\server2\\unc\\path", 99 buffer, sizeof(buffer)); 100 101 if (length != 34) { 102 return EXIT_FAILURE; 103 } 104 105 if (strcmp(buffer, "\\\\server\\unc\\path\\server2\\unc\\path") != 0) { 106 return EXIT_FAILURE; 107 } 108 109 return EXIT_SUCCESS; 110 } 111 112 int join_two_absolute(void) 113 { 114 char buffer[FILENAME_MAX]; 115 size_t length; 116 117 cwk_path_set_style(CWK_STYLE_UNIX); 118 length = cwk_path_join("/first", "/second", buffer, sizeof(buffer)); 119 120 if (length != 13) { 121 return EXIT_FAILURE; 122 } 123 124 if (strcmp(buffer, "/first/second") != 0) { 125 return EXIT_FAILURE; 126 } 127 128 return EXIT_SUCCESS; 129 } 130 131 int join_empty(void) 132 { 133 char buffer[FILENAME_MAX]; 134 size_t length; 135 136 cwk_path_set_style(CWK_STYLE_UNIX); 137 length = cwk_path_join("hello", "..", buffer, sizeof(buffer)); 138 139 if (length != 1) { 140 return EXIT_FAILURE; 141 } 142 143 if (strcmp(buffer, ".") != 0) { 144 return EXIT_FAILURE; 145 } 146 147 return EXIT_SUCCESS; 148 } 149 150 int join_navigate_back(void) 151 { 152 char buffer[FILENAME_MAX]; 153 size_t length; 154 155 cwk_path_set_style(CWK_STYLE_UNIX); 156 length = cwk_path_join("hello/there", "..", buffer, sizeof(buffer)); 157 158 if (length != 5) { 159 return EXIT_FAILURE; 160 } 161 162 if (strcmp(buffer, "hello") != 0) { 163 return EXIT_FAILURE; 164 } 165 166 return EXIT_SUCCESS; 167 } 168 169 int join_simple(void) 170 { 171 char buffer[FILENAME_MAX]; 172 size_t length; 173 174 cwk_path_set_style(CWK_STYLE_UNIX); 175 length = cwk_path_join("hello", "there", buffer, sizeof(buffer)); 176 177 if (length != 11) { 178 return EXIT_FAILURE; 179 } 180 181 if (strcmp(buffer, "hello/there") != 0) { 182 return EXIT_FAILURE; 183 } 184 185 return EXIT_SUCCESS; 186 }