update cutest

This commit is contained in:
John Bintz 2024-02-28 13:10:57 -05:00
parent 4cc709c2a0
commit dd52b6da67
3 changed files with 26 additions and 18 deletions

View File

@ -1,3 +1,6 @@
#ifdef _MSC_VER
#define _CRT_SECURE_NO_WARNINGS
#endif
#include <assert.h>
#include <setjmp.h>
#include <stdlib.h>
@ -11,7 +14,7 @@
* CuStr
*-------------------------------------------------------------------------*/
char* CuStrAlloc(int size)
char* CuStrAlloc(size_t size)
{
char* newStr = (char*) malloc( sizeof(char) * (size) );
return newStr;
@ -19,7 +22,7 @@ char* CuStrAlloc(int size)
char* CuStrCopy(const char* old)
{
int len = strlen(old);
size_t len = strlen(old);
char* newStr = CuStrAlloc(len + 1);
strcpy(newStr, old);
return newStr;
@ -54,7 +57,7 @@ void CuStringDelete(CuString *str)
free(str);
}
void CuStringResize(CuString* str, int newSize)
void CuStringResize(CuString* str, size_t newSize)
{
str->buffer = (char*) realloc(str->buffer, sizeof(char) * newSize);
str->size = newSize;
@ -62,7 +65,7 @@ void CuStringResize(CuString* str, int newSize)
void CuStringAppend(CuString* str, const char* text)
{
int length;
size_t length;
if (text == NULL) {
text = "NULL";
@ -93,9 +96,9 @@ void CuStringAppendFormat(CuString* str, const char* format, ...)
CuStringAppend(str, buf);
}
void CuStringInsert(CuString* str, const char* text, int pos)
void CuStringInsert(CuString* str, const char* text, size_t pos)
{
int length = strlen(text);
size_t length = strlen(text);
if (pos > str->length)
pos = str->length;
if (str->length + length + 1 >= str->size)
@ -114,7 +117,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 +132,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 +157,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 +333,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");

View File

@ -3,12 +3,13 @@
#include <setjmp.h>
#include <stdarg.h>
#include <stddef.h>
#define CUTEST_VERSION "CuTest 1.5"
#define CUTEST_VERSION "CuTest 1.5c"
/* CuString */
char* CuStrAlloc(int size);
char* CuStrAlloc(size_t size);
char* CuStrCopy(const char* old);
#define CU_ALLOC(TYPE) ((TYPE*) malloc(sizeof(TYPE)))
@ -19,8 +20,8 @@ char* CuStrCopy(const char* old);
typedef struct
{
int length;
int size;
size_t length;
size_t size;
char* buffer;
} CuString;
@ -30,8 +31,8 @@ void CuStringRead(CuString* str, const char* path);
void CuStringAppend(CuString* str, const char* text);
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 CuStringInsert(CuString* str, const char* text, size_t pos);
void CuStringResize(CuString* str, size_t newSize);
void CuStringDelete(CuString* str);
/* CuTest */
@ -46,7 +47,7 @@ struct CuTest
TestFunction function;
int failed;
int ran;
const char* message;
CuString *message;
jmp_buf *jumpBuf;
};
@ -86,8 +87,8 @@ void CuAssertPtrEquals_LineMsg(CuTest* tc,
#define CuAssertPtrEquals(tc,ex,ac) CuAssertPtrEquals_LineMsg((tc),__FILE__,__LINE__,NULL,(ex),(ac))
#define CuAssertPtrEquals_Msg(tc,ms,ex,ac) CuAssertPtrEquals_LineMsg((tc),__FILE__,__LINE__,(ms),(ex),(ac))
#define CuAssertPtrNotNull(tc,p) CuAssert_Line((tc),__FILE__,__LINE__,"null pointer unexpected",(p != NULL))
#define CuAssertPtrNotNullMsg(tc,msg,p) CuAssert_Line((tc),__FILE__,__LINE__,(msg),(p != NULL))
#define CuAssertPtrNotNull(tc,p) CuAssert_Line((tc),__FILE__,__LINE__,"null pointer unexpected",((p) != NULL))
#define CuAssertPtrNotNullMsg(tc,msg,p) CuAssert_Line((tc),__FILE__,__LINE__,(msg),((p) != NULL))
/* CuSuite */

View File

@ -24,5 +24,6 @@ test: test.c spawn_test.c spawn.c .SYMBOLIC
clean: .SYMBOLIC
rm *.o
rm *.err
rm system/*.o
rm system/*.lib