logo

utils-std

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

t_strip_lastelem.c (1175B)


  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 "../libutils/strip_lastelem.h"
  6. #include <assert.h>
  7. #include <stdio.h> // printf
  8. #include <string.h> // strcmp
  9. int counter = 0;
  10. int err = 0;
  11. static void
  12. t_strip_lastelem(const char *in, const char *exp)
  13. {
  14. int id = ++counter;
  15. static char buf[512];
  16. strcpy(buf, in);
  17. strip_lastelem(buf);
  18. if(strcmp(buf, exp) == 0)
  19. {
  20. printf("ok %d - \"%s\" -> \"%s\"\n", id, in, exp);
  21. return;
  22. }
  23. err = 1;
  24. printf("not ok %d - \"%s\" -> \"%s\"\n", id, in, exp);
  25. printf("# Got: \"%s\"\n", buf);
  26. }
  27. int
  28. main(void)
  29. {
  30. int plan = 11;
  31. printf("1..%d\n", plan);
  32. t_strip_lastelem("", "");
  33. t_strip_lastelem("/", "");
  34. t_strip_lastelem("//", "");
  35. t_strip_lastelem("/foo", "");
  36. t_strip_lastelem("/foo/", "");
  37. t_strip_lastelem("/foo/bar", "/foo/");
  38. t_strip_lastelem("/foo/bar/", "/foo/");
  39. t_strip_lastelem("a", "");
  40. t_strip_lastelem("a/", "");
  41. t_strip_lastelem("a/b", "a/");
  42. t_strip_lastelem("a/b/", "a/");
  43. assert(counter == plan);
  44. return err;
  45. }