absolute_test.c (4075B)
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 absolute_check(void) 10 { 11 const char *relative_paths[] = {"..", "test", "test/test", "../another_test", 12 "./simple", ".././simple"}; 13 const char *absolute_paths[] = {"/", "/test", "/../test/", "/../another_test", 14 "/./simple", "/.././simple"}; 15 size_t i; 16 17 cwk_path_set_style(CWK_STYLE_UNIX); 18 19 for (i = 0; i < ARRAY_SIZE(relative_paths); ++i) { 20 if (cwk_path_is_absolute(relative_paths[i])) { 21 return EXIT_FAILURE; 22 } 23 } 24 25 for (i = 0; i < ARRAY_SIZE(absolute_paths); ++i) { 26 if (!cwk_path_is_absolute(absolute_paths[i])) { 27 return EXIT_FAILURE; 28 } 29 } 30 31 return EXIT_SUCCESS; 32 } 33 34 int absolute_too_far(void) 35 { 36 char buffer[FILENAME_MAX]; 37 size_t length; 38 39 cwk_path_set_style(CWK_STYLE_UNIX); 40 length = cwk_path_get_absolute("/hello/there", "../../../../../", buffer, 41 sizeof(buffer)); 42 43 if (length != 1) { 44 return EXIT_FAILURE; 45 } 46 47 if (strcmp(buffer, "/") != 0) { 48 return EXIT_FAILURE; 49 } 50 51 return EXIT_SUCCESS; 52 } 53 54 int absolute_normalization(void) 55 { 56 char buffer[FILENAME_MAX]; 57 size_t length; 58 59 cwk_path_set_style(CWK_STYLE_UNIX); 60 length = cwk_path_get_absolute("/hello//../there", "test//thing", buffer, 61 sizeof(buffer)); 62 63 if (length != 17) { 64 return EXIT_FAILURE; 65 } 66 67 if (strcmp(buffer, "/there/test/thing") != 0) { 68 return EXIT_FAILURE; 69 } 70 71 return EXIT_SUCCESS; 72 } 73 74 int absolute_mixed(void) 75 { 76 char buffer[FILENAME_MAX]; 77 size_t length; 78 79 cwk_path_set_style(CWK_STYLE_UNIX); 80 length = cwk_path_get_absolute("hello/there", "/test", buffer, 81 sizeof(buffer)); 82 83 if (length != 5) { 84 return EXIT_FAILURE; 85 } 86 87 if (strcmp(buffer, "/test") != 0) { 88 return EXIT_FAILURE; 89 } 90 91 return EXIT_SUCCESS; 92 } 93 94 int absolute_unix_relative_base(void) 95 { 96 char buffer[FILENAME_MAX]; 97 size_t length; 98 99 cwk_path_set_style(CWK_STYLE_UNIX); 100 length = cwk_path_get_absolute("hello/there", "test", buffer, sizeof(buffer)); 101 102 if (length != 17) { 103 return EXIT_FAILURE; 104 } 105 106 if (strcmp(buffer, "/hello/there/test") != 0) { 107 return EXIT_FAILURE; 108 } 109 110 return EXIT_SUCCESS; 111 } 112 113 int absolute_windows_relative_base(void) 114 { 115 char buffer[FILENAME_MAX]; 116 size_t length; 117 118 cwk_path_set_style(CWK_STYLE_WINDOWS); 119 length = cwk_path_get_absolute("hello\\there", "test", buffer, sizeof(buffer)); 120 121 if (length != 17) { 122 return EXIT_FAILURE; 123 } 124 125 if (strcmp(buffer, "\\hello\\there\\test") != 0) { 126 return EXIT_FAILURE; 127 } 128 129 return EXIT_SUCCESS; 130 } 131 132 int absolute_absolute_path(void) 133 { 134 char buffer[FILENAME_MAX]; 135 size_t length; 136 137 cwk_path_set_style(CWK_STYLE_UNIX); 138 length = cwk_path_get_absolute("/hello/there", "/test", buffer, 139 sizeof(buffer)); 140 141 if (length != 5) { 142 return EXIT_FAILURE; 143 } 144 145 if (strcmp(buffer, "/test") != 0) { 146 return EXIT_FAILURE; 147 } 148 149 return EXIT_SUCCESS; 150 } 151 152 int absolute_simple(void) 153 { 154 char buffer[FILENAME_MAX]; 155 size_t length; 156 157 cwk_path_set_style(CWK_STYLE_UNIX); 158 length = cwk_path_get_absolute("/hello/there", "..", buffer, sizeof(buffer)); 159 160 if (length != 6) { 161 return EXIT_FAILURE; 162 } 163 164 if (strcmp(buffer, "/hello") != 0) { 165 return EXIT_FAILURE; 166 } 167 168 return EXIT_SUCCESS; 169 } 170 171 172 int absolute_buffer_reuse(void) 173 { 174 char path[FILENAME_MAX]; 175 176 memset(path, 1, FILENAME_MAX); 177 path[0] = '\0'; 178 179 cwk_path_set_style(CWK_STYLE_UNIX); 180 181 cwk_path_get_absolute(path, "/", path, FILENAME_MAX); 182 if (strcmp(path, "/") != 0) { 183 return EXIT_FAILURE; 184 } 185 cwk_path_get_absolute(path, "see", path, FILENAME_MAX); 186 if (strcmp(path, "/see") != 0) { 187 return EXIT_FAILURE; 188 } 189 190 cwk_path_get_absolute(path, "dog", path, FILENAME_MAX); 191 if (strcmp(path, "/see/dog") != 0) { 192 return EXIT_FAILURE; 193 } 194 195 cwk_path_get_absolute(path, "..", path, FILENAME_MAX); 196 if (strcmp(path, "/see") != 0) { 197 return EXIT_FAILURE; 198 } 199 200 cwk_path_get_absolute(path, "cat", path, FILENAME_MAX); 201 if (strcmp(path, "/see/cat") != 0) { 202 return EXIT_FAILURE; 203 } 204 205 return EXIT_SUCCESS; 206 }