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