testutils.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. /*
  2. * Title : testutils
  3. * Author : Tim Brecht
  4. * Date : Thu Dec 23 21:19:00 EST 2004
  5. * Modified : Wed 1 Jan 2014 14:17:19 EST
  6. * Added TEST_EQUAL_ONE_OF
  7. * Fri Jun 28 22:37:24 EDT 2013 for use with OS161
  8. *
  9. * Some simple utilities that can be helpful for testing.
  10. * Feel free to add/modify this code to suit your needs.
  11. * NOTE: this hasn't been fully tested.
  12. *
  13. * See test-testutil, file1 and file2 for example use cases.
  14. */
  15. #include <stdio.h>
  16. #include "testutils.h"
  17. /* Used to track the number of tests and failures */
  18. static int test_num = 0;
  19. static int num_failures = 0;
  20. static int verbose = 0;
  21. /* Use to print the source file, function and line */
  22. static void
  23. print_location(const char *file, const char *func, int line, const char *in_test)
  24. {
  25. printf(" %s : function = %s, line = %d test was %s\n", file, func, line, in_test);
  26. }
  27. /* Check if val is equal to expected_val, if not it is an error */
  28. void
  29. test_equal(int val, int expected_val, const char *str,
  30. const char *file, const char *func, int line)
  31. {
  32. int failed = 0;
  33. char const *out = "SUCCESS";
  34. test_num++;
  35. if (val != expected_val) {
  36. num_failures++;
  37. failed = 1;
  38. out = "FAILURE";
  39. }
  40. if (failed || verbose) {
  41. printf("%s ON TEST = %d : Got %d : expected %d\n",
  42. out, test_num, val, expected_val);
  43. if (failed) {
  44. printf(" %s\n", str);
  45. }
  46. print_location(file, func, line, __FUNCTION__);
  47. }
  48. } /* test_equal */
  49. /* Check if val is equal to expected_val, if not it is an error */
  50. void
  51. test_equal_one_of(int val, int expected_val1, int expected_val2, const char *str,
  52. const char *file, const char *func, int line)
  53. {
  54. int failed = 0;
  55. char const *out = "SUCCESS";
  56. test_num++;
  57. if (val != expected_val1 && val != expected_val2) {
  58. num_failures++;
  59. failed = 1;
  60. out = "FAILURE";
  61. }
  62. if (failed || verbose) {
  63. printf("%s ON TEST = %d : Got %d : expected one of %d or %d\n",
  64. out, test_num, val, expected_val1, expected_val2);
  65. if (failed) {
  66. printf(" %s\n", str);
  67. }
  68. print_location(file, func, line, __FUNCTION__);
  69. }
  70. } /* test_equal */
  71. /* Check if val is > 0, if not it is an error */
  72. void
  73. test_positive(int val, const char *str,
  74. const char *file, const char *func, int line)
  75. {
  76. int failed = 0;
  77. char const *out = "SUCCESS";
  78. test_num++;
  79. if (val <= 0) {
  80. num_failures++;
  81. failed = 1;
  82. out = "FAILURE";
  83. }
  84. if (failed || verbose) {
  85. printf("%s ON TEST = %d : Got %d : expected Positive Value\n",
  86. out, test_num, val);
  87. if (failed) {
  88. printf(" %s\n", str);
  89. }
  90. print_location(file, func, line, __FUNCTION__);
  91. }
  92. }
  93. /* Check if val is < 0, if not it is an error */
  94. void
  95. test_negative(int val, const char *str,
  96. const char *file, const char *func, int line)
  97. {
  98. int failed = 0;
  99. char const *out = "SUCCESS";
  100. test_num++;
  101. if (val >= 0) {
  102. num_failures++;
  103. failed = 1;
  104. out = "FAILURE";
  105. }
  106. if (failed || verbose) {
  107. printf("%s ON TEST = %d : Got %d : expected Negative Value\n",
  108. out, test_num, val);
  109. if (failed) {
  110. printf(" %s\n", str);
  111. }
  112. print_location(file, func, line, __FUNCTION__);
  113. }
  114. }
  115. /* Check if val1 is not equal to val2, if not it is an error */
  116. void
  117. test_not_equal(int val1, int val2, const char *str,
  118. const char *file, const char *func, int line)
  119. {
  120. int failed = 0;
  121. char const *out = "SUCCESS";
  122. test_num++;
  123. if (val1 == val2) {
  124. num_failures++;
  125. failed = 1;
  126. out = "FAILURE";
  127. }
  128. if (failed || verbose) {
  129. printf("%s ON TEST = %d : Got %d : Expected anything but %d\n",
  130. out, test_num, val1, val2);
  131. if (failed) {
  132. printf(" %s\n", str);
  133. }
  134. print_location(file, func, line, __FUNCTION__);
  135. }
  136. }
  137. /* Reset the statistic counters */
  138. void
  139. test_reset_stats(void)
  140. {
  141. num_failures = 0;
  142. test_num = 0;
  143. }
  144. void
  145. test_verbose_on(void)
  146. {
  147. printf("TEST VERBOSE ON\n");
  148. verbose = 1;
  149. }
  150. void
  151. test_verbose_off(void)
  152. {
  153. printf("TEST VERBOSE OFF\n");
  154. verbose = 0;
  155. }
  156. void
  157. test_print_stats(const char *file, const char* func, int line)
  158. {
  159. printf("TEST STATS for %s : from function = %s, line = %d\n",
  160. file, func, line);
  161. printf(" Number of failures = %d Number of successes = %d Number of Tests = %d\n",
  162. num_failures, test_num - num_failures, test_num);
  163. printf("\n");
  164. }
  165. // #define UNIT_TEST
  166. #ifdef UNIT_TEST
  167. int
  168. main()
  169. {
  170. TEST_VERBOSE_ON();
  171. TEST_EQUAL(1, 1, "Should pass\n");
  172. TEST_EQUAL(1, 2, "Should fail\n");
  173. TEST_EQUAL_ONE_OF(1, 1, 2, "Should pass\n");
  174. TEST_EQUAL_ONE_OF(1, 2, 1, "Should pass\n");
  175. TEST_EQUAL_ONE_OF(1, 2, 3, "Should fail\n");
  176. TEST_NOT_EQUAL(1, 2, "Should pass\n");
  177. TEST_NOT_EQUAL(2, 1, "Should pass\n");
  178. TEST_NOT_EQUAL(2, 2, "Should fail\n");
  179. TEST_NEGATIVE(-1, "Should pass\n");
  180. TEST_NEGATIVE(0, "Should fail\n");
  181. TEST_NEGATIVE(1, "Should fail\n");
  182. TEST_POSITIVE(1, "Should pass\n");
  183. TEST_POSITIVE(0, "Should fail\n");
  184. TEST_POSITIVE(-1, "Should fail\n");
  185. TEST_STATS();
  186. printf("Should have 7 passes and 7 failures\n");
  187. }
  188. #endif /* UNIT_TEST */