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