logo

utils-std

Collection of commonly available Unix tools git clone https://anongit.hacktivis.me/git/utils-std.git/

t_relative_path.c (1453B)


  1. // utils-std: Collection of commonly available Unix tools
  2. // SPDX-FileCopyrightText: 2017 Haelwenn (lanodan) Monnier <contact+utils@hacktivis.me>
  3. // SPDX-License-Identifier: MPL-2.0
  4. #define _POSIX_C_SOURCE 200809L
  5. #include "../lib/relative_path.h"
  6. #include <assert.h>
  7. #include <stdio.h>
  8. #include <string.h>
  9. #define TEST_PLAN 7
  10. int counter = 0;
  11. static int
  12. t_relpath(const char *base, const char *target, const char *expected)
  13. {
  14. int id = ++counter;
  15. static char buf[PATH_MAX];
  16. if(relative_path(base, target, buf) < 0)
  17. {
  18. fprintf(stderr, "# returned -1\n");
  19. fprintf(stderr, "not ok %d - ('%s', '%s') == '%s'\n", id, base, target, expected);
  20. return 1;
  21. }
  22. if(strcmp(buf, expected) != 0)
  23. {
  24. fprintf(stderr, "# Got '%s'\n", buf);
  25. fprintf(stderr, "not ok %d - ('%s', '%s') == '%s'\n", id, base, target, expected);
  26. return 1;
  27. }
  28. fprintf(stderr, "ok %d - ('%s', '%s') == '%s'\n", id, base, target, expected);
  29. return 0;
  30. }
  31. int
  32. main(void)
  33. {
  34. printf("1..%d\n", TEST_PLAN);
  35. int err = 0;
  36. err += t_relpath("/usr/bin/a", "/usr/bin/b", "b");
  37. err += t_relpath("/usr/a", "/usr/bin/b", "bin/b");
  38. err += t_relpath("/usr/bin/a", "/usr/b", "../b");
  39. err += t_relpath("/usr/bin/a", "/usr/lib/b", "../lib/b");
  40. err += t_relpath("/", "/", ".");
  41. err += t_relpath("/a", "/", ".");
  42. err += t_relpath("/", "/b", "b");
  43. if(counter != TEST_PLAN)
  44. {
  45. fprintf(stderr, "# planned for %d tests, ran %d\n", TEST_PLAN, counter);
  46. return 1;
  47. }
  48. return err;
  49. }