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