mirror of
https://github.com/ennorehling/cutest.git
synced 2024-12-21 23:52:20 +00:00
fix memory leak in CuTest when a test fails
This commit is contained in:
parent
6e268687db
commit
0fe17b4c0f
9
CuTest.c
9
CuTest.c
@ -114,7 +114,7 @@ void CuTestInit(CuTest* t, const char* name, TestFunction function)
|
||||
t->name = CuStrCopy(name);
|
||||
t->failed = 0;
|
||||
t->ran = 0;
|
||||
t->message = NULL;
|
||||
t->message = NULL;
|
||||
t->function = function;
|
||||
t->jumpBuf = NULL;
|
||||
}
|
||||
@ -129,6 +129,7 @@ CuTest* CuTestNew(const char* name, TestFunction function)
|
||||
void CuTestDelete(CuTest *t)
|
||||
{
|
||||
if (!t) return;
|
||||
CuStringDelete(t->message);
|
||||
free(t->name);
|
||||
free(t);
|
||||
}
|
||||
@ -153,7 +154,9 @@ static void CuFailInternal(CuTest* tc, const char* file, int line, CuString* str
|
||||
CuStringInsert(string, buf, 0);
|
||||
|
||||
tc->failed = 1;
|
||||
tc->message = string->buffer;
|
||||
free(tc->message);
|
||||
tc->message = CuStringNew();
|
||||
CuStringAppend(tc->message, string->buffer);
|
||||
if (tc->jumpBuf != 0) longjmp(*(tc->jumpBuf), 0);
|
||||
}
|
||||
|
||||
@ -327,7 +330,7 @@ void CuSuiteDetails(CuSuite* testSuite, CuString* details)
|
||||
{
|
||||
failCount++;
|
||||
CuStringAppendFormat(details, "%d) %s: %s\n",
|
||||
failCount, testCase->name, testCase->message);
|
||||
failCount, testCase->name, testCase->message->buffer);
|
||||
}
|
||||
}
|
||||
CuStringAppend(details, "\n!!!FAILURES!!!\n");
|
||||
|
4
CuTest.h
4
CuTest.h
@ -4,7 +4,7 @@
|
||||
#include <setjmp.h>
|
||||
#include <stdarg.h>
|
||||
|
||||
#define CUTEST_VERSION "CuTest 1.5"
|
||||
#define CUTEST_VERSION "CuTest 1.5b"
|
||||
|
||||
/* CuString */
|
||||
|
||||
@ -46,7 +46,7 @@ struct CuTest
|
||||
TestFunction function;
|
||||
int failed;
|
||||
int ran;
|
||||
const char* message;
|
||||
CuString *message;
|
||||
jmp_buf *jumpBuf;
|
||||
};
|
||||
|
||||
|
22
CuTestTest.c
22
CuTestTest.c
@ -12,17 +12,17 @@
|
||||
|
||||
#define CompareAsserts(tc, message, expected, actual) X_CompareAsserts((tc), __FILE__, __LINE__, (message), (expected), (actual))
|
||||
|
||||
static void X_CompareAsserts(CuTest* tc, const char *file, int line, const char* message, const char* expected, const char* actual)
|
||||
static void X_CompareAsserts(CuTest* tc, const char *file, int line, const char* message, const char* expected, CuString *actual)
|
||||
{
|
||||
int mismatch;
|
||||
if (expected == NULL || actual == NULL) {
|
||||
if (expected == NULL || actual == NULL || actual==NULL) {
|
||||
mismatch = (expected != NULL || actual != NULL);
|
||||
} else {
|
||||
const char *front = __FILE__ ":";
|
||||
const size_t frontLen = strlen(front);
|
||||
const size_t expectedLen = strlen(expected);
|
||||
|
||||
const char *matchStr = actual;
|
||||
const char *matchStr = actual->buffer;
|
||||
|
||||
mismatch = (strncmp(matchStr, front, frontLen) != 0);
|
||||
if (!mismatch) {
|
||||
@ -148,7 +148,7 @@ void TestCuTestNew(CuTest* tc)
|
||||
CuTest* tc2 = CuTestNew("MyTest", TestPasses);
|
||||
CuAssertStrEquals(tc, "MyTest", tc2->name);
|
||||
CuAssertTrue(tc, !tc2->failed);
|
||||
CuAssertTrue(tc, tc2->message == NULL);
|
||||
CuAssertTrue(tc, tc2->message == NULL);
|
||||
CuAssertTrue(tc, tc2->function == TestPasses);
|
||||
CuAssertTrue(tc, tc2->ran == 0);
|
||||
CuAssertTrue(tc, tc2->jumpBuf == NULL);
|
||||
@ -161,7 +161,7 @@ void TestCuTestInit(CuTest *tc)
|
||||
CuTestInit(&tc2, "MyTest", TestPasses);
|
||||
CuAssertStrEquals(tc, "MyTest", tc2.name);
|
||||
CuAssertTrue(tc, !tc2.failed);
|
||||
CuAssertTrue(tc, tc2.message == NULL);
|
||||
CuAssertTrue(tc, tc2.message == NULL);
|
||||
CuAssertTrue(tc, tc2.function == TestPasses);
|
||||
CuAssertTrue(tc, tc2.ran == 0);
|
||||
CuAssertTrue(tc, tc2.jumpBuf == NULL);
|
||||
@ -174,7 +174,7 @@ void TestCuAssert(CuTest* tc)
|
||||
|
||||
CuAssert(&tc2, "test 1", 5 == 4 + 1);
|
||||
CuAssertTrue(tc, !tc2.failed);
|
||||
CuAssertTrue(tc, tc2.message == NULL);
|
||||
CuAssertTrue(tc, tc2.message == NULL);
|
||||
|
||||
CuAssert(&tc2, "test 2", 0);
|
||||
CuAssertTrue(tc, tc2.failed);
|
||||
@ -200,7 +200,7 @@ void TestCuAssertPtrEquals_Success(CuTest* tc)
|
||||
/* test success case */
|
||||
CuAssertPtrEquals(&tc2, &x, &x);
|
||||
CuAssertTrue(tc, ! tc2.failed);
|
||||
CuAssertTrue(tc, NULL == tc2.message);
|
||||
CuAssertTrue(tc, tc2.message == NULL);
|
||||
}
|
||||
|
||||
void TestCuAssertPtrEquals_Failure(CuTest* tc)
|
||||
@ -229,7 +229,7 @@ void TestCuAssertPtrNotNull_Success(CuTest* tc)
|
||||
/* test success case */
|
||||
CuAssertPtrNotNull(&tc2, &x);
|
||||
CuAssertTrue(tc, ! tc2.failed);
|
||||
CuAssertTrue(tc, NULL == tc2.message);
|
||||
CuAssertTrue(tc, tc2.message == NULL);
|
||||
}
|
||||
|
||||
void TestCuAssertPtrNotNull_Failure(CuTest* tc)
|
||||
@ -645,11 +645,11 @@ void TestAssertDblEquals(CuTest* tc)
|
||||
|
||||
CuAssertDblEquals(tc2, x, x, 0.0);
|
||||
CuAssertTrue(tc, ! tc2->failed);
|
||||
CuAssertTrue(tc, NULL == tc2->message);
|
||||
|
||||
CuAssertTrue(tc, tc2->message == NULL);
|
||||
|
||||
CuAssertDblEquals(tc2, x, y, 0.01);
|
||||
CuAssertTrue(tc, ! tc2->failed);
|
||||
CuAssertTrue(tc, NULL == tc2->message);
|
||||
CuAssertTrue(tc, tc2->message == NULL);
|
||||
|
||||
tc2->jumpBuf = &buf;
|
||||
if (setjmp(buf) == 0)
|
||||
|
Loading…
Reference in New Issue
Block a user