From 0a91acfb4840d72b1c61524f5151247abeea9dee Mon Sep 17 00:00:00 2001 From: Lieven Govaerts Date: Sat, 28 Dec 2013 10:37:05 +0100 Subject: [PATCH 1/7] Add CuAssertStrnEquals(), CuAssertStrnEquals_Msg() and CuAssertStrnEquals_LineMsg(). These functions allow a partial compare of expected/actual strings. Code was originally contributed to the version of CuTest used by the serf project. --- CuTest.c | 32 +++++++++++++++++++++++++++++--- CuTest.h | 9 +++++++++ 2 files changed, 38 insertions(+), 3 deletions(-) diff --git a/CuTest.c b/CuTest.c index f075cfb..0f887df 100644 --- a/CuTest.c +++ b/CuTest.c @@ -177,13 +177,14 @@ void CuAssert_Line(CuTest* tc, const char* file, int line, const char* message, CuFail_Line(tc, file, line, NULL, message); } -void CuAssertStrEquals_LineMsg(CuTest* tc, const char* file, int line, const char* message, - const char* expected, const char* actual) +void CuAssertStrnEquals_LineMsg(CuTest* tc, const char* file, int line, + const char* message, const char* expected, + size_t explen, const char* actual) { CuString string; if ((expected == NULL && actual == NULL) || (expected != NULL && actual != NULL && - strcmp(expected, actual) == 0)) + strncmp(expected, actual, explen) == 0)) { return; } @@ -202,6 +203,31 @@ void CuAssertStrEquals_LineMsg(CuTest* tc, const char* file, int line, const cha CuFailInternal(tc, file, line, &string); } +void CuAssertStrEquals_LineMsg(CuTest* tc, const char* file, int line, + const char* message, const char* expected, + const char* actual) +{ + CuString string; + if ((expected == NULL && actual == NULL) | + (expected != NULL && actual != NULL && + strcmp(expected, actual) == 0)) + { + return; + } + CuStringInit(&string); + if (message != NULL) + { + CuStringAppend(&string, message); + CuStringAppend(&string, ": "); + } + CuStringAppend(&string, "expected <"); + CuStringAppend(&string, expected); + CuStringAppend(&string, "> but was <"); + CuStringAppend(&string, actual); + CuStringAppend(&string, ">"); + CuFailInternal(tc, file, line, &string); +} + void CuAssertIntEquals_LineMsg(CuTest* tc, const char* file, int line, const char* message, int expected, int actual) { diff --git a/CuTest.h b/CuTest.h index 603dc18..f153964 100644 --- a/CuTest.h +++ b/CuTest.h @@ -6,6 +6,10 @@ /* CuString */ +/* Customizations in this version of CuTest: + * 1. added CuAssertStrnEquals(), CuAssertStrnEquals_Msg() and + * CuAssertStrnEquals_LineMsg() + */ char* CuStrAlloc(int size); char* CuStrCopy(const char* old); @@ -59,6 +63,9 @@ void CuAssert_Line(CuTest* tc, const char* file, int line, const char* message, void CuAssertStrEquals_LineMsg(CuTest* tc, const char* file, int line, const char* message, const char* expected, const char* actual); +void CuAssertStrnEquals_LineMsg(CuTest* tc, + const char* file, int line, const char* message, + const char* expected, size_t explen, const char* actual); void CuAssertIntEquals_LineMsg(CuTest* tc, const char* file, int line, const char* message, int expected, int actual); @@ -77,6 +84,8 @@ void CuAssertPtrEquals_LineMsg(CuTest* tc, #define CuAssertStrEquals(tc,ex,ac) CuAssertStrEquals_LineMsg((tc),__FILE__,__LINE__,NULL,(ex),(ac)) #define CuAssertStrEquals_Msg(tc,ms,ex,ac) CuAssertStrEquals_LineMsg((tc),__FILE__,__LINE__,(ms),(ex),(ac)) +#define CuAssertStrnEquals(tc,ex,exlen,ac) CuAssertStrnEquals_LineMsg((tc),__FILE__,__LINE__,NULL,(ex),(exlen),(ac)) +#define CuAssertStrnEquals_Msg(tc,ms,ex,exlen,ac) CuAssertStrnEquals_LineMsg((tc),__FILE__,__LINE__,(ms),(ex),(exlen),ac)) #define CuAssertIntEquals(tc,ex,ac) CuAssertIntEquals_LineMsg((tc),__FILE__,__LINE__,NULL,(ex),(ac)) #define CuAssertIntEquals_Msg(tc,ms,ex,ac) CuAssertIntEquals_LineMsg((tc),__FILE__,__LINE__,(ms),(ex),(ac)) #define CuAssertDblEquals(tc,ex,ac,dl) CuAssertDblEquals_LineMsg((tc),__FILE__,__LINE__,NULL,(ex),(ac),(dl)) From 437fd3179b29acd1bfb63ce949f5c591892672b6 Mon Sep 17 00:00:00 2001 From: Lieven Govaerts Date: Sat, 28 Dec 2013 10:42:25 +0100 Subject: [PATCH 2/7] Fix unitialized value in CuAssertStrnEquals_LineMsg: CuTest.c/CuAssertStrnEquals_LineMsg: Check for expected length == 0 first, the actual data pointer is not necessarily initialized in that case. Code was originally contributed to the version of CuTest used by the serf project. --- CuTest.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/CuTest.c b/CuTest.c index 0f887df..a0335f4 100644 --- a/CuTest.c +++ b/CuTest.c @@ -182,7 +182,8 @@ void CuAssertStrnEquals_LineMsg(CuTest* tc, const char* file, int line, size_t explen, const char* actual) { CuString string; - if ((expected == NULL && actual == NULL) || + if ((explen == 0) || + (expected == NULL && actual == NULL) || (expected != NULL && actual != NULL && strncmp(expected, actual, explen) == 0)) { From 2302af5fad3bd3d0a1cd68e35291a9d57fe6cd10 Mon Sep 17 00:00:00 2001 From: Lieven Govaerts Date: Sat, 28 Dec 2013 10:54:38 +0100 Subject: [PATCH 3/7] Add a per-suite constructor and destructor that's run resp. before and after every test. Code was originally contributed to the version of CuTest used by the serf project. --- CuTest.c | 25 ++++++++++++++++++++++++- CuTest.h | 10 +++++++++- 2 files changed, 33 insertions(+), 2 deletions(-) diff --git a/CuTest.c b/CuTest.c index a0335f4..59536a1 100644 --- a/CuTest.c +++ b/CuTest.c @@ -117,6 +117,9 @@ void CuTestInit(CuTest* t, const char* name, TestFunction function) t->message = NULL; t->function = function; t->jumpBuf = NULL; + t->setup = NULL; + t->teardown = NULL; + t->testBaton = NULL; } CuTest* CuTestNew(const char* name, TestFunction function) @@ -137,11 +140,15 @@ void CuTestRun(CuTest* tc) { jmp_buf buf; tc->jumpBuf = &buf; + if (tc->setup) + tc->testBaton = tc->setup(tc); if (setjmp(buf) == 0) { tc->ran = 1; (tc->function)(tc); } + if (tc->teardown) + tc->teardown(tc->testBaton); tc->jumpBuf = 0; } @@ -267,7 +274,9 @@ void CuSuiteInit(CuSuite* testSuite) { testSuite->count = 0; testSuite->failCount = 0; - memset(testSuite->list, 0, sizeof(testSuite->list)); + testSuite->setup = NULL; + testSuite->teardown = NULL; + memset(testSuite->list, 0, sizeof(testSuite->list)); } CuSuite* CuSuiteNew(void) @@ -296,6 +305,13 @@ void CuSuiteAdd(CuSuite* testSuite, CuTest *testCase) assert(testSuite->count < MAX_TEST_CASES); testSuite->list[testSuite->count] = testCase; testSuite->count++; + + /* CuSuiteAdd is called twice per test, don't reset the callbacks if + already set. */ + if (!testCase->setup) + testCase->setup = testSuite->setup; + if (!testCase->teardown) + testCase->teardown = testSuite->teardown; } void CuSuiteAddSuite(CuSuite* testSuite, CuSuite* testSuite2) @@ -365,3 +381,10 @@ void CuSuiteDetails(CuSuite* testSuite, CuString* details) CuStringAppendFormat(details, "Fails: %d\n", testSuite->failCount); } } + +void CuSuiteSetSetupTeardownCallbacks(CuSuite* testSuite, TestCallback setup, + TestCallback teardown) +{ + testSuite->setup = setup; + testSuite->teardown = teardown; +} diff --git a/CuTest.h b/CuTest.h index f153964..835362d 100644 --- a/CuTest.h +++ b/CuTest.h @@ -41,6 +41,7 @@ void CuStringDelete(CuString* str); typedef struct CuTest CuTest; typedef void (*TestFunction)(CuTest *); +typedef void *(*TestCallback)(void *baton); struct CuTest { @@ -50,6 +51,9 @@ struct CuTest int ran; const char* message; jmp_buf *jumpBuf; + TestCallback setup; + TestCallback teardown; + void *testBaton; }; void CuTestInit(CuTest* t, const char* name, TestFunction function); @@ -107,7 +111,9 @@ typedef struct int count; CuTest* list[MAX_TEST_CASES]; int failCount; - + TestCallback setup; + TestCallback teardown; + void *testBaton; } CuSuite; @@ -119,5 +125,7 @@ void CuSuiteAddSuite(CuSuite* testSuite, CuSuite* testSuite2); void CuSuiteRun(CuSuite* testSuite); void CuSuiteSummary(CuSuite* testSuite, CuString* summary); void CuSuiteDetails(CuSuite* testSuite, CuString* details); +void CuSuiteSetSetupTeardownCallbacks(CuSuite* testSuite, TestCallback setup, + TestCallback teardown); #endif /* CU_TEST_H */ From 288df0f1418175d5eccfb41d678a6f83d732aaa6 Mon Sep 17 00:00:00 2001 From: Lieven Govaerts Date: Sat, 28 Dec 2013 10:59:20 +0100 Subject: [PATCH 4/7] Add doc for customization to my version. --- CuTest.h | 1 + 1 file changed, 1 insertion(+) diff --git a/CuTest.h b/CuTest.h index 835362d..1e58b88 100644 --- a/CuTest.h +++ b/CuTest.h @@ -9,6 +9,7 @@ /* Customizations in this version of CuTest: * 1. added CuAssertStrnEquals(), CuAssertStrnEquals_Msg() and * CuAssertStrnEquals_LineMsg() + * 2. Add CuSuiteSetSetupTeardownCallbacks */ char* CuStrAlloc(int size); char* CuStrCopy(const char* old); From 14db2aa6877578ba2facc39c3bebb7adf4dd2070 Mon Sep 17 00:00:00 2001 From: Lieven Govaerts Date: Mon, 30 Dec 2013 21:42:04 +0100 Subject: [PATCH 5/7] Make expected and actual pointers const. This makes it possible to test that a 'const char *' value is NULL without cast. --- CuTest.c | 2 +- CuTest.h | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/CuTest.c b/CuTest.c index 59536a1..771d916 100644 --- a/CuTest.c +++ b/CuTest.c @@ -257,7 +257,7 @@ void CuAssertDblEquals_LineMsg(CuTest* tc, const char* file, int line, const cha } void CuAssertPtrEquals_LineMsg(CuTest* tc, const char* file, int line, const char* message, - void* expected, void* actual) + const void* expected, const void* actual) { char buf[STRING_MAX]; if (expected == actual) return; diff --git a/CuTest.h b/CuTest.h index 1e58b88..4dd6bc9 100644 --- a/CuTest.h +++ b/CuTest.h @@ -79,7 +79,7 @@ void CuAssertDblEquals_LineMsg(CuTest* tc, double expected, double actual, double delta); void CuAssertPtrEquals_LineMsg(CuTest* tc, const char* file, int line, const char* message, - void* expected, void* actual); + const void* expected, const void* actual); /* public assert functions */ From 3647cdcd9e4bb03856f7cee229d3659d68f4ad8f Mon Sep 17 00:00:00 2001 From: Lieven Govaerts Date: Mon, 30 Dec 2013 21:43:45 +0100 Subject: [PATCH 6/7] Update list of modifications. --- CuTest.h | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CuTest.h b/CuTest.h index 4dd6bc9..bffacef 100644 --- a/CuTest.h +++ b/CuTest.h @@ -10,7 +10,9 @@ * 1. added CuAssertStrnEquals(), CuAssertStrnEquals_Msg() and * CuAssertStrnEquals_LineMsg() * 2. Add CuSuiteSetSetupTeardownCallbacks + * 3. Make CuAssertPtrEquals_LineMsg take const pointers. */ + char* CuStrAlloc(int size); char* CuStrCopy(const char* old); From af38b605a0ad52b9fc8da548c3a5d5f456d6f27a Mon Sep 17 00:00:00 2001 From: Lieven Govaerts Date: Mon, 30 Dec 2013 21:50:28 +0100 Subject: [PATCH 7/7] Add the correct license header to the distributable files per instructions in license.txt. --- CuTest.c | 23 +++++++++++++++++++++++ CuTest.h | 23 +++++++++++++++++++++++ 2 files changed, 46 insertions(+) diff --git a/CuTest.c b/CuTest.c index 771d916..3d90e4b 100644 --- a/CuTest.c +++ b/CuTest.c @@ -1,3 +1,26 @@ +/* + * Copyright (c) 2003 Asim Jalis + * + * This software is provided 'as-is', without any express or implied + * warranty. In no event will the authors be held liable for any + * damages arising from the use of this software. + * + * Permission is granted to anyone to use this software for any + * purpose, including commercial applications, and to alter it and + * redistribute it freely, subject to the following restrictions: + * + * 1. The origin of this software must not be misrepresented; you + * must not claim that you wrote the original software. If you use + * this software in a product, an acknowledgment in the product + * documentation would be appreciated but is not required. + * + * 2. Altered source versions must be plainly marked as such, and + * must not be misrepresented as being the original software. + * + * 3. This notice may not be removed or altered from any source + * distribution. + */ + #include #include #include diff --git a/CuTest.h b/CuTest.h index bffacef..5250313 100644 --- a/CuTest.h +++ b/CuTest.h @@ -1,3 +1,26 @@ +/* + * Copyright (c) 2003 Asim Jalis + * + * This software is provided 'as-is', without any express or implied + * warranty. In no event will the authors be held liable for any + * damages arising from the use of this software. + * + * Permission is granted to anyone to use this software for any + * purpose, including commercial applications, and to alter it and + * redistribute it freely, subject to the following restrictions: + * + * 1. The origin of this software must not be misrepresented; you + * must not claim that you wrote the original software. If you use + * this software in a product, an acknowledgment in the product + * documentation would be appreciated but is not required. + * + * 2. Altered source versions must be plainly marked as such, and + * must not be misrepresented as being the original software. + * + * 3. This notice may not be removed or altered from any source + * distribution. + */ + #ifndef CU_TEST_H #define CU_TEST_H