1 /*******************
2 
3 LICENSE
4 
5 Copyright (c) 2003 Asim Jalis
6 
7 This software is provided 'as-is', without any express or implied
8 warranty. In no event will the authors be held liable for any damages
9 arising from the use of this software.
10 
11 Permission is granted to anyone to use this software for any purpose,
12 including commercial applications, and to alter it and redistribute it
13 freely, subject to the following restrictions:
14 
15 1. The origin of this software must not be misrepresented; you must not
16 claim that you wrote the original software. If you use this software in
17 a product, an acknowledgment in the product documentation would be
18 appreciated but is not required.
19 
20 2. Altered source versions must be plainly marked as such, and must not
21 be misrepresented as being the original software.
22 
23 3. This notice may not be removed or altered from any source
24 distribution.
25 
26 **********************/
27 
28 #ifndef CU_TEST_H
29 #define CU_TEST_H
30 
31 #include <setjmp.h>
32 #include <stdarg.h>
33 
34 /* CuString */
35 
36 char* CuStrAlloc(int size);
37 char* CuStrCopy(const char* old);
38 
39 #define CU_ALLOC(TYPE)		((TYPE*) malloc(sizeof(TYPE)))
40 
41 #define HUGE_STRING_LEN	8192
42 #define STRING_MAX		256
43 #define STRING_INC		256
44 
45 typedef struct
46 {
47 	int length;
48 	int size;
49 	char* buffer;
50 } CuString;
51 
52 void CuStringInit(CuString* str);
53 CuString* CuStringNew(void);
54 void CuStringRead(CuString* str, const char* path);
55 void CuStringAppend(CuString* str, const char* text);
56 void CuStringAppendChar(CuString* str, char ch);
57 void CuStringAppendFormat(CuString* str, const char* format, ...);
58 void CuStringInsert(CuString* str, const char* text, int pos);
59 void CuStringResize(CuString* str, int newSize);
60 
61 /* CuTest */
62 
63 typedef struct CuTest CuTest;
64 
65 typedef void (*TestFunction)(CuTest *);
66 
67 struct CuTest
68 {
69 	const char* name;
70 	TestFunction function;
71 	int failed;
72 	int ran;
73 	const char* message;
74 	jmp_buf *jumpBuf;
75 };
76 
77 void CuTestInit(CuTest* t, const char* name, TestFunction function);
78 CuTest* CuTestNew(const char* name, TestFunction function);
79 void CuTestRun(CuTest* tc);
80 
81 /* Internal versions of assert functions -- use the public versions */
82 void CuFail_Line(CuTest* tc, const char* file, int line, const char* message2, const char* message);
83 void CuAssert_Line(CuTest* tc, const char* file, int line, const char* message, int condition);
84 void CuAssertStrEquals_LineMsg(CuTest* tc,
85 	const char* file, int line, const char* message,
86 	const char* expected, const char* actual);
87 void CuAssertIntEquals_LineMsg(CuTest* tc,
88 	const char* file, int line, const char* message,
89 	int expected, int actual);
90 void CuAssertDblEquals_LineMsg(CuTest* tc,
91 	const char* file, int line, const char* message,
92 	double expected, double actual, double delta);
93 void CuAssertPtrEquals_LineMsg(CuTest* tc,
94 	const char* file, int line, const char* message,
95 	void* expected, void* actual);
96 
97 /* public assert functions */
98 
99 #define CuFail(tc, ms)                        CuFail_Line(  (tc), __FILE__, __LINE__, NULL, (ms))
100 #define CuAssert(tc, ms, cond)                CuAssert_Line((tc), __FILE__, __LINE__, (ms), (cond))
101 #define CuAssertTrue(tc, cond)                CuAssert_Line((tc), __FILE__, __LINE__, "assert failed", (cond))
102 
103 #define CuAssertStrEquals(tc,ex,ac)           CuAssertStrEquals_LineMsg((tc),__FILE__,__LINE__,NULL,(ex),(ac))
104 #define CuAssertStrEquals_Msg(tc,ms,ex,ac)    CuAssertStrEquals_LineMsg((tc),__FILE__,__LINE__,(ms),(ex),(ac))
105 #define CuAssertIntEquals(tc,ex,ac)           CuAssertIntEquals_LineMsg((tc),__FILE__,__LINE__,NULL,(ex),(ac))
106 #define CuAssertIntEquals_Msg(tc,ms,ex,ac)    CuAssertIntEquals_LineMsg((tc),__FILE__,__LINE__,(ms),(ex),(ac))
107 #define CuAssertDblEquals(tc,ex,ac,dl)        CuAssertDblEquals_LineMsg((tc),__FILE__,__LINE__,NULL,(ex),(ac),(dl))
108 #define CuAssertDblEquals_Msg(tc,ms,ex,ac,dl) CuAssertDblEquals_LineMsg((tc),__FILE__,__LINE__,(ms),(ex),(ac),(dl))
109 #define CuAssertPtrEquals(tc,ex,ac)           CuAssertPtrEquals_LineMsg((tc),__FILE__,__LINE__,NULL,(ex),(ac))
110 #define CuAssertPtrEquals_Msg(tc,ms,ex,ac)    CuAssertPtrEquals_LineMsg((tc),__FILE__,__LINE__,(ms),(ex),(ac))
111 
112 #define CuAssertPtrNotNull(tc,p)        CuAssert_Line((tc),__FILE__,__LINE__,"null pointer unexpected",(p != NULL))
113 #define CuAssertPtrNotNullMsg(tc,msg,p) CuAssert_Line((tc),__FILE__,__LINE__,(msg),(p != NULL))
114 
115 /* CuSuite */
116 
117 #define MAX_TEST_CASES	1024
118 
119 #define SUITE_ADD_TEST(SUITE,TEST)	CuSuiteAdd(SUITE, CuTestNew(#TEST, TEST))
120 
121 typedef struct
122 {
123 	int count;
124 	CuTest* list[MAX_TEST_CASES];
125 	int failCount;
126 
127 } CuSuite;
128 
129 
130 void CuSuiteInit(CuSuite* testSuite);
131 CuSuite* CuSuiteNew(void);
132 void CuSuiteAdd(CuSuite* testSuite, CuTest *testCase);
133 void CuSuiteAddSuite(CuSuite* testSuite, CuSuite* testSuite2);
134 void CuSuiteRun(CuSuite* testSuite);
135 void CuSuiteSummary(CuSuite* testSuite, CuString* summary);
136 void CuSuiteDetails(CuSuite* testSuite, CuString* details);
137 
138 #endif /* CU_TEST_H */
139