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 34char* CuStrAlloc(int size); 35char* 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 43typedef struct 44{ 45 int length; 46 int size; 47 char* buffer; 48} CuString; 49 50void CuStringInit(CuString* str); 51CuString* CuStringNew(void); 52void CuStringRead(CuString* str, const char* path); 53void CuStringAppend(CuString* str, const char* text); 54void CuStringAppendChar(CuString* str, char ch); 55void CuStringAppendFormat(CuString* str, const char* format, ...); 56void CuStringInsert(CuString* str, const char* text, int pos); 57void CuStringResize(CuString* str, int newSize); 58void CuStringDelete(CuString* str); 59 60/* CuTest */ 61 62typedef struct CuTest CuTest; 63 64typedef void (*TestFunction)(CuTest *); 65 66struct CuTest 67{ 68 char* name; 69 TestFunction function; 70 int failed; 71 int ran; 72 const char* message; 73 jmp_buf *jumpBuf; 74}; 75 76void CuTestInit(CuTest* t, const char* name, TestFunction function); 77CuTest* CuTestNew(const char* name, TestFunction function); 78void CuTestRun(CuTest* tc); 79void CuTestDelete(CuTest *t); 80 81/* Internal versions of assert functions -- use the public versions */ 82void CuFail_Line(CuTest* tc, const char* file, int line, const char* message2, const char* message); 83void CuAssert_Line(CuTest* tc, const char* file, int line, const char* message, int condition); 84void CuAssertStrEquals_LineMsg(CuTest* tc, 85 const char* file, int line, const char* message, 86 const char* expected, const char* actual); 87void CuAssertIntEquals_LineMsg(CuTest* tc, 88 const char* file, int line, const char* message, 89 int expected, int actual); 90void CuAssertDblEquals_LineMsg(CuTest* tc, 91 const char* file, int line, const char* message, 92 double expected, double actual, double delta); 93void 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 121typedef struct 122{ 123 int count; 124 CuTest* list[MAX_TEST_CASES]; 125 int failCount; 126 127} CuSuite; 128 129 130void CuSuiteInit(CuSuite* testSuite); 131CuSuite* CuSuiteNew(void); 132void CuSuiteDelete(CuSuite *testSuite); 133void CuSuiteAdd(CuSuite* testSuite, CuTest *testCase); 134void CuSuiteAddSuite(CuSuite* testSuite, CuSuite* testSuite2); 135void CuSuiteRun(CuSuite* testSuite); 136void CuSuiteSummary(CuSuite* testSuite, CuString* summary); 137void CuSuiteDetails(CuSuite* testSuite, CuString* details); 138 139#endif /* CU_TEST_H */ 140