2
0

abts.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  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 = malloc(sizeof(*tc));
  145. tc->failed = 0;
  146. tc->suite = ss;
  147. ss->num_test++;
  148. update_status();
  149. f(tc, value);
  150. if (tc->failed) {
  151. ss->failed++;
  152. }
  153. free(tc);
  154. }
  155. static int report(abts_suite *suite)
  156. {
  157. int count = 0;
  158. sub_suite *dptr;
  159. if (suite && suite->tail &&!suite->tail->not_run) {
  160. end_suite(suite);
  161. }
  162. for (dptr = suite->head; dptr; dptr = dptr->next) {
  163. count += dptr->failed;
  164. }
  165. if (list_tests) {
  166. return 0;
  167. }
  168. if (count == 0) {
  169. printf("All tests passed.\n");
  170. return 0;
  171. }
  172. dptr = suite->head;
  173. fprintf(stdout, "%-15s\t\tTotal\tFail\tFailed %%\n", "Failed Tests");
  174. fprintf(stdout, "===================================================\n");
  175. while (dptr != NULL) {
  176. if (dptr->failed != 0) {
  177. float percent = ((float)dptr->failed / (float)dptr->num_test);
  178. fprintf(stdout, "%-15s\t\t%5d\t%4d\t%6.2f%%\n", dptr->name,
  179. dptr->num_test, dptr->failed, percent * 100);
  180. }
  181. dptr = dptr->next;
  182. }
  183. return 1;
  184. }
  185. void abts_log_message(const char *fmt, ...)
  186. {
  187. va_list args;
  188. update_status();
  189. if (verbose) {
  190. va_start(args, fmt);
  191. vfprintf(stderr, fmt, args);
  192. va_end(args);
  193. fprintf(stderr, "\n");
  194. fflush(stderr);
  195. }
  196. }
  197. void abts_int_equal(abts_case *tc, const int expected, const int actual, int lineno)
  198. {
  199. update_status();
  200. if (tc->failed) return;
  201. if (expected == actual) return;
  202. tc->failed = TRUE;
  203. if (verbose) {
  204. fprintf(stderr, "Line %d: expected <%d>, but saw <%d>\n", lineno, expected, actual);
  205. fflush(stderr);
  206. }
  207. }
  208. void abts_int_nequal(abts_case *tc, const int expected, const int actual, int lineno)
  209. {
  210. update_status();
  211. if (tc->failed) return;
  212. if (expected != actual) return;
  213. tc->failed = TRUE;
  214. if (verbose) {
  215. fprintf(stderr, "Line %d: expected <%d>, but saw <%d>\n", lineno, expected, actual);
  216. fflush(stderr);
  217. }
  218. }
  219. void abts_str_equal(abts_case *tc, const char *expected, const char *actual, int lineno)
  220. {
  221. update_status();
  222. if (tc->failed) return;
  223. /* If both are NULL, match is good */
  224. if (!expected && !actual) return;
  225. if (expected && actual)
  226. if (!strcmp(expected, actual)) return;
  227. tc->failed = TRUE;
  228. if (verbose) {
  229. fprintf(stderr, "Line %d: expected <%s>, but saw <%s>\n", lineno, expected, actual);
  230. fflush(stderr);
  231. }
  232. }
  233. void abts_str_nequal(abts_case *tc, const char *expected, const char *actual,
  234. size_t n, int lineno)
  235. {
  236. update_status();
  237. if (tc->failed) return;
  238. if (!strncmp(expected, actual, n)) return;
  239. tc->failed = TRUE;
  240. if (verbose) {
  241. fprintf(stderr, "Line %d: expected <%s>, but saw <%s>\n", lineno, expected, actual);
  242. fflush(stderr);
  243. }
  244. }
  245. void abts_ptr_notnull(abts_case *tc, const void *ptr, int lineno)
  246. {
  247. update_status();
  248. if (tc->failed) return;
  249. if (ptr != NULL) return;
  250. tc->failed = TRUE;
  251. if (verbose) {
  252. fprintf(stderr, "Line %d: Expected NULL, but saw <%p>\n", lineno, ptr);
  253. fflush(stderr);
  254. }
  255. }
  256. void abts_ptr_equal(abts_case *tc, const void *expected, const void *actual, int lineno)
  257. {
  258. update_status();
  259. if (tc->failed) return;
  260. if (expected == actual) return;
  261. tc->failed = TRUE;
  262. if (verbose) {
  263. fprintf(stderr, "Line %d: expected <%p>, but saw <%p>\n", lineno, expected, actual);
  264. fflush(stderr);
  265. }
  266. }
  267. void abts_fail(abts_case *tc, const char *message, int lineno)
  268. {
  269. update_status();
  270. if (tc->failed) return;
  271. tc->failed = TRUE;
  272. if (verbose) {
  273. fprintf(stderr, "Line %d: %s\n", lineno, message);
  274. fflush(stderr);
  275. }
  276. }
  277. void abts_assert(abts_case *tc, const char *message, int condition, int lineno)
  278. {
  279. update_status();
  280. if (tc->failed) return;
  281. if (condition) return;
  282. tc->failed = TRUE;
  283. if (verbose) {
  284. fprintf(stderr, "Line %d: %s\n", lineno, message);
  285. fflush(stderr);
  286. }
  287. }
  288. void abts_true(abts_case *tc, int condition, int lineno)
  289. {
  290. update_status();
  291. if (tc->failed) return;
  292. if (condition) return;
  293. tc->failed = TRUE;
  294. if (verbose) {
  295. fprintf(stderr, "Line %d: Condition is false, but expected true\n", lineno);
  296. fflush(stderr);
  297. }
  298. }
  299. void abts_not_impl(abts_case *tc, const char *message, int lineno)
  300. {
  301. update_status();
  302. tc->suite->not_impl++;
  303. if (verbose) {
  304. fprintf(stderr, "Line %d: %s\n", lineno, message);
  305. fflush(stderr);
  306. }
  307. }
  308. int main(int argc, const char *const argv[]) {
  309. int i;
  310. int rv;
  311. int list_provided = 0;
  312. abts_suite *suite = NULL;
  313. initialize();
  314. for (i = 1; i < argc; i++) {
  315. if (!strcmp(argv[i], "-v")) {
  316. verbose = 1;
  317. continue;
  318. }
  319. if (!strcmp(argv[i], "-x")) {
  320. exclude = 1;
  321. continue;
  322. }
  323. if (!strcmp(argv[i], "-l")) {
  324. list_tests = 1;
  325. continue;
  326. }
  327. if (!strcmp(argv[i], "-q")) {
  328. quiet = 1;
  329. continue;
  330. }
  331. if (argv[i][0] == '-') {
  332. fprintf(stderr, "Invalid option: `%s'\n", argv[i]);
  333. exit(1);
  334. }
  335. list_provided = 1;
  336. }
  337. if (list_provided) {
  338. /* Waste a little space here, because it is easier than counting the
  339. * number of tests listed. Besides it is at most three char *.
  340. */
  341. testlist = calloc(argc + 1, sizeof(char *));
  342. for (i = 1; i < argc; i++) {
  343. testlist[i - 1] = argv[i];
  344. }
  345. }
  346. for (i = 0; i < (sizeof(alltests) / sizeof(struct testlist *)); i++) {
  347. suite = alltests[i].func(suite);
  348. }
  349. rv = report(suite);
  350. return rv;
  351. }