driver.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. /*
  2. * EGLib Unit Test Driver
  3. *
  4. * Author:
  5. * Aaron Bockover ([email protected])
  6. *
  7. * (C) 2006 Novell, Inc.
  8. *
  9. * Permission is hereby granted, free of charge, to any person obtaining
  10. * a copy of this software and associated documentation files (the
  11. * "Software"), to deal in the Software without restriction, including
  12. * without limitation the rights to use, copy, modify, merge, publish,
  13. * distribute, sublicense, and/or sell copies of the Software, and to
  14. * permit persons to whom the Software is furnished to do so, subject to
  15. * the following conditions:
  16. *
  17. * The above copyright notice and this permission notice shall be
  18. * included in all copies or substantial portions of the Software.
  19. *
  20. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  21. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  22. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  23. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  24. * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  25. * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  26. * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  27. */
  28. #include <config.h>
  29. #include "test.h"
  30. #include "tests.h"
  31. #include <stdio.h>
  32. #include <getopt.h>
  33. #if defined(HAVE_UNISTD_H)
  34. #include <unistd.h>
  35. #endif
  36. #ifndef DRIVER_NAME
  37. #define DRIVER_NAME "EGlib"
  38. #endif
  39. typedef struct _StringArray {
  40. gchar **strings;
  41. gint length;
  42. } StringArray;
  43. static StringArray *
  44. string_array_append(StringArray *array, gchar *string)
  45. {
  46. if(array == NULL) {
  47. array = g_new0(StringArray, 1);
  48. array->length = 1;
  49. array->strings = g_malloc(sizeof(gchar *) * 2);
  50. } else {
  51. array->length++;
  52. array->strings = g_realloc(array->strings, sizeof(gchar *)
  53. * (array->length + 1));
  54. }
  55. array->strings[array->length - 1] = string;
  56. array->strings[array->length] = NULL;
  57. return array;
  58. }
  59. gint global_passed = 0, global_tests = 0;
  60. static void
  61. string_array_free(StringArray *array)
  62. {
  63. g_free(array->strings);
  64. g_free(array);
  65. }
  66. static void print_help(char *s)
  67. {
  68. gint i;
  69. printf("Usage: %s [OPTION]... [TESTGROUP]...\n\n", s);
  70. printf("OPTIONS are:\n");
  71. printf(" -h, --help show this help\n");
  72. printf(" -t, --time time the tests\n");
  73. printf(" -i, --iterations number of times to run tests\n");
  74. printf(" -q, --quiet do not print test results; "
  75. "final time always prints\n");
  76. printf(" -n, --no-labels print final time without labels, "
  77. "nice for scripts\n");
  78. printf(" -d, --debug do not run tests, "
  79. "debug the driver itself for valgrind\n\n");
  80. printf("TESTGROUPS available:\n");
  81. for(i = 0; test_groups[i].name != NULL; i++) {
  82. if(test_groups[i].handler != fake_tests_init) {
  83. printf(" %s\n", test_groups[i].name);
  84. }
  85. }
  86. printf("\n");
  87. }
  88. gint main(gint argc, gchar **argv)
  89. {
  90. gint i, j, c, iterations = 1;
  91. StringArray *tests_to_run = NULL;
  92. gdouble time_start;
  93. gboolean report_time = FALSE;
  94. gboolean quiet = FALSE;
  95. gboolean global_failure = FALSE;
  96. gboolean no_final_time_labels = FALSE;
  97. gboolean debug = FALSE;
  98. static struct option long_options [] = {
  99. {"help", no_argument, 0, 'h'},
  100. {"time", no_argument, 0, 't'},
  101. {"quiet", no_argument, 0, 'q'},
  102. {"iterations", required_argument, 0, 'i'},
  103. {"debug", no_argument, 0, 'd'},
  104. {"no-labels", no_argument, 0, 'n'},
  105. {0, 0, 0, 0}
  106. };
  107. while((c = getopt_long(argc, argv, "dhtqni:", long_options, NULL)) != -1) { switch(c) {
  108. case 'h':
  109. print_help(argv[0]);
  110. return 1;
  111. case 't':
  112. report_time = TRUE;
  113. break;
  114. case 'i':
  115. iterations = atoi(optarg);
  116. break;
  117. case 'q':
  118. quiet = TRUE;
  119. break;
  120. case 'n':
  121. no_final_time_labels = TRUE;
  122. break;
  123. case 'd':
  124. debug = TRUE;
  125. break;
  126. }
  127. }
  128. for(i = optind; i < argc; i++) {
  129. if(argv[i][0] == '-') {
  130. continue;
  131. }
  132. tests_to_run = string_array_append(tests_to_run, argv[i]);
  133. }
  134. time_start = get_timestamp();
  135. for(j = 0; test_groups[j].name != NULL; j++) {
  136. gboolean run = TRUE;
  137. gchar *tests = NULL;
  138. gchar *group = NULL;
  139. if(tests_to_run != NULL) {
  140. gint k;
  141. run = FALSE;
  142. for(k = 0; k < tests_to_run->length; k++) {
  143. gchar *user = tests_to_run->strings[k];
  144. const gchar *table = test_groups[j].name;
  145. size_t user_len = strlen(user);
  146. size_t table_len = strlen(table);
  147. if(strncmp(user, table, table_len) == 0) {
  148. if(user_len > table_len && user[table_len] != ':') {
  149. break;
  150. }
  151. run = TRUE;
  152. group = tests_to_run->strings[k];
  153. break;
  154. }
  155. }
  156. }
  157. if(run) {
  158. gboolean passed;
  159. gchar **split = NULL;
  160. if(debug && test_groups[j].handler != fake_tests_init) {
  161. printf("Skipping %s, in driver debug mode\n",
  162. test_groups[j].name);
  163. continue;
  164. } else if(!debug && test_groups[j].handler == fake_tests_init) {
  165. continue;
  166. }
  167. if(group != NULL) {
  168. split = eg_strsplit(group, ":", -1);
  169. if(split != NULL) {
  170. gint m;
  171. for(m = 0; split[m] != NULL; m++) {
  172. if(m == 1) {
  173. tests = strdup(split[m]);
  174. break;
  175. }
  176. }
  177. eg_strfreev(split);
  178. }
  179. }
  180. passed = run_group(&(test_groups[j]),
  181. iterations, quiet, report_time, tests);
  182. if(tests != NULL) {
  183. g_free(tests);
  184. }
  185. if(!passed && !global_failure) {
  186. global_failure = TRUE;
  187. }
  188. }
  189. }
  190. if(!quiet) {
  191. gdouble pass_percentage = ((gdouble)global_passed / (gdouble)global_tests) * 100.0;
  192. printf("=============================\n");
  193. printf("Overall result: %s : %d / %d (%g%%)\n", global_failure ? "FAILED" : "OK", global_passed, global_tests, pass_percentage);
  194. }
  195. if(report_time) {
  196. gdouble duration = get_timestamp() - time_start;
  197. if(no_final_time_labels) {
  198. printf("%g\n", duration);
  199. } else {
  200. printf("%s Total Time: %g\n", DRIVER_NAME, duration);
  201. }
  202. }
  203. if(tests_to_run != NULL) {
  204. string_array_free(tests_to_run);
  205. }
  206. return global_tests - global_passed;
  207. }