2
0

tap.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /*
  2. libtap - Write tests in C
  3. Copyright 2012 Jake Gelbman <gelbman@gmail.com>
  4. This file is licensed under the LGPL
  5. */
  6. #ifndef __TAP_H__
  7. #define __TAP_H__
  8. #ifdef __cplusplus
  9. extern "C" {
  10. #endif
  11. #ifndef va_copy
  12. #ifdef __va_copy
  13. #define va_copy __va_copy
  14. #else
  15. #define va_copy(d, s) ((d) = (s))
  16. #endif
  17. #endif
  18. #include <stdio.h>
  19. #include <stdlib.h>
  20. #include <stdarg.h>
  21. int vok_at_loc (const char *file, int line, int test, const char *fmt,
  22. va_list args);
  23. int ok_at_loc (const char *file, int line, int test, const char *fmt,
  24. ...);
  25. int is_at_loc (const char *file, int line, const char *got,
  26. const char *expected, const char *fmt, ...);
  27. int isnt_at_loc (const char *file, int line, const char *got,
  28. const char *expected, const char *fmt, ...);
  29. int cmp_ok_at_loc (const char *file, int line, int a, const char *op,
  30. int b, const char *fmt, ...);
  31. int cmp_mem_at_loc (const char *file, int line, const void *got,
  32. const void *expected, size_t n, const char *fmt, ...);
  33. int bail_out (int ignore, const char *fmt, ...);
  34. void tap_plan (int tests, const char *fmt, ...);
  35. int diag (const char *fmt, ...);
  36. int exit_status (void);
  37. void tap_skip (int n, const char *fmt, ...);
  38. void tap_todo (int ignore, const char *fmt, ...);
  39. void tap_end_todo (void);
  40. #define NO_PLAN -1
  41. #define SKIP_ALL -2
  42. #define ok(...) ok_at_loc(__FILE__, __LINE__, __VA_ARGS__, NULL)
  43. #define is(...) is_at_loc(__FILE__, __LINE__, __VA_ARGS__, NULL)
  44. #define isnt(...) isnt_at_loc(__FILE__, __LINE__, __VA_ARGS__, NULL)
  45. #define cmp_ok(...) cmp_ok_at_loc(__FILE__, __LINE__, __VA_ARGS__, NULL)
  46. #define cmp_mem(...) cmp_mem_at_loc(__FILE__, __LINE__, __VA_ARGS__, NULL);
  47. #define plan(...) tap_plan(__VA_ARGS__, NULL)
  48. #define done_testing() return exit_status()
  49. #define BAIL_OUT(...) bail_out(0, "" __VA_ARGS__, NULL)
  50. #define pass(...) ok(1, "" __VA_ARGS__)
  51. #define fail(...) ok(0, "" __VA_ARGS__)
  52. #define skip(test, ...) do {if (test) {tap_skip(__VA_ARGS__, NULL); break;}
  53. #define end_skip } while (0)
  54. #define todo(...) tap_todo(0, "" __VA_ARGS__, NULL)
  55. #define end_todo tap_end_todo()
  56. #define dies_ok(...) dies_ok_common(1, __VA_ARGS__)
  57. #define lives_ok(...) dies_ok_common(0, __VA_ARGS__)
  58. #ifdef _WIN32
  59. #define like(...) tap_skip(1, "like is not implemented on Windows")
  60. #define unlike tap_skip(1, "unlike is not implemented on Windows")
  61. #define dies_ok_common(...) \
  62. tap_skip(1, "Death detection is not supported on Windows")
  63. #else
  64. #define like(...) like_at_loc(1, __FILE__, __LINE__, __VA_ARGS__, NULL)
  65. #define unlike(...) like_at_loc(0, __FILE__, __LINE__, __VA_ARGS__, NULL)
  66. int like_at_loc (int for_match, const char *file, int line,
  67. const char *got, const char *expected,
  68. const char *fmt, ...);
  69. #include <unistd.h>
  70. #include <sys/types.h>
  71. #include <sys/wait.h>
  72. int tap_test_died (int status);
  73. #define dies_ok_common(for_death, code, ...) \
  74. do { \
  75. int cpid; \
  76. int it_died; \
  77. tap_test_died(1); \
  78. cpid = fork(); \
  79. switch (cpid) { \
  80. case -1: \
  81. perror("fork error"); \
  82. exit(1); \
  83. case 0: \
  84. close(1); \
  85. close(2); \
  86. code \
  87. tap_test_died(0); \
  88. exit(0); \
  89. } \
  90. if (waitpid(cpid, NULL, 0) < 0) { \
  91. perror("waitpid error"); \
  92. exit(1); \
  93. } \
  94. it_died = tap_test_died(0); \
  95. if (!it_died) \
  96. {code} \
  97. ok(for_death ? it_died : !it_died, "" __VA_ARGS__); \
  98. } while (0)
  99. #endif
  100. #ifdef __cplusplus
  101. }
  102. #endif
  103. #endif