Skip to content

fix: Correct UINT max value handling #784

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
185 changes: 124 additions & 61 deletions src/unity.c
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ void UnityPrintLen(const char* string, const UNITY_UINT32 length)
}

/*-----------------------------------------------*/
void UnityPrintNumberByStyle(const UNITY_INT number, const UNITY_DISPLAY_STYLE_T style)
void UnityPrintIntNumberByStyle(const UNITY_INT number, const UNITY_DISPLAY_STYLE_T style)
{
if ((style & UNITY_DISPLAY_RANGE_INT) == UNITY_DISPLAY_RANGE_INT)
{
Expand Down Expand Up @@ -223,9 +223,13 @@ void UnityPrintNumberByStyle(const UNITY_INT number, const UNITY_DISPLAY_STYLE_T
UnityPrintNumber(number);
}
}
else if ((style & UNITY_DISPLAY_RANGE_UINT) == UNITY_DISPLAY_RANGE_UINT)
}

void UnityPrintUintNumberByStyle(const UNITY_UINT number, const UNITY_DISPLAY_STYLE_T style)
{
if ((style & UNITY_DISPLAY_RANGE_UINT) == UNITY_DISPLAY_RANGE_UINT)
{
UnityPrintNumberUnsigned((UNITY_UINT)number);
UnityPrintNumberUnsigned(number);
}
else
{
Expand Down Expand Up @@ -709,61 +713,102 @@ void UnityAssertBits(const UNITY_INT mask,
}

/*-----------------------------------------------*/
void UnityAssertEqualNumber(const UNITY_INT expected,
const UNITY_INT actual,
const char* msg,
const UNITY_LINE_TYPE lineNumber,
const UNITY_DISPLAY_STYLE_T style)
void UnityAssertEqualIntNumber(const UNITY_INT expected,
const UNITY_INT actual,
const char* msg,
const UNITY_LINE_TYPE lineNumber,
const UNITY_DISPLAY_STYLE_T style)
{
RETURN_IF_FAIL_OR_IGNORE;

if (expected != actual)
{
UnityTestResultsFailBegin(lineNumber);
UnityPrint(UnityStrExpected);
UnityPrintNumberByStyle(expected, style);
UnityPrintIntNumberByStyle(expected, style);
UnityPrint(UnityStrWas);
UnityPrintNumberByStyle(actual, style);
UnityPrintIntNumberByStyle(actual, style);
UnityAddMsgIfSpecified(msg);
UNITY_FAIL_AND_BAIL;
}
}

void UnityAssertEqualUintNumber(const UNITY_UINT expected,
const UNITY_UINT actual,
const char* msg,
const UNITY_LINE_TYPE lineNumber,
const UNITY_DISPLAY_STYLE_T style)
{
RETURN_IF_FAIL_OR_IGNORE;

if (expected != actual)
{
UnityTestResultsFailBegin(lineNumber);
UnityPrint(UnityStrExpected);
UnityPrintUintNumberByStyle(expected, style);
UnityPrint(UnityStrWas);
UnityPrintUintNumberByStyle(actual, style);
UnityAddMsgIfSpecified(msg);
UNITY_FAIL_AND_BAIL;
}
}
/*-----------------------------------------------*/
void UnityAssertGreaterOrLessOrEqualNumber(const UNITY_INT threshold,
const UNITY_INT actual,
const UNITY_COMPARISON_T compare,
const char *msg,
const UNITY_LINE_TYPE lineNumber,
const UNITY_DISPLAY_STYLE_T style)
void UnityAssertIntGreaterOrLessOrEqualNumber(const UNITY_INT threshold,
const UNITY_INT actual,
const UNITY_COMPARISON_T compare,
const char *msg,
const UNITY_LINE_TYPE lineNumber,
const UNITY_DISPLAY_STYLE_T style)
{
int failed = 0;
RETURN_IF_FAIL_OR_IGNORE;

if ((threshold == actual) && (compare & UNITY_EQUAL_TO)) { return; }
if ((threshold == actual)) { failed = 1; }
if ((threshold == actual) && !(compare & UNITY_EQUAL_TO)) { failed = 1; }

if ((style & UNITY_DISPLAY_RANGE_INT) == UNITY_DISPLAY_RANGE_INT)
{
if ((actual > threshold) && (compare & UNITY_SMALLER_THAN)) { failed = 1; }
if ((actual < threshold) && (compare & UNITY_GREATER_THAN)) { failed = 1; }
}
else /* UINT or HEX */
if ((actual > threshold) && (compare & UNITY_SMALLER_THAN)) { failed = 1; }
if ((actual < threshold) && (compare & UNITY_GREATER_THAN)) { failed = 1; }

if (failed)
{
if (((UNITY_UINT)actual > (UNITY_UINT)threshold) && (compare & UNITY_SMALLER_THAN)) { failed = 1; }
if (((UNITY_UINT)actual < (UNITY_UINT)threshold) && (compare & UNITY_GREATER_THAN)) { failed = 1; }
UnityTestResultsFailBegin(lineNumber);
UnityPrint(UnityStrExpected);
UnityPrintIntNumberByStyle(actual, style);
if (compare & UNITY_GREATER_THAN) { UnityPrint(UnityStrGt); }
if (compare & UNITY_SMALLER_THAN) { UnityPrint(UnityStrLt); }
if (compare & UNITY_EQUAL_TO) { UnityPrint(UnityStrOrEqual); }
if (compare == UNITY_NOT_EQUAL) { UnityPrint(UnityStrNotEqual); }
UnityPrintIntNumberByStyle(threshold, style);
UnityAddMsgIfSpecified(msg);
UNITY_FAIL_AND_BAIL;
}
}

void UnityAssertUintGreaterOrLessOrEqualNumber(const UNITY_UINT threshold,
const UNITY_UINT actual,
const UNITY_COMPARISON_T compare,
const char *msg,
const UNITY_LINE_TYPE lineNumber,
const UNITY_DISPLAY_STYLE_T style)
{
int failed = 0;
RETURN_IF_FAIL_OR_IGNORE;

if ((threshold == actual) && !(compare & UNITY_EQUAL_TO)) { failed = 1; }

/* UINT or HEX */
if ((actual > threshold) && (compare & UNITY_SMALLER_THAN)) { failed = 1; }
if ((actual < threshold) && (compare & UNITY_GREATER_THAN)) { failed = 1; }

if (failed)
{
UnityTestResultsFailBegin(lineNumber);
UnityPrint(UnityStrExpected);
UnityPrintNumberByStyle(actual, style);
UnityPrintUintNumberByStyle(actual, style);
if (compare & UNITY_GREATER_THAN) { UnityPrint(UnityStrGt); }
if (compare & UNITY_SMALLER_THAN) { UnityPrint(UnityStrLt); }
if (compare & UNITY_EQUAL_TO) { UnityPrint(UnityStrOrEqual); }
if (compare == UNITY_NOT_EQUAL) { UnityPrint(UnityStrNotEqual); }
UnityPrintNumberByStyle(threshold, style);
UnityPrintUintNumberByStyle(threshold, style);
UnityAddMsgIfSpecified(msg);
UNITY_FAIL_AND_BAIL;
}
Expand Down Expand Up @@ -877,9 +922,9 @@ void UnityAssertEqualIntArray(UNITY_INTERNAL_PTR expected,
UnityPrint(UnityStrElement);
UnityPrintNumberUnsigned(num_elements - elements - 1);
UnityPrint(UnityStrExpected);
UnityPrintNumberByStyle(expect_val, style);
UnityPrintIntNumberByStyle(expect_val, style);
UnityPrint(UnityStrWas);
UnityPrintNumberByStyle(actual_val, style);
UnityPrintIntNumberByStyle(actual_val, style);
UnityAddMsgIfSpecified(msg);
UNITY_FAIL_AND_BAIL;
}
Expand Down Expand Up @@ -1377,47 +1422,65 @@ void UnityAssertDoubleSpecial(const UNITY_DOUBLE actual,
#endif /* not UNITY_EXCLUDE_DOUBLE */

/*-----------------------------------------------*/
void UnityAssertNumbersWithin(const UNITY_UINT delta,
const UNITY_INT expected,
const UNITY_INT actual,
const char* msg,
const UNITY_LINE_TYPE lineNumber,
const UNITY_DISPLAY_STYLE_T style)
void UnityAssertIntNumbersWithin(const UNITY_UINT delta,
const UNITY_INT expected,
const UNITY_INT actual,
const char* msg,
const UNITY_LINE_TYPE lineNumber,
const UNITY_DISPLAY_STYLE_T style)
{
RETURN_IF_FAIL_OR_IGNORE;

if ((style & UNITY_DISPLAY_RANGE_INT) == UNITY_DISPLAY_RANGE_INT)
if (actual > expected)
{
if (actual > expected)
{
Unity.CurrentTestFailed = (((UNITY_UINT)actual - (UNITY_UINT)expected) > delta);
}
else
{
Unity.CurrentTestFailed = (((UNITY_UINT)expected - (UNITY_UINT)actual) > delta);
}
Unity.CurrentTestFailed = (((UNITY_UINT)actual - (UNITY_UINT)expected) > delta);
}
else
{
if ((UNITY_UINT)actual > (UNITY_UINT)expected)
{
Unity.CurrentTestFailed = (((UNITY_UINT)actual - (UNITY_UINT)expected) > delta);
}
else
{
Unity.CurrentTestFailed = (((UNITY_UINT)expected - (UNITY_UINT)actual) > delta);
}
Unity.CurrentTestFailed = (((UNITY_UINT)expected - (UNITY_UINT)actual) > delta);
}

if (Unity.CurrentTestFailed)
{
UnityTestResultsFailBegin(lineNumber);
UnityPrint(UnityStrDelta);
UnityPrintIntNumberByStyle((UNITY_INT)delta, style);
UnityPrint(UnityStrExpected);
UnityPrintIntNumberByStyle(expected, style);
UnityPrint(UnityStrWas);
UnityPrintIntNumberByStyle(actual, style);
UnityAddMsgIfSpecified(msg);
UNITY_FAIL_AND_BAIL;
}
}

void UnityAssertUintNumbersWithin(const UNITY_UINT delta,
const UNITY_UINT expected,
const UNITY_UINT actual,
const char* msg,
const UNITY_LINE_TYPE lineNumber,
const UNITY_DISPLAY_STYLE_T style)
{
RETURN_IF_FAIL_OR_IGNORE;

if (actual > expected)
{
Unity.CurrentTestFailed = ((actual - expected) > delta);
}
else
{
Unity.CurrentTestFailed = ((expected - actual) > delta);
}

if (Unity.CurrentTestFailed)
{
UnityTestResultsFailBegin(lineNumber);
UnityPrint(UnityStrDelta);
UnityPrintNumberByStyle((UNITY_INT)delta, style);
UnityPrintUintNumberByStyle(delta, style);
UnityPrint(UnityStrExpected);
UnityPrintNumberByStyle(expected, style);
UnityPrintUintNumberByStyle(expected, style);
UnityPrint(UnityStrWas);
UnityPrintNumberByStyle(actual, style);
UnityPrintUintNumberByStyle(actual, style);
UnityAddMsgIfSpecified(msg);
UNITY_FAIL_AND_BAIL;
}
Expand Down Expand Up @@ -1568,13 +1631,13 @@ void UnityAssertNumbersArrayWithin(const UNITY_UINT delta,
}
UnityTestResultsFailBegin(lineNumber);
UnityPrint(UnityStrDelta);
UnityPrintNumberByStyle((UNITY_INT)delta, style);
UnityPrintIntNumberByStyle((UNITY_INT)delta, style);
UnityPrint(UnityStrElement);
UnityPrintNumberUnsigned(num_elements - elements - 1);
UnityPrint(UnityStrExpected);
UnityPrintNumberByStyle(expect_val, style);
UnityPrintIntNumberByStyle(expect_val, style);
UnityPrint(UnityStrWas);
UnityPrintNumberByStyle(actual_val, style);
UnityPrintIntNumberByStyle(actual_val, style);
UnityAddMsgIfSpecified(msg);
UNITY_FAIL_AND_BAIL;
}
Expand Down Expand Up @@ -1805,9 +1868,9 @@ void UnityAssertEqualMemory(UNITY_INTERNAL_PTR expected,
UnityPrint(UnityStrByte);
UnityPrintNumberUnsigned(length - bytes - 1);
UnityPrint(UnityStrExpected);
UnityPrintNumberByStyle(*ptr_exp, UNITY_DISPLAY_STYLE_HEX8);
UnityPrintIntNumberByStyle(*ptr_exp, UNITY_DISPLAY_STYLE_HEX8);
UnityPrint(UnityStrWas);
UnityPrintNumberByStyle(*ptr_act, UNITY_DISPLAY_STYLE_HEX8);
UnityPrintIntNumberByStyle(*ptr_act, UNITY_DISPLAY_STYLE_HEX8);
UnityAddMsgIfSpecified(msg);
UNITY_FAIL_AND_BAIL;
}
Expand Down
Loading