source: trunk/poppler/expat-1.95.8/tests/minicheck.c @ 2

Last change on this file since 2 was 2, checked in by Eugene Romanenko, 16 years ago

First import

File size: 3.4 KB
Line 
1/* Miniature re-implementation of the "check" library.
2 *
3 * This is intended to support just enough of check to run the Expat
4 * tests.  This interface is based entirely on the portion of the
5 * check library being used.
6 */
7
8#include <stdio.h>
9#include <stdlib.h>
10#include <setjmp.h>
11#include <assert.h>
12
13#include "minicheck.h"
14
15Suite *
16suite_create(char *name)
17{
18    Suite *suite = (Suite *) calloc(1, sizeof(Suite));
19    if (suite != NULL) {
20        suite->name = name;
21    }
22    return suite;
23}
24
25TCase *
26tcase_create(char *name)
27{
28    TCase *tc = (TCase *) calloc(1, sizeof(TCase));
29    if (tc != NULL) {
30        tc->name = name;
31    }
32    return tc;
33}
34
35void
36suite_add_tcase(Suite *suite, TCase *tc) 
37{
38    assert(suite != NULL);
39    assert(tc != NULL);
40    assert(tc->next_tcase == NULL);
41
42    tc->next_tcase = suite->tests;
43    suite->tests = tc;
44}
45
46void
47tcase_add_checked_fixture(TCase *tc,
48                          tcase_setup_function setup,
49                          tcase_teardown_function teardown)
50{
51    assert(tc != NULL);
52    tc->setup = setup;
53    tc->teardown = teardown;
54}
55
56void
57tcase_add_test(TCase *tc, tcase_test_function test)
58{
59    assert(tc != NULL);
60    if (tc->allocated == tc->ntests) {
61        int nalloc = tc->allocated + 100;
62        size_t new_size = sizeof(tcase_test_function) * nalloc;
63        tcase_test_function *new_tests = realloc(tc->tests, new_size);
64        assert(new_tests != NULL);
65        if (new_tests != tc->tests) {
66            free(tc->tests);
67            tc->tests = new_tests;
68        }
69        tc->allocated = nalloc;
70    }
71    tc->tests[tc->ntests] = test;
72    tc->ntests++;
73}
74
75SRunner *
76srunner_create(Suite *suite)
77{
78    SRunner *runner = calloc(1, sizeof(SRunner));
79    if (runner != NULL) {
80        runner->suite = suite;
81    }
82    return runner;
83}
84
85void
86srunner_set_fork_status(SRunner *runner, int status)
87{
88    /* We ignore this. */
89}
90
91static jmp_buf env;
92
93void
94srunner_run_all(SRunner *runner, int verbosity)
95{
96    Suite *suite;
97    TCase *tc;
98    assert(runner != NULL);
99    suite = runner->suite;
100    tc = suite->tests;
101    while (tc != NULL) {
102        int i;
103        for (i = 0; i < tc->ntests; ++i) {
104            runner->nchecks++;
105
106            if (tc->setup != NULL) {
107                /* setup */
108                if (setjmp(env)) {
109                    runner->nfailures++;
110                    continue;
111                }
112                tc->setup();
113            }
114            /* test */
115            if (setjmp(env)) {
116                runner->nfailures++;
117                continue;
118            }
119            (tc->tests[i])();
120
121            /* teardown */
122            if (tc->teardown != NULL) {
123                if (setjmp(env)) {
124                    runner->nfailures++;
125                    continue;
126                }
127                tc->teardown();
128            }
129        }
130        tc = tc->next_tcase;
131    }
132    if (verbosity) {
133        int passed = runner->nchecks - runner->nfailures;
134        double percentage = ((double) passed) / runner->nchecks;
135        int display = (int) (percentage * 100);
136        printf("%d%%: Checks: %d, Failed: %d\n",
137               display, runner->nchecks, runner->nfailures);
138    }
139}
140
141void
142_fail_unless(int condition, const char *file, int line, char *msg)
143{
144    longjmp(env, 1);
145}
146
147int
148srunner_ntests_failed(SRunner *runner)
149{
150    assert(runner != NULL);
151    return runner->nfailures;
152}
153
154void
155srunner_free(SRunner *runner)
156{
157    free(runner->suite);
158    free(runner);
159}
Note: See TracBrowser for help on using the repository browser.