testc_base.c 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <sys/stat.h>
  5. #include <sys/types.h>
  6. #include "faux/faux.h"
  7. #include "faux/str.h"
  8. #include "faux/testc_helpers.h"
  9. int testc_faux_filesize(void)
  10. {
  11. const char *dirname = "subdir";
  12. const char *basedir = getenv(FAUX_TESTC_TMPDIR_ENV);
  13. const char *fd1 = "asdfghjkl"; // 9 bytes
  14. const char *fd2 = "asdfghjklzxcvbnm"; // 16 bytes
  15. const char *fd3 = "asdfghjklzxcvbnmqwertyuiop"; // 26 bytes
  16. ssize_t r = 0;
  17. ssize_t etalon_filesize = 0;
  18. int ret = -1; // Pessimistic
  19. char *fn1 = NULL;
  20. char *dn1 = NULL;
  21. char *fn2 = NULL;
  22. char *fn3 = NULL;
  23. // Prepare filenames
  24. fn1 = faux_str_sprintf("%s/f1", basedir);
  25. dn1 = faux_str_sprintf("%s/%s", basedir, dirname);
  26. fn2 = faux_str_sprintf("%s/f2", dn1);
  27. fn3 = faux_str_sprintf("%s/f3", dn1);
  28. // Create files and dirs
  29. mkdir(dn1, 0777);
  30. if ((r = faux_testc_file_deploy_str(fn1, fd1)) < 0)
  31. goto err;
  32. etalon_filesize += r;
  33. if ((r = faux_testc_file_deploy_str(fn2, fd2)) < 0)
  34. goto err;
  35. etalon_filesize += r;
  36. if ((r = faux_testc_file_deploy_str(fn3, fd3)) < 0)
  37. goto err;
  38. etalon_filesize += r;
  39. // Debug
  40. printf("Dir: %s\n", basedir);
  41. printf("File: %s\n", fn1);
  42. // Get filesize
  43. if ((ssize_t)strlen(fd1) != faux_filesize(fn1)) {
  44. printf("Wrong filesize (%ld %ld)\n",
  45. strlen(fd1), faux_filesize(fn1));
  46. goto err;
  47. }
  48. if (etalon_filesize != faux_filesize(basedir)) {
  49. printf("Wrong dirsize (%ld %ld)\n",
  50. etalon_filesize, faux_filesize(basedir));
  51. goto err;
  52. }
  53. ret = 0;
  54. err:
  55. faux_str_free(fn1);
  56. faux_str_free(dn1);
  57. faux_str_free(fn2);
  58. faux_str_free(fn3);
  59. return ret;
  60. }