Debian Codegraph @

cextern/astrometry.net/CuTest.c

raw
1 #include <assert.h>
2 #include <setjmp.h>
3 #include <stdlib.h>
4 #include <stdio.h>
5 #include <string.h>
6 #include <math.h>
7
8 #include "CuTest.h"
9
10 /*-------------------------------------------------------------------------*
11 * CuStr
12 *-------------------------------------------------------------------------*/
13
14 char* CuStrAlloc(int size)
15 {
16 char* newStr = (char*) malloc( sizeof(char) * (size) );
17 return newStr;
18 }
19
20 char* CuStrCopy(const char* old)
21 {
22 int len = strlen(old);
23 char* newStr = CuStrAlloc(len + 1);
24 strcpy(newStr, old);
25 return newStr;
26 }
27
28 /*-------------------------------------------------------------------------*
29 * CuString
30 *-------------------------------------------------------------------------*/
31
32 void CuStringInit(CuString* str)
33 {
34 str->length = 0;
35 str->size = STRING_MAX;
36 str->buffer = (char*) malloc(sizeof(char) * str->size);
37 str->buffer[0] = '\0';
38 }
39
40 CuString* CuStringNew(void)
41 {
42 CuString* str = (CuString*) malloc(sizeof(CuString));
43 str->length = 0;
44 str->size = STRING_MAX;
45 str->buffer = (char*) malloc(sizeof(char) * str->size);
46 str->buffer[0] = '\0';
47 return str;
48 }
49
50 void CuStringDelete(CuString *str)
51 {
52 if (!str) return;
53 free(str->buffer);
54 free(str);
55 }
56
57 void CuStringResize(CuString* str, int newSize)
58 {
59 str->buffer = (char*) realloc(str->buffer, sizeof(char) * newSize);
60 str->size = newSize;
61 }
62
63 void CuStringAppend(CuString* str, const char* text)
64 {
65 int length;
66
67 if (text == NULL) {
68 text = "NULL";
69 }
70
71 length = strlen(text);
72 if (str->length + length + 1 >= str->size)
73 CuStringResize(str, str->length + length + 1 + STRING_INC);
74 str->length += length;
75 strcat(str->buffer, text);
76 }
77
78 void CuStringAppendChar(CuString* str, char ch)
79 {
80 char text[2];
81 text[0] = ch;
82 text[1] = '\0';
83 CuStringAppend(str, text);
84 }
85
86 void CuStringAppendFormat(CuString* str, const char* format, ...)
87 {
88 va_list argp;
89 char buf[HUGE_STRING_LEN];
90 va_start(argp, format);
91 vsprintf(buf, format, argp);
92 va_end(argp);
93 CuStringAppend(str, buf);
94 }
95
96 void CuStringInsert(CuString* str, const char* text, int pos)
97 {
98 int length = strlen(text);
99 if (pos > str->length)
100 pos = str->length;
101 if (str->length + length + 1 >= str->size)
102 CuStringResize(str, str->length + length + 1 + STRING_INC);
103 memmove(str->buffer + pos + length, str->buffer + pos, (str->length - pos) + 1);
104 str->length += length;
105 memcpy(str->buffer + pos, text, length);
106 }
107
108 /*-------------------------------------------------------------------------*
109 * CuTest
110 *-------------------------------------------------------------------------*/
111
112 void CuTestInit(CuTest* t, const char* name, TestFunction function)
113 {
114 t->name = CuStrCopy(name);
115 t->failed = 0;
116 t->ran = 0;
117 t->message = NULL;
118 t->function = function;
119 t->jumpBuf = NULL;
120 }
121
122 CuTest* CuTestNew(const char* name, TestFunction function)
123 {
124 CuTest* tc = CU_ALLOC(CuTest);
125 CuTestInit(tc, name, function);
126 return tc;
127 }
128
129 void CuTestDelete(CuTest *t)
130 {
131 if (!t) return;
132 free(t->name);
133 free(t);
134 }
135
136 void CuTestRun(CuTest* tc)
137 {
138 jmp_buf buf;
139 tc->jumpBuf = &buf;
140 if (setjmp(buf) == 0)
141 {
142 tc->ran = 1;
143 (tc->function)(tc);
144 }
145 tc->jumpBuf = 0;
146 }
147
148 static void CuFailInternal(CuTest* tc, const char* file, int line, CuString* string)
149 {
150 char buf[HUGE_STRING_LEN];
151
152 sprintf(buf, "%s:%d: ", file, line);
153 CuStringInsert(string, buf, 0);
154
155 tc->failed = 1;
156 tc->message = string->buffer;
157 if (tc->jumpBuf != 0) longjmp(*(tc->jumpBuf), 0);
158 }
159
160 void CuFail_Line(CuTest* tc, const char* file, int line, const char* message2, const char* message)
161 {
162 CuString string;
163
164 CuStringInit(&string);
165 if (message2 != NULL)
166 {
167 CuStringAppend(&string, message2);
168 CuStringAppend(&string, ": ");
169 }
170 CuStringAppend(&string, message);
171 CuFailInternal(tc, file, line, &string);
172 }
173
174 void CuAssert_Line(CuTest* tc, const char* file, int line, const char* message, int condition)
175 {
176 if (condition) return;
177 CuFail_Line(tc, file, line, NULL, message);
178 }
179
180 void CuAssertStrEquals_LineMsg(CuTest* tc, const char* file, int line, const char* message,
181 const char* expected, const char* actual)
182 {
183 CuString string;
184 if ((expected == NULL && actual == NULL) ||
185 (expected != NULL && actual != NULL &&
186 strcmp(expected, actual) == 0))
187 {
188 return;
189 }
190
191 CuStringInit(&string);
192 if (message != NULL)
193 {
194 CuStringAppend(&string, message);
195 CuStringAppend(&string, ": ");
196 }
197 CuStringAppend(&string, "expected <");
198 CuStringAppend(&string, expected);
199 CuStringAppend(&string, "> but was <");
200 CuStringAppend(&string, actual);
201 CuStringAppend(&string, ">");
202 CuFailInternal(tc, file, line, &string);
203 }
204
205 void CuAssertIntEquals_LineMsg(CuTest* tc, const char* file, int line, const char* message,
206 int expected, int actual)
207 {
208 char buf[STRING_MAX];
209 if (expected == actual) return;
210 sprintf(buf, "expected <%d> but was <%d>", expected, actual);
211 CuFail_Line(tc, file, line, message, buf);
212 }
213
214 void CuAssertDblEquals_LineMsg(CuTest* tc, const char* file, int line, const char* message,
215 double expected, double actual, double delta)
216 {
217 char buf[STRING_MAX];
218 if (fabs(expected - actual) <= delta) return;
219 /* sprintf(buf, "expected <%lf> but was <%lf>", expected, actual); */
220 sprintf(buf, "expected <%f> but was <%f>", expected, actual);
221
222 CuFail_Line(tc, file, line, message, buf);
223 }
224
225 void CuAssertPtrEquals_LineMsg(CuTest* tc, const char* file, int line, const char* message,
226 void* expected, void* actual)
227 {
228 char buf[STRING_MAX];
229 if (expected == actual) return;
230 sprintf(buf, "expected pointer <0x%p> but was <0x%p>", expected, actual);
231 CuFail_Line(tc, file, line, message, buf);
232 }
233
234
235 /*-------------------------------------------------------------------------*
236 * CuSuite
237 *-------------------------------------------------------------------------*/
238
239 void CuSuiteInit(CuSuite* testSuite)
240 {
241 testSuite->count = 0;
242 testSuite->failCount = 0;
243 memset(testSuite->list, 0, sizeof(testSuite->list));
244 }
245
246 CuSuite* CuSuiteNew(void)
247 {
248 CuSuite* testSuite = CU_ALLOC(CuSuite);
249 CuSuiteInit(testSuite);
250 return testSuite;
251 }
252
253 void CuSuiteDelete(CuSuite *testSuite)
254 {
255 unsigned int n;
256 for (n=0; n < MAX_TEST_CASES; n++)
257 {
258 if (testSuite->list[n])
259 {
260 CuTestDelete(testSuite->list[n]);
261 }
262 }
263 free(testSuite);
264
265 }
266
267 void CuSuiteAdd(CuSuite* testSuite, CuTest *testCase)
268 {
269 assert(testSuite->count < MAX_TEST_CASES);
270 testSuite->list[testSuite->count] = testCase;
271 testSuite->count++;
272 }
273
274 void CuSuiteAddSuite(CuSuite* testSuite, CuSuite* testSuite2)
275 {
276 int i;
277 for (i = 0 ; i < testSuite2->count ; ++i)
278 {
279 CuTest* testCase = testSuite2->list[i];
280 CuSuiteAdd(testSuite, testCase);
281 }
282 }
283
284 void CuSuiteRun(CuSuite* testSuite)
285 {
286 int i;
287 for (i = 0 ; i < testSuite->count ; ++i)
288 {
289 CuTest* testCase = testSuite->list[i];
290 CuTestRun(testCase);
291 if (testCase->failed) { testSuite->failCount += 1; }
292 }
293 }
294
295 void CuSuiteSummary(CuSuite* testSuite, CuString* summary)
296 {
297 int i;
298 for (i = 0 ; i < testSuite->count ; ++i)
299 {
300 CuTest* testCase = testSuite->list[i];
301 CuStringAppend(summary, testCase->failed ? "F" : ".");
302 }
303 CuStringAppend(summary, "\n\n");
304 }
305
306 void CuSuiteDetails(CuSuite* testSuite, CuString* details)
307 {
308 int i;
309 int failCount = 0;
310
311 if (testSuite->failCount == 0)
312 {
313 int passCount = testSuite->count - testSuite->failCount;
314 const char* testWord = passCount == 1 ? "test" : "tests";
315 CuStringAppendFormat(details, "OK (%d %s)\n", passCount, testWord);
316 }
317 else
318 {
319 if (testSuite->failCount == 1)
320 CuStringAppend(details, "There was 1 failure:\n");
321 else
322 CuStringAppendFormat(details, "There were %d failures:\n", testSuite->failCount);
323
324 for (i = 0 ; i < testSuite->count ; ++i)
325 {
326 CuTest* testCase = testSuite->list[i];
327 if (testCase->failed)
328 {
329 failCount++;
330 CuStringAppendFormat(details, "%d) %s: %s\n",
331 failCount, testCase->name, testCase->message);
332 }
333 }
334 CuStringAppend(details, "\n!!!FAILURES!!!\n");
335
336 CuStringAppendFormat(details, "Runs: %d ", testSuite->count);
337 CuStringAppendFormat(details, "Passes: %d ", testSuite->count - testSuite->failCount);
338 CuStringAppendFormat(details, "Fails: %d\n", testSuite->failCount);
339 }
340 }