tests.h 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef __LIBPERF_INTERNAL_TESTS_H
  3. #define __LIBPERF_INTERNAL_TESTS_H
  4. #include <stdio.h>
  5. #include <unistd.h>
  6. extern int tests_failed;
  7. extern int tests_verbose;
  8. static inline int get_verbose(char **argv, int argc)
  9. {
  10. int c;
  11. int verbose = 0;
  12. while ((c = getopt(argc, argv, "v")) != -1) {
  13. switch (c)
  14. {
  15. case 'v':
  16. verbose = 1;
  17. break;
  18. default:
  19. break;
  20. }
  21. }
  22. optind = 1;
  23. return verbose;
  24. }
  25. #define __T_START \
  26. do { \
  27. tests_verbose = get_verbose(argv, argc); \
  28. fprintf(stdout, "- running %s...", __FILE__); \
  29. fflush(NULL); \
  30. tests_failed = 0; \
  31. } while (0)
  32. #define __T_END \
  33. do { \
  34. if (tests_failed) \
  35. fprintf(stdout, " FAILED (%d)\n", tests_failed); \
  36. else \
  37. fprintf(stdout, "OK\n"); \
  38. } while (0)
  39. #define __T(text, cond) \
  40. do { \
  41. if (!(cond)) { \
  42. fprintf(stderr, "FAILED %s:%d %s\n", __FILE__, __LINE__, text); \
  43. tests_failed++; \
  44. return -1; \
  45. } \
  46. } while (0)
  47. #define __T_VERBOSE(...) \
  48. do { \
  49. if (tests_verbose) { \
  50. if (tests_verbose == 1) { \
  51. fputc('\n', stderr); \
  52. tests_verbose++; \
  53. } \
  54. fprintf(stderr, ##__VA_ARGS__); \
  55. } \
  56. } while (0)
  57. #endif /* __LIBPERF_INTERNAL_TESTS_H */