abts.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  1. /* Copyright 2000-2004 Ryan Bloom
  2. *
  3. * Licensed under the Apache License, Version 2.0 (the "License");
  4. * you may not use this file except in compliance with the License.
  5. * You may obtain a copy of the License at
  6. *
  7. * http://www.apache.org/licenses/LICENSE-2.0
  8. *
  9. * Unless required by applicable law or agreed to in writing, software
  10. * distributed under the License is distributed on an "AS IS" BASIS,
  11. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. * See the License for the specific language governing permissions and
  13. * limitations under the License.
  14. *
  15. * Portions of this file were taken from testall.c in the APR test suite,
  16. * written by members of the Apache Software Foundation.
  17. */
  18. #include "abts.h"
  19. #include "abts_tests.h"
  20. #include "testutil.h"
  21. #define ABTS_STAT_SIZE 6
  22. static char status[ABTS_STAT_SIZE] = {'|', '/', '-', '|', '\\', '-'};
  23. static int curr_char;
  24. static int verbose = 0;
  25. static int exclude = 0;
  26. static int quiet = 0;
  27. static int list_tests = 0;
  28. const char **testlist = NULL;
  29. static int find_test_name(const char *testname) {
  30. int i;
  31. for (i = 0; testlist[i] != NULL; i++) {
  32. if (!strcmp(testlist[i], testname)) {
  33. return 1;
  34. }
  35. }
  36. return 0;
  37. }
  38. /* Determine if the test should be run at all */
  39. static int should_test_run(const char *testname) {
  40. int found = 0;
  41. if (list_tests == 1) {
  42. return 0;
  43. }
  44. if (testlist == NULL) {
  45. return 1;
  46. }
  47. found = find_test_name(testname);
  48. if ((found && !exclude) || (!found && exclude)) {
  49. return 1;
  50. }
  51. return 0;
  52. }
  53. static void reset_status(void)
  54. {
  55. curr_char = 0;
  56. }
  57. static void update_status(void)
  58. {
  59. if (!quiet) {
  60. curr_char = (curr_char + 1) % ABTS_STAT_SIZE;
  61. fprintf(stdout, "\b%c", status[curr_char]);
  62. fflush(stdout);
  63. }
  64. }
  65. static void end_suite(abts_suite *suite)
  66. {
  67. if (suite != NULL) {
  68. sub_suite *last = suite->tail;
  69. if (!quiet) {
  70. fprintf(stdout, "\b");
  71. fflush(stdout);
  72. }
  73. if (last->failed == 0) {
  74. fprintf(stdout, "SUCCESS\n");
  75. fflush(stdout);
  76. }
  77. else {
  78. fprintf(stdout, "FAILED %d of %d\n", last->failed, last->num_test);
  79. fflush(stdout);
  80. }
  81. }
  82. }
  83. abts_suite *abts_add_suite(abts_suite *suite, const char *suite_name_full)
  84. {
  85. sub_suite *subsuite;
  86. char *p;
  87. const char *suite_name;
  88. curr_char = 0;
  89. /* Only end the suite if we actually ran it */
  90. if (suite && suite->tail &&!suite->tail->not_run) {
  91. end_suite(suite);
  92. }
  93. subsuite = malloc(sizeof(*subsuite));
  94. subsuite->num_test = 0;
  95. subsuite->failed = 0;
  96. subsuite->next = NULL;
  97. /* suite_name_full may be an absolute path depending on __FILE__
  98. * expansion */
  99. suite_name = strrchr(suite_name_full, '/');
  100. if (suite_name) {
  101. suite_name++;
  102. } else {
  103. suite_name = suite_name_full;
  104. }
  105. p = strrchr(suite_name, '.');
  106. if (p) {
  107. subsuite->name = memcpy(calloc(p - suite_name + 1, 1),
  108. suite_name, p - suite_name);
  109. }
  110. else {
  111. subsuite->name = suite_name;
  112. }
  113. if (list_tests) {
  114. fprintf(stdout, "%s\n", subsuite->name);
  115. }
  116. subsuite->not_run = 0;
  117. if (suite == NULL) {
  118. suite = malloc(sizeof(*suite));
  119. suite->head = subsuite;
  120. suite->tail = subsuite;
  121. }
  122. else {
  123. suite->tail->next = subsuite;
  124. suite->tail = subsuite;
  125. }
  126. if (!should_test_run(subsuite->name)) {
  127. subsuite->not_run = 1;
  128. return suite;
  129. }
  130. reset_status();
  131. fprintf(stdout, "%-20s: ", subsuite->name);
  132. update_status();
  133. fflush(stdout);
  134. return suite;
  135. }
  136. void abts_run_test(abts_suite *ts, test_func f, void *value)
  137. {
  138. abts_case tc;
  139. sub_suite *ss;
  140. if (!should_test_run(ts->tail->name)) {
  141. return;
  142. }
  143. ss = ts->tail;
  144. tc.failed = 0;
  145. tc.suite = ss;
  146. ss->num_test++;
  147. update_status();
  148. f(&tc, value);
  149. if (tc.failed) {
  150. ss->failed++;
  151. }
  152. }
  153. static int report(abts_suite *suite)
  154. {
  155. int count = 0;
  156. sub_suite *dptr;
  157. if (suite && suite->tail &&!suite->tail->not_run) {
  158. end_suite(suite);
  159. }
  160. for (dptr = suite->head; dptr; dptr = dptr->next) {
  161. count += dptr->failed;
  162. }
  163. if (list_tests) {
  164. return 0;
  165. }
  166. if (count == 0) {
  167. printf("All tests passed.\n");
  168. return 0;
  169. }
  170. dptr = suite->head;
  171. fprintf(stdout, "%-15s\t\tTotal\tFail\tFailed %%\n", "Failed Tests");
  172. fprintf(stdout, "===================================================\n");
  173. while (dptr != NULL) {
  174. if (dptr->failed != 0) {
  175. float percent = ((float)dptr->failed / (float)dptr->num_test);
  176. fprintf(stdout, "%-15s\t\t%5d\t%4d\t%6.2f%%\n", dptr->name,
  177. dptr->num_test, dptr->failed, percent * 100);
  178. }
  179. dptr = dptr->next;
  180. }
  181. return 1;
  182. }
  183. void abts_log_message(const char *fmt, ...)
  184. {
  185. va_list args;
  186. update_status();
  187. if (verbose) {
  188. va_start(args, fmt);
  189. vfprintf(stderr, fmt, args);
  190. va_end(args);
  191. fprintf(stderr, "\n");
  192. fflush(stderr);
  193. }
  194. }
  195. void abts_int_equal(abts_case *tc, const int expected, const int actual, int lineno)
  196. {
  197. update_status();
  198. if (tc->failed) return;
  199. if (expected == actual) return;
  200. tc->failed = TRUE;
  201. if (verbose) {
  202. fprintf(stderr, "Line %d: expected <%d>, but saw <%d>\n", lineno, expected, actual);
  203. fflush(stderr);
  204. }
  205. }
  206. void abts_int_nequal(abts_case *tc, const int expected, const int actual, int lineno)
  207. {
  208. update_status();
  209. if (tc->failed) return;
  210. if (expected != actual) return;
  211. tc->failed = TRUE;
  212. if (verbose) {
  213. fprintf(stderr, "Line %d: expected <%d>, but saw <%d>\n", lineno, expected, actual);
  214. fflush(stderr);
  215. }
  216. }
  217. void abts_str_equal(abts_case *tc, const char *expected, const char *actual, int lineno)
  218. {
  219. update_status();
  220. if (tc->failed) return;
  221. if (!expected && !actual) return;
  222. if (expected && actual)
  223. if (!strcmp(expected, actual)) return;
  224. tc->failed = TRUE;
  225. if (verbose) {
  226. fprintf(stderr, "Line %d: expected <%s>, but saw <%s>\n", lineno, expected, actual);
  227. fflush(stderr);
  228. }
  229. }
  230. void abts_str_nequal(abts_case *tc, const char *expected, const char *actual,
  231. size_t n, int lineno)
  232. {
  233. update_status();
  234. if (tc->failed) return;
  235. if (!strncmp(expected, actual, n)) return;
  236. tc->failed = TRUE;
  237. if (verbose) {
  238. fprintf(stderr, "Line %d: expected <%s>, but saw <%s>\n", lineno, expected, actual);
  239. fflush(stderr);
  240. }
  241. }
  242. void abts_ptr_notnull(abts_case *tc, const void *ptr, int lineno)
  243. {
  244. update_status();
  245. if (tc->failed) return;
  246. if (ptr != NULL) return;
  247. tc->failed = TRUE;
  248. if (verbose) {
  249. fprintf(stderr, "Line %d: Expected NULL, but saw <%p>\n", lineno, ptr);
  250. fflush(stderr);
  251. }
  252. }
  253. void abts_ptr_equal(abts_case *tc, const void *expected, const void *actual, int lineno)
  254. {
  255. update_status();
  256. if (tc->failed) return;
  257. if (expected == actual) return;
  258. tc->failed = TRUE;
  259. if (verbose) {
  260. fprintf(stderr, "Line %d: expected <%p>, but saw <%p>\n", lineno, expected, actual);
  261. fflush(stderr);
  262. }
  263. }
  264. void abts_fail(abts_case *tc, const char *message, int lineno)
  265. {
  266. update_status();
  267. if (tc->failed) return;
  268. tc->failed = TRUE;
  269. if (verbose) {
  270. fprintf(stderr, "Line %d: %s\n", lineno, message);
  271. fflush(stderr);
  272. }
  273. }
  274. void abts_assert(abts_case *tc, const char *message, int condition, int lineno)
  275. {
  276. update_status();
  277. if (tc->failed) return;
  278. if (condition) return;
  279. tc->failed = TRUE;
  280. if (verbose) {
  281. fprintf(stderr, "Line %d: %s\n", lineno, message);
  282. fflush(stderr);
  283. }
  284. }
  285. void abts_true(abts_case *tc, int condition, int lineno)
  286. {
  287. update_status();
  288. if (tc->failed) return;
  289. if (condition) return;
  290. tc->failed = TRUE;
  291. if (verbose) {
  292. fprintf(stderr, "Line %d: Condition is false, but expected true\n", lineno);
  293. fflush(stderr);
  294. }
  295. }
  296. void abts_not_impl(abts_case *tc, const char *message, int lineno)
  297. {
  298. update_status();
  299. tc->suite->not_impl++;
  300. if (verbose) {
  301. fprintf(stderr, "Line %d: %s\n", lineno, message);
  302. fflush(stderr);
  303. }
  304. }
  305. int main(int argc, const char *const argv[]) {
  306. int i;
  307. int rv;
  308. int list_provided = 0;
  309. abts_suite *suite = NULL;
  310. initialize();
  311. for (i = 1; i < argc; i++) {
  312. if (!strcmp(argv[i], "-v")) {
  313. verbose = 1;
  314. continue;
  315. }
  316. if (!strcmp(argv[i], "-x")) {
  317. exclude = 1;
  318. continue;
  319. }
  320. if (!strcmp(argv[i], "-l")) {
  321. list_tests = 1;
  322. continue;
  323. }
  324. if (!strcmp(argv[i], "-q")) {
  325. quiet = 1;
  326. continue;
  327. }
  328. if (argv[i][0] == '-') {
  329. fprintf(stderr, "Invalid option: `%s'\n", argv[i]);
  330. exit(1);
  331. }
  332. list_provided = 1;
  333. }
  334. if (list_provided) {
  335. /* Waste a little space here, because it is easier than counting the
  336. * number of tests listed. Besides it is at most three char *.
  337. */
  338. testlist = calloc(argc + 1, sizeof(char *));
  339. for (i = 1; i < argc; i++) {
  340. testlist[i - 1] = argv[i];
  341. }
  342. }
  343. for (i = 0; i < (sizeof(alltests) / sizeof(struct testlist *)); i++) {
  344. suite = alltests[i].func(suite);
  345. }
  346. rv = report(suite);
  347. return rv;
  348. }