-
Notifications
You must be signed in to change notification settings - Fork 0
/
csuite.c
219 lines (195 loc) · 4.72 KB
/
csuite.c
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
/* csuite.c
* Author: Chuck Allison
* Source:
* The Simplest Automated Unit Test Framework That Could Possibly Work:
* http://www.ddj.com/184401279?pgno=1
*
* Modified:
* By Seth Ellsworth @ Quest.
* (c) 2007 Quest Software, Inc. All rights reserved.
*/
#include "csuite.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
enum { CHUNK = 10 };
struct Suite {
char *name;
FILE *pStream;
size_t nTests;
size_t maxTests;
Test **pTests;
};
Suite *cs_create(const char *name)
{
int backOutLevel = 0;
Suite *pSuite = malloc(sizeof(Suite));
if (pSuite) {
pSuite->nTests = 0;
pSuite->pStream = stdout;
/* Allocate array of fptrs: */
assert(CHUNK);
pSuite->pTests = calloc(CHUNK, sizeof(Test *));
if (pSuite->pTests) {
pSuite->maxTests = CHUNK;
/* Allocate test name: */
if (name) {
pSuite->name = malloc(strlen(name) + 1);
strcpy(pSuite->name, name);
} else
++backOutLevel;
} else
++backOutLevel;
}
/* Back-out allocations if memory failed: */
if (backOutLevel) {
switch (backOutLevel) {
case 2:
free(pSuite->pTests);
case 1:
free(pSuite);
pSuite = NULL;
}
}
return pSuite;
}
void cs_destroy(Suite * pSuite,
bool dropTests)
{
assert(pSuite);
assert(pSuite->pTests);
if (dropTests) {
size_t i;
for (i = 0; i < pSuite->nTests; ++i)
ct_destroy(pSuite->pTests[i]);
}
free(pSuite->pTests);
assert(pSuite->name);
free(pSuite->name);
free(pSuite);
}
const char *cs_getName(Suite * pSuite)
{
assert(pSuite);
return pSuite->name;
}
FILE *cs_getStream(Suite * pSuite)
{
assert(pSuite);
return pSuite->pStream;
}
void cs_setStream(Suite * pSuite,
FILE * pStream)
{
assert(pSuite);
pSuite->pStream = pStream;
}
bool cs_addTest(Suite * pSuite,
Test * pTest)
{
FILE *pTestStream;
assert(pSuite);
assert(pTest);
pTestStream = ct_getStream(pTest);
if (pSuite->pStream && pTestStream == NULL)
ct_setStream(pTest, pSuite->pStream);
assert(pSuite->pTests);
if (pSuite->nTests == pSuite->maxTests) {
size_t newSize = pSuite->nTests + CHUNK;
Test **new_pTests = realloc(pSuite->pTests, newSize * sizeof(Test *));
if (!new_pTests)
return FALSE;
pSuite->pTests = new_pTests;
pSuite->maxTests += CHUNK;
}
assert(pSuite->nTests < pSuite->maxTests);
pSuite->pTests[pSuite->nTests++] = pTest;
ct_reset(pTest);
return TRUE;
}
bool cs_addSuite(Suite * pSuite,
Suite * pSuite2)
{
size_t i;
bool rc;
assert(pSuite);
assert(pSuite2);
for (i = 0; i < pSuite->nTests; ++i)
rc = cs_addTest(pSuite, pSuite2->pTests[i]);
return rc;
}
void cs_run(Suite * pSuite)
{
size_t i;
assert(pSuite);
cs_reset(pSuite);
for (i = 0; i < pSuite->nTests; ++i) {
assert(pSuite->pTests[i]);
ct_run(pSuite->pTests[i]);
}
}
long cs_report(Suite * pSuite)
{
assert(pSuite);
if (pSuite->pStream) {
size_t i;
long totFail = 0;
FILE *out = pSuite->pStream;
const char *name = pSuite->name;
size_t nameLen = strlen(name);
fprintf(out, "Suite \"%s\n=======", name);
for (i = 0; i < nameLen; ++i)
fputc('=', out);
fputs("=\n", out);
for (i = 0; i < pSuite->nTests; ++i) {
assert(pSuite->pTests[i]);
totFail += ct_report(pSuite->pTests[i]);
}
fputs("=======", out);
for (i = 0; i < nameLen; ++i)
fputc('=', out);
fputs("=\n", out);
return totFail;
} else
return cs_getNumFailed(pSuite);
}
long cs_getNumPassed(Suite * pSuite)
{
size_t i;
long totPass = 0;
assert(pSuite);
assert(pSuite->pTests);
for (i = 0; i < pSuite->nTests; ++i) {
assert(pSuite->pTests[i]);
totPass += ct_getNumPassed(pSuite->pTests[i]);
}
return totPass;
}
long cs_getNumFailed(Suite * pSuite)
{
size_t i;
long totFail = 0;
assert(pSuite);
assert(pSuite->pTests);
for (i = 0; i < pSuite->nTests; ++i) {
assert(pSuite->pTests[i]);
totFail += ct_getNumFailed(pSuite->pTests[i]);
}
return totFail;
}
void cs_reset(Suite * pSuite)
{
size_t i;
assert(pSuite);
assert(pSuite->pTests);
for (i = 0; i < pSuite->nTests; ++i) {
assert(pSuite->pTests[i]);
ct_reset(pSuite->pTests[i]);
}
}
long cs_getNumTests(Suite * pSuite)
{
assert(pSuite);
return pSuite->nTests;
}