2
0

test_helpers.h 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. #ifndef _TEST_HELPERS
  2. #define _TEST_HELPERS
  3. #include <errno.h>
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <string.h>
  7. #include <unistd.h>
  8. // Throws errors on a well-defined API error values.
  9. //
  10. // Only use with API functions that sets the errno variable.
  11. // Do not pass functions as the status or condition arguments, they might be
  12. // evaluated multiple times.
  13. //
  14. // Usage example:
  15. //
  16. // > Upon successful completion, fclose() returns 0.
  17. // > Otherwise, it returns EOF and sets errno to indicate the error.
  18. //
  19. // int status = fclose(fp);
  20. // ERROR_IF(fclose, status, == EOF);
  21. //
  22. // Use it only for checking the API error values.
  23. // Do not use it for checking the correctness of the results. If you need to
  24. // do that, print the values to the standard output and use the expected outputs
  25. // directory.
  26. //
  27. // For example:
  28. //
  29. // int c = fgetc(f); // !!! DO NOT USE THIS WAY !!!
  30. // ERROR_IF(fgetc, c, != 'H'); // !!! DO NOT USE THIS WAY !!!
  31. //
  32. // Correct usage:
  33. //
  34. // int c = fgetc(f); // OK
  35. // ERROR_IF(fgetc, c, == EOF); // OK
  36. // printf("result: %c\n", c); // OK
  37. //
  38. #define ERROR_IF(func, status, condition) \
  39. do { \
  40. if (status condition) { \
  41. fprintf(stderr, "%s:%s:%d: '%s' failed: %s (%d)\n", \
  42. __FILE__, __func__, __LINE__, #func, strerror(errno), errno); \
  43. _exit(EXIT_FAILURE); \
  44. } \
  45. } while(0)
  46. // Throws errors on API return values not defined by the standards.
  47. //
  48. // Do not pass functions as the status or condition arguments, they might be
  49. // evaluated multiple times.
  50. //
  51. // Use it only for detecting return values that should have never been returned
  52. // in any case by the API functions.
  53. //
  54. // Usage example:
  55. //
  56. // > The fgetc() function obtains the next byte as an unsigned char
  57. // > converted to an int.
  58. //
  59. // int c = fgetc(f);
  60. // UNEXP_IF(fgetc, c, < 0);
  61. // UNEXP_IF(fgetc, c, > 255);
  62. //
  63. #define UNEXP_IF(func, status, condition) \
  64. do { \
  65. if (status condition) { \
  66. fprintf(stderr, "%s:%s:%d: '%s' returned a non-standard value: ", \
  67. __FILE__, __func__, __LINE__, #func); \
  68. fprintf(stderr, _Generic((status), \
  69. char *: "char*(%p) = \"%1$s\"", \
  70. void *: "void*(%p)", \
  71. default: "%i" \
  72. ), status); \
  73. fprintf(stderr, "\n"); \
  74. _exit(EXIT_FAILURE); \
  75. } \
  76. } while (0)
  77. // A convenience macro to show where the test fail.
  78. #define exit(code) \
  79. do { \
  80. if (code != EXIT_SUCCESS) { \
  81. fprintf(stderr, "%s:%s:%d: Test failed with exit(%s)\n", \
  82. __FILE__, __func__, __LINE__, #code); \
  83. } \
  84. _exit(code); \
  85. } while(0)
  86. #endif /* _TEST_HELPERS */