1 /*
2  * Copyright (c) 2003 Asim Jalis
3  *
4  * This software is provided 'as-is', without any express or implied
5  * warranty. In no event will the authors be held liable for any damages
6  * arising from the use of this software.
7  *
8  * Permission is granted to anyone to use this software for any purpose,
9  * including commercial applications, and to alter it and redistribute it
10  * freely, subject to the following restrictions:
11  *
12  * 1. The origin of this software must not be misrepresented; you must not
13  * claim that you wrote the original software. If you use this software in
14  * a product, an acknowledgment in the product documentation would be
15  * appreciated but is not required.
16  *
17  * 2. Altered source versions must be plainly marked as such, and must not
18  * be misrepresented as being the original software.
19  *
20  * 3. This notice may not be removed or altered from any source
21  * distribution.
22  */
23 
24 #ifndef CU_TEST_H
25 #define CU_TEST_H
26 
27 #include <setjmp.h>
28 #include <stdarg.h>
29 
30 #define CUTEST_VERSION  "CuTest 1.5"
31 
32 /* CuString */
33 
34 char* CuStrAlloc(int size);
35 char* CuStrCopy(const char* old);
36 
37 #define CU_ALLOC(TYPE)		((TYPE*) malloc(sizeof(TYPE)))
38 
39 #define HUGE_STRING_LEN	8192
40 #define STRING_MAX		256
41 #define STRING_INC		256
42 
43 typedef struct
44 {
45 	int length;
46 	int size;
47 	char* buffer;
48 } CuString;
49 
50 void CuStringInit(CuString* str);
51 CuString* CuStringNew(void);
52 void CuStringRead(CuString* str, const char* path);
53 void CuStringAppend(CuString* str, const char* text);
54 void CuStringAppendChar(CuString* str, char ch);
55 void CuStringAppendFormat(CuString* str, const char* format, ...);
56 void CuStringInsert(CuString* str, const char* text, int pos);
57 void CuStringResize(CuString* str, int newSize);
58 void CuStringDelete(CuString* str);
59 
60 /* CuTest */
61 
62 typedef struct CuTest CuTest;
63 
64 typedef void (*TestFunction)(CuTest *);
65 
66 struct CuTest
67 {
68 	char* name;
69 	TestFunction function;
70 	int failed;
71 	int ran;
72 	const char* message;
73 	jmp_buf *jumpBuf;
74 };
75 
76 void CuTestInit(CuTest* t, const char* name, TestFunction function);
77 CuTest* CuTestNew(const char* name, TestFunction function);
78 void CuTestRun(CuTest* tc);
79 void CuTestDelete(CuTest *t);
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 CuSuiteDelete(CuSuite *testSuite);
133 void CuSuiteAdd(CuSuite* testSuite, CuTest *testCase);
134 void CuSuiteAddSuite(CuSuite* testSuite, CuSuite* testSuite2);
135 void CuSuiteRun(CuSuite* testSuite);
136 void CuSuiteSummary(CuSuite* testSuite, CuString* summary);
137 void CuSuiteDetails(CuSuite* testSuite, CuString* details);
138 
139 #endif /* CU_TEST_H */
140