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