run.c 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. #include <ccan/noerr/noerr.h>
  2. #include <ccan/tap/tap.h>
  3. #include <ccan/noerr/noerr.c>
  4. #include <sys/types.h>
  5. #include <sys/stat.h>
  6. #include <fcntl.h>
  7. #include <stdio.h>
  8. #include <assert.h>
  9. int main(void)
  10. {
  11. const char *name = "noerr.file";
  12. int fd;
  13. FILE *fp;
  14. plan_tests(16);
  15. /* Should fail to unlink. */
  16. ok1(unlink(name) != 0);
  17. ok1(errno == ENOENT);
  18. /* This one should not set errno. */
  19. errno = 100;
  20. ok1(unlink_noerr(name) == ENOENT);
  21. ok1(errno == 100);
  22. /* Should fail to close. */
  23. ok1(close(-1) != 0);
  24. ok1(errno == EBADF);
  25. /* This one should not set errno. */
  26. errno = 100;
  27. ok1(close_noerr(-1) == EBADF);
  28. ok1(errno == 100);
  29. /* Test successful close/unlink doesn't hit errno either. */
  30. fd = open(name, O_WRONLY|O_CREAT|O_EXCL, 0600);
  31. assert(fd >= 0);
  32. errno = 100;
  33. ok1(close_noerr(fd) == 0);
  34. ok1(errno == 100);
  35. errno = 100;
  36. ok1(unlink_noerr(name) == 0);
  37. ok1(errno == 100);
  38. /* Test failing fclose */
  39. fp = fopen(name, "wb");
  40. assert(fp);
  41. close(fileno(fp));
  42. ok1(fclose_noerr(fp) == EBADF);
  43. /* Test successful fclose */
  44. fp = fopen(name, "wb");
  45. assert(fp);
  46. errno = 100;
  47. ok1(fclose_noerr(fp) == 0);
  48. ok1(errno == 100);
  49. unlink(name);
  50. errno = 101;
  51. free_noerr(malloc(7));
  52. ok1(errno == 101);
  53. return exit_status();
  54. }