segment_test.c (13658B)
1 #include <cwalk.h> 2 #include <memory.h> 3 #include <stdio.h> 4 #include <stdlib.h> 5 #include <string.h> 6 7 int segment_change_overlap(void) 8 { 9 char buffer[FILENAME_MAX] = "C:\\this\\cool\\path\\"; 10 struct cwk_segment segment; 11 12 cwk_path_set_style(CWK_STYLE_WINDOWS); 13 14 if (!cwk_path_get_first_segment(buffer, &segment)) { 15 return EXIT_FAILURE; 16 } 17 18 if (!cwk_path_get_next_segment(&segment)) { 19 return EXIT_FAILURE; 20 } 21 22 cwk_path_change_segment(&segment, "longer_segment", buffer, sizeof(buffer)); 23 24 if (strcmp(buffer, "C:\\this\\longer_segment\\path\\") != 0) { 25 return EXIT_FAILURE; 26 } 27 28 return EXIT_SUCCESS; 29 } 30 31 int segment_change_with_separator(void) 32 { 33 const char *path; 34 char buffer[FILENAME_MAX]; 35 struct cwk_segment segment; 36 37 cwk_path_set_style(CWK_STYLE_WINDOWS); 38 39 path = "C:\\this\\cool\\path\\"; 40 if (!cwk_path_get_first_segment(path, &segment)) { 41 return EXIT_FAILURE; 42 } 43 44 if (!cwk_path_get_next_segment(&segment)) { 45 return EXIT_FAILURE; 46 } 47 48 cwk_path_change_segment(&segment, "other\\fancy", buffer, sizeof(buffer)); 49 50 if (strcmp(buffer, "C:\\this\\other\\fancy\\path\\") != 0) { 51 return EXIT_FAILURE; 52 } 53 54 return EXIT_SUCCESS; 55 } 56 57 int segment_change_empty(void) 58 { 59 const char *path; 60 char buffer[FILENAME_MAX]; 61 struct cwk_segment segment; 62 63 cwk_path_set_style(CWK_STYLE_WINDOWS); 64 65 path = "C:\\this\\cool\\path\\"; 66 if (!cwk_path_get_first_segment(path, &segment)) { 67 return EXIT_FAILURE; 68 } 69 70 if (!cwk_path_get_next_segment(&segment)) { 71 return EXIT_FAILURE; 72 } 73 74 cwk_path_change_segment(&segment, "", buffer, sizeof(buffer)); 75 76 if (strcmp(buffer, "C:\\this\\\\path\\") != 0) { 77 return EXIT_FAILURE; 78 } 79 80 return EXIT_SUCCESS; 81 } 82 83 int segment_change_trim(void) 84 { 85 const char *path; 86 char buffer[FILENAME_MAX]; 87 struct cwk_segment segment; 88 89 cwk_path_set_style(CWK_STYLE_WINDOWS); 90 91 path = "C:\\this\\cool\\path\\"; 92 if (!cwk_path_get_first_segment(path, &segment)) { 93 return EXIT_FAILURE; 94 } 95 96 if (!cwk_path_get_next_segment(&segment)) { 97 return EXIT_FAILURE; 98 } 99 100 cwk_path_change_segment(&segment, "//other/\\\\", buffer, sizeof(buffer)); 101 102 if (strcmp(buffer, "C:\\this\\other\\path\\") != 0) { 103 return EXIT_FAILURE; 104 } 105 106 return EXIT_SUCCESS; 107 } 108 109 int segment_change_last(void) 110 { 111 const char *path; 112 char buffer[FILENAME_MAX]; 113 struct cwk_segment segment; 114 115 cwk_path_set_style(CWK_STYLE_WINDOWS); 116 117 path = "C:\\this\\cool\\path\\"; 118 if (!cwk_path_get_last_segment(path, &segment)) { 119 return EXIT_FAILURE; 120 } 121 122 cwk_path_change_segment(&segment, "other", buffer, sizeof(buffer)); 123 124 if (strcmp(buffer, "C:\\this\\cool\\other\\") != 0) { 125 return EXIT_FAILURE; 126 } 127 128 path = "C:\\this\\cool\\path"; 129 if (!cwk_path_get_last_segment(path, &segment)) { 130 return EXIT_FAILURE; 131 } 132 133 cwk_path_change_segment(&segment, "other", buffer, sizeof(buffer)); 134 135 if (strcmp(buffer, "C:\\this\\cool\\other") != 0) { 136 return EXIT_FAILURE; 137 } 138 139 return EXIT_SUCCESS; 140 } 141 142 int segment_change_first(void) 143 { 144 const char *path; 145 char buffer[FILENAME_MAX]; 146 struct cwk_segment segment; 147 148 cwk_path_set_style(CWK_STYLE_WINDOWS); 149 150 path = "C:\\this\\cool\\path\\"; 151 if (!cwk_path_get_first_segment(path, &segment)) { 152 return EXIT_FAILURE; 153 } 154 155 cwk_path_change_segment(&segment, "other", buffer, sizeof(buffer)); 156 157 if (strcmp(buffer, "C:\\other\\cool\\path\\") != 0) { 158 return EXIT_FAILURE; 159 } 160 161 path = "this\\cool\\path\\"; 162 if (!cwk_path_get_first_segment(path, &segment)) { 163 return EXIT_FAILURE; 164 } 165 166 cwk_path_change_segment(&segment, "other", buffer, sizeof(buffer)); 167 168 if (strcmp(buffer, "other\\cool\\path\\") != 0) { 169 return EXIT_FAILURE; 170 } 171 172 return EXIT_SUCCESS; 173 } 174 175 int segment_change_simple(void) 176 { 177 const char *path; 178 char buffer[FILENAME_MAX]; 179 struct cwk_segment segment; 180 181 cwk_path_set_style(CWK_STYLE_WINDOWS); 182 183 path = "C:\\this\\cool\\path\\"; 184 if (!cwk_path_get_first_segment(path, &segment)) { 185 return EXIT_FAILURE; 186 } 187 188 if (!cwk_path_get_next_segment(&segment)) { 189 return EXIT_FAILURE; 190 } 191 192 cwk_path_change_segment(&segment, "other", buffer, sizeof(buffer)); 193 194 if (strcmp(buffer, "C:\\this\\other\\path\\") != 0) { 195 return EXIT_FAILURE; 196 } 197 198 return EXIT_SUCCESS; 199 } 200 201 int segment_back_with_root(void) 202 { 203 const char *path; 204 struct cwk_segment segment; 205 206 cwk_path_set_style(CWK_STYLE_WINDOWS); 207 208 path = "C:\\this\\path"; 209 if (!cwk_path_get_last_segment(path, &segment)) { 210 return EXIT_FAILURE; 211 } 212 213 if (strncmp(segment.begin, "path", segment.size) != 0) { 214 return EXIT_FAILURE; 215 } 216 217 if (!cwk_path_get_previous_segment(&segment)) { 218 return EXIT_FAILURE; 219 } 220 221 if (strncmp(segment.begin, "this", segment.size) != 0) { 222 return EXIT_FAILURE; 223 } 224 225 if (cwk_path_get_previous_segment(&segment)) { 226 return EXIT_FAILURE; 227 } 228 229 return EXIT_SUCCESS; 230 } 231 232 int segment_type(void) 233 { 234 const char *path; 235 struct cwk_segment segment; 236 237 cwk_path_set_style(CWK_STYLE_UNIX); 238 239 path = "/a/./../.folder/..folder"; 240 241 if (!cwk_path_get_first_segment(path, &segment)) { 242 return EXIT_FAILURE; 243 } 244 245 if (cwk_path_get_segment_type(&segment) != CWK_NORMAL) { 246 return EXIT_FAILURE; 247 } 248 249 if (!cwk_path_get_next_segment(&segment)) { 250 return EXIT_FAILURE; 251 } 252 253 if (cwk_path_get_segment_type(&segment) != CWK_CURRENT) { 254 return EXIT_FAILURE; 255 } 256 257 if (!cwk_path_get_next_segment(&segment)) { 258 return EXIT_FAILURE; 259 } 260 261 if (cwk_path_get_segment_type(&segment) != CWK_BACK) { 262 return EXIT_FAILURE; 263 } 264 265 if (!cwk_path_get_next_segment(&segment)) { 266 return EXIT_FAILURE; 267 } 268 269 if (cwk_path_get_segment_type(&segment) != CWK_NORMAL) { 270 return EXIT_FAILURE; 271 } 272 273 if (!cwk_path_get_next_segment(&segment)) { 274 return EXIT_FAILURE; 275 } 276 277 if (cwk_path_get_segment_type(&segment) != CWK_NORMAL) { 278 return EXIT_FAILURE; 279 } 280 281 return EXIT_SUCCESS; 282 } 283 284 int segment_previous_too_far_root(void) 285 { 286 const char *path; 287 struct cwk_segment segment; 288 289 cwk_path_set_style(CWK_STYLE_UNIX); 290 291 path = "//now/hello_world/abc/"; 292 293 if (!cwk_path_get_last_segment(path, &segment)) { 294 return EXIT_FAILURE; 295 } 296 297 if (!cwk_path_get_previous_segment(&segment)) { 298 return EXIT_FAILURE; 299 } 300 301 if (!cwk_path_get_previous_segment(&segment)) { 302 return EXIT_FAILURE; 303 } 304 305 if (cwk_path_get_previous_segment(&segment)) { 306 return EXIT_FAILURE; 307 } 308 309 if (cwk_path_get_previous_segment(&segment)) { 310 return EXIT_FAILURE; 311 } 312 313 return EXIT_SUCCESS; 314 } 315 316 int segment_previous_too_far(void) 317 { 318 const char *path; 319 struct cwk_segment segment; 320 321 cwk_path_set_style(CWK_STYLE_UNIX); 322 323 path = "//now/hello_world/abc/"; 324 325 if (!cwk_path_get_last_segment(path, &segment)) { 326 return EXIT_FAILURE; 327 } 328 329 if (!cwk_path_get_previous_segment(&segment)) { 330 return EXIT_FAILURE; 331 } 332 333 if (!cwk_path_get_previous_segment(&segment)) { 334 return EXIT_FAILURE; 335 } 336 337 if (cwk_path_get_previous_segment(&segment)) { 338 return EXIT_FAILURE; 339 } 340 341 if (cwk_path_get_previous_segment(&segment)) { 342 return EXIT_FAILURE; 343 } 344 345 return EXIT_SUCCESS; 346 } 347 348 int segment_previous_relative(void) 349 { 350 const char *path; 351 struct cwk_segment segment; 352 353 cwk_path_set_style(CWK_STYLE_UNIX); 354 355 path = "now/hello_world/abc/"; 356 357 if (!cwk_path_get_last_segment(path, &segment)) { 358 return EXIT_FAILURE; 359 } 360 361 if (strncmp(segment.begin, "abc", segment.size) != 0) { 362 return EXIT_FAILURE; 363 } 364 365 if (!cwk_path_get_previous_segment(&segment)) { 366 return EXIT_FAILURE; 367 } 368 369 if (strncmp(segment.begin, "hello_world", segment.size) != 0) { 370 return EXIT_FAILURE; 371 } 372 373 if (!cwk_path_get_previous_segment(&segment)) { 374 return EXIT_FAILURE; 375 } 376 377 if (strncmp(segment.begin, "now", segment.size) != 0) { 378 return EXIT_FAILURE; 379 } 380 381 if (cwk_path_get_previous_segment(&segment)) { 382 return EXIT_FAILURE; 383 } 384 385 return EXIT_SUCCESS; 386 } 387 388 int segment_previous_absolute(void) 389 { 390 const char *path; 391 struct cwk_segment segment; 392 393 cwk_path_set_style(CWK_STYLE_UNIX); 394 395 path = "/now/hello_world/abc/"; 396 397 if (!cwk_path_get_last_segment(path, &segment)) { 398 return EXIT_FAILURE; 399 } 400 401 if (strncmp(segment.begin, "abc", segment.size) != 0) { 402 return EXIT_FAILURE; 403 } 404 405 if (!cwk_path_get_previous_segment(&segment)) { 406 return EXIT_FAILURE; 407 } 408 409 if (strncmp(segment.begin, "hello_world", segment.size) != 0) { 410 return EXIT_FAILURE; 411 } 412 413 if (!cwk_path_get_previous_segment(&segment)) { 414 return EXIT_FAILURE; 415 } 416 417 if (strncmp(segment.begin, "now", segment.size) != 0) { 418 return EXIT_FAILURE; 419 } 420 421 if (cwk_path_get_previous_segment(&segment)) { 422 return EXIT_FAILURE; 423 } 424 425 return EXIT_SUCCESS; 426 } 427 428 int segment_previous_relative_one_char_first(void) 429 { 430 const char *path; 431 struct cwk_segment segment; 432 433 cwk_path_set_style(CWK_STYLE_UNIX); 434 435 path = "n/hello_world/abc/"; 436 437 if (!cwk_path_get_last_segment(path, &segment)) { 438 return EXIT_FAILURE; 439 } 440 441 if (strncmp(segment.begin, "abc", segment.size) != 0) { 442 return EXIT_FAILURE; 443 } 444 445 if (!cwk_path_get_previous_segment(&segment)) { 446 return EXIT_FAILURE; 447 } 448 449 if (strncmp(segment.begin, "hello_world", segment.size) != 0) { 450 return EXIT_FAILURE; 451 } 452 453 if (!cwk_path_get_previous_segment(&segment)) { 454 return EXIT_FAILURE; 455 } 456 457 if (strncmp(segment.begin, "n", segment.size) != 0) { 458 return EXIT_FAILURE; 459 } 460 461 if (cwk_path_get_previous_segment(&segment)) { 462 return EXIT_FAILURE; 463 } 464 465 cwk_path_set_style(CWK_STYLE_WINDOWS); 466 467 path = "t\\cool\\path\\"; 468 469 if (!cwk_path_get_last_segment(path, &segment)) { 470 return EXIT_FAILURE; 471 } 472 473 if (strncmp(segment.begin, "path", segment.size) != 0) { 474 return EXIT_FAILURE; 475 } 476 477 if (!cwk_path_get_previous_segment(&segment)) { 478 return EXIT_FAILURE; 479 } 480 481 if (strncmp(segment.begin, "cool", segment.size) != 0) { 482 return EXIT_FAILURE; 483 } 484 485 if (!cwk_path_get_previous_segment(&segment)) { 486 return EXIT_FAILURE; 487 } 488 489 if (strncmp(segment.begin, "t", segment.size) != 0) { 490 return EXIT_FAILURE; 491 } 492 493 if (cwk_path_get_previous_segment(&segment)) { 494 return EXIT_FAILURE; 495 } 496 497 return EXIT_SUCCESS; 498 } 499 500 int segment_previous_absolute_one_char_first(void) 501 { 502 const char *path; 503 struct cwk_segment segment; 504 505 cwk_path_set_style(CWK_STYLE_UNIX); 506 507 path = "/n/hello_world/abc/"; 508 509 if (!cwk_path_get_last_segment(path, &segment)) { 510 return EXIT_FAILURE; 511 } 512 513 if (strncmp(segment.begin, "abc", segment.size) != 0) { 514 return EXIT_FAILURE; 515 } 516 517 if (!cwk_path_get_previous_segment(&segment)) { 518 return EXIT_FAILURE; 519 } 520 521 if (strncmp(segment.begin, "hello_world", segment.size) != 0) { 522 return EXIT_FAILURE; 523 } 524 525 if (!cwk_path_get_previous_segment(&segment)) { 526 return EXIT_FAILURE; 527 } 528 529 if (strncmp(segment.begin, "n", segment.size) != 0) { 530 return EXIT_FAILURE; 531 } 532 533 if (cwk_path_get_previous_segment(&segment)) { 534 return EXIT_FAILURE; 535 } 536 537 cwk_path_set_style(CWK_STYLE_WINDOWS); 538 539 path = "C:\\t\\cool\\path\\"; 540 541 if (!cwk_path_get_last_segment(path, &segment)) { 542 return EXIT_FAILURE; 543 } 544 545 if (strncmp(segment.begin, "path", segment.size) != 0) { 546 return EXIT_FAILURE; 547 } 548 549 if (!cwk_path_get_previous_segment(&segment)) { 550 return EXIT_FAILURE; 551 } 552 553 if (strncmp(segment.begin, "cool", segment.size) != 0) { 554 return EXIT_FAILURE; 555 } 556 557 if (!cwk_path_get_previous_segment(&segment)) { 558 return EXIT_FAILURE; 559 } 560 561 if (strncmp(segment.begin, "t", segment.size) != 0) { 562 return EXIT_FAILURE; 563 } 564 565 if (cwk_path_get_previous_segment(&segment)) { 566 return EXIT_FAILURE; 567 } 568 569 return EXIT_SUCCESS; 570 } 571 572 int segment_next_too_far(void) 573 { 574 const char *path; 575 struct cwk_segment segment; 576 577 cwk_path_set_style(CWK_STYLE_UNIX); 578 579 path = "/hello_world/abc/"; 580 581 if (!cwk_path_get_first_segment(path, &segment)) { 582 return EXIT_FAILURE; 583 } 584 585 if (!cwk_path_get_next_segment(&segment)) { 586 return EXIT_FAILURE; 587 } 588 589 if (cwk_path_get_next_segment(&segment)) { 590 return EXIT_FAILURE; 591 } 592 593 if (cwk_path_get_next_segment(&segment)) { 594 return EXIT_FAILURE; 595 } 596 597 return EXIT_SUCCESS; 598 } 599 600 int segment_next(void) 601 { 602 const char *path; 603 struct cwk_segment segment; 604 605 cwk_path_set_style(CWK_STYLE_UNIX); 606 607 path = "/hello_world/abc/"; 608 609 if (!cwk_path_get_first_segment(path, &segment)) { 610 return EXIT_FAILURE; 611 } 612 613 if (strncmp(segment.begin, "hello_world", segment.size) != 0) { 614 return EXIT_FAILURE; 615 } 616 617 if (!cwk_path_get_next_segment(&segment)) { 618 return EXIT_FAILURE; 619 } 620 621 if (strncmp(segment.begin, "abc", segment.size) != 0) { 622 return EXIT_FAILURE; 623 } 624 625 if (cwk_path_get_next_segment(&segment)) { 626 return EXIT_FAILURE; 627 } 628 629 return EXIT_SUCCESS; 630 } 631 632 int segment_last(void) 633 { 634 const char *path; 635 struct cwk_segment segment; 636 637 cwk_path_set_style(CWK_STYLE_UNIX); 638 639 path = "/hello_world/abc"; 640 641 if (!cwk_path_get_last_segment(path, &segment)) { 642 return EXIT_FAILURE; 643 } 644 645 if (strncmp(segment.begin, "abc", segment.size) != 0) { 646 return EXIT_FAILURE; 647 } 648 649 path = "hello_world/abc"; 650 651 if (!cwk_path_get_last_segment(path, &segment)) { 652 return EXIT_FAILURE; 653 } 654 655 if (strncmp(segment.begin, "abc", segment.size) != 0) { 656 return EXIT_FAILURE; 657 } 658 659 return EXIT_SUCCESS; 660 } 661 662 int segment_first(void) 663 { 664 const char *path; 665 struct cwk_segment segment; 666 667 cwk_path_set_style(CWK_STYLE_UNIX); 668 669 path = "/hello_world/abc"; 670 671 if (!cwk_path_get_first_segment(path, &segment)) { 672 return EXIT_FAILURE; 673 } 674 675 if (strncmp(segment.begin, "hello_world", segment.size) != 0) { 676 return EXIT_FAILURE; 677 } 678 679 path = "hello_world/abc"; 680 681 if (!cwk_path_get_first_segment(path, &segment)) { 682 return EXIT_FAILURE; 683 } 684 685 if (strncmp(segment.begin, "hello_world", segment.size) != 0) { 686 return EXIT_FAILURE; 687 } 688 689 return EXIT_SUCCESS; 690 }