| 1 |
#ifndef CU_TEST_H |
| 2 |
#define CU_TEST_H |
| 3 |
|
| 4 |
#include <setjmp.h> |
| 5 |
#include <stdarg.h> |
| 6 |
|
| 7 |
/* CuString */ |
| 8 |
|
| 9 |
char* CuStrAlloc(int size); |
| 10 |
char* CuStrCopy(const char* old); |
| 11 |
|
| 12 |
#define CU_ALLOC(TYPE) ((TYPE*) malloc(sizeof(TYPE))) |
| 13 |
|
| 14 |
#define HUGE_STRING_LEN 8192 |
| 15 |
#define STRING_MAX 256 |
| 16 |
#define STRING_INC 256 |
| 17 |
|
| 18 |
typedef struct |
| 19 |
{ |
| 20 |
int length; |
| 21 |
int size; |
| 22 |
char* buffer; |
| 23 |
} CuString; |
| 24 |
|
| 25 |
void CuStringInit(CuString* str); |
| 26 |
CuString* CuStringNew(void); |
| 27 |
void CuStringRead(CuString* str, const char* path); |
| 28 |
void CuStringAppend(CuString* str, const char* text); |
| 29 |
void CuStringAppendChar(CuString* str, char ch); |
| 30 |
void CuStringAppendFormat(CuString* str, const char* format, ...); |
| 31 |
void CuStringInsert(CuString* str, const char* text, int pos); |
| 32 |
void CuStringResize(CuString* str, int newSize); |
| 33 |
void CuStringDelete(CuString* str); |
| 34 |
|
| 35 |
/* CuTest */ |
| 36 |
|
| 37 |
typedef struct CuTest CuTest; |
| 38 |
|
| 39 |
typedef void (*TestFunction)(CuTest *); |
| 40 |
|
| 41 |
struct CuTest |
| 42 |
{ |
| 43 |
char* name; |
| 44 |
TestFunction function; |
| 45 |
int failed; |
| 46 |
int ran; |
| 47 |
const char* message; |
| 48 |
jmp_buf *jumpBuf; |
| 49 |
}; |
| 50 |
|
| 51 |
void CuTestInit(CuTest* t, const char* name, TestFunction function); |
| 52 |
CuTest* CuTestNew(const char* name, TestFunction function); |
| 53 |
void CuTestRun(CuTest* tc); |
| 54 |
void CuTestDelete(CuTest *t); |
| 55 |
|
| 56 |
/* Internal versions of assert functions -- use the public versions */ |
| 57 |
void CuFail_Line(CuTest* tc, const char* file, int line, const char* message2, const char* message); |
| 58 |
void CuAssert_Line(CuTest* tc, const char* file, int line, const char* message, int condition); |
| 59 |
void CuAssertStrEquals_LineMsg(CuTest* tc, |
| 60 |
const char* file, int line, const char* message, |
| 61 |
const char* expected, const char* actual); |
| 62 |
void CuAssertIntEquals_LineMsg(CuTest* tc, |
| 63 |
const char* file, int line, const char* message, |
| 64 |
int expected, int actual); |
| 65 |
void CuAssertDblEquals_LineMsg(CuTest* tc, |
| 66 |
const char* file, int line, const char* message, |
| 67 |
double expected, double actual, double delta); |
| 68 |
void CuAssertPtrEquals_LineMsg(CuTest* tc, |
| 69 |
const char* file, int line, const char* message, |
| 70 |
void* expected, void* actual); |
| 71 |
|
| 72 |
/* public assert functions */ |
| 73 |
|
| 74 |
#define CuFail(tc, ms) CuFail_Line( (tc), __FILE__, __LINE__, NULL, (ms)) |
| 75 |
#define CuAssert(tc, ms, cond) CuAssert_Line((tc), __FILE__, __LINE__, (ms), (cond)) |
| 76 |
#define CuAssertTrue(tc, cond) CuAssert_Line((tc), __FILE__, __LINE__, "assert failed", (cond)) |
| 77 |
|
| 78 |
#define CuAssertStrEquals(tc,ex,ac) CuAssertStrEquals_LineMsg((tc),__FILE__,__LINE__,NULL,(ex),(ac)) |
| 79 |
#define CuAssertStrEquals_Msg(tc,ms,ex,ac) CuAssertStrEquals_LineMsg((tc),__FILE__,__LINE__,(ms),(ex),(ac)) |
| 80 |
#define CuAssertIntEquals(tc,ex,ac) CuAssertIntEquals_LineMsg((tc),__FILE__,__LINE__,NULL,(ex),(ac)) |
| 81 |
#define CuAssertIntEquals_Msg(tc,ms,ex,ac) CuAssertIntEquals_LineMsg((tc),__FILE__,__LINE__,(ms),(ex),(ac)) |
| 82 |
#define CuAssertDblEquals(tc,ex,ac,dl) CuAssertDblEquals_LineMsg((tc),__FILE__,__LINE__,NULL,(ex),(ac),(dl)) |
| 83 |
#define CuAssertDblEquals_Msg(tc,ms,ex,ac,dl) CuAssertDblEquals_LineMsg((tc),__FILE__,__LINE__,(ms),(ex),(ac),(dl)) |
| 84 |
#define CuAssertPtrEquals(tc,ex,ac) CuAssertPtrEquals_LineMsg((tc),__FILE__,__LINE__,NULL,(ex),(ac)) |
| 85 |
#define CuAssertPtrEquals_Msg(tc,ms,ex,ac) CuAssertPtrEquals_LineMsg((tc),__FILE__,__LINE__,(ms),(ex),(ac)) |
| 86 |
|
| 87 |
#define CuAssertPtrNotNull(tc,p) CuAssert_Line((tc),__FILE__,__LINE__,"null pointer unexpected",(p != NULL)) |
| 88 |
#define CuAssertPtrNotNullMsg(tc,msg,p) CuAssert_Line((tc),__FILE__,__LINE__,(msg),(p != NULL)) |
| 89 |
|
| 90 |
/* CuSuite */ |
| 91 |
|
| 92 |
#define MAX_TEST_CASES 1024 |
| 93 |
|
| 94 |
#define SUITE_ADD_TEST(SUITE,TEST) CuSuiteAdd(SUITE, CuTestNew(#TEST, TEST)) |
| 95 |
|
| 96 |
typedef struct |
| 97 |
{ |
| 98 |
int count; |
| 99 |
CuTest* list[MAX_TEST_CASES]; |
| 100 |
int failCount; |
| 101 |
|
| 102 |
} CuSuite; |
| 103 |
|
| 104 |
|
| 105 |
void CuSuiteInit(CuSuite* testSuite); |
| 106 |
CuSuite* CuSuiteNew(void); |
| 107 |
void CuSuiteDelete(CuSuite *testSuite); |
| 108 |
void CuSuiteAdd(CuSuite* testSuite, CuTest *testCase); |
| 109 |
void CuSuiteAddSuite(CuSuite* testSuite, CuSuite* testSuite2); |
| 110 |
void CuSuiteRun(CuSuite* testSuite); |
| 111 |
void CuSuiteSummary(CuSuite* testSuite, CuString* summary); |
| 112 |
void CuSuiteDetails(CuSuite* testSuite, CuString* details); |
| 113 |
|
| 114 |
#endif /* CU_TEST_H */ |