dea1cd21d3a3882355c64a6c8573ce2b69c48b2e
[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 <stddef.h>
32 #include <sys/time.h>
33 #include <getopt.h>
34
35 #include "test.h"
36 #include "tests.h"
37
38 static void print_help(char *s)
39 {
40         gint i;
41         
42         printf("Usage: %s [options] [test1 test2 ... testN]\n\n", s);
43         printf(" options are:\n");
44         printf("   --help|-h          show this help\n");
45         printf("   --time|-t          time the tests\n");
46         printf("   --iterations|-i    number of times to run tests\n");
47         printf("   --quiet|-q         do not print test results; if -t\n");
48         printf("                      is passed, the time will print\n\n");
49         printf(" test1..testN  name of test to run (all run by default)\n\n");
50         printf(" available tests:\n");
51
52         for(i = 0; test_groups[i].name != NULL; i++) {
53                 printf("   %s\n", test_groups[i].name);
54         }
55
56         printf("\n");
57 }
58
59 gint main(gint argc, gchar **argv)
60 {
61         gint i, j, c, iterations = 1;
62         GList *tests_to_run = NULL;
63         double time_start, time_end;
64         struct timeval tp;
65         gboolean report_time = FALSE;
66         gboolean quiet = FALSE;
67         
68         static struct option long_options [] = {
69                 {"help", no_argument, 0, 'h'},
70                 {"time", no_argument, 0, 't'},
71                 {"iterations", required_argument, 0, 'i'},
72                 {0, 0, 0, 0}
73         };
74
75         while((c = getopt_long(argc, argv, "htqi:", long_options, NULL)) != -1) {                       switch(c) {
76                         case 'h':
77                                 print_help(argv[0]);
78                                 return 1;
79                         case 't':
80                                 report_time = TRUE;
81                                 break;
82                         case 'i':
83                                 iterations = atoi(optarg);
84                                 break;
85                         case 'q':
86                                 quiet = TRUE;
87                                 break;
88                 }
89         }
90
91         for(i = optind; i < argc; i++) {
92                 if(argv[i][0] == '-') {
93                         continue;
94                 }
95
96                 tests_to_run = g_list_append(tests_to_run, argv[i]);
97         }
98
99         gettimeofday(&tp, NULL);
100         time_start = (double)tp.tv_sec + (1.e-6) * tp.tv_usec;
101
102         for(i = 0; i < iterations; i++) {
103                 for(j = 0; test_groups[j].name != NULL; j++) {
104                         gboolean run = TRUE;
105                         
106                         if(tests_to_run != NULL) {
107                                 gint k, n;
108                                 run = FALSE;
109                                 for(k = 0, n = g_list_length(tests_to_run); k < n; k++) {
110                                         if(strcmp((char *)g_list_nth_data(tests_to_run, k), 
111                                                 test_groups[j].name) == 0) {
112                                                 run = TRUE;
113                                                 break;
114                                         }
115                                 }
116                         }
117                         
118                         if(run) {
119                                 gint total, passed;
120                                 run_group(&(test_groups[j]), &total, &passed, quiet);
121                                 if(!quiet) {
122                                         printf("  -- %d / %d (%g%%) --\n", passed, total,
123                                                 ((gdouble)passed / (gdouble)total) * 100.0);
124                                 }
125                         }
126                 }
127         }
128         
129         gettimeofday(&tp, NULL);
130         time_end = (double)tp.tv_sec + (1.e-6) * tp.tv_usec;
131         
132         if(report_time) {
133                 gdouble duration = time_end - time_start;
134                 printf("Total Time: %gs\n", duration);
135         }
136
137         if(tests_to_run != NULL) {
138                 g_list_free(tests_to_run);
139         }
140
141         return 0;
142 }
143