~ubuntu-branches/ubuntu/feisty/apache2/feisty

« back to all changes in this revision

Viewing changes to srclib/apr-util/test/abts.c

  • Committer: Bazaar Package Importer
  • Author(s): Andreas Barth
  • Date: 2006-12-09 21:05:45 UTC
  • mfrom: (0.6.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20061209210545-h70s0xaqc2v8vqr2
Tags: 2.2.3-3.2
* Non-maintainer upload.
* 043_ajp_connection_reuse: Patch from upstream Bugzilla, fixing a critical
  issue with regard to connection reuse in mod_proxy_ajp.
  Closes: #396265

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* Copyright 2000-2004 Ryan Bloom
 
2
 *
 
3
 * Licensed under the Apache License, Version 2.0 (the "License");
 
4
 * you may not use this file except in compliance with the License.
 
5
 * You may obtain a copy of the License at
 
6
 *
 
7
 *     http://www.apache.org/licenses/LICENSE-2.0
 
8
 *
 
9
 * Unless required by applicable law or agreed to in writing, software
 
10
 * distributed under the License is distributed on an "AS IS" BASIS,
 
11
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 
12
 * See the License for the specific language governing permissions and
 
13
 * limitations under the License.
 
14
 *
 
15
 * Portions of this file were taken from testall.c in the APR test suite,
 
16
 * written by members of the Apache Software Foundation.
 
17
 */
 
18
 
 
19
#include "abts.h"
 
20
#include "abts_tests.h"
 
21
#include "testutil.h"
 
22
 
 
23
#define ABTS_STAT_SIZE 6
 
24
static char status[ABTS_STAT_SIZE] = {'|', '/', '-', '|', '\\', '-'};
 
25
static int curr_char;
 
26
static int verbose = 0;
 
27
static int exclude = 0;
 
28
static int quiet = 0;
 
29
static int list_tests = 0;
 
30
 
 
31
const char **testlist = NULL;
 
32
 
 
33
static int find_test_name(const char *testname) {
 
34
    int i;
 
35
    for (i = 0; testlist[i] != NULL; i++) {
 
36
        if (!strcmp(testlist[i], testname)) {
 
37
            return 1;
 
38
        }
 
39
    }
 
40
    return 0;
 
41
}
 
42
 
 
43
/* Determine if the test should be run at all */
 
44
static int should_test_run(const char *testname) {
 
45
    int found = 0;
 
46
    if (list_tests == 1) {
 
47
        return 0;
 
48
    }
 
49
    if (testlist == NULL) {
 
50
        return 1;
 
51
    }
 
52
    found = find_test_name(testname);
 
53
    if ((found && !exclude) || (!found && exclude)) {
 
54
        return 1;
 
55
    }
 
56
    return 0;
 
57
}
 
58
 
 
59
static void reset_status(void)
 
60
{
 
61
    curr_char = 0;
 
62
}
 
63
 
 
64
static void update_status(void)
 
65
{
 
66
    if (!quiet) {
 
67
        curr_char = (curr_char + 1) % ABTS_STAT_SIZE;
 
68
        fprintf(stdout, "\b%c", status[curr_char]);
 
69
        fflush(stdout);
 
70
    }
 
71
}
 
72
 
 
73
static void end_suite(abts_suite *suite)
 
74
{
 
75
    if (suite != NULL) {
 
76
        sub_suite *last = suite->tail;
 
77
        if (!quiet) {
 
78
            fprintf(stdout, "\b");
 
79
            fflush(stdout);
 
80
        }
 
81
        if (last->failed == 0) {
 
82
            fprintf(stdout, "SUCCESS\n");
 
83
            fflush(stdout);
 
84
        }
 
85
        else {
 
86
            fprintf(stdout, "FAILED %d of %d\n", last->failed, last->num_test);
 
87
            fflush(stdout);
 
88
        }
 
89
    }
 
90
}
 
91
 
 
92
abts_suite *abts_add_suite(abts_suite *suite, const char *suite_name_full)
 
93
{
 
94
    sub_suite *subsuite;
 
95
    char *p;
 
96
    const char *suite_name;
 
97
    curr_char = 0;
 
98
    
 
99
    /* Only end the suite if we actually ran it */
 
100
    if (suite && suite->tail &&!suite->tail->not_run) {
 
101
        end_suite(suite);
 
102
    }
 
103
 
 
104
    subsuite = malloc(sizeof(*subsuite));
 
105
    subsuite->num_test = 0;
 
106
    subsuite->failed = 0;
 
107
    subsuite->next = NULL;
 
108
    /* suite_name_full may be an absolute path depending on __FILE__ 
 
109
     * expansion */
 
110
    suite_name = strrchr(suite_name_full, '/');
 
111
    if (suite_name) {
 
112
        suite_name++;
 
113
    } else {
 
114
        suite_name = suite_name_full;
 
115
    }
 
116
    p = strrchr(suite_name, '.');
 
117
    if (p) {
 
118
        subsuite->name = memcpy(calloc(p - suite_name + 1, 1),
 
119
                                suite_name, p - suite_name);
 
120
    }
 
121
    else {
 
122
        subsuite->name = suite_name;
 
123
    }
 
124
 
 
125
    if (list_tests) {
 
126
        fprintf(stdout, "%s\n", subsuite->name);
 
127
    }
 
128
    
 
129
    subsuite->not_run = 0;
 
130
 
 
131
    if (suite == NULL) {
 
132
        suite = malloc(sizeof(*suite));
 
133
        suite->head = subsuite;
 
134
        suite->tail = subsuite;
 
135
    }
 
136
    else {
 
137
        suite->tail->next = subsuite;
 
138
        suite->tail = subsuite;
 
139
    }
 
140
 
 
141
    if (!should_test_run(subsuite->name)) {
 
142
        subsuite->not_run = 1;
 
143
        return suite;
 
144
    }
 
145
 
 
146
    reset_status();
 
147
    fprintf(stdout, "%-20s:  ", subsuite->name);
 
148
    update_status();
 
149
    fflush(stdout);
 
150
 
 
151
    return suite;
 
152
}
 
153
 
 
154
void abts_run_test(abts_suite *ts, test_func f, void *value)
 
155
{
 
156
    abts_case *tc;
 
157
    sub_suite *ss;
 
158
 
 
159
    if (!should_test_run(ts->tail->name)) {
 
160
        return;
 
161
    }
 
162
    ss = ts->tail;
 
163
 
 
164
    tc = malloc(sizeof(*tc));
 
165
    tc->failed = 0;
 
166
    tc->suite = ss;
 
167
    
 
168
    ss->num_test++;
 
169
    update_status();
 
170
 
 
171
    f(tc, value);
 
172
    
 
173
    if (tc->failed) {
 
174
        ss->failed++;
 
175
    }
 
176
    free(tc);
 
177
}
 
178
 
 
179
static int report(abts_suite *suite)
 
180
{
 
181
    int count = 0;
 
182
    sub_suite *dptr;
 
183
 
 
184
    if (suite && suite->tail &&!suite->tail->not_run) {
 
185
        end_suite(suite);
 
186
    }
 
187
 
 
188
    for (dptr = suite->head; dptr; dptr = dptr->next) {
 
189
        count += dptr->failed;
 
190
    }
 
191
 
 
192
    if (list_tests) {
 
193
        return 0;
 
194
    }
 
195
 
 
196
    if (count == 0) {
 
197
        printf("All tests passed.\n");
 
198
        return 0;
 
199
    }
 
200
 
 
201
    dptr = suite->head;
 
202
    fprintf(stdout, "%-15s\t\tTotal\tFail\tFailed %%\n", "Failed Tests");
 
203
    fprintf(stdout, "===================================================\n");
 
204
    while (dptr != NULL) {
 
205
        if (dptr->failed != 0) {
 
206
            float percent = ((float)dptr->failed / (float)dptr->num_test);
 
207
            fprintf(stdout, "%-15s\t\t%5d\t%4d\t%6.2f%%\n", dptr->name, 
 
208
                    dptr->num_test, dptr->failed, percent * 100);
 
209
        }
 
210
        dptr = dptr->next;
 
211
    }
 
212
    return 1;
 
213
}
 
214
 
 
215
void abts_log_message(const char *fmt, ...)
 
216
{
 
217
    va_list args;
 
218
    update_status();
 
219
 
 
220
    if (verbose) {
 
221
        va_start(args, fmt);
 
222
        vfprintf(stderr, fmt, args);
 
223
        va_end(args);
 
224
        fprintf(stderr, "\n");
 
225
        fflush(stderr);
 
226
    }
 
227
}
 
228
 
 
229
void abts_int_equal(abts_case *tc, const int expected, const int actual, int lineno)
 
230
{
 
231
    update_status();
 
232
    if (tc->failed) return;
 
233
 
 
234
    if (expected == actual) return;
 
235
 
 
236
    tc->failed = TRUE;
 
237
    if (verbose) {
 
238
        fprintf(stderr, "Line %d: expected <%d>, but saw <%d>\n", lineno, expected, actual);
 
239
        fflush(stderr);
 
240
    }
 
241
}
 
242
 
 
243
void abts_int_nequal(abts_case *tc, const int expected, const int actual, int lineno)
 
244
{
 
245
    update_status();
 
246
    if (tc->failed) return;
 
247
 
 
248
    if (expected != actual) return;
 
249
 
 
250
    tc->failed = TRUE;
 
251
    if (verbose) {
 
252
        fprintf(stderr, "Line %d: expected <%d>, but saw <%d>\n", lineno, expected, actual);
 
253
        fflush(stderr);
 
254
    }
 
255
}
 
256
 
 
257
void abts_str_equal(abts_case *tc, const char *expected, const char *actual, int lineno)
 
258
{
 
259
    update_status();
 
260
    if (tc->failed) return;
 
261
 
 
262
    /* If both are NULL, match is good */
 
263
    if (!expected && !actual) return;
 
264
    if (expected && actual)
 
265
        if (!strcmp(expected, actual)) return;
 
266
 
 
267
    tc->failed = TRUE;
 
268
    if (verbose) {
 
269
        fprintf(stderr, "Line %d: expected <%s>, but saw <%s>\n", lineno, expected, actual);
 
270
        fflush(stderr);
 
271
    }
 
272
}
 
273
 
 
274
void abts_str_nequal(abts_case *tc, const char *expected, const char *actual,
 
275
                       size_t n, int lineno)
 
276
{
 
277
    update_status();
 
278
    if (tc->failed) return;
 
279
 
 
280
    if (!strncmp(expected, actual, n)) return;
 
281
 
 
282
    tc->failed = TRUE;
 
283
    if (verbose) {
 
284
        fprintf(stderr, "Line %d: expected <%s>, but saw <%s>\n", lineno, expected, actual);
 
285
        fflush(stderr);
 
286
    }
 
287
}
 
288
 
 
289
void abts_ptr_notnull(abts_case *tc, const void *ptr, int lineno)
 
290
{
 
291
    update_status();
 
292
    if (tc->failed) return;
 
293
 
 
294
    if (ptr != NULL) return;
 
295
 
 
296
    tc->failed = TRUE;
 
297
    if (verbose) {
 
298
        fprintf(stderr, "Line %d: Expected NULL, but saw <%p>\n", lineno, ptr);
 
299
        fflush(stderr);
 
300
    }
 
301
}
 
302
 
 
303
void abts_ptr_equal(abts_case *tc, const void *expected, const void *actual, int lineno)
 
304
{
 
305
    update_status();
 
306
    if (tc->failed) return;
 
307
 
 
308
    if (expected == actual) return;
 
309
 
 
310
    tc->failed = TRUE;
 
311
    if (verbose) {
 
312
        fprintf(stderr, "Line %d: expected <%p>, but saw <%p>\n", lineno, expected, actual);
 
313
        fflush(stderr);
 
314
    }
 
315
}
 
316
 
 
317
void abts_fail(abts_case *tc, const char *message, int lineno)
 
318
{
 
319
    update_status();
 
320
    if (tc->failed) return;
 
321
 
 
322
    tc->failed = TRUE;
 
323
    if (verbose) {
 
324
        fprintf(stderr, "Line %d: %s\n", lineno, message);
 
325
        fflush(stderr);
 
326
    }
 
327
}
 
328
 
 
329
void abts_assert(abts_case *tc, const char *message, int condition, int lineno)
 
330
{
 
331
    update_status();
 
332
    if (tc->failed) return;
 
333
 
 
334
    if (condition) return;
 
335
 
 
336
    tc->failed = TRUE;
 
337
    if (verbose) {
 
338
        fprintf(stderr, "Line %d: %s\n", lineno, message);
 
339
        fflush(stderr);
 
340
    }
 
341
}
 
342
 
 
343
void abts_true(abts_case *tc, int condition, int lineno)
 
344
{
 
345
    update_status();
 
346
    if (tc->failed) return;
 
347
 
 
348
    if (condition) return;
 
349
 
 
350
    tc->failed = TRUE;
 
351
    if (verbose) {
 
352
        fprintf(stderr, "Line %d: Condition is false, but expected true\n", lineno);
 
353
        fflush(stderr);
 
354
    }
 
355
}
 
356
 
 
357
void abts_not_impl(abts_case *tc, const char *message, int lineno)
 
358
{
 
359
    update_status();
 
360
 
 
361
    tc->suite->not_impl++;
 
362
    if (verbose) {
 
363
        fprintf(stderr, "Line %d: %s\n", lineno, message);
 
364
        fflush(stderr);
 
365
    }
 
366
}
 
367
 
 
368
int main(int argc, const char *const argv[]) {
 
369
    int i;
 
370
    int rv;
 
371
    int list_provided = 0;
 
372
    abts_suite *suite = NULL;
 
373
   
 
374
    initialize();
 
375
    for (i = 1; i < argc; i++) {
 
376
        if (!strcmp(argv[i], "-v")) {
 
377
            verbose = 1;
 
378
            continue;
 
379
        }
 
380
        if (!strcmp(argv[i], "-x")) {
 
381
            exclude = 1;
 
382
            continue;
 
383
        }
 
384
        if (!strcmp(argv[i], "-l")) {
 
385
            list_tests = 1;
 
386
            continue;
 
387
        }
 
388
        if (!strcmp(argv[i], "-q")) {
 
389
            quiet = 1;
 
390
            continue;
 
391
        }
 
392
        if (argv[i][0] == '-') {
 
393
            fprintf(stderr, "Invalid option: `%s'\n", argv[i]);
 
394
            exit(1);
 
395
        }
 
396
        list_provided = 1;
 
397
    }
 
398
 
 
399
    if (list_provided) {
 
400
        /* Waste a little space here, because it is easier than counting the
 
401
         * number of tests listed.  Besides it is at most three char *.
 
402
         */
 
403
        testlist = calloc(argc + 1, sizeof(char *));
 
404
        for (i = 1; i < argc; i++) {
 
405
            testlist[i - 1] = argv[i];
 
406
        }
 
407
    }
 
408
 
 
409
    for (i = 0; i < (sizeof(alltests) / sizeof(struct testlist *)); i++) {
 
410
        suite = alltests[i].func(suite);
 
411
    }
 
412
 
 
413
    rv = report(suite);
 
414
    return rv;
 
415
}
 
416