Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
85 changes: 79 additions & 6 deletions CuTest.c
Original file line number Diff line number Diff line change
@@ -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 <assert.h>
#include <setjmp.h>
#include <stdlib.h>
Expand Down Expand Up @@ -117,6 +140,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)
Expand All @@ -137,11 +163,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;
}

Expand Down Expand Up @@ -177,13 +207,15 @@ 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) ||
if ((explen == 0) ||
(expected == NULL && actual == NULL) ||
(expected != NULL && actual != NULL &&
strcmp(expected, actual) == 0))
strncmp(expected, actual, explen) == 0))
{
return;
}
Expand All @@ -202,6 +234,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)
{
Expand All @@ -223,7 +280,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;
Expand All @@ -240,7 +297,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)
Expand Down Expand Up @@ -269,6 +328,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)
Expand Down Expand Up @@ -338,3 +404,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;
}
47 changes: 45 additions & 2 deletions CuTest.h
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -6,6 +29,13 @@

/* CuString */

/* Customizations in this version of CuTest:
* 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);

Expand Down Expand Up @@ -37,6 +67,7 @@ void CuStringDelete(CuString* str);
typedef struct CuTest CuTest;

typedef void (*TestFunction)(CuTest *);
typedef void *(*TestCallback)(void *baton);

struct CuTest
{
Expand All @@ -46,6 +77,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);
Expand All @@ -59,6 +93,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);
Expand All @@ -67,7 +104,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 */

Expand All @@ -77,6 +114,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))
Expand All @@ -98,7 +137,9 @@ typedef struct
int count;
CuTest* list[MAX_TEST_CASES];
int failCount;

TestCallback setup;
TestCallback teardown;
void *testBaton;
} CuSuite;


Expand All @@ -110,5 +151,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 */