basename_test.c (5528B)
1 #include <cwalk.h> 2 #include <memory.h> 3 #include <stdio.h> 4 #include <stdlib.h> 5 #include <string.h> 6 7 int basename_change_trim_only_root(void) 8 { 9 size_t n; 10 char buffer[FILENAME_MAX]; 11 12 cwk_path_set_style(CWK_STYLE_UNIX); 13 14 n = cwk_path_change_basename("/", "///another.txt///", buffer, 15 sizeof(buffer)); 16 if (n != 12) { 17 return EXIT_FAILURE; 18 } 19 20 if (strcmp(buffer, "/another.txt") != 0) { 21 return EXIT_FAILURE; 22 } 23 24 return EXIT_SUCCESS; 25 } 26 27 int basename_change_trim(void) 28 { 29 size_t n; 30 char buffer[FILENAME_MAX]; 31 32 cwk_path_set_style(CWK_STYLE_UNIX); 33 34 n = cwk_path_change_basename("/test.txt", "///another.txt///", buffer, 35 sizeof(buffer)); 36 if (n != 12) { 37 return EXIT_FAILURE; 38 } 39 40 if (strcmp(buffer, "/another.txt") != 0) { 41 return EXIT_FAILURE; 42 } 43 44 return EXIT_SUCCESS; 45 } 46 47 int basename_change_relative(void) 48 { 49 size_t n; 50 char buffer[FILENAME_MAX]; 51 52 cwk_path_set_style(CWK_STYLE_WINDOWS); 53 54 n = cwk_path_change_basename("../test.txt", "another.txt", buffer, 55 sizeof(buffer)); 56 if (n != 14) { 57 return EXIT_FAILURE; 58 } 59 60 if (strcmp(buffer, "../another.txt") != 0) { 61 return EXIT_FAILURE; 62 } 63 64 return EXIT_SUCCESS; 65 } 66 67 int basename_change_empty_basename(void) 68 { 69 size_t n; 70 char buffer[FILENAME_MAX]; 71 72 cwk_path_set_style(CWK_STYLE_WINDOWS); 73 74 n = cwk_path_change_basename("C:\\test.txt", "", buffer, sizeof(buffer)); 75 if (n != 3) { 76 return EXIT_FAILURE; 77 } 78 79 if (strcmp(buffer, "C:\\") != 0) { 80 return EXIT_FAILURE; 81 } 82 83 return EXIT_SUCCESS; 84 } 85 86 int basename_change_only_root(void) 87 { 88 size_t n; 89 char buffer[FILENAME_MAX]; 90 91 cwk_path_set_style(CWK_STYLE_WINDOWS); 92 93 n = cwk_path_change_basename("C:\\", "another.txt", buffer, sizeof(buffer)); 94 if (n != 14) { 95 return EXIT_FAILURE; 96 } 97 98 if (strcmp(buffer, "C:\\another.txt") != 0) { 99 return EXIT_FAILURE; 100 } 101 102 return EXIT_SUCCESS; 103 } 104 105 int basename_change_empty_path(void) 106 { 107 size_t n; 108 char buffer[FILENAME_MAX]; 109 110 cwk_path_set_style(CWK_STYLE_WINDOWS); 111 112 n = cwk_path_change_basename("", "another.txt", buffer, sizeof(buffer)); 113 if (n != 11) { 114 return EXIT_FAILURE; 115 } 116 117 if (strcmp(buffer, "another.txt") != 0) { 118 return EXIT_FAILURE; 119 } 120 121 return EXIT_SUCCESS; 122 } 123 124 int basename_change_simple(void) 125 { 126 size_t n; 127 char buffer[FILENAME_MAX]; 128 129 cwk_path_set_style(CWK_STYLE_WINDOWS); 130 131 n = cwk_path_change_basename("C:\\test.txt", "another.txt", buffer, 132 sizeof(buffer)); 133 if (n != 14) { 134 return EXIT_FAILURE; 135 } 136 137 if (strcmp(buffer, "C:\\another.txt") != 0) { 138 return EXIT_FAILURE; 139 } 140 141 return EXIT_SUCCESS; 142 } 143 144 int basename_windows(void) 145 { 146 const char *path, *basename; 147 size_t length; 148 149 cwk_path_set_style(CWK_STYLE_WINDOWS); 150 path = "C:\\path\\test.txt"; 151 cwk_path_get_basename(path, &basename, &length); 152 153 if (length != 8) { 154 return EXIT_FAILURE; 155 } 156 157 if (strcmp(basename, "test.txt") != 0) { 158 return EXIT_FAILURE; 159 } 160 161 return EXIT_SUCCESS; 162 } 163 164 int basename_root(void) 165 { 166 const char *path, *basename; 167 size_t length; 168 169 cwk_path_set_style(CWK_STYLE_UNIX); 170 path = "/"; 171 cwk_path_get_basename(path, &basename, &length); 172 173 if (length != 0) { 174 return EXIT_FAILURE; 175 } 176 177 if (basename != NULL) { 178 return EXIT_FAILURE; 179 } 180 181 return EXIT_SUCCESS; 182 } 183 184 int basename_special_directories(void) 185 { 186 const char *path, *basename; 187 size_t length; 188 189 cwk_path_set_style(CWK_STYLE_UNIX); 190 path = ".."; 191 cwk_path_get_basename(path, &basename, &length); 192 193 if (length != 2) { 194 return EXIT_FAILURE; 195 } 196 197 if (strncmp(basename, "..", length) != 0) { 198 return EXIT_FAILURE; 199 } 200 201 path = "."; 202 cwk_path_get_basename(path, &basename, &length); 203 204 if (length != 1) { 205 return EXIT_FAILURE; 206 } 207 208 if (strncmp(basename, ".", length) != 0) { 209 return EXIT_FAILURE; 210 } 211 212 return EXIT_SUCCESS; 213 } 214 215 int basename_no_separators(void) 216 { 217 const char *path, *basename; 218 size_t length; 219 220 cwk_path_set_style(CWK_STYLE_UNIX); 221 path = "file_name"; 222 cwk_path_get_basename(path, &basename, &length); 223 224 if (length != 9) { 225 return EXIT_FAILURE; 226 } 227 228 if (strncmp(basename, "file_name", length) != 0) { 229 return EXIT_FAILURE; 230 } 231 232 return EXIT_SUCCESS; 233 } 234 235 int basename_trailing_separators(void) 236 { 237 const char *path, *basename; 238 size_t length; 239 240 cwk_path_set_style(CWK_STYLE_UNIX); 241 path = "/my/path.txt////"; 242 cwk_path_get_basename(path, &basename, &length); 243 244 if (length != 8) { 245 return EXIT_FAILURE; 246 } 247 248 if (strncmp(basename, "path.txt", length) != 0) { 249 return EXIT_FAILURE; 250 } 251 252 return EXIT_SUCCESS; 253 } 254 255 int basename_trailing_separator(void) 256 { 257 const char *path, *basename; 258 size_t length; 259 260 cwk_path_set_style(CWK_STYLE_UNIX); 261 path = "/my/path.txt/"; 262 cwk_path_get_basename(path, &basename, &length); 263 264 if (length != 8) { 265 return EXIT_FAILURE; 266 } 267 268 if (strncmp(basename, "path.txt", length) != 0) { 269 return EXIT_FAILURE; 270 } 271 272 return EXIT_SUCCESS; 273 } 274 275 int basename_empty(void) 276 { 277 const char *path, *basename; 278 size_t length; 279 280 cwk_path_set_style(CWK_STYLE_UNIX); 281 path = ""; 282 cwk_path_get_basename(path, &basename, &length); 283 284 if (length != 0) { 285 return EXIT_FAILURE; 286 } 287 288 if (basename != NULL) { 289 return EXIT_FAILURE; 290 } 291 292 return EXIT_SUCCESS; 293 } 294 295 int basename_simple(void) 296 { 297 const char *path, *basename; 298 size_t length; 299 300 cwk_path_set_style(CWK_STYLE_UNIX); 301 path = "/my/path.txt"; 302 cwk_path_get_basename(path, &basename, &length); 303 304 if (length != 8) { 305 return EXIT_FAILURE; 306 } 307 308 if (strncmp(basename, "path.txt", length) != 0) { 309 return EXIT_FAILURE; 310 } 311 312 return EXIT_SUCCESS; 313 }