| 1 |
#include "run_tests.h" |
| 2 |
#include "util.h" |
| 3 |
|
| 4 |
#include <stdlib.h> |
| 5 |
#include <stdio.h> |
| 6 |
#include <string.h> |
| 7 |
|
| 8 |
/* helper routines */ |
| 9 |
Boolean test_str_equality(int verbosity, const char *str1, const char *str2) |
| 10 |
{ |
| 11 |
int res = strcmp(str1, str2); |
| 12 |
if (res != 0) { |
| 13 |
if (verbosity) { |
| 14 |
ERROR((stderr, "|%s| != |%s|\n", str1, str2)); |
| 15 |
} |
| 16 |
return False; |
| 17 |
} |
| 18 |
if (verbosity) { |
| 19 |
INFO((stderr, "|%s| == |%s|\n", str1, str2)); |
| 20 |
} |
| 21 |
return True; |
| 22 |
} |
| 23 |
|
| 24 |
Boolean |
| 25 |
test_str_list_equality(int verbosity, char **str_list1, char **str_list2) |
| 26 |
{ |
| 27 |
size_t i; |
| 28 |
for (i = 0; str_list1[i] != NULL || str_list2[i] != NULL; i++) |
| 29 |
{ |
| 30 |
int res; |
| 31 |
|
| 32 |
if (str_list1[i] == NULL || str_list2[i] == NULL) { |
| 33 |
if (verbosity) { |
| 34 |
ERROR((stderr, "Items %ld differ: |%s| != |%s|\n", (long) i, str_list1[i], str_list2[i])); |
| 35 |
} |
| 36 |
return False; |
| 37 |
} |
| 38 |
|
| 39 |
res = strcmp(str_list1[i], str_list2[i]); |
| 40 |
if (res != 0) { |
| 41 |
if (verbosity) { |
| 42 |
ERROR((stderr, "Items %ld differ: |%s| != |%s|\n", (long) i, str_list1[i], str_list2[i])); |
| 43 |
} |
| 44 |
return False; |
| 45 |
} |
| 46 |
else if (verbosity) { |
| 47 |
INFO((stderr, "Items %ld equal: |%s| == |%s|\n", (long) i, str_list1[i], str_list2[i])); |
| 48 |
} |
| 49 |
} |
| 50 |
return True; |
| 51 |
} |
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
struct test_proc { |
| 56 |
const char *name; |
| 57 |
testProcT proc; |
| 58 |
}; |
| 59 |
|
| 60 |
static size_t m_test_proc_size = 0; |
| 61 |
static struct test_proc *m_test_proc_list = NULL; |
| 62 |
|
| 63 |
void register_test(testProcT proc, const char *name) |
| 64 |
{ |
| 65 |
size_t old_idx = m_test_proc_size; |
| 66 |
m_test_proc_size++; |
| 67 |
m_test_proc_list = xrealloc(m_test_proc_list, m_test_proc_size * sizeof *m_test_proc_list); |
| 68 |
m_test_proc_list[old_idx].name = xstrdup(name); |
| 69 |
m_test_proc_list[old_idx].proc = proc; |
| 70 |
} |
| 71 |
|
| 72 |
|
| 73 |
static Boolean |
| 74 |
run_all_tests(int verbosity) |
| 75 |
{ |
| 76 |
size_t i; |
| 77 |
|
| 78 |
int tests_failed = 0; |
| 79 |
int tests_ok = 0; |
| 80 |
|
| 81 |
fprintf(stdout, "\nGoing to run %ld tests ...\n", (long) m_test_proc_size); |
| 82 |
|
| 83 |
for (i = 0; i < m_test_proc_size; i++) { |
| 84 |
Boolean retval = m_test_proc_list[i].proc(verbosity); |
| 85 |
if (!retval) { |
| 86 |
tests_failed++; |
| 87 |
m_test_proc_list[i].proc(True); |
| 88 |
} |
| 89 |
else { |
| 90 |
tests_ok++; |
| 91 |
} |
| 92 |
fprintf(stdout, "%sTest %ld: %s (%s)\n", |
| 93 |
retval ? "" : "*****", |
| 94 |
(long) i + 1, |
| 95 |
retval ? "OK" : "FAILURE", |
| 96 |
m_test_proc_list[i].name); |
| 97 |
} |
| 98 |
|
| 99 |
/* fprintf(stdout, "Ran %d tests: %d OK, %d failures.\n", m_test_proc_size, tests_ok, tests_failed); */ |
| 100 |
if (tests_failed == 0) { |
| 101 |
fprintf(stdout, "Great! All tests succeeded.\n\n"); |
| 102 |
return True; |
| 103 |
} |
| 104 |
else { |
| 105 |
fprintf(stdout, "Darn, %d of %ld tests failed!\n\n", tests_failed, (long) m_test_proc_size); |
| 106 |
return False; |
| 107 |
} |
| 108 |
} |
| 109 |
|
| 110 |
int main(int argc, char *argv[]) |
| 111 |
{ |
| 112 |
int verbosity = 0; |
| 113 |
/* fprintf(stderr, "argc: %d, argv1: %s\n", argc, argv[1]); */ |
| 114 |
if (argc > 1 && strcmp(argv[1], "-v") == 0) { |
| 115 |
verbosity = 1; |
| 116 |
} |
| 117 |
|
| 118 |
register_all_from_test_dl_list(); |
| 119 |
register_all_from_test_string_utils(); |
| 120 |
register_all_from_test_string_list(); |
| 121 |
register_all_from_test_util(); |
| 122 |
|
| 123 |
if (!run_all_tests(verbosity)) |
| 124 |
return 1; |
| 125 |
else |
| 126 |
return 0; |
| 127 |
} |