Compare commits
4 Commits
caa802c98a
...
4497561ccf
Author | SHA1 | Date | |
---|---|---|---|
4497561ccf | |||
1da9d3e65e | |||
719ff16481 | |||
80426666be |
78
CuTest.c
78
CuTest.c
@@ -105,6 +105,27 @@ void CuStringInsert(CuString* str, const char* text, int pos)
|
|||||||
memcpy(str->buffer + pos, text, length);
|
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
|
* CuTest
|
||||||
*-------------------------------------------------------------------------*/
|
*-------------------------------------------------------------------------*/
|
||||||
@@ -202,6 +223,42 @@ void CuAssertStrEquals_LineMsg(CuTest* tc, const char* file, int line, const cha
|
|||||||
CuFailInternal(tc, file, line, &string);
|
CuFailInternal(tc, file, line, &string);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void CuAssertMemEquals_LineMsg(CuTest* tc, const char* file, int line, const char* message,
|
||||||
|
const void *expected, const void *actual, const int len)
|
||||||
|
{
|
||||||
|
size_t diffc = 0;
|
||||||
|
const uchar *exp = expected, *act = actual;
|
||||||
|
/* 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 (! (exp || act)) /* both NULL */
|
||||||
|
return;
|
||||||
|
if (! (exp && act)) { /* one is null */
|
||||||
|
sprintf(buf, "<%s> pointer is null", exp ? "actual": "expected");
|
||||||
|
} else {
|
||||||
|
if (!(diffc = CuMemCmp(exp, act, len)))
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (diffc--) { /* compare was done and wrong */
|
||||||
|
size_t i;
|
||||||
|
char common[len * 2 + 1];
|
||||||
|
/* we will build the "common" mem string
|
||||||
|
*/
|
||||||
|
for (i = 0; i < diffc; ++i)
|
||||||
|
sprintf(common + i * 2, "%02X", *(exp + i));
|
||||||
|
/* and final message
|
||||||
|
*/
|
||||||
|
sprintf(buf,
|
||||||
|
"expected <0x%s[%02X]...> but was <0x%s[%02X]...> (differ at char %lu)\n",
|
||||||
|
common, *(exp + i),
|
||||||
|
common, *(act + i),
|
||||||
|
diffc);
|
||||||
|
}
|
||||||
|
CuFail_Line(tc, file, line, message, buf);
|
||||||
|
}
|
||||||
|
|
||||||
void CuAssertIntEquals_LineMsg(CuTest* tc, const char* file, int line, const char* message,
|
void CuAssertIntEquals_LineMsg(CuTest* tc, const char* file, int line, const char* message,
|
||||||
int expected, int actual)
|
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);
|
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,
|
void CuAssertDblEquals_LineMsg(CuTest* tc, const char* file, int line, const char* message,
|
||||||
double expected, double actual, double delta)
|
double expected, double actual, double delta)
|
||||||
{
|
{
|
||||||
char buf[STRING_MAX];
|
char buf[STRING_MAX];
|
||||||
if (fabs(expected - actual) <= delta) return;
|
if (fabs(expected - actual) <= delta) return;
|
||||||
/* sprintf(buf, "expected <%lf> but was <%lf>", expected, actual); */
|
|
||||||
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);
|
CuFail_Line(tc, file, line, message, buf);
|
||||||
|
34
CuTest.h
34
CuTest.h
@@ -3,6 +3,17 @@
|
|||||||
|
|
||||||
#include <setjmp.h>
|
#include <setjmp.h>
|
||||||
#include <stdarg.h>
|
#include <stdarg.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
#ifndef u32
|
||||||
|
#define u32 unsigned int
|
||||||
|
#endif
|
||||||
|
#ifndef u4
|
||||||
|
#define u64 unsigned long long int
|
||||||
|
#endif
|
||||||
|
#ifndef uchar
|
||||||
|
#define uchar unsigned char
|
||||||
|
#endif
|
||||||
|
|
||||||
/* CuString */
|
/* CuString */
|
||||||
|
|
||||||
@@ -31,6 +42,7 @@ void CuStringAppendFormat(CuString* str, const char* format, ...);
|
|||||||
void CuStringInsert(CuString* str, const char* text, int pos);
|
void CuStringInsert(CuString* str, const char* text, int pos);
|
||||||
void CuStringResize(CuString* str, int newSize);
|
void CuStringResize(CuString* str, int newSize);
|
||||||
void CuStringDelete(CuString* str);
|
void CuStringDelete(CuString* str);
|
||||||
|
size_t CuMemCmp(const void *m1, const void *m2, size_t n);
|
||||||
|
|
||||||
/* CuTest */
|
/* CuTest */
|
||||||
|
|
||||||
@@ -59,9 +71,20 @@ void CuAssert_Line(CuTest* tc, const char* file, int line, const char* message,
|
|||||||
void CuAssertStrEquals_LineMsg(CuTest* tc,
|
void CuAssertStrEquals_LineMsg(CuTest* tc,
|
||||||
const char* file, int line, const char* message,
|
const char* file, int line, const char* message,
|
||||||
const char* expected, const char* actual);
|
const char* expected, const char* actual);
|
||||||
|
void CuAssertMemEquals_LineMsg(CuTest* tc,
|
||||||
|
const char *file, int line, const char *message,
|
||||||
|
const void *expected, const void *actual, const int len);
|
||||||
|
void CuAssertMem_LineMsg(CuTest* tc, const char* file, int line, const char* message,
|
||||||
|
const void *expected, const void *actual, const int len);
|
||||||
void CuAssertIntEquals_LineMsg(CuTest* tc,
|
void CuAssertIntEquals_LineMsg(CuTest* tc,
|
||||||
const char* file, int line, const char* message,
|
const char* file, int line, const char* message,
|
||||||
int expected, int actual);
|
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,
|
void CuAssertDblEquals_LineMsg(CuTest* tc,
|
||||||
const char* file, int line, const char* message,
|
const char* file, int line, const char* message,
|
||||||
double expected, double actual, double delta);
|
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(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 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(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 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(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 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))
|
#define CuAssertPtrEquals(tc,ex,ac) CuAssertPtrEquals_LineMsg((tc),__FILE__,__LINE__,NULL,(ex),(ac))
|
||||||
|
@@ -6,40 +6,41 @@
|
|||||||
# Author: Asim Jalis
|
# Author: Asim Jalis
|
||||||
# Date: 01/08/2003
|
# Date: 01/08/2003
|
||||||
|
|
||||||
if test $# -eq 0 ; then FILES=*.c ; else FILES=$* ; fi
|
if test $# -eq 0 ; then
|
||||||
|
FILES=(*.c)
|
||||||
echo '
|
else
|
||||||
|
FILES=("$@") ; fi
|
||||||
|
|
||||||
|
cat << _EOF
|
||||||
/* This is auto-generated code. Edit at your own peril. */
|
/* This is auto-generated code. Edit at your own peril. */
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
|
||||||
#include "CuTest.h"
|
#include "CuTest.h"
|
||||||
|
|
||||||
'
|
_EOF
|
||||||
|
|
||||||
cat $FILES | grep '^void Test' |
|
cat "${FILES[@]}" | grep '^void Test' |
|
||||||
sed -e 's/(.*$//' \
|
sed -e 's/(.*$//' \
|
||||||
-e 's/$/(CuTest*);/' \
|
-e 's/$/(CuTest*);/' \
|
||||||
-e 's/^/extern /'
|
-e 's/^/extern /'
|
||||||
|
|
||||||
echo \
|
cat << _EOF
|
||||||
'
|
|
||||||
|
|
||||||
void RunAllTests(void)
|
void RunAllTests(void)
|
||||||
{
|
{
|
||||||
CuString *output = CuStringNew();
|
CuString *output = CuStringNew();
|
||||||
CuSuite* suite = CuSuiteNew();
|
CuSuite* suite = CuSuiteNew();
|
||||||
|
|
||||||
'
|
_EOF
|
||||||
cat $FILES | grep '^void Test' |
|
|
||||||
|
cat "${FILES[@]}" | grep '^void Test' |
|
||||||
sed -e 's/^void //' \
|
sed -e 's/^void //' \
|
||||||
-e 's/(.*$//' \
|
-e 's/(.*$//' \
|
||||||
-e 's/^/ SUITE_ADD_TEST(suite, /' \
|
-e 's/^/ SUITE_ADD_TEST(suite, /' \
|
||||||
-e 's/$/);/'
|
-e 's/$/);/'
|
||||||
|
|
||||||
echo \
|
cat << _EOF
|
||||||
'
|
|
||||||
CuSuiteRun(suite);
|
CuSuiteRun(suite);
|
||||||
CuSuiteSummary(suite, output);
|
CuSuiteSummary(suite, output);
|
||||||
CuSuiteDetails(suite, output);
|
CuSuiteDetails(suite, output);
|
||||||
@@ -52,4 +53,4 @@ int main(void)
|
|||||||
{
|
{
|
||||||
RunAllTests();
|
RunAllTests();
|
||||||
}
|
}
|
||||||
'
|
_EOF
|
||||||
|
Reference in New Issue
Block a user