New test.
[mono.git] / eglib / test / test.c
1 /*
2  * EGLib Unit Group/Test Runners
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 #define _GNU_SOURCE
30 #include <stdlib.h>
31 #include <stdio.h>
32 #include <string.h>
33 #include <stdarg.h>
34 #include <sys/time.h>
35 #include <glib.h>
36
37 #include "test.h"
38
39 static gchar *last_result = NULL;
40
41 gboolean 
42 run_test(Test *test, gchar **result_out)
43 {
44         gchar *result; 
45
46         if((result = test->handler()) == NULL) {
47                 *result_out = NULL;
48                 return TRUE;
49         } else {
50                 *result_out = result;   
51                 return FALSE;
52         }
53 }
54
55 gboolean
56 run_group(Group *group, gint iterations, gboolean quiet, 
57         gboolean time, gchar *tests_to_run_s)
58 {
59         Test *tests = group->handler();
60         gint i, j, passed = 0, total = 0;
61         gdouble start_time_group, start_time_test;
62         gchar **tests_to_run = NULL;
63
64         if(!quiet) {
65                 if(iterations > 1) {
66                         printf("[%s] (%dx)\n", group->name, iterations);
67                 } else {
68                         printf("[%s]\n", group->name);
69                 }
70         }
71
72         if(tests_to_run_s != NULL) {
73                 tests_to_run = eg_strsplit(tests_to_run_s, ",", -1);
74         }
75
76         start_time_group = get_timestamp();
77
78         for(i = 0; tests[i].name != NULL; i++) {
79                 gchar *result;
80                 gboolean iter_pass, run;
81         
82                 iter_pass = FALSE;
83                 if(tests_to_run != NULL) {
84                         gint j;
85                         run = FALSE;
86                         for(j = 0; tests_to_run[j] != NULL; j++) {
87                                 if(strcmp(tests_to_run[j], tests[i].name) == 0) {
88                                         run = TRUE;
89                                         break;
90                                 }
91                         }
92                 } else {
93                         run = TRUE;
94                 }
95
96                 if(!run) {
97                         continue;
98                 }
99         
100                 total++;
101         
102                 if(!quiet) {
103                         printf("  %s: ", tests[i].name);
104                 }
105
106                 start_time_test = get_timestamp();
107                 
108                 for(j = 0; j < iterations; j++) {
109                         iter_pass = run_test(&(tests[i]), &result);
110                         if(!iter_pass) {
111                                 break;
112                         }
113                 }
114
115                 if(iter_pass) {
116                         passed++;
117                         if(!quiet) {
118                                 if(time) {
119                                         printf("OK (%g)\n", get_timestamp() - start_time_test);
120                                 } else {
121                                         printf("OK\n");
122                                 }
123                         }
124                 } else  {                       
125                         if(!quiet) {
126                                 printf("FAILED (%s)\n", result);
127                         }
128                         
129                         if(last_result == result) {
130                                 last_result = NULL;
131                                 g_free(result);
132                         }
133                 }
134         }
135
136         if(!quiet) {
137                 gdouble pass_percentage = ((gdouble)passed / (gdouble)total) * 100.0;
138                 if(time) {
139                         printf("  %d / %d (%g%%, %g)\n", passed, total,
140                                 pass_percentage, get_timestamp() - start_time_group);
141                 } else {
142                         printf("  %d / %d (%g%%)\n", passed, total, pass_percentage);
143                 }
144         }
145
146         if(tests_to_run != NULL) {
147                 eg_strfreev(tests_to_run);
148         }
149
150         return passed == total;
151 }
152
153 RESULT
154 FAILED(const gchar *format, ...)
155 {
156         gchar *ret;
157         va_list args;
158         gint n;
159
160         va_start(args, format);
161         n = vasprintf(&ret, format, args);
162         va_end(args);
163
164         if(n == -1) {
165                 last_result = NULL;
166                 return NULL;
167         }
168
169         last_result = ret;
170         return ret;
171 }
172
173 gdouble
174 get_timestamp()
175 {
176         struct timeval tp;
177         gettimeofday(&tp, NULL);
178         return (gdouble)tp.tv_sec + (1.e-6) * tp.tv_usec;
179 }
180
181 /* 
182  * Duplicating code here from EGlib to avoid g_strsplit skew between
183  * EGLib and GLib
184  */
185  
186 gchar ** 
187 eg_strsplit (const gchar *string, const gchar *delimiter, gint max_tokens)
188 {
189         gchar *string_c;
190         gchar *strtok_save, **vector;
191         gchar *token, *token_c;
192         gint size = 1;
193         gint token_length;
194
195         g_return_val_if_fail(string != NULL, NULL);
196         g_return_val_if_fail(delimiter != NULL, NULL);
197         g_return_val_if_fail(delimiter[0] != 0, NULL);
198         
199         token_length = strlen(string);
200         string_c = (gchar *)g_malloc(token_length + 1);
201         memcpy(string_c, string, token_length);
202         string_c[token_length] = 0;
203         
204         vector = NULL;
205         token = (gchar *)strtok_r(string_c, delimiter, &strtok_save);
206
207         while(token != NULL) {
208                 token_length = strlen(token);
209                 token_c = (gchar *)g_malloc(token_length + 1);
210                 memcpy(token_c, token, token_length);
211                 token_c[token_length] = 0;
212
213                 vector = vector == NULL ? 
214                         (gchar **)g_malloc(2 * sizeof(vector)) :
215                         (gchar **)g_realloc(vector, (size + 1) * sizeof(vector));
216         
217                 vector[size - 1] = token_c;     
218                 size++;
219
220                 if(max_tokens > 0 && size >= max_tokens) {
221                         if(size > max_tokens) {
222                                 break;
223                         }
224
225                         token = strtok_save;
226                 } else {
227                         token = (gchar *)strtok_r(NULL, delimiter, &strtok_save);
228                 }
229         }
230
231         if(vector != NULL && size > 0) {
232                 vector[size - 1] = NULL;
233         }
234         
235         g_free(string_c);
236         string_c = NULL;
237
238         return vector;
239 }
240
241 void
242 eg_strfreev (gchar **str_array)
243 {
244         gchar **orig = str_array;
245         if (str_array == NULL)
246                 return;
247         while (*str_array != NULL){
248                 g_free (*str_array);
249                 str_array++;
250         }
251         g_free (orig);
252 }
253
254