test.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. #include <stdio.h>
  2. #include <stdarg.h>
  3. #include <float.h>
  4. unsigned int total_passed = 0;
  5. unsigned int total_failed = 0;
  6. static int passed;
  7. static int failed;
  8. int verbose = 0;
  9. typedef enum {
  10. DEFAULT = 39,
  11. RED = 31,
  12. GREEN = 32,
  13. YELLOW = 33,
  14. BLUE = 34,
  15. MAGENTA = 35
  16. } Color;
  17. static void set_output_color(Color c)
  18. {
  19. printf("\033[%dm", c);
  20. }
  21. static void print_result(int success, const char *format, ...)
  22. {
  23. va_list ap;
  24. Color color = GREEN;
  25. char indicator = '+';
  26. if (success) {
  27. color = GREEN;
  28. passed++;
  29. } else {
  30. color = RED;
  31. indicator = '-';
  32. failed++;
  33. }
  34. set_output_color(color);
  35. putchar(indicator);
  36. if (verbose) {
  37. putchar(' ');
  38. va_start(ap, format);
  39. vprintf(format, ap);
  40. va_end(ap);
  41. putchar(success ? '\n' : '\n');
  42. }
  43. set_output_color(DEFAULT);
  44. }
  45. void testing_header(const char *header)
  46. {
  47. printf("Testing header ");
  48. set_output_color(MAGENTA);
  49. printf("<%s>", header);
  50. set_output_color(DEFAULT);
  51. printf("\n");
  52. }
  53. void testing_end(void)
  54. {
  55. if (!verbose) {
  56. putchar('\n');
  57. }
  58. printf("%d tests passed, %d tests failed\n", passed, failed);
  59. total_passed += passed;
  60. passed = 0;
  61. total_failed += failed;
  62. failed = 0;
  63. }
  64. void testing_comment(const char *comment)
  65. {
  66. if (verbose) {
  67. printf("- %s\n", comment);
  68. }
  69. }
  70. void test_int_equals_imp(const char *expression, int result, int expected)
  71. {
  72. print_result(result == expected, "%s == %d", expression, expected);
  73. }
  74. void test_long_equals_imp(const char *expression, long result, long expected)
  75. {
  76. print_result(result == expected, "%s == %ld", expression, expected);
  77. }
  78. void test_double_imp(const char *expression, double result, double expected)
  79. {
  80. int success = (result - expected < DBL_EPSILON);
  81. print_result(success, "%s == %d", expression, (int)expected);
  82. }
  83. void test_void_imp(const char *expression)
  84. {
  85. putchar('?');
  86. if (verbose) {
  87. printf(" %s\n", expression);
  88. }
  89. }
  90. void test_bool_imp(const char *expression, int result, int expected)
  91. {
  92. int success = (result && expected) || (!result && !expected);
  93. print_result(success, "%s%s", expected ? "" : "!", expression);
  94. }
  95. void test_string_imp(const char *expression, const char *totest, const char *tocompare)
  96. {
  97. int success = 1;
  98. int i;
  99. for (i = 0; totest[i] != '\0'; i++) {
  100. if (totest[i] != tocompare[i]) {
  101. success = 0;
  102. }
  103. }
  104. if (tocompare[i] != '\0') {
  105. success = 0;
  106. }
  107. print_result(success, "%s == \"%s\"", expression, tocompare);
  108. }
  109. void test_distinct_imp(const char *arrayname, int *array, size_t nelements)
  110. {
  111. int success = 1;
  112. size_t i, j;
  113. for (i = 0; i < nelements; i++) {
  114. for (j = i + 1; j < nelements; j++) {
  115. if (array[i] == array[j]) {
  116. success = 0;
  117. }
  118. }
  119. }
  120. print_result(success, "Elements in %s are%s distinct", arrayname, success ? "" : " not");
  121. }