diff --git a/CuTest.c b/CuTest.c index d9e325d..c675482 100644 --- a/CuTest.c +++ b/CuTest.c @@ -105,6 +105,27 @@ void CuStringInsert(CuString* str, const char* text, int pos) memcpy(str->buffer + pos, text, length); } +/** + * CuMemCmp - compare memory areas + * @m1, @m2: memory addresses to test + * @n: number of bytes to test + * + * m1 and m2 must ne non NULL. + * + * @Return: unsigned integer: + * 0 if equal + * (1 + pos) if *(m1 + pos) and *(m2 + pos) differ. + */ +size_t CuMemCmp(const void *m1, const void *m2, size_t n) +{ + uchar *p1 = (uchar *) m1, *p2 = (uchar *) m2, *start = p1; + for (; n; --n) { + if (*p1++ != *p2++) + return p1 - start; + } + return 0; +} + /*-------------------------------------------------------------------------* * CuTest *-------------------------------------------------------------------------*/ @@ -202,6 +223,42 @@ void CuAssertStrEquals_LineMsg(CuTest* tc, const char* file, int line, const cha CuFailInternal(tc, file, line, &string); } +void CuAssertMemEquals_LineMsg(CuTest* tc, const char* file, int line, const char* message, + const char* expected, const char* actual, const int len) +{ + size_t diffc = 0; + /* below, len*2*2 is to display twice len hex strings, 128 is for extra chars. + * This is rather conservative. + */ + char buf[128 + len * 2 * 2]; + + if (! (expected || actual)) /* both NULL */ + return; + if (! (expected && actual)) { /* one is null */ + sprintf(buf, "%s pointer is null", expected ? "actual": "expected"); + } else { + if (!(diffc = CuMemCmp(expected, actual, len))) + return; + } + if (diffc--) { /* compare was done and wrong */ + size_t i; + char common[len * 2 + 1]; + /* we will build the "common" mem string + */ + printf("len=%d diffc = %lu\n", len, diffc); + for (i = 0; i < diffc; ++i) + sprintf(common + i * 2, "%02X", *(expected + i)); + /* and final message + */ + sprintf(buf, + "expected <0x%s-%02X...> but was <0x%s-%02X...> (differ char %lu)\n", + common, *(expected + i), + common, *(actual + i), + diffc); + } + CuFail_Line(tc, file, line, message, buf); +} + void CuAssertIntEquals_LineMsg(CuTest* tc, const char* file, int line, const char* message, int expected, int actual) { @@ -211,12 +268,31 @@ void CuAssertIntEquals_LineMsg(CuTest* tc, const char* file, int line, const cha CuFail_Line(tc, file, line, message, buf); } +void CuAssertU32Equals_LineMsg(CuTest* tc, const char* file, int line, + const char* message, + u32 expected, u32 actual) +{ + char buf[STRING_MAX]; + if (expected == actual) return; + sprintf(buf, "expected <%#010x> but was <%#010x>", expected, actual); + CuFail_Line(tc, file, line, message, buf); +} + +void CuAssertU64Equals_LineMsg(CuTest* tc, const char* file, int line, + const char* message, + u64 expected, u64 actual) +{ + char buf[STRING_MAX]; + if (expected == actual) return; + sprintf(buf, "expected <%#016llx> but was <%#016llx>", expected, actual); + CuFail_Line(tc, file, line, message, buf); +} + void CuAssertDblEquals_LineMsg(CuTest* tc, const char* file, int line, const char* message, double expected, double actual, double delta) { char buf[STRING_MAX]; if (fabs(expected - actual) <= delta) return; -/* sprintf(buf, "expected <%lf> but was <%lf>", expected, actual); */ sprintf(buf, "expected <%f> but was <%f>", expected, actual); CuFail_Line(tc, file, line, message, buf); diff --git a/CuTest.h b/CuTest.h index 5c8b4bb..784c599 100644 --- a/CuTest.h +++ b/CuTest.h @@ -3,6 +3,17 @@ #include #include +#include + +#ifndef u32 +#define u32 unsigned int +#endif +#ifndef u4 +#define u64 unsigned long long int +#endif +#ifndef uchar +#define uchar unsigned char +#endif /* CuString */ @@ -31,6 +42,7 @@ 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); +size_t CuMemCmp(const void *m1, const void *m2, size_t n); /* CuTest */ @@ -59,9 +71,20 @@ 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 CuAssertMemEquals_LineMsg(CuTest* tc, + const char *file, int line, const char *message, + const char *expected, const char *actual, const int len); +void CuAssertMem_LineMsg(CuTest* tc, const char* file, int line, const char* message, + const char* expected, const char* actual, const int len); void CuAssertIntEquals_LineMsg(CuTest* tc, const char* file, int line, const char* message, int expected, int actual); +void CuAssertU32Equals_LineMsg(CuTest* tc, + const char* file, int line, const char* message, + u32 expected, u32 actual); +void CuAssertU64Equals_LineMsg(CuTest* tc, + const char* file, int line, const char* message, + u64 expected, u64 actual); void CuAssertDblEquals_LineMsg(CuTest* tc, const char* file, int line, const char* message, double expected, double actual, double delta); @@ -77,8 +100,19 @@ 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 CuAssertMemEquals(tc,ex,ac,l) CuAssertMemEquals_LineMsg((tc),__FILE__,__LINE__,NULL,(ex),(ac),(l)) +#define CuAssertMemEquals_Msg(tc,ms,ex,ac,l) CuAssertMemEquals_LineMsg((tc),__FILE__,__LINE__,(ms),(ex),(ac),(l)) + #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 CuAssertU32Equals(tc,ex,ac) CuAssertU32Equals_LineMsg((tc),__FILE__,__LINE__,NULL,(ex),(ac)) +#define CuAssertU32Equals_msg(tc,ms,ex,ac) CuAssertU32Equals_LineMsg((tc),__FILE__,__LINE__,(ms),(ex),(ac)) + +#define CuAssertU64Equals(tc,ex,ac) CuAssertU64Equals_LineMsg((tc),__FILE__,__LINE__,NULL,(ex),(ac)) +#define CuAssertU64Equals_msg(tc,ms,ex,ac) CuAssertU64Equals_LineMsg((tc),__FILE__,__LINE__,(ms),(ex),(ac)) + #define CuAssertDblEquals(tc,ex,ac,dl) CuAssertDblEquals_LineMsg((tc),__FILE__,__LINE__,NULL,(ex),(ac),(dl)) #define CuAssertDblEquals_Msg(tc,ms,ex,ac,dl) CuAssertDblEquals_LineMsg((tc),__FILE__,__LINE__,(ms),(ex),(ac),(dl)) #define CuAssertPtrEquals(tc,ex,ac) CuAssertPtrEquals_LineMsg((tc),__FILE__,__LINE__,NULL,(ex),(ac))