separate tests out into their own files
[nbds] / test / CuTest.h
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
34 /* CuTest */
35
36 typedef struct CuTest CuTest;
37
38 typedef void (*TestFunction)(CuTest *);
39
40 struct CuTest
41 {
42         const char* name;
43         TestFunction function;
44         int failed;
45         int ran;
46         const char* message;
47         jmp_buf *jumpBuf;
48 };
49
50 void CuTestInit(CuTest* t, const char* name, TestFunction function);
51 CuTest* CuTestNew(const char* name, TestFunction function);
52 void CuTestRun(CuTest* tc);
53
54 /* Internal versions of assert functions -- use the public versions */
55 void CuFail_Line(CuTest* tc, 
56     const char* file, int line, const char* message2, const char* message);
57 void CuAssert_Line(CuTest* tc, 
58     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, const char* expected, const char* actual);
61 void CuAssertIntEquals_LineMsg(CuTest* tc, 
62         const char* file, int line, const char* message, int expected, int actual);
63 void CuAssertDblEquals_LineMsg(CuTest* tc, 
64         const char* file, int line, const char* message, double expected, double actual, double delta);
65 void CuAssertPtrEquals_LineMsg(CuTest* tc, 
66         const char* file, int line, const char* message, void* expected, void* actual);
67
68 /* public assert functions */
69
70 #define CuFail(tc, ms)                        CuFail_Line(  (tc), __FILE__, __LINE__, NULL, (ms))
71 #define CuAssert(tc, ms, cond)                CuAssert_Line((tc), __FILE__, __LINE__, (ms), (cond))
72 #define CuAssertTrue(tc, cond)                CuAssert_Line((tc), __FILE__, __LINE__, "assert failed", (cond))
73
74 #define CuAssertStrEquals(tc,ex,ac)           CuAssertStrEquals_LineMsg((tc),__FILE__,__LINE__,NULL,(ex),(ac))
75 #define CuAssertStrEquals_Msg(tc,ms,ex,ac)    CuAssertStrEquals_LineMsg((tc),__FILE__,__LINE__,(ms),(ex),(ac))
76 #define CuAssertIntEquals(tc,ex,ac)           CuAssertIntEquals_LineMsg((tc),__FILE__,__LINE__,NULL,(ex),(ac))
77 #define CuAssertIntEquals_Msg(tc,ms,ex,ac)    CuAssertIntEquals_LineMsg((tc),__FILE__,__LINE__,(ms),(ex),(ac))
78 #define CuAssertDblEquals(tc,ex,ac,dl)        CuAssertDblEquals_LineMsg((tc),__FILE__,__LINE__,NULL,(ex),(ac),(dl))
79 #define CuAssertDblEquals_Msg(tc,ms,ex,ac,dl) CuAssertDblEquals_LineMsg((tc),__FILE__,__LINE__,(ms),(ex),(ac),(dl))
80 #define CuAssertPtrEquals(tc,ex,ac)           CuAssertPtrEquals_LineMsg((tc),__FILE__,__LINE__,NULL,(ex),(ac))
81 #define CuAssertPtrEquals_Msg(tc,ms,ex,ac)    CuAssertPtrEquals_LineMsg((tc),__FILE__,__LINE__,(ms),(ex),(ac))
82
83 #define CuAssertPtrNotNull(tc,p)        CuAssert_Line((tc),__FILE__,__LINE__,"null pointer unexpected",(p != NULL))
84 #define CuAssertPtrNotNullMsg(tc,msg,p) CuAssert_Line((tc),__FILE__,__LINE__,(msg),(p != NULL))
85
86 /* CuSuite */
87
88 #define MAX_TEST_CASES  1024
89
90 #define SUITE_ADD_TEST(SUITE,TEST)      CuSuiteAdd(SUITE, CuTestNew(#TEST, TEST))
91
92 typedef struct
93 {
94         int count;
95         CuTest* list[MAX_TEST_CASES];
96         int failCount;
97
98 } CuSuite;
99
100
101 void CuSuiteInit(CuSuite* testSuite);
102 CuSuite* CuSuiteNew(void);
103 void CuSuiteAdd(CuSuite* testSuite, CuTest *testCase);
104 void CuSuiteAddSuite(CuSuite* testSuite, CuSuite* testSuite2);
105 void CuSuiteRun(CuSuite* testSuite);
106 void CuSuiteSummary(CuSuite* testSuite, CuString* summary);
107 void CuSuiteDetails(CuSuite* testSuite, CuString* details);
108
109 #endif /* CU_TEST_H */