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
6  *  damages arising from the use of this software.
7  *
8  *  Permission is granted to anyone to use this software for any
9  *  purpose, including commercial applications, and to alter it and
10  *  redistribute it freely, subject to the following restrictions:
11  *
12  *  1. The origin of this software must not be misrepresented; you
13  *  must not claim that you wrote the original software. If you use
14  *  this software in a product, an acknowledgment in the product
15  *  documentation would be appreciated but is not required.
16  *
17  *  2. Altered source versions must be plainly marked as such, and
18  *  must not 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  * Originally obtained from "http://cutest.sourceforge.net/" version 1.4.
25  *
26  * Modified for serf as follows
27  *    4) added CuSuiteSetSetupTeardownCallbacks to set a constructor and
28  *       destructor per test suite, run for each test.
29  *    3) added CuAssertStrnEquals(), CuAssertStrnEquals_Msg() and
30  *       CuAssertStrnEquals_LineMsg()
31  *    2) removed const from struct CuTest.name
32  *    1) added CuStringFree(), CuTestFree(), CuSuiteFree(), and
33  *       CuSuiteFreeDeep()
34  *    0) reformatted the whitespace (doh!)
35  */
36 #ifndef CU_TEST_H
37 #define CU_TEST_H
38 
39 #include <setjmp.h>
40 #include <stdarg.h>
41 
42 /* CuString */
43 
44 char* CuStrAlloc(int size);
45 char* CuStrCopy(const char* old);
46 
47 #define CU_ALLOC(TYPE)		((TYPE*) malloc(sizeof(TYPE)))
48 
49 #define HUGE_STRING_LEN	8192
50 #define STRING_MAX		256
51 #define STRING_INC		256
52 
53 typedef struct
54 {
55     int length;
56     int size;
57     char* buffer;
58 } CuString;
59 
60 void CuStringInit(CuString* str);
61 CuString* CuStringNew(void);
62 void CuStringFree(CuString *str);
63 void CuStringRead(CuString* str, const char* path);
64 void CuStringAppend(CuString* str, const char* text);
65 void CuStringAppendChar(CuString* str, char ch);
66 void CuStringAppendFormat(CuString* str, const char* format, ...);
67 void CuStringInsert(CuString* str, const char* text, int pos);
68 void CuStringResize(CuString* str, int newSize);
69 
70 /* CuTest */
71 
72 typedef struct CuTest CuTest;
73 
74 typedef void (*TestFunction)(CuTest *);
75 
76 typedef void *(*TestCallback)(void *baton);
77 
78 struct CuTest
79 {
80     char* name;
81     TestFunction function;
82     int failed;
83     int ran;
84     const char* message;
85     jmp_buf *jumpBuf;
86 
87     TestCallback setup;
88     TestCallback teardown;
89     void *testBaton;
90 };
91 
92 void CuTestInit(CuTest* t, const char* name, TestFunction function);
93 CuTest* CuTestNew(const char* name, TestFunction function);
94 void CuTestFree(CuTest* tc);
95 void CuTestRun(CuTest* tc);
96 
97 /* Internal versions of assert functions -- use the public versions */
98 void CuFail_Line(CuTest* tc, const char* file, int line, const char* message2, const char* message);
99 void CuAssert_Line(CuTest* tc, const char* file, int line, const char* message, int condition);
100 void CuAssertStrEquals_LineMsg(CuTest* tc,
101     const char* file, int line, const char* message,
102     const char* expected, const char* actual);
103 void CuAssertStrnEquals_LineMsg(CuTest* tc,
104     const char* file, int line, const char* message,
105     const char* expected, size_t explen, const char* actual);
106 void CuAssertIntEquals_LineMsg(CuTest* tc,
107     const char* file, int line, const char* message,
108     int expected, int actual);
109 void CuAssertDblEquals_LineMsg(CuTest* tc,
110     const char* file, int line, const char* message,
111     double expected, double actual, double delta);
112 void CuAssertPtrEquals_LineMsg(CuTest* tc,
113     const char* file, int line, const char* message,
114     void* expected, void* actual);
115 
116 /* public assert functions */
117 
118 #define CuFail(tc, ms)                        CuFail_Line(  (tc), __FILE__, __LINE__, NULL, (ms))
119 #define CuAssert(tc, ms, cond)                CuAssert_Line((tc), __FILE__, __LINE__, (ms), (cond))
120 #define CuAssertTrue(tc, cond)                CuAssert_Line((tc), __FILE__, __LINE__, "assert failed", (cond))
121 
122 #define CuAssertStrEquals(tc,ex,ac)           CuAssertStrEquals_LineMsg((tc),__FILE__,__LINE__,NULL,(ex),(ac))
123 #define CuAssertStrEquals_Msg(tc,ms,ex,ac)    CuAssertStrEquals_LineMsg((tc),__FILE__,__LINE__,(ms),(ex),(ac))
124 #define CuAssertStrnEquals(tc,ex,exlen,ac)        CuAssertStrnEquals_LineMsg((tc),__FILE__,__LINE__,NULL,(ex),(exlen),(ac))
125 #define CuAssertStrnEquals_Msg(tc,ms,ex,exlen,ac) CuAssertStrnEquals_LineMsg((tc),__FILE__,__LINE__,(ms),(ex),(exlen),(ac))
126 #define CuAssertIntEquals(tc,ex,ac)           CuAssertIntEquals_LineMsg((tc),__FILE__,__LINE__,NULL,(ex),(ac))
127 #define CuAssertIntEquals_Msg(tc,ms,ex,ac)    CuAssertIntEquals_LineMsg((tc),__FILE__,__LINE__,(ms),(ex),(ac))
128 #define CuAssertDblEquals(tc,ex,ac,dl)        CuAssertDblEquals_LineMsg((tc),__FILE__,__LINE__,NULL,(ex),(ac),(dl))
129 #define CuAssertDblEquals_Msg(tc,ms,ex,ac,dl) CuAssertDblEquals_LineMsg((tc),__FILE__,__LINE__,(ms),(ex),(ac),(dl))
130 #define CuAssertPtrEquals(tc,ex,ac)           CuAssertPtrEquals_LineMsg((tc),__FILE__,__LINE__,NULL,(ex),(ac))
131 #define CuAssertPtrEquals_Msg(tc,ms,ex,ac)    CuAssertPtrEquals_LineMsg((tc),__FILE__,__LINE__,(ms),(ex),(ac))
132 
133 #define CuAssertPtrNotNull(tc,p)        CuAssert_Line((tc),__FILE__,__LINE__,"null pointer unexpected",(p != NULL))
134 #define CuAssertPtrNotNullMsg(tc,msg,p) CuAssert_Line((tc),__FILE__,__LINE__,(msg),(p != NULL))
135 
136 /* CuSuite */
137 
138 #define MAX_TEST_CASES	1024
139 
140 #define SUITE_ADD_TEST(SUITE,TEST)	CuSuiteAdd(SUITE, CuTestNew(#TEST, TEST))
141 
142 typedef struct
143 {
144     int count;
145     CuTest* list[MAX_TEST_CASES];
146     int failCount;
147 
148     TestCallback setup;
149     TestCallback teardown;
150     void *testBaton;
151 } CuSuite;
152 
153 
154 void CuSuiteInit(CuSuite* testSuite);
155 CuSuite* CuSuiteNew(void);
156 void CuSuiteFree(CuSuite *testSuite);
157 void CuSuiteFreeDeep(CuSuite *testSuite);
158 void CuSuiteAdd(CuSuite* testSuite, CuTest *testCase);
159 void CuSuiteAddSuite(CuSuite* testSuite, CuSuite* testSuite2);
160 void CuSuiteRun(CuSuite* testSuite);
161 void CuSuiteSummary(CuSuite* testSuite, CuString* summary);
162 void CuSuiteDetails(CuSuite* testSuite, CuString* details);
163 
164 void CuSuiteSetSetupTeardownCallbacks(CuSuite* testSuite, TestCallback setup,
165                                       TestCallback teardown);
166 
167 #endif /* CU_TEST_H */
168