-
Notifications
You must be signed in to change notification settings - Fork 0
/
ctest.c
180 lines (161 loc) · 3.75 KB
/
ctest.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
/* ctest.c: Implements the CTest Framework
* Author: Chuck Allison
* Source:
* The Simplest Automated Unit Test Framework That Could Possibly Work:
* http://www.ddj.com/184401279?pgno=1
*/
#include "ctest.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
/* Number of tests to hold incrementally */
enum { CHUNK = 10 };
Test *ct_create(const char *name,
void (*init) (Test *))
{
int backOutLevel = 0;
Test *pTest = malloc(sizeof(Test));
if (pTest) {
pTest->nPass = pTest->nFail = pTest->nTests = 0;
pTest->pStream = stdout;
/* Allocate array of fptrs: */
assert(CHUNK);
pTest->pTestFuns = calloc(CHUNK, sizeof(TestFunc));
if (pTest->pTestFuns) {
pTest->maxTests = CHUNK;
/* Allocate test name: */
assert(name);
pTest->name = malloc(strlen(name) + 1);
if (pTest->name)
strcpy(pTest->name, name);
else
++backOutLevel;
} else
++backOutLevel;
}
/* Back-out allocations if memory failed: */
if (backOutLevel) {
switch (backOutLevel) {
case 2:
free(pTest->pTestFuns);
pTest->pTestFuns = NULL;
case 1:
free(pTest);
pTest = NULL;
}
} else if (init) {
assert(pTest);
init(pTest);
}
return pTest;
}
void ct_destroy(Test * pTest)
{
assert(pTest);
assert(pTest->pTestFuns);
free(pTest->pTestFuns);
pTest->pTestFuns = NULL;
assert(pTest->name);
free(pTest->name);
pTest->name = NULL;
free(pTest);
}
bool ct_addTestFunction(Test * pTest,
TestFunc tfun)
{
assert(pTest);
assert(pTest->pTestFuns);
if (pTest->nTests == pTest->maxTests) {
size_t newSize = pTest->nTests + CHUNK;
TestFunc *new_pTestFuns = realloc(pTest->pTestFuns,
newSize * sizeof(TestFunc));
if (!new_pTestFuns)
return false;
pTest->pTestFuns = new_pTestFuns;
pTest->maxTests += CHUNK;
}
assert(pTest->nTests < pTest->maxTests);
pTest->pTestFuns[pTest->nTests++] = tfun;
return true;
}
void ct_setStream(Test * pTest,
FILE * pStream)
{
pTest->pStream = pStream;
}
FILE *ct_getStream(Test * pTest)
{
return pTest->pStream;
}
long ct_report(Test * pTest)
{
assert(pTest);
if (pTest->pStream) {
fprintf(pTest->pStream, "Test \"%s\":\n\tPassed: %ld\n\tFailed: %ld\n",
pTest->name, pTest->nPass, pTest->nFail);
}
return pTest->nFail;
}
void ct_succeed(Test * pTest)
{
assert(pTest);
++pTest->nPass;
}
void ct_do_test(Test * pTest,
const char *str,
bool cond,
const char *file,
long line)
{
assert(pTest);
if (!cond)
ct_do_fail(pTest, str, file, line);
else
ct_succeed(pTest);
}
void ct_do_fail(Test * pTest,
const char *str,
const char *file,
long line)
{
assert(pTest);
++pTest->nFail;
if (pTest->pStream) {
fprintf(pTest->pStream, "%s failure: (%s), %s (line %ld)\n",
pTest->name, str, file, line);
}
}
long ct_getNumPassed(Test * pTest)
{
assert(pTest);
return pTest->nPass;
}
long ct_getNumFailed(Test * pTest)
{
assert(pTest);
return pTest->nFail;
}
long ct_run(Test * pTest)
{
size_t testNum;
assert(pTest);
for (testNum = 0; testNum < pTest->nTests; ++testNum)
pTest->pTestFuns[testNum] (pTest);
return pTest->nFail;
}
void ct_reset(Test * pTest)
{
assert(pTest);
pTest->nFail = pTest->nPass = 0;
}
const char *ct_getName(Test * pTest)
{
assert(pTest);
return (pTest->name);
}
long ct_getNumTests(Test * pTest)
{
assert(pTest);
return pTest->nTests;
}