test_os_path.c 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. #include <stdarg.h>
  2. #include <stddef.h>
  3. #include <setjmp.h>
  4. #include <cmocka.h>
  5. #include <util/platform.h>
  6. struct testcase {
  7. const char *path;
  8. const char *ext;
  9. };
  10. static void run_testcases(struct testcase *testcases)
  11. {
  12. for (size_t i = 0; testcases[i].path; i++) {
  13. const char *path = testcases[i].path;
  14. const char *ext = os_get_path_extension(path);
  15. printf("path: '%s' ext: '%s'\n", path, ext);
  16. if (testcases[i].ext)
  17. assert_string_equal(ext, testcases[i].ext);
  18. else
  19. assert_ptr_equal(ext, testcases[i].ext);
  20. }
  21. }
  22. static void os_get_path_extension_test(void **state)
  23. {
  24. UNUSED_PARAMETER(state);
  25. static struct testcase testcases[] = {
  26. {"/home/user/a.txt", ".txt"},
  27. {"C:\\Users\\user\\Documents\\video.mp4", ".mp4"},
  28. {"./\\", NULL},
  29. {".\\/", NULL},
  30. {"/.\\", NULL},
  31. {"/\\.", "."},
  32. {"\\/.", "."},
  33. {"\\./", NULL},
  34. {"", NULL},
  35. {NULL, NULL}};
  36. run_testcases(testcases);
  37. }
  38. int main()
  39. {
  40. const struct CMUnitTest tests[] = {
  41. cmocka_unit_test(os_get_path_extension_test),
  42. };
  43. return cmocka_run_group_tests(tests, NULL, NULL);
  44. }