diff --git a/CuTest.c b/CuTest.c index 52f5a89..8f61199 100644 --- a/CuTest.c +++ b/CuTest.c @@ -19,7 +19,7 @@ char* CuStrAlloc(int size) char* CuStrCopy(const char* old) { - int len = (int)strlen(old); + int len = strlen(old); char* newStr = CuStrAlloc(len + 1); strcpy(newStr, old); return newStr; @@ -47,6 +47,13 @@ CuString* CuStringNew(void) return str; } +void CuStringDelete(CuString *str) +{ + if (!str) return; + free(str->buffer); + free(str); +} + void CuStringResize(CuString* str, int newSize) { str->buffer = (char*) realloc(str->buffer, sizeof(char) * newSize); @@ -61,7 +68,7 @@ void CuStringAppend(CuString* str, const char* text) text = "NULL"; } - length = (int)strlen(text); + length = strlen(text); if (str->length + length + 1 >= str->size) CuStringResize(str, str->length + length + 1 + STRING_INC); str->length += length; @@ -88,7 +95,7 @@ void CuStringAppendFormat(CuString* str, const char* format, ...) void CuStringInsert(CuString* str, const char* text, int pos) { - int length = (int)strlen(text); + int length = strlen(text); if (pos > str->length) pos = str->length; if (str->length + length + 1 >= str->size) @@ -119,6 +126,13 @@ CuTest* CuTestNew(const char* name, TestFunction function) return tc; } +void CuTestDelete(CuTest *t) +{ + if (!t) return; + free(t->name); + free(t); +} + void CuTestRun(CuTest* tc) { jmp_buf buf; @@ -202,7 +216,8 @@ void CuAssertDblEquals_LineMsg(CuTest* tc, const char* file, int line, const cha { char buf[STRING_MAX]; if (fabs(expected - actual) <= delta) return; - sprintf(buf, "expected <%f> but was <%f>", expected, actual); + sprintf(buf, "expected <%f> but was <%f>", expected, actual); + CuFail_Line(tc, file, line, message, buf); } @@ -224,6 +239,7 @@ void CuSuiteInit(CuSuite* testSuite) { testSuite->count = 0; testSuite->failCount = 0; + memset(testSuite->list, 0, sizeof(testSuite->list)); } CuSuite* CuSuiteNew(void) @@ -233,6 +249,20 @@ CuSuite* CuSuiteNew(void) return testSuite; } +void CuSuiteDelete(CuSuite *testSuite) +{ + unsigned int n; + for (n=0; n < MAX_TEST_CASES; n++) + { + if (testSuite->list[n]) + { + CuTestDelete(testSuite->list[n]); + } + } + free(testSuite); + +} + void CuSuiteAdd(CuSuite* testSuite, CuTest *testCase) { assert(testSuite->count < MAX_TEST_CASES); diff --git a/CuTest.h b/CuTest.h index 78c4146..8b32773 100644 --- a/CuTest.h +++ b/CuTest.h @@ -4,6 +4,8 @@ #include #include +#define CUTEST_VERSION "CuTest 1.5" + /* CuString */ char* CuStrAlloc(int size); @@ -30,6 +32,7 @@ void CuStringAppendChar(CuString* str, char ch); void CuStringAppendFormat(CuString* str, const char* format, ...); void CuStringInsert(CuString* str, const char* text, int pos); void CuStringResize(CuString* str, int newSize); +void CuStringDelete(CuString* str); /* CuTest */ @@ -39,7 +42,7 @@ typedef void (*TestFunction)(CuTest *); struct CuTest { - const char* name; + char* name; TestFunction function; int failed; int ran; @@ -50,6 +53,7 @@ struct CuTest void CuTestInit(CuTest* t, const char* name, TestFunction function); CuTest* CuTestNew(const char* name, TestFunction function); void CuTestRun(CuTest* tc); +void CuTestDelete(CuTest *t); /* Internal versions of assert functions -- use the public versions */ void CuFail_Line(CuTest* tc, const char* file, int line, const char* message2, const char* message); @@ -102,6 +106,7 @@ typedef struct void CuSuiteInit(CuSuite* testSuite); CuSuite* CuSuiteNew(void); +void CuSuiteDelete(CuSuite *testSuite); void CuSuiteAdd(CuSuite* testSuite, CuTest *testCase); void CuSuiteAddSuite(CuSuite* testSuite, CuSuite* testSuite2); void CuSuiteRun(CuSuite* testSuite); diff --git a/README b/README index 425381a..12b9796 100644 --- a/README +++ b/README @@ -202,8 +202,10 @@ AllTests.c or dealing with any of the other suite code. CREDITS -[02.23.2003] Dave Glowacki has added -(1) file name and line numbers to the error messages, (2) -AssertDblEquals for doubles, (3) AssertEquals_Msg version of -all the AssertEquals to pass in optional message which is -printed out on assert failure. +These people have contributed useful code changes to the CuTest project. +Thanks! + +- [02.23.2003] Dave Glowacki +- [04.17.2009] Tobias Lippert +- [11.13.2009] Eli Bendersky +- [12.14.2009] Andrew Brown diff --git a/make-tests.sh b/make-tests.sh index 42f27ce..3988c5e 100644 --- a/make-tests.sh +++ b/make-tests.sh @@ -1,51 +1,55 @@ -#!/usr/local/bin/bash - -# Auto generate single AllTests file for CuTest. -# Searches through all *.c files in the current directory. -# Prints to stdout. -# Author: Asim Jalis -# Date: 01/08/2003 - -if test $# -eq 0 ; then FILES=*.c ; else FILES=$* ; fi - -echo ' - -/* This is auto-generated code. Edit at your own peril. */ - -#include "CuTest.h" - -' - -cat $FILES | grep '^void Test' | - sed -e 's/(.*$//' \ - -e 's/$/(CuTest*);/' \ - -e 's/^/extern /' - -echo \ -' - -void RunAllTests(void) -{ - CuString *output = CuStringNew(); - CuSuite* suite = CuSuiteNew(); - -' -cat $FILES | grep '^void Test' | - sed -e 's/^void //' \ - -e 's/(.*$//' \ - -e 's/^/ SUITE_ADD_TEST(suite, /' \ - -e 's/$/);/' - -echo \ -' - CuSuiteRun(suite); - CuSuiteSummary(suite, output); - CuSuiteDetails(suite, output); - printf("%s\n", output->buffer); -} - -int main(void) -{ - RunAllTests(); -} -' +#!/usr/bin/env bash + +# Auto generate single AllTests file for CuTest. +# Searches through all *.c files in the current directory. +# Prints to stdout. +# Author: Asim Jalis +# Date: 01/08/2003 + +if test $# -eq 0 ; then FILES=*.c ; else FILES=$* ; fi + +echo ' + +/* This is auto-generated code. Edit at your own peril. */ +#include +#include + +#include "CuTest.h" + +' + +cat $FILES | grep '^void Test' | + sed -e 's/(.*$//' \ + -e 's/$/(CuTest*);/' \ + -e 's/^/extern /' + +echo \ +' + +void RunAllTests(void) +{ + CuString *output = CuStringNew(); + CuSuite* suite = CuSuiteNew(); + +' +cat $FILES | grep '^void Test' | + sed -e 's/^void //' \ + -e 's/(.*$//' \ + -e 's/^/ SUITE_ADD_TEST(suite, /' \ + -e 's/$/);/' + +echo \ +' + CuSuiteRun(suite); + CuSuiteSummary(suite, output); + CuSuiteDetails(suite, output); + printf("%s\n", output->buffer); + CuStringDelete(output); + CuSuiteDelete(suite); +} + +int main(void) +{ + RunAllTests(); +} +'