mirror of
https://github.com/ThrowTheSwitch/Unity.git
synced 2025-06-06 13:08:41 +08:00
- happier with const (and more optimized on some compilers)
- better helper examples - general purpose memory compare git-svn-id: http://unity.svn.sourceforge.net/svnroot/unity/trunk@16 e7d17a6e-8845-0410-bbbc-c8efb4fdad7e
This commit is contained in:
@ -59,7 +59,7 @@ class UnityTestRunnerGenerator
|
||||
def find_mocks(includes)
|
||||
mock_headers = []
|
||||
includes.each do |include_file|
|
||||
mock_headers << include_file if include_file.include? "Mock"
|
||||
mock_headers << include_file if (include_file =~ /^mock/i)
|
||||
end
|
||||
return mock_headers
|
||||
end
|
||||
|
@ -40,8 +40,6 @@ void AssertFloatArrayWithin(const float tolerance, const float* expected, const
|
||||
|
||||
void AssertEqualExampleStruct(EXAMPLE_STRUCT_T expected, EXAMPLE_STRUCT_T actual)
|
||||
{
|
||||
sprintf(message, "Example Struct Failed For Field x", i);
|
||||
TEST_ASSERT_EQUAL_INT_MESSAGE(expected.x, actual.x, message);
|
||||
sprintf(message, "Example Struct Failed For Field y", i);
|
||||
TEST_ASSERT_EQUAL_INT_MESSAGE(expected.y, actual.y, message);
|
||||
TEST_ASSERT_EQUAL_INT_MESSAGE(expected.x, actual.x, "Example Struct Failed For Field x");
|
||||
TEST_ASSERT_EQUAL_INT_MESSAGE(expected.y, actual.y, "Example Struct Failed For Field y");
|
||||
}
|
||||
|
@ -1,20 +1,17 @@
|
||||
#ifndef _TESTHELPER_H
|
||||
#define _TESTHELPER_H
|
||||
|
||||
typedef struct _EXAMPLE_STRUCT_T
|
||||
{
|
||||
int x;
|
||||
int y;
|
||||
} EXAMPLE_STRUCT_T;
|
||||
#include "Types.h"
|
||||
|
||||
void AssertEqualUintArray(const unsigned int* expected, const unsigned int* actual, const unsigned int length);
|
||||
void AssertEqualIntArray(const int* expected, const int* actual, const unsigned int length);
|
||||
void AssertEqualCharArray(const char[] expected, const char actual[], const int length);
|
||||
void AssertEqualCharArray(const char expected[], const char actual[], const int length);
|
||||
void AssertFloatArrayWithin(const float tolerance, const float* expected, const float* actual, const unsigned int length);
|
||||
void AssertEqualExampleStruct(const EXAMPLE_STRUCT_T expected, const EXAMPLE_STRUCT_T actual);
|
||||
|
||||
#define TEST_ASSERT_EQUAL_UINT_ARRAY(expected, actual, length) {AssertEqualUintArray(expected, actual, length);}
|
||||
#define TEST_ASSERT_EQUAL_INT_ARRAY(expected, actual, length) {AssertEqualIntArray(expected, actual, length);}
|
||||
#define TEST_ASSERT_FLOAT_ARRAY_WITHIN(tolerance, expected, actual, length) {AssertFloatArrayWithin(tolerance, expected, actual, length);}
|
||||
#define TEST_ASSERT_EQUAL_EXAMPLE_STRUCT(expected, actual) {AssertEqualExampleStruct(expected, actual);}
|
||||
#define TEST_ASSERT_EQUAL_EXAMPLE_STRUCT_T(expected, actual) {AssertEqualExampleStruct(expected, actual);}
|
||||
|
||||
#endif // _TESTHELPER_H
|
||||
|
45
src/unity.c
45
src/unity.c
@ -316,6 +316,51 @@ void UnityAssertEqualString(const char *expected,
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void UnityAssertEqualMemory(void *expected,
|
||||
void *actual,
|
||||
unsigned int length,
|
||||
const char *msg,
|
||||
unsigned short lineNumber)
|
||||
{
|
||||
unsigned int i;
|
||||
if (length == 0)
|
||||
return;
|
||||
|
||||
// if both pointers not null compare the memory
|
||||
if (expected && actual)
|
||||
{
|
||||
if (memcmp(expected, actual, length) != 0)
|
||||
{
|
||||
Unity.CurrentTestFailed = 1;
|
||||
}
|
||||
}
|
||||
else
|
||||
{ // handle case of one pointers being null (if both null, test should pass)
|
||||
if (expected != actual)
|
||||
{
|
||||
Unity.CurrentTestFailed = 1;
|
||||
}
|
||||
}
|
||||
|
||||
if (Unity.CurrentTestFailed)
|
||||
{
|
||||
UnityTestResultsBegin(lineNumber);
|
||||
UnityPrint("Expected '");
|
||||
UnityPrint(expected);
|
||||
UnityPrint("' was '");
|
||||
UnityPrint(actual);
|
||||
UnityPrintChar('\'');
|
||||
UnityPrintChar('.');
|
||||
if (msg)
|
||||
{
|
||||
UnityPrintChar(' ');
|
||||
UnityPrint(msg);
|
||||
}
|
||||
UnityPrintChar('\n');
|
||||
}
|
||||
}
|
||||
|
||||
void UnityFail(const char *message, const int line)
|
||||
{
|
||||
Unity.CurrentTestFailed = 1;
|
||||
|
12
src/unity.h
12
src/unity.h
@ -64,6 +64,12 @@ void UnityAssertEqualString(const char *expected,
|
||||
const char *msg,
|
||||
const unsigned short lineNumber );
|
||||
|
||||
void UnityAssertEqualMemory(void *expected,
|
||||
void *actual,
|
||||
unsigned int length,
|
||||
const char *msg,
|
||||
const unsigned short lineNumber );
|
||||
|
||||
void UnityAssertFloatsWithin(const float delta,
|
||||
const float expected,
|
||||
const float actual,
|
||||
@ -193,6 +199,12 @@ void UnityIgnore(const char *message, const int line);
|
||||
ABORT_IF_NECESSARY();
|
||||
#define TEST_ASSERT_EQUAL_STRING(expected, actual) TEST_ASSERT_EQUAL_STRING_MESSAGE(expected, actual, NULL)
|
||||
|
||||
#define TEST_ASSERT_EQUAL_MEMORY_MESSAGE(expected, actual, len, message) \
|
||||
Unity.TestFile=__FILE__; \
|
||||
UnityAssertEqualMemory((expected), (actual), (len), (message), (unsigned short)__LINE__); \
|
||||
ABORT_IF_NECESSARY();
|
||||
#define TEST_ASSERT_EQUAL_MEMORY(expected, actual, len) TEST_ASSERT_EQUAL_MEMORY_MESSAGE(expected, actual, len, NULL)
|
||||
|
||||
#define TEST_FAIL(message) { Unity.TestFile=__FILE__; UnityFail((message), (unsigned short)__LINE__); TEST_ABORT(); }
|
||||
#define TEST_IGNORE_MESSAGE(message) { Unity.TestFile=__FILE__; UnityIgnore((message), (unsigned short)__LINE__); TEST_ABORT(); }
|
||||
#define TEST_IGNORE() TEST_IGNORE_MESSAGE(NULL)
|
||||
|
@ -553,6 +553,75 @@ void testNotEqualString_ActualStringIsNull(void)
|
||||
TEST_ASSERT_MESSAGE(1U == failed, "This is also expected");
|
||||
}
|
||||
|
||||
void testEqualMemory(void)
|
||||
{
|
||||
const char *testString = "whatever";
|
||||
|
||||
TEST_ASSERT_EQUAL_MEMORY(testString, testString, 8);
|
||||
TEST_ASSERT_EQUAL_MEMORY("whatever", "whatever", 8);
|
||||
TEST_ASSERT_EQUAL_MEMORY("whatever", testString, 8);
|
||||
TEST_ASSERT_EQUAL_MEMORY(testString, "whatever", 8);
|
||||
TEST_ASSERT_EQUAL_MEMORY(testString, "whatever", 0);
|
||||
TEST_ASSERT_EQUAL_MEMORY(NULL, NULL, 0);
|
||||
TEST_ASSERT_EQUAL_INT(0U, Unity.TestFailures);
|
||||
}
|
||||
|
||||
void testNotEqualMemory1(void)
|
||||
{
|
||||
int failed;
|
||||
|
||||
EXPECT_ABORT_BEGIN
|
||||
TEST_ASSERT_EQUAL_MEMORY("foo", "bar", 3);
|
||||
EXPECT_ABORT_END
|
||||
|
||||
failed = Unity.CurrentTestFailed;
|
||||
Unity.CurrentTestFailed = 0;
|
||||
|
||||
TEST_ASSERT_MESSAGE(1U == failed, "This is also expected");
|
||||
}
|
||||
|
||||
void testNotEqualMemory2(void)
|
||||
{
|
||||
int failed;
|
||||
|
||||
EXPECT_ABORT_BEGIN
|
||||
TEST_ASSERT_EQUAL_MEMORY("fool", "food", 4);
|
||||
EXPECT_ABORT_END
|
||||
|
||||
failed = Unity.CurrentTestFailed;
|
||||
Unity.CurrentTestFailed = 0;
|
||||
|
||||
TEST_ASSERT_MESSAGE(1U == failed, "This is also expected");
|
||||
}
|
||||
|
||||
void testNotEqualMemory3(void)
|
||||
{
|
||||
int failed;
|
||||
|
||||
EXPECT_ABORT_BEGIN
|
||||
TEST_ASSERT_EQUAL_MEMORY(NULL, "food", 4);
|
||||
EXPECT_ABORT_END
|
||||
|
||||
failed = Unity.CurrentTestFailed;
|
||||
Unity.CurrentTestFailed = 0;
|
||||
|
||||
TEST_ASSERT_MESSAGE(1U == failed, "This is also expected");
|
||||
}
|
||||
|
||||
void testNotEqualMemory4(void)
|
||||
{
|
||||
int failed;
|
||||
|
||||
EXPECT_ABORT_BEGIN
|
||||
TEST_ASSERT_EQUAL_MEMORY("fool", NULL, 4);
|
||||
EXPECT_ABORT_END
|
||||
|
||||
failed = Unity.CurrentTestFailed;
|
||||
Unity.CurrentTestFailed = 0;
|
||||
|
||||
TEST_ASSERT_MESSAGE(1U == failed, "This is also expected");
|
||||
}
|
||||
|
||||
void testProtection(void)
|
||||
{
|
||||
volatile int mask = 0;
|
||||
|
@ -43,6 +43,11 @@ void testNotEqualString2(void);
|
||||
void testNotEqualString3(void);
|
||||
void testNotEqualString_ExpectedStringIsNull(void);
|
||||
void testNotEqualString_ActualStringIsNull(void);
|
||||
void testEqualMemory(void);
|
||||
void testNotEqualMemory1(void);
|
||||
void testNotEqualMemory2(void);
|
||||
void testNotEqualMemory3(void);
|
||||
void testNotEqualMemory4(void);
|
||||
void testProtection(void);
|
||||
|
||||
|
||||
@ -103,6 +108,11 @@ int main(void)
|
||||
RUN_TEST(testNotEqualString3);
|
||||
RUN_TEST(testNotEqualString_ExpectedStringIsNull);
|
||||
RUN_TEST(testNotEqualString_ActualStringIsNull);
|
||||
RUN_TEST(testEqualMemory);
|
||||
RUN_TEST(testNotEqualMemory1);
|
||||
RUN_TEST(testNotEqualMemory2);
|
||||
RUN_TEST(testNotEqualMemory3);
|
||||
RUN_TEST(testNotEqualMemory4);
|
||||
RUN_TEST(testProtection);
|
||||
|
||||
UnityEnd();
|
||||
|
Reference in New Issue
Block a user