1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
|
/*
* EGLib Unit Test Driver
*
* Author:
* Aaron Bockover (abockover@novell.com)
*
* (C) 2006 Novell, Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include <config.h>
#include "test.h"
#include "tests.h"
#include <stdio.h>
#ifdef HAVE_GETOPT_H
#include <getopt.h>
#endif
#if defined(HAVE_UNISTD_H)
#include <unistd.h>
#endif
#ifndef DRIVER_NAME
#define DRIVER_NAME "EGlib"
#endif
typedef struct _StringArray {
gchar **strings;
gint length;
} StringArray;
static StringArray *
string_array_append(StringArray *array, gchar *string)
{
if(array == NULL) {
array = g_new0(StringArray, 1);
array->length = 1;
array->strings = g_malloc(sizeof(gchar *) * 2);
} else {
array->length++;
array->strings = g_realloc(array->strings, sizeof(gchar *)
* (array->length + 1));
}
array->strings[array->length - 1] = string;
array->strings[array->length] = NULL;
return array;
}
gint global_passed = 0, global_tests = 0;
static void
string_array_free(StringArray *array)
{
g_free(array->strings);
g_free(array);
}
static void print_help(char *s)
{
gint i;
printf("Usage: %s [OPTION]... [TESTGROUP]...\n\n", s);
printf("OPTIONS are:\n");
printf(" -h, --help show this help\n");
printf(" -t, --time time the tests\n");
printf(" -i, --iterations number of times to run tests\n");
printf(" -q, --quiet do not print test results; "
"final time always prints\n");
printf(" -n, --no-labels print final time without labels, "
"nice for scripts\n");
printf(" -d, --debug do not run tests, "
"debug the driver itself for valgrind\n\n");
printf("TESTGROUPS available:\n");
for(i = 0; test_groups[i].name != NULL; i++) {
if(test_groups[i].handler != fake_tests_init) {
printf(" %s\n", test_groups[i].name);
}
}
printf("\n");
}
gint main(gint argc, gchar **argv)
{
gint i, j, c, iterations = 1;
StringArray *tests_to_run = NULL;
gdouble time_start;
gboolean report_time = FALSE;
gboolean quiet = FALSE;
gboolean global_failure = FALSE;
gboolean no_final_time_labels = FALSE;
gboolean debug = FALSE;
#if HAVE_GETOPT_H
static struct option long_options [] = {
{"help", no_argument, 0, 'h'},
{"time", no_argument, 0, 't'},
{"quiet", no_argument, 0, 'q'},
{"iterations", required_argument, 0, 'i'},
{"debug", no_argument, 0, 'd'},
{"no-labels", no_argument, 0, 'n'},
{0, 0, 0, 0}
};
while((c = getopt_long(argc, argv, "dhtqni:", long_options, NULL)) != -1) { switch(c) {
case 'h':
print_help(argv[0]);
return 1;
case 't':
report_time = TRUE;
break;
case 'i':
iterations = atoi(optarg);
break;
case 'q':
quiet = TRUE;
break;
case 'n':
no_final_time_labels = TRUE;
break;
case 'd':
debug = TRUE;
break;
}
}
for(i = optind; i < argc; i++) {
if(argv[i][0] == '-') {
continue;
}
tests_to_run = string_array_append(tests_to_run, argv[i]);
}
#endif
time_start = get_timestamp();
for(j = 0; test_groups[j].name != NULL; j++) {
gboolean run = TRUE;
gchar *tests = NULL;
gchar *group = NULL;
if(tests_to_run != NULL) {
gint k;
run = FALSE;
for(k = 0; k < tests_to_run->length; k++) {
gchar *user = tests_to_run->strings[k];
const gchar *table = test_groups[j].name;
size_t user_len = strlen(user);
size_t table_len = strlen(table);
if(strncmp(user, table, table_len) == 0) {
if(user_len > table_len && user[table_len] != ':') {
break;
}
run = TRUE;
group = tests_to_run->strings[k];
break;
}
}
}
if(run) {
gboolean passed;
gchar **split = NULL;
if(debug && test_groups[j].handler != fake_tests_init) {
printf("Skipping %s, in driver debug mode\n",
test_groups[j].name);
continue;
} else if(!debug && test_groups[j].handler == fake_tests_init) {
continue;
}
if(group != NULL) {
split = eg_strsplit(group, ":", -1);
if(split != NULL) {
gint m;
for(m = 0; split[m] != NULL; m++) {
if(m == 1) {
tests = strdup(split[m]);
break;
}
}
eg_strfreev(split);
}
}
passed = run_group(&(test_groups[j]),
iterations, quiet, report_time, tests);
if(tests != NULL) {
g_free(tests);
}
if(!passed && !global_failure) {
global_failure = TRUE;
}
}
}
if(!quiet) {
gdouble pass_percentage = ((gdouble)global_passed / (gdouble)global_tests) * 100.0;
printf("=============================\n");
printf("Overall result: %s : %d / %d (%g%%)\n", global_failure ? "FAILED" : "OK", global_passed, global_tests, pass_percentage);
}
if(report_time) {
gdouble duration = get_timestamp() - time_start;
if(no_final_time_labels) {
printf("%g\n", duration);
} else {
printf("%s Total Time: %g\n", DRIVER_NAME, duration);
}
}
if(tests_to_run != NULL) {
string_array_free(tests_to_run);
}
return global_tests - global_passed;
}
|