aboutsummaryrefslogtreecommitdiff
path: root/api_test/harness.c
blob: 6b38d416ab430e8c56ee305f3a2a15582bedf421 (plain)
  1. #include <stdarg.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include "harness.h"
  6. test_batch_runner*
  7. test_batch_runner_new()
  8. {
  9. return (test_batch_runner *)calloc(1, sizeof(test_batch_runner));
  10. }
  11. static void
  12. test_result(test_batch_runner *runner, int cond, const char *msg, va_list ap)
  13. {
  14. ++runner->test_num;
  15. if (cond) {
  16. ++runner->num_passed;
  17. }
  18. else {
  19. fprintf(stderr, "FAILED test %d: ", runner->test_num);
  20. vfprintf(stderr, msg, ap);
  21. fprintf(stderr, "\n");
  22. ++runner->num_failed;
  23. }
  24. }
  25. void
  26. SKIP(test_batch_runner *runner, int num_tests)
  27. {
  28. runner->test_num += num_tests;
  29. runner->num_skipped += num_tests;
  30. }
  31. void
  32. OK(test_batch_runner *runner, int cond, const char *msg, ...)
  33. {
  34. va_list ap;
  35. va_start(ap, msg);
  36. test_result(runner, cond, msg, ap);
  37. va_end(ap);
  38. }
  39. void
  40. INT_EQ(test_batch_runner *runner, int got, int expected, const char *msg, ...)
  41. {
  42. int cond = got == expected;
  43. va_list ap;
  44. va_start(ap, msg);
  45. test_result(runner, cond, msg, ap);
  46. va_end(ap);
  47. if (!cond) {
  48. fprintf(stderr, " Got: %d\n", got);
  49. fprintf(stderr, " Expected: %d\n", expected);
  50. }
  51. }
  52. void
  53. STR_EQ(test_batch_runner *runner, const char *got, const char *expected,
  54. const char *msg, ...)
  55. {
  56. int cond = strcmp(got, expected) == 0;
  57. va_list ap;
  58. va_start(ap, msg);
  59. test_result(runner, cond, msg, ap);
  60. va_end(ap);
  61. if (!cond) {
  62. fprintf(stderr, " Got: \"%s\"\n", got);
  63. fprintf(stderr, " Expected: \"%s\"\n", expected);
  64. }
  65. }
  66. int
  67. test_ok(test_batch_runner *runner)
  68. {
  69. return runner->num_failed == 0;
  70. }
  71. void
  72. test_print_summary(test_batch_runner *runner)
  73. {
  74. int num_passed = runner->num_passed;
  75. int num_skipped = runner->num_skipped;
  76. int num_failed = runner->num_failed;
  77. fprintf(stderr, "%d tests passed, %d failed, %d skipped\n",
  78. num_passed, num_skipped, num_failed);
  79. if (test_ok(runner)) {
  80. fprintf(stderr, "PASS\n");
  81. }
  82. else {
  83. fprintf(stderr, "FAIL\n");
  84. }
  85. }