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->name = CuStrCopy(name);
|
||||||
t->failed = 0;
|
t->failed = 0;
|
||||||
t->ran = 0;
|
t->ran = 0;
|
||||||
t->message = NULL;
|
t->message = NULL;
|
||||||
t->function = function;
|
t->function = function;
|
||||||
t->jumpBuf = NULL;
|
t->jumpBuf = NULL;
|
||||||
}
|
}
|
||||||
@ -129,6 +129,7 @@ CuTest* CuTestNew(const char* name, TestFunction function)
|
|||||||
void CuTestDelete(CuTest *t)
|
void CuTestDelete(CuTest *t)
|
||||||
{
|
{
|
||||||
if (!t) return;
|
if (!t) return;
|
||||||
|
CuStringDelete(t->message);
|
||||||
free(t->name);
|
free(t->name);
|
||||||
free(t);
|
free(t);
|
||||||
}
|
}
|
||||||
@ -153,7 +154,9 @@ static void CuFailInternal(CuTest* tc, const char* file, int line, CuString* str
|
|||||||
CuStringInsert(string, buf, 0);
|
CuStringInsert(string, buf, 0);
|
||||||
|
|
||||||
tc->failed = 1;
|
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);
|
if (tc->jumpBuf != 0) longjmp(*(tc->jumpBuf), 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -327,7 +330,7 @@ void CuSuiteDetails(CuSuite* testSuite, CuString* details)
|
|||||||
{
|
{
|
||||||
failCount++;
|
failCount++;
|
||||||
CuStringAppendFormat(details, "%d) %s: %s\n",
|
CuStringAppendFormat(details, "%d) %s: %s\n",
|
||||||
failCount, testCase->name, testCase->message);
|
failCount, testCase->name, testCase->message->buffer);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
CuStringAppend(details, "\n!!!FAILURES!!!\n");
|
CuStringAppend(details, "\n!!!FAILURES!!!\n");
|
||||||
|
4
CuTest.h
4
CuTest.h
@ -4,7 +4,7 @@
|
|||||||
#include <setjmp.h>
|
#include <setjmp.h>
|
||||||
#include <stdarg.h>
|
#include <stdarg.h>
|
||||||
|
|
||||||
#define CUTEST_VERSION "CuTest 1.5"
|
#define CUTEST_VERSION "CuTest 1.5b"
|
||||||
|
|
||||||
/* CuString */
|
/* CuString */
|
||||||
|
|
||||||
@ -46,7 +46,7 @@ struct CuTest
|
|||||||
TestFunction function;
|
TestFunction function;
|
||||||
int failed;
|
int failed;
|
||||||
int ran;
|
int ran;
|
||||||
const char* message;
|
CuString *message;
|
||||||
jmp_buf *jumpBuf;
|
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))
|
#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;
|
int mismatch;
|
||||||
if (expected == NULL || actual == NULL) {
|
if (expected == NULL || actual == NULL || actual==NULL) {
|
||||||
mismatch = (expected != NULL || actual != NULL);
|
mismatch = (expected != NULL || actual != NULL);
|
||||||
} else {
|
} else {
|
||||||
const char *front = __FILE__ ":";
|
const char *front = __FILE__ ":";
|
||||||
const size_t frontLen = strlen(front);
|
const size_t frontLen = strlen(front);
|
||||||
const size_t expectedLen = strlen(expected);
|
const size_t expectedLen = strlen(expected);
|
||||||
|
|
||||||
const char *matchStr = actual;
|
const char *matchStr = actual->buffer;
|
||||||
|
|
||||||
mismatch = (strncmp(matchStr, front, frontLen) != 0);
|
mismatch = (strncmp(matchStr, front, frontLen) != 0);
|
||||||
if (!mismatch) {
|
if (!mismatch) {
|
||||||
@ -148,7 +148,7 @@ void TestCuTestNew(CuTest* tc)
|
|||||||
CuTest* tc2 = CuTestNew("MyTest", TestPasses);
|
CuTest* tc2 = CuTestNew("MyTest", TestPasses);
|
||||||
CuAssertStrEquals(tc, "MyTest", tc2->name);
|
CuAssertStrEquals(tc, "MyTest", tc2->name);
|
||||||
CuAssertTrue(tc, !tc2->failed);
|
CuAssertTrue(tc, !tc2->failed);
|
||||||
CuAssertTrue(tc, tc2->message == NULL);
|
CuAssertTrue(tc, tc2->message == NULL);
|
||||||
CuAssertTrue(tc, tc2->function == TestPasses);
|
CuAssertTrue(tc, tc2->function == TestPasses);
|
||||||
CuAssertTrue(tc, tc2->ran == 0);
|
CuAssertTrue(tc, tc2->ran == 0);
|
||||||
CuAssertTrue(tc, tc2->jumpBuf == NULL);
|
CuAssertTrue(tc, tc2->jumpBuf == NULL);
|
||||||
@ -161,7 +161,7 @@ void TestCuTestInit(CuTest *tc)
|
|||||||
CuTestInit(&tc2, "MyTest", TestPasses);
|
CuTestInit(&tc2, "MyTest", TestPasses);
|
||||||
CuAssertStrEquals(tc, "MyTest", tc2.name);
|
CuAssertStrEquals(tc, "MyTest", tc2.name);
|
||||||
CuAssertTrue(tc, !tc2.failed);
|
CuAssertTrue(tc, !tc2.failed);
|
||||||
CuAssertTrue(tc, tc2.message == NULL);
|
CuAssertTrue(tc, tc2.message == NULL);
|
||||||
CuAssertTrue(tc, tc2.function == TestPasses);
|
CuAssertTrue(tc, tc2.function == TestPasses);
|
||||||
CuAssertTrue(tc, tc2.ran == 0);
|
CuAssertTrue(tc, tc2.ran == 0);
|
||||||
CuAssertTrue(tc, tc2.jumpBuf == NULL);
|
CuAssertTrue(tc, tc2.jumpBuf == NULL);
|
||||||
@ -174,7 +174,7 @@ void TestCuAssert(CuTest* tc)
|
|||||||
|
|
||||||
CuAssert(&tc2, "test 1", 5 == 4 + 1);
|
CuAssert(&tc2, "test 1", 5 == 4 + 1);
|
||||||
CuAssertTrue(tc, !tc2.failed);
|
CuAssertTrue(tc, !tc2.failed);
|
||||||
CuAssertTrue(tc, tc2.message == NULL);
|
CuAssertTrue(tc, tc2.message == NULL);
|
||||||
|
|
||||||
CuAssert(&tc2, "test 2", 0);
|
CuAssert(&tc2, "test 2", 0);
|
||||||
CuAssertTrue(tc, tc2.failed);
|
CuAssertTrue(tc, tc2.failed);
|
||||||
@ -200,7 +200,7 @@ void TestCuAssertPtrEquals_Success(CuTest* tc)
|
|||||||
/* test success case */
|
/* test success case */
|
||||||
CuAssertPtrEquals(&tc2, &x, &x);
|
CuAssertPtrEquals(&tc2, &x, &x);
|
||||||
CuAssertTrue(tc, ! tc2.failed);
|
CuAssertTrue(tc, ! tc2.failed);
|
||||||
CuAssertTrue(tc, NULL == tc2.message);
|
CuAssertTrue(tc, tc2.message == NULL);
|
||||||
}
|
}
|
||||||
|
|
||||||
void TestCuAssertPtrEquals_Failure(CuTest* tc)
|
void TestCuAssertPtrEquals_Failure(CuTest* tc)
|
||||||
@ -229,7 +229,7 @@ void TestCuAssertPtrNotNull_Success(CuTest* tc)
|
|||||||
/* test success case */
|
/* test success case */
|
||||||
CuAssertPtrNotNull(&tc2, &x);
|
CuAssertPtrNotNull(&tc2, &x);
|
||||||
CuAssertTrue(tc, ! tc2.failed);
|
CuAssertTrue(tc, ! tc2.failed);
|
||||||
CuAssertTrue(tc, NULL == tc2.message);
|
CuAssertTrue(tc, tc2.message == NULL);
|
||||||
}
|
}
|
||||||
|
|
||||||
void TestCuAssertPtrNotNull_Failure(CuTest* tc)
|
void TestCuAssertPtrNotNull_Failure(CuTest* tc)
|
||||||
@ -645,11 +645,11 @@ void TestAssertDblEquals(CuTest* tc)
|
|||||||
|
|
||||||
CuAssertDblEquals(tc2, x, x, 0.0);
|
CuAssertDblEquals(tc2, x, x, 0.0);
|
||||||
CuAssertTrue(tc, ! tc2->failed);
|
CuAssertTrue(tc, ! tc2->failed);
|
||||||
CuAssertTrue(tc, NULL == tc2->message);
|
CuAssertTrue(tc, tc2->message == NULL);
|
||||||
|
|
||||||
CuAssertDblEquals(tc2, x, y, 0.01);
|
CuAssertDblEquals(tc2, x, y, 0.01);
|
||||||
CuAssertTrue(tc, ! tc2->failed);
|
CuAssertTrue(tc, ! tc2->failed);
|
||||||
CuAssertTrue(tc, NULL == tc2->message);
|
CuAssertTrue(tc, tc2->message == NULL);
|
||||||
|
|
||||||
tc2->jumpBuf = &buf;
|
tc2->jumpBuf = &buf;
|
||||||
if (setjmp(buf) == 0)
|
if (setjmp(buf) == 0)
|
||||||
|
2
Makefile
2
Makefile
@ -12,7 +12,7 @@ test: bin/CuTestTest
|
|||||||
@bin/CuTestTest
|
@bin/CuTestTest
|
||||||
|
|
||||||
bin/CuTestTest: AllTests.c CuTestTest.c CuTest.c | bin
|
bin/CuTestTest: AllTests.c CuTestTest.c CuTest.c | bin
|
||||||
$(CC) $(CFLAGS) $(INCLUDES) -lm -o $@ $^
|
$(CC) $(CFLAGS) $(INCLUDES) -Wno-address -lm -o $@ $^
|
||||||
|
|
||||||
clean:
|
clean:
|
||||||
@rm -rf *~ bin
|
@rm -rf *~ bin
|
||||||
|
Loading…
Reference in New Issue
Block a user