mirror of
https://github.com/ThrowTheSwitch/Unity.git
synced 2025-05-21 01:16:39 +08:00
- added string array support
- laid foundation for smarter array size handling for integer types git-svn-id: http://unity.svn.sourceforge.net/svnroot/unity/trunk@74 e7d17a6e-8845-0410-bbbc-c8efb4fdad7e
This commit is contained in:
100
src/unity.c
100
src/unity.c
@ -42,6 +42,7 @@ void UnityPrint(const char* string)
|
||||
}
|
||||
}
|
||||
|
||||
//-----------------------------------------------
|
||||
void UnityPrintNumberByStyle(const _US32 number, const UNITY_DISPLAY_STYLE_T style)
|
||||
{
|
||||
switch (style)
|
||||
@ -54,6 +55,7 @@ void UnityPrintNumberByStyle(const _US32 number, const UNITY_DISPLAY_STYLE_T sty
|
||||
}
|
||||
}
|
||||
|
||||
//-----------------------------------------------
|
||||
/// basically do an itoa using as little ram as possible
|
||||
void UnityPrintNumber(const _US32 number_to_print)
|
||||
{
|
||||
@ -86,6 +88,7 @@ void UnityPrintNumber(const _US32 number_to_print)
|
||||
while (divisor > 0);
|
||||
}
|
||||
|
||||
//-----------------------------------------------
|
||||
/// basically do an itoa using as little ram as possible
|
||||
void UnityPrintNumberUnsigned(const _UU32 number)
|
||||
{
|
||||
@ -111,6 +114,7 @@ void UnityPrintNumberUnsigned(const _UU32 number)
|
||||
while (divisor > 0);
|
||||
}
|
||||
|
||||
//-----------------------------------------------
|
||||
void UnityPrintNumberHex(const _UU32 number, const char nibbles_to_print)
|
||||
{
|
||||
_UU32 nibble;
|
||||
@ -131,6 +135,7 @@ void UnityPrintNumberHex(const _UU32 number, const char nibbles_to_print)
|
||||
}
|
||||
}
|
||||
|
||||
//-----------------------------------------------
|
||||
void UnityPrintMask(const _UU32 mask, const _UU32 number)
|
||||
{
|
||||
_UU32 bit = 0x80000000;
|
||||
@ -157,6 +162,7 @@ void UnityPrintMask(const _UU32 mask, const _UU32 number)
|
||||
}
|
||||
}
|
||||
|
||||
//-----------------------------------------------
|
||||
#ifdef UNITY_FLOAT_VERBOSE
|
||||
void UnityPrintFloat(_UF number)
|
||||
{
|
||||
@ -166,6 +172,7 @@ void UnityPrintFloat(_UF number)
|
||||
}
|
||||
#endif
|
||||
|
||||
//-----------------------------------------------
|
||||
void UnityTestResultsBegin(const char* file, const UNITY_LINE_TYPE line)
|
||||
{
|
||||
UnityPrint(file);
|
||||
@ -176,12 +183,14 @@ void UnityTestResultsBegin(const char* file, const UNITY_LINE_TYPE line)
|
||||
UNITY_OUTPUT_CHAR(':');
|
||||
}
|
||||
|
||||
//-----------------------------------------------
|
||||
void UnityTestResultsFailBegin(const UNITY_LINE_TYPE line)
|
||||
{
|
||||
UnityTestResultsBegin(Unity.TestFile, line);
|
||||
UnityPrint("FAIL:");
|
||||
}
|
||||
|
||||
//-----------------------------------------------
|
||||
void UnityConcludeTest()
|
||||
{
|
||||
if (Unity.CurrentTestIgnored)
|
||||
@ -202,6 +211,7 @@ void UnityConcludeTest()
|
||||
Unity.CurrentTestIgnored = 0;
|
||||
}
|
||||
|
||||
//-----------------------------------------------
|
||||
void UnityAddMsgIfSpecified(const char* msg)
|
||||
{
|
||||
if (msg)
|
||||
@ -233,6 +243,7 @@ void UnityAssertBits(const _US32 mask,
|
||||
}
|
||||
}
|
||||
|
||||
//-----------------------------------------------
|
||||
void UnityAssertEqualNumber(const _US32 expected,
|
||||
const _US32 actual,
|
||||
const char* msg,
|
||||
@ -251,6 +262,7 @@ void UnityAssertEqualNumber(const _US32 expected,
|
||||
}
|
||||
}
|
||||
|
||||
//-----------------------------------------------
|
||||
void UnityAssertEqualIntArray(const int* expected,
|
||||
const int* actual,
|
||||
const _UU32 num_elements,
|
||||
@ -277,6 +289,8 @@ void UnityAssertEqualIntArray(const int* expected,
|
||||
switch(style)
|
||||
{
|
||||
case UNITY_DISPLAY_STYLE_HEX8:
|
||||
case UNITY_DISPLAY_STYLE_INT8:
|
||||
case UNITY_DISPLAY_STYLE_UINT8:
|
||||
while (elements--)
|
||||
{
|
||||
if (*ptr_exp8++ != *ptr_act8++)
|
||||
@ -294,6 +308,8 @@ void UnityAssertEqualIntArray(const int* expected,
|
||||
}
|
||||
break;
|
||||
case UNITY_DISPLAY_STYLE_HEX16:
|
||||
case UNITY_DISPLAY_STYLE_INT16:
|
||||
case UNITY_DISPLAY_STYLE_UINT16:
|
||||
while (elements--)
|
||||
{
|
||||
if (*ptr_exp16++ != *ptr_act16++)
|
||||
@ -330,6 +346,7 @@ void UnityAssertEqualIntArray(const int* expected,
|
||||
}
|
||||
}
|
||||
|
||||
//-----------------------------------------------
|
||||
#ifndef UNITY_EXCLUDE_FLOAT
|
||||
void UnityAssertEqualFloatArray(const _UF* expected,
|
||||
const _UF* actual,
|
||||
@ -379,6 +396,7 @@ void UnityAssertEqualFloatArray(const _UF* expected,
|
||||
}
|
||||
}
|
||||
|
||||
//-----------------------------------------------
|
||||
void UnityAssertFloatsWithin(const _UF delta,
|
||||
const _UF expected,
|
||||
const _UF actual,
|
||||
@ -414,6 +432,7 @@ void UnityAssertFloatsWithin(const _UF delta,
|
||||
}
|
||||
#endif
|
||||
|
||||
//-----------------------------------------------
|
||||
void UnityAssertNumbersWithin( const _US32 delta,
|
||||
const _US32 expected,
|
||||
const _US32 actual,
|
||||
@ -450,6 +469,7 @@ void UnityAssertNumbersWithin( const _US32 delta,
|
||||
}
|
||||
}
|
||||
|
||||
//-----------------------------------------------
|
||||
void UnityAssertEqualString(const char* expected,
|
||||
const char* actual,
|
||||
const char* msg,
|
||||
@ -493,6 +513,83 @@ void UnityAssertEqualString(const char* expected,
|
||||
}
|
||||
}
|
||||
|
||||
//-----------------------------------------------
|
||||
void UnityAssertEqualStringArray( const char** expected,
|
||||
const char** actual,
|
||||
const _UU32 num_elements,
|
||||
const char* msg,
|
||||
const UNITY_LINE_TYPE lineNumber)
|
||||
{
|
||||
_UU32 i, j = 0;
|
||||
|
||||
// if no elements, it's an error
|
||||
if (num_elements == 0)
|
||||
{
|
||||
UnityTestResultsFailBegin(lineNumber);
|
||||
UnityPrint(UnityStrPointless);
|
||||
UnityAddMsgIfSpecified(msg);
|
||||
UNITY_FAIL_AND_BAIL;
|
||||
}
|
||||
|
||||
// if both are null, it passes. if one is null, it fails
|
||||
if ((expected == NULL) || (actual == NULL))
|
||||
{
|
||||
if (expected == actual)
|
||||
{
|
||||
return;
|
||||
}
|
||||
else
|
||||
{
|
||||
Unity.CurrentTestFailed = 1;
|
||||
UNITY_FAIL_AND_BAIL;
|
||||
}
|
||||
}
|
||||
|
||||
do
|
||||
{
|
||||
// if both pointers not null compare the strings
|
||||
if (expected[j] && actual[j])
|
||||
{
|
||||
for (i = 0; expected[j][i] || actual[j][i]; i++)
|
||||
{
|
||||
if (expected[j][i] != actual[j][i])
|
||||
{
|
||||
Unity.CurrentTestFailed = 1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{ // handle case of one pointers being null (if both null, test should pass)
|
||||
if (expected[j] != actual[j])
|
||||
{
|
||||
Unity.CurrentTestFailed = 1;
|
||||
}
|
||||
}
|
||||
|
||||
if (Unity.CurrentTestFailed)
|
||||
{
|
||||
UnityTestResultsFailBegin(lineNumber);
|
||||
if (num_elements > 1)
|
||||
{
|
||||
UnityPrint(UnityStrElement);
|
||||
UnityPrintNumberByStyle((num_elements - j - 1), UNITY_DISPLAY_STYLE_UINT);
|
||||
}
|
||||
UnityPrint(UnityStrExpected);
|
||||
UNITY_OUTPUT_CHAR('\'');
|
||||
UnityPrint(expected[j]);
|
||||
UNITY_OUTPUT_CHAR('\'');
|
||||
UnityPrint(UnityStrWas);
|
||||
UNITY_OUTPUT_CHAR('\'');
|
||||
UnityPrint(actual[j]);
|
||||
UNITY_OUTPUT_CHAR('\'');
|
||||
UnityAddMsgIfSpecified(msg);
|
||||
UNITY_FAIL_AND_BAIL;
|
||||
}
|
||||
} while (++j < num_elements);
|
||||
}
|
||||
|
||||
//-----------------------------------------------
|
||||
void UnityAssertEqualMemory( const void* expected,
|
||||
const void* actual,
|
||||
_UU32 length,
|
||||
@ -563,6 +660,7 @@ void UnityFail(const char* msg, const UNITY_LINE_TYPE line)
|
||||
UNITY_FAIL_AND_BAIL;
|
||||
}
|
||||
|
||||
//-----------------------------------------------
|
||||
void UnityIgnore(const char* msg, const UNITY_LINE_TYPE line)
|
||||
{
|
||||
UnityTestResultsBegin(Unity.TestFile, line);
|
||||
@ -575,11 +673,13 @@ void UnityIgnore(const char* msg, const UNITY_LINE_TYPE line)
|
||||
UNITY_IGNORE_AND_BAIL;
|
||||
}
|
||||
|
||||
//-----------------------------------------------
|
||||
void UnityBegin()
|
||||
{
|
||||
Unity.NumberOfTests = 0;
|
||||
}
|
||||
|
||||
//-----------------------------------------------
|
||||
void UnityEnd(void)
|
||||
{
|
||||
UnityPrint("-----------------------\n");
|
||||
|
@ -105,6 +105,7 @@
|
||||
#define TEST_ASSERT_EQUAL_HEX16_ARRAY(expected, actual, num_elements) UNITY_TEST_ASSERT_EQUAL_HEX16_ARRAY(expected, actual, num_elements, __LINE__, NULL)
|
||||
#define TEST_ASSERT_EQUAL_HEX32_ARRAY(expected, actual, num_elements) UNITY_TEST_ASSERT_EQUAL_HEX32_ARRAY(expected, actual, num_elements, __LINE__, NULL)
|
||||
#define TEST_ASSERT_EQUAL_HEX_ARRAY(expected, actual, num_elements) UNITY_TEST_ASSERT_EQUAL_HEX32_ARRAY(expected, actual, num_elements, __LINE__, NULL)
|
||||
#define TEST_ASSERT_EQUAL_STRING_ARRAY(expected, actual, num_elements) UNITY_TEST_ASSERT_EQUAL_STRING_ARRAY(expected, actual, num_elements, __LINE__, NULL)
|
||||
#define TEST_ASSERT_EQUAL_MEMORY_ARRAY(expected, actual, len, num_elements) UNITY_TEST_ASSERT_EQUAL_MEMORY_ARRAY(expected, actual, len, num_elements, __LINE__, NULL)
|
||||
|
||||
//Floating Point (If Enabled)
|
||||
@ -158,6 +159,7 @@
|
||||
#define TEST_ASSERT_EQUAL_HEX16_ARRAY_MESSAGE(expected, actual, num_elements, message) UNITY_TEST_ASSERT_EQUAL_HEX16_ARRAY(expected, actual, num_elements, __LINE__, message)
|
||||
#define TEST_ASSERT_EQUAL_HEX32_ARRAY_MESSAGE(expected, actual, num_elements, message) UNITY_TEST_ASSERT_EQUAL_HEX32_ARRAY(expected, actual, num_elements, __LINE__, message)
|
||||
#define TEST_ASSERT_EQUAL_HEX_ARRAY_MESSAGE(expected, actual, num_elements, message) UNITY_TEST_ASSERT_EQUAL_HEX32_ARRAY(expected, actual, num_elements, __LINE__, message)
|
||||
#define TEST_ASSERT_EQUAL_STRING_ARRAY_MESSAGE(expected, actual, num_elements, message) UNITY_TEST_ASSERT_EQUAL_STRING_ARRAY(expected, actual, num_elements, __LINE__, message)
|
||||
#define TEST_ASSERT_EQUAL_MEMORY_ARRAY_MESSAGE(expected, actual, len, num_elements, message) UNITY_TEST_ASSERT_EQUAL_MEMORY_ARRAY(expected, actual, len, num_elements, __LINE__, message)
|
||||
|
||||
//Floating Point (If Enabled)
|
||||
|
@ -89,7 +89,13 @@ typedef void (*UnityTestFunction)(void);
|
||||
typedef enum
|
||||
{
|
||||
UNITY_DISPLAY_STYLE_INT,
|
||||
UNITY_DISPLAY_STYLE_INT8,
|
||||
UNITY_DISPLAY_STYLE_INT16,
|
||||
UNITY_DISPLAY_STYLE_INT32,
|
||||
UNITY_DISPLAY_STYLE_UINT,
|
||||
UNITY_DISPLAY_STYLE_UINT8,
|
||||
UNITY_DISPLAY_STYLE_UINT16,
|
||||
UNITY_DISPLAY_STYLE_UINT32,
|
||||
UNITY_DISPLAY_STYLE_HEX8,
|
||||
UNITY_DISPLAY_STYLE_HEX16,
|
||||
UNITY_DISPLAY_STYLE_HEX32
|
||||
@ -165,6 +171,11 @@ void UnityAssertEqualString(const char* expected,
|
||||
const char* msg,
|
||||
const UNITY_LINE_TYPE lineNumber);
|
||||
|
||||
void UnityAssertEqualStringArray( const char** expected,
|
||||
const char** actual,
|
||||
const _UU32 num_elements,
|
||||
const char* msg,
|
||||
const UNITY_LINE_TYPE lineNumber);
|
||||
|
||||
void UnityAssertEqualMemory( const void* expected,
|
||||
const void* actual,
|
||||
@ -234,6 +245,7 @@ void UnityAssertEqualFloatArray(const _UF* expected,
|
||||
#define UNITY_TEST_ASSERT_EQUAL_HEX8_ARRAY(expected, actual, num_elements, line, message) UnityAssertEqualIntArray((const _US32*)(expected), (const _US32*)(actual), (_UU32)(num_elements), (message), (UNITY_LINE_TYPE)line, UNITY_DISPLAY_STYLE_HEX8)
|
||||
#define UNITY_TEST_ASSERT_EQUAL_HEX16_ARRAY(expected, actual, num_elements, line, message) UnityAssertEqualIntArray((const _US32*)(expected), (const _US32*)(actual), (_UU32)(num_elements), (message), (UNITY_LINE_TYPE)line, UNITY_DISPLAY_STYLE_HEX16)
|
||||
#define UNITY_TEST_ASSERT_EQUAL_HEX32_ARRAY(expected, actual, num_elements, line, message) UnityAssertEqualIntArray((const _US32*)(expected), (const _US32*)(actual), (_UU32)(num_elements), (message), (UNITY_LINE_TYPE)line, UNITY_DISPLAY_STYLE_HEX32)
|
||||
#define UNITY_TEST_ASSERT_EQUAL_STRING_ARRAY(expected, actual, num_elements, line, message) UnityAssertEqualStringArray((const char**)(expected), (const char**)(actual), (_UU32)(num_elements), (message), (UNITY_LINE_TYPE)line)
|
||||
#define UNITY_TEST_ASSERT_EQUAL_MEMORY_ARRAY(expected, actual, len, num_elements, line, message) UnityAssertEqualMemory((void*)(expected), (void*)(actual), (_UU32)(len), (_UU32)(num_elements), (message), (UNITY_LINE_TYPE)line)
|
||||
|
||||
#ifdef UNITY_EXCLUDE_FLOAT
|
||||
|
104
test/testunity.c
104
test/testunity.c
@ -913,6 +913,110 @@ void testNotEqualString_ActualStringIsNull(void)
|
||||
VERIFY_FAILURE_WAS_CAUGHT
|
||||
}
|
||||
|
||||
void testEqualStringArrays(void)
|
||||
{
|
||||
const char *testStrings[] = { "foo", "boo", "woo", "moo" };
|
||||
const char *expStrings[] = { "foo", "boo", "woo", "zoo" };
|
||||
|
||||
TEST_ASSERT_EQUAL_STRING_ARRAY(expStrings, expStrings, 3);
|
||||
TEST_ASSERT_EQUAL_STRING_ARRAY(expStrings, testStrings, 3);
|
||||
TEST_ASSERT_EQUAL_STRING_ARRAY(expStrings, testStrings, 2);
|
||||
TEST_ASSERT_EQUAL_STRING_ARRAY(expStrings, testStrings, 1);
|
||||
}
|
||||
|
||||
void testNotEqualStringArray1(void)
|
||||
{
|
||||
int failed;
|
||||
|
||||
const char *testStrings[] = { "foo", "boo", "woo", "moo" };
|
||||
const char *expStrings[] = { "foo", "boo", "woo", "zoo" };
|
||||
|
||||
EXPECT_ABORT_BEGIN
|
||||
TEST_ASSERT_EQUAL_STRING_ARRAY(expStrings, testStrings, 4);
|
||||
EXPECT_ABORT_END
|
||||
|
||||
failed = Unity.CurrentTestFailed;
|
||||
Unity.CurrentTestFailed = 0;
|
||||
|
||||
VERIFY_FAILURE_WAS_CAUGHT
|
||||
}
|
||||
|
||||
void testNotEqualStringArray2(void)
|
||||
{
|
||||
int failed;
|
||||
|
||||
const char *testStrings[] = { "zoo", "boo", "woo", "moo" };
|
||||
const char *expStrings[] = { "foo", "boo", "woo", "moo" };
|
||||
|
||||
EXPECT_ABORT_BEGIN
|
||||
TEST_ASSERT_EQUAL_STRING_ARRAY(expStrings, testStrings, 4);
|
||||
EXPECT_ABORT_END
|
||||
|
||||
failed = Unity.CurrentTestFailed;
|
||||
Unity.CurrentTestFailed = 0;
|
||||
|
||||
VERIFY_FAILURE_WAS_CAUGHT
|
||||
}
|
||||
|
||||
void testNotEqualStringArray3(void)
|
||||
{
|
||||
int failed;
|
||||
|
||||
const char *testStrings[] = { "foo", "boo", "woo", NULL };
|
||||
const char *expStrings[] = { "foo", "boo", "woo", "zoo" };
|
||||
|
||||
EXPECT_ABORT_BEGIN
|
||||
TEST_ASSERT_EQUAL_STRING_ARRAY(expStrings, testStrings, 4);
|
||||
EXPECT_ABORT_END
|
||||
|
||||
failed = Unity.CurrentTestFailed;
|
||||
Unity.CurrentTestFailed = 0;
|
||||
|
||||
VERIFY_FAILURE_WAS_CAUGHT
|
||||
}
|
||||
|
||||
void testNotEqualStringArray4(void)
|
||||
{
|
||||
int failed;
|
||||
|
||||
const char *testStrings[] = { "foo", "boo", "woo", "moo" };
|
||||
const char *expStrings[] = { "foo", NULL, "woo", "moo" };
|
||||
|
||||
EXPECT_ABORT_BEGIN
|
||||
TEST_ASSERT_EQUAL_STRING_ARRAY(expStrings, testStrings, 4);
|
||||
EXPECT_ABORT_END
|
||||
|
||||
failed = Unity.CurrentTestFailed;
|
||||
Unity.CurrentTestFailed = 0;
|
||||
|
||||
VERIFY_FAILURE_WAS_CAUGHT
|
||||
}
|
||||
|
||||
void testNotEqualStringArray5(void)
|
||||
{
|
||||
int failed;
|
||||
|
||||
const char **testStrings = NULL;
|
||||
const char *expStrings[] = { "foo", "boo", "woo", "zoo" };
|
||||
|
||||
EXPECT_ABORT_BEGIN
|
||||
TEST_ASSERT_EQUAL_STRING_ARRAY(expStrings, testStrings, 4);
|
||||
EXPECT_ABORT_END
|
||||
|
||||
failed = Unity.CurrentTestFailed;
|
||||
Unity.CurrentTestFailed = 0;
|
||||
|
||||
VERIFY_FAILURE_WAS_CAUGHT
|
||||
}
|
||||
|
||||
void testEqualStringArrayIfBothNulls(void)
|
||||
{
|
||||
const char **testStrings = NULL;
|
||||
const char **expStrings = NULL;
|
||||
|
||||
TEST_ASSERT_EQUAL_STRING_ARRAY(expStrings, testStrings, 4);
|
||||
}
|
||||
|
||||
void testEqualMemory(void)
|
||||
{
|
||||
const char *testString = "whatever";
|
||||
|
Reference in New Issue
Block a user