Wed Feb 24 15:47:16 CET 2010 Paolo Molaro <lupus@ximian.com>
[mono.git] / eglib / test / driver.c
1 /*
2  * EGLib Unit Test Driver
3  *
4  * Author:
5  *   Aaron Bockover (abockover@novell.com)
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
29 #include <config.h>
30 #include "test.h"
31 #include "tests.h"
32
33 #include <stdio.h>
34 #ifdef HAVE_GETOPT_H
35 #include <getopt.h>
36 #endif
37
38 #if defined(HAVE_UNISTD_H)
39 #include <unistd.h>
40 #endif
41
42 #ifndef DRIVER_NAME
43 #define DRIVER_NAME "EGlib"
44 #endif
45
46 typedef struct _StringArray {
47         gchar **strings;
48         gint length;
49 } StringArray;
50
51 static StringArray *
52 string_array_append(StringArray *array, gchar *string)
53 {
54         if(array == NULL) {
55                 array = g_new0(StringArray, 1);
56                 array->length = 1;
57                 array->strings = g_malloc(sizeof(gchar *) * 2);
58         } else {
59                 array->length++;
60                 array->strings = g_realloc(array->strings, sizeof(gchar *) 
61                         * (array->length + 1));
62         }
63         
64         array->strings[array->length - 1] = string;
65         array->strings[array->length] = NULL;
66
67         return array;
68 }
69
70 gint global_passed = 0, global_tests = 0;
71
72 static void
73 string_array_free(StringArray *array)
74 {
75         g_free(array->strings);
76         g_free(array);
77 }
78
79 static void print_help(char *s)
80 {
81         gint i;
82         
83         printf("Usage: %s [OPTION]... [TESTGROUP]...\n\n", s);
84         printf("OPTIONS are:\n");
85         printf("  -h, --help          show this help\n");
86         printf("  -t, --time          time the tests\n");
87         printf("  -i, --iterations    number of times to run tests\n");
88         printf("  -q, --quiet         do not print test results; "
89                 "final time always prints\n");
90         printf("  -n, --no-labels     print final time without labels, "
91                 "nice for scripts\n");
92         printf("  -d, --debug         do not run tests, "
93                 "debug the driver itself for valgrind\n\n");
94         printf("TESTGROUPS available:\n");
95
96         for(i = 0; test_groups[i].name != NULL; i++) {
97                 if(test_groups[i].handler != fake_tests_init) {
98                         printf("  %s\n", test_groups[i].name);
99                 }
100         }
101
102         printf("\n");
103 }
104
105 gint main(gint argc, gchar **argv)
106 {
107         gint i, j, c, iterations = 1;
108         StringArray *tests_to_run = NULL;
109         gdouble time_start;
110         gboolean report_time = FALSE;
111         gboolean quiet = FALSE;
112         gboolean global_failure = FALSE;
113         gboolean no_final_time_labels = FALSE;
114         gboolean debug = FALSE;
115
116 #if HAVE_GETOPT_H
117         static struct option long_options [] = {
118                 {"help",       no_argument,       0, 'h'},
119                 {"time",       no_argument,       0, 't'},
120                 {"quiet",      no_argument,       0, 'q'},
121                 {"iterations", required_argument, 0, 'i'},
122                 {"debug",      no_argument,       0, 'd'},
123                 {"no-labels",  no_argument,       0, 'n'},
124                 {0, 0, 0, 0}
125         };
126
127         while((c = getopt_long(argc, argv, "dhtqni:", long_options, NULL)) != -1) {                     switch(c) {
128                         case 'h':
129                                 print_help(argv[0]);
130                                 return 1;
131                         case 't':
132                                 report_time = TRUE;
133                                 break;
134                         case 'i':
135                                 iterations = atoi(optarg);
136                                 break;
137                         case 'q':
138                                 quiet = TRUE;
139                                 break;
140                         case 'n':
141                                 no_final_time_labels = TRUE;
142                                 break;
143                         case 'd':
144                                 debug = TRUE;
145                                 break;
146                 }
147         }
148
149         for(i = optind; i < argc; i++) {
150                 if(argv[i][0] == '-') {
151                         continue;
152                 }
153
154                 tests_to_run = string_array_append(tests_to_run, argv[i]);
155         }
156 #endif
157
158         time_start = get_timestamp();
159         
160         for(j = 0; test_groups[j].name != NULL; j++) {
161                 gboolean run = TRUE;
162                 gchar *tests = NULL;
163                 gchar *group = NULL;
164                 
165                 if(tests_to_run != NULL) {
166                         gint k;
167                         run = FALSE;
168                         
169                         for(k = 0; k < tests_to_run->length; k++) {     
170                                 gchar *user = tests_to_run->strings[k];
171                                 const gchar *table = test_groups[j].name;
172                                 size_t user_len = strlen(user);
173                                 size_t table_len = strlen(table);
174                                 
175                                 if(strncmp(user, table, table_len) == 0) {
176                                         if(user_len > table_len && user[table_len] != ':') {
177                                                 break;
178                                         }
179                                         
180                                         run = TRUE;
181                                         group = tests_to_run->strings[k];
182                                         break;
183                                 }
184                         }
185                 }
186         
187                 if(run) {
188                         gboolean passed;
189                         gchar **split = NULL;
190                         
191                         if(debug && test_groups[j].handler != fake_tests_init) {
192                                 printf("Skipping %s, in driver debug mode\n", 
193                                         test_groups[j].name);
194                                 continue;
195                         } else if(!debug && test_groups[j].handler == fake_tests_init) {
196                                 continue;
197                         }
198
199                         if(group != NULL) {
200                                 split = eg_strsplit(group, ":", -1);    
201                                 if(split != NULL) {
202                                         gint m;
203                                         for(m = 0; split[m] != NULL; m++) {
204                                                 if(m == 1) {
205                                                         tests = strdup(split[m]);
206                                                         break;
207                                                 }
208                                         }
209                                         eg_strfreev(split);
210                                 }
211                         }
212                         
213                         passed = run_group(&(test_groups[j]), 
214                                 iterations, quiet, report_time, tests);
215
216                         if(tests != NULL) {
217                                 g_free(tests);
218                         }
219
220                         if(!passed && !global_failure) {
221                                 global_failure = TRUE;
222                         }
223                 }
224         }
225         
226         if(!quiet) {
227                 gdouble pass_percentage = ((gdouble)global_passed / (gdouble)global_tests) * 100.0;
228                 printf("=============================\n");
229                 printf("Overall result: %s : %d / %d (%g%%)\n", global_failure ? "FAILED" : "OK", global_passed, global_tests, pass_percentage);
230         }
231         
232         if(report_time) {
233                 gdouble duration = get_timestamp() - time_start;
234                 if(no_final_time_labels) {
235                         printf("%g\n", duration);
236                 } else {
237                         printf("%s Total Time: %g\n", DRIVER_NAME, duration);
238                 }
239         }
240
241         if(tests_to_run != NULL) {
242                 string_array_free(tests_to_run);
243         }
244
245         return global_tests - global_passed;
246 }
247
248