relative_test.c (6612B)
1 #include <cwalk.h> 2 #include <memory.h> 3 #include <stdio.h> 4 #include <stdlib.h> 5 #include <string.h> 6 7 #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0])) 8 9 int relative_root_forward_slashes(void) 10 { 11 char result[FILENAME_MAX]; 12 size_t length; 13 14 cwk_path_set_style(CWK_STYLE_WINDOWS); 15 16 length = cwk_path_get_relative("C:\\foo\\bar\\baz\\", "C:/foo/bar/file.txt", 17 result, sizeof(result)); 18 if (length != 11) { 19 return EXIT_FAILURE; 20 } 21 22 if (strcmp(result, "..\\file.txt") != 0) { 23 return EXIT_FAILURE; 24 } 25 26 return EXIT_SUCCESS; 27 } 28 29 int relative_root_path_windows(void) 30 { 31 char result[FILENAME_MAX]; 32 size_t length; 33 34 cwk_path_set_style(CWK_STYLE_WINDOWS); 35 36 length = cwk_path_get_relative("C:\\this\\is\\path_one", "C:\\", result, 37 sizeof(result)); 38 if (length != 8) { 39 return EXIT_FAILURE; 40 } 41 42 if (strcmp(result, "..\\..\\..") != 0) { 43 return EXIT_FAILURE; 44 } 45 46 return EXIT_SUCCESS; 47 } 48 49 int relative_root_path_unix(void) 50 { 51 char result[FILENAME_MAX]; 52 size_t length; 53 54 cwk_path_set_style(CWK_STYLE_UNIX); 55 56 length = cwk_path_get_relative("/this/is/path_one", "/", result, 57 sizeof(result)); 58 if (length != 8) { 59 return EXIT_FAILURE; 60 } 61 62 if (strcmp(result, "../../..") != 0) { 63 return EXIT_FAILURE; 64 } 65 66 return EXIT_SUCCESS; 67 } 68 69 int relative_check(void) 70 { 71 const char *relative_paths[] = {"..", "test", "test/test", "../another_test", 72 "./simple", ".././simple"}; 73 const char *absolute_paths[] = {"/", "/test", "/../test/", "/../another_test", 74 "/./simple", "/.././simple"}; 75 size_t i; 76 77 cwk_path_set_style(CWK_STYLE_UNIX); 78 79 for (i = 0; i < ARRAY_SIZE(relative_paths); ++i) { 80 if (!cwk_path_is_relative(relative_paths[i])) { 81 return EXIT_FAILURE; 82 } 83 } 84 85 for (i = 0; i < ARRAY_SIZE(absolute_paths); ++i) { 86 if (cwk_path_is_relative(absolute_paths[i])) { 87 return EXIT_FAILURE; 88 } 89 } 90 91 return EXIT_SUCCESS; 92 } 93 94 int relative_relative_and_absolute(void) 95 { 96 char result[FILENAME_MAX]; 97 size_t length; 98 99 cwk_path_set_style(CWK_STYLE_UNIX); 100 101 *result = 1; 102 103 length = cwk_path_get_relative("./foo", "/bar", result, sizeof(result)); 104 105 if (length != 0) { 106 return EXIT_FAILURE; 107 } 108 109 if (*result != '\0') { 110 return EXIT_FAILURE; 111 } 112 113 return EXIT_SUCCESS; 114 } 115 116 int relative_different_roots(void) 117 { 118 char result[FILENAME_MAX]; 119 size_t length; 120 121 cwk_path_set_style(CWK_STYLE_WINDOWS); 122 123 *result = 1; 124 125 length = cwk_path_get_relative("C:/path/same", "D:/path/same", result, 126 sizeof(result)); 127 128 if (length != 0) { 129 return EXIT_FAILURE; 130 } 131 132 if (*result != '\0') { 133 return EXIT_FAILURE; 134 } 135 136 return EXIT_SUCCESS; 137 } 138 139 int relative_skip_all(void) 140 { 141 char result[FILENAME_MAX]; 142 size_t length; 143 144 cwk_path_set_style(CWK_STYLE_UNIX); 145 146 length = cwk_path_get_relative("/../../", "/../../", result, sizeof(result)); 147 if (length != 1) { 148 return EXIT_FAILURE; 149 } 150 151 if (strcmp(result, ".") != 0) { 152 return EXIT_FAILURE; 153 } 154 155 return EXIT_SUCCESS; 156 } 157 158 int relative_target_div_skipped_end(void) 159 { 160 char result[FILENAME_MAX]; 161 size_t length; 162 163 cwk_path_set_style(CWK_STYLE_UNIX); 164 165 length = cwk_path_get_relative("/path/same", "/path/not_same/ho/..", result, 166 sizeof(result)); 167 if (length != 11) { 168 return EXIT_FAILURE; 169 } 170 171 if (strcmp(result, "../not_same") != 0) { 172 return EXIT_FAILURE; 173 } 174 175 return EXIT_SUCCESS; 176 } 177 178 int relative_base_div_skipped_end(void) 179 { 180 char result[FILENAME_MAX]; 181 size_t length; 182 183 cwk_path_set_style(CWK_STYLE_UNIX); 184 185 length = cwk_path_get_relative("/path/not_same/ho/..", "/path/same", result, 186 sizeof(result)); 187 if (length != 7) { 188 return EXIT_FAILURE; 189 } 190 191 if (strcmp(result, "../same") != 0) { 192 return EXIT_FAILURE; 193 } 194 195 return EXIT_SUCCESS; 196 } 197 198 int relative_target_skipped_end(void) 199 { 200 char result[FILENAME_MAX]; 201 size_t length; 202 203 cwk_path_set_style(CWK_STYLE_UNIX); 204 205 length = cwk_path_get_relative("/path/same", "/path/same/ho/..", result, 206 sizeof(result)); 207 if (length != 1) { 208 return EXIT_FAILURE; 209 } 210 211 if (strcmp(result, ".") != 0) { 212 return EXIT_FAILURE; 213 } 214 215 return EXIT_SUCCESS; 216 } 217 218 int relative_base_skipped_end(void) 219 { 220 char result[FILENAME_MAX]; 221 size_t length; 222 223 cwk_path_set_style(CWK_STYLE_UNIX); 224 225 length = cwk_path_get_relative("/path/same/ho/..", "/path/same", result, 226 sizeof(result)); 227 if (length != 1) { 228 return EXIT_FAILURE; 229 } 230 231 if (strcmp(result, ".") != 0) { 232 return EXIT_FAILURE; 233 } 234 235 return EXIT_SUCCESS; 236 } 237 238 int relative_equal(void) 239 { 240 char result[FILENAME_MAX]; 241 size_t length; 242 243 cwk_path_set_style(CWK_STYLE_UNIX); 244 245 length = cwk_path_get_relative("/path/same", "/path/same", result, 246 sizeof(result)); 247 if (length != 1) { 248 return EXIT_FAILURE; 249 } 250 251 if (strcmp(result, ".") != 0) { 252 return EXIT_FAILURE; 253 } 254 255 return EXIT_SUCCESS; 256 } 257 258 int relative_same_base(void) 259 { 260 char result[FILENAME_MAX]; 261 size_t length; 262 263 cwk_path_set_style(CWK_STYLE_UNIX); 264 265 length = cwk_path_get_relative("/dir/dir", "/dir/dir3/file", result, 266 sizeof(result)); 267 if (length != 12) { 268 return EXIT_FAILURE; 269 } 270 271 if (strcmp(result, "../dir3/file") != 0) { 272 return EXIT_FAILURE; 273 } 274 275 return EXIT_SUCCESS; 276 } 277 278 int relative_long_target(void) 279 { 280 char result[FILENAME_MAX]; 281 size_t length; 282 283 cwk_path_set_style(CWK_STYLE_UNIX); 284 285 length = cwk_path_get_relative("/path/long/one", "/path/long/one/two", result, 286 sizeof(result)); 287 if (length != 3) { 288 return EXIT_FAILURE; 289 } 290 291 if (strcmp(result, "two") != 0) { 292 return EXIT_FAILURE; 293 } 294 295 return EXIT_SUCCESS; 296 } 297 298 int relative_long_base(void) 299 { 300 char result[FILENAME_MAX]; 301 size_t length; 302 303 cwk_path_set_style(CWK_STYLE_UNIX); 304 305 length = cwk_path_get_relative("/path/long/one/two", "/path/long/one", result, 306 sizeof(result)); 307 if (length != 2) { 308 return EXIT_FAILURE; 309 } 310 311 if (strcmp(result, "..") != 0) { 312 return EXIT_FAILURE; 313 } 314 315 return EXIT_SUCCESS; 316 } 317 318 int relative_relative(void) 319 { 320 char result[FILENAME_MAX]; 321 size_t length; 322 323 cwk_path_set_style(CWK_STYLE_UNIX); 324 325 length = cwk_path_get_relative("./this/is/path_one", "./this/is/path_two", 326 result, sizeof(result)); 327 if (length != 11) { 328 return EXIT_FAILURE; 329 } 330 331 if (strcmp(result, "../path_two") != 0) { 332 return EXIT_FAILURE; 333 } 334 335 return EXIT_SUCCESS; 336 } 337 338 int relative_simple(void) 339 { 340 char result[FILENAME_MAX]; 341 size_t length; 342 343 cwk_path_set_style(CWK_STYLE_UNIX); 344 345 length = cwk_path_get_relative("/this/is/path_one", "/this/is/path_two", 346 result, sizeof(result)); 347 if (length != 11) { 348 return EXIT_FAILURE; 349 } 350 351 if (strcmp(result, "../path_two") != 0) { 352 return EXIT_FAILURE; 353 } 354 355 return EXIT_SUCCESS; 356 }