test_os_path.c 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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[] = {{"/home/user/a.txt", ".txt"},
  26. {"C:\\Users\\user\\Documents\\video.mp4", ".mp4"},
  27. {"./\\", NULL},
  28. {".\\/", NULL},
  29. {"/.\\", NULL},
  30. {"/\\.", "."},
  31. {"\\/.", "."},
  32. {"\\./", NULL},
  33. {"", NULL},
  34. {NULL, NULL}};
  35. run_testcases(testcases);
  36. }
  37. int main()
  38. {
  39. const struct CMUnitTest tests[] = {
  40. cmocka_unit_test(os_get_path_extension_test),
  41. };
  42. return cmocka_run_group_tests(tests, NULL, NULL);
  43. }