/* =========================================================================
Unity Project - A Test Framework for C
Copyright (c) 2007-14 Mike Karlesky, Mark VanderVoord, Greg Williams
[Released under MIT License. Please refer to license.txt for details]
============================================================================ */
#include "unity.h"
#define UNITY_FAIL_AND_BAIL { Unity.CurrentTestFailed = 1; longjmp(Unity.AbortFrame, 1); }
#define UNITY_IGNORE_AND_BAIL { Unity.CurrentTestIgnored = 1; longjmp(Unity.AbortFrame, 1); }
/// return prematurely if we are already in failure or ignore state
#define UNITY_SKIP_EXECUTION { if ((Unity.CurrentTestFailed != 0) || (Unity.CurrentTestIgnored != 0)) {return;} }
#define UNITY_PRINT_EOL { UNITY_OUTPUT_CHAR('\n'); }
//-----------------------------------------------
/// basically do an itoa using as little ram as possible
void UnityPrintNumber(const _U_SINT number_to_print)
{
_U_SINT divisor = 1;
_U_SINT next_divisor;
_U_UINT number;
if (number_to_print == (1l << (UNITY_LONG_WIDTH-1)))
{
//The largest representable negative number
UNITY_OUTPUT_CHAR('-');
number = (1ul << (UNITY_LONG_WIDTH-1));
}
else if (number_to_print < 0)
{
//Some other negative number
UNITY_OUTPUT_CHAR('-');
number = (_U_UINT)(-number_to_print);
}
else
{
//Positive number
number = (_U_UINT)number_to_print;
}
// figure out initial divisor
while (number / divisor > 9)
{
next_divisor = divisor * 10;
if (next_divisor > divisor)
divisor = next_divisor;
else
break;
}
// now mod and print, then divide divisor
do
{
UNITY_OUTPUT_CHAR((char)('0' + (number / divisor % 10)));
divisor /= 10;
}
while (divisor > 0);
}
//-----------------------------------------------
/// basically do an itoa using as little ram as possible
void UnityPrintNumberUnsigned(const _U_UINT number)
{
_U_UINT divisor = 1;
_U_UINT next_divisor;
// figure out initial divisor
while (number / divisor > 9)
{
next_divisor = divisor * 10;
if (next_divisor > divisor)
divisor = next_divisor;
else
break;
}
// now mod and print, then divide divisor
do
{
UNITY_OUTPUT_CHAR((char)('0' + (number / divisor % 10)));
divisor /= 10;
}
while (divisor > 0);
}
//-----------------------------------------------
void UnityConcludeTest(void)
{
#if 0
if (Unity.isExpectingFail == 1 && Unity.CurrentTestFailed == 0)
{
printf("FAIL WAS EXPECTED, BUT IT DIDN'T HAPPEN?!");
Unity.TestXPASSES++;
}
else
#endif
//cant be ignored and accepting fail at the same time!
if (Unity.isExpectingFail == 1 && Unity.CurrentTestFailed == 1)
{
Unity.TestXFAILS++; //error message?!
if (Unity.XFAILMessage != NULL)
{
if (Unity.XFAILMessage[0] != ' ')
{
printf(" ");
}
//-----------------------------------------------
// Assertion & Control Helpers
//-----------------------------------------------
static int UnityCheckArraysForNull(UNITY_PTR_ATTRIBUTE const void* expected, UNITY_PTR_ATTRIBUTE const void* actual, const UNITY_LINE_TYPE lineNumber, const char* msg)
{
//return true if they are both NULL
if ((expected == NULL) && (actual == NULL))
return 1;
//throw error if just expected is NULL
if (expected == NULL)
{
UnityTestResultsFailBegin(lineNumber);
UnityPrint(UnityStrNullPointerForExpected);
UnityAddMsgIfSpecified(msg);
UNITY_FAIL_AND_BAIL;
}
//throw error if just actual is NULL
if (actual == NULL)
{
UnityTestResultsFailBegin(lineNumber);
UnityPrint(UnityStrNullPointerForActual);
UnityAddMsgIfSpecified(msg);
UNITY_FAIL_AND_BAIL;
}
while (elements--)
{
diff = *ptr_expected - *ptr_actual;
if (diff < 0.0f)
diff = 0.0f - diff;
tol = UNITY_FLOAT_PRECISION * *ptr_expected;
if (tol < 0.0f)
tol = 0.0f - tol;
//This first part of this condition will catch any NaN or Infinite values
if ((diff * 0.0f != 0.0f) || (diff > tol))
{
UnityTestResultsFailBegin(lineNumber);
UnityPrint(UnityStrElement);
UnityPrintNumberByStyle((num_elements - elements - 1), UNITY_DISPLAY_STYLE_UINT);
#ifdef UNITY_FLOAT_VERBOSE
UnityPrint(UnityStrExpected);
UnityPrintFloat(*ptr_expected);
UnityPrint(UnityStrWas);
UnityPrintFloat(*ptr_actual);
#else
UnityPrint(UnityStrDelta);
#endif
UnityAddMsgIfSpecified(msg);
UNITY_FAIL_AND_BAIL;
}
ptr_expected++;
ptr_actual++;
}
}
//This first part of this condition will catch any NaN or Infinite values
if ((diff * 0.0f != 0.0f) || (pos_delta < diff))
{
UnityTestResultsFailBegin(lineNumber);
#ifdef UNITY_FLOAT_VERBOSE
UnityPrint(UnityStrExpected);
UnityPrintFloat(expected);
UnityPrint(UnityStrWas);
UnityPrintFloat(actual);
#else
UnityPrint(UnityStrDelta);
#endif
UnityAddMsgIfSpecified(msg);
UNITY_FAIL_AND_BAIL;
}
}
switch(style)
{
//To determine Inf / Neg Inf, we compare to an Inf / Neg Inf value we create on the fly
//We are using a variable to hold the zero value because some compilers complain about dividing by zero otherwise
case UNITY_FLOAT_IS_INF:
case UNITY_FLOAT_IS_NOT_INF:
is_trait = ((1.0f / f_zero) == actual) ? 1 : 0;
break;
case UNITY_FLOAT_IS_NEG_INF:
case UNITY_FLOAT_IS_NOT_NEG_INF:
is_trait = ((-1.0f / f_zero) == actual) ? 1 : 0;
break;
//NaN is the only floating point value that does NOT equal itself. Therefore if Actual == Actual, then it is NOT NaN.
case UNITY_FLOAT_IS_NAN:
case UNITY_FLOAT_IS_NOT_NAN:
is_trait = (actual == actual) ? 0 : 1;
break;
//A determinate number is non infinite and not NaN. (therefore the opposite of the two above)
case UNITY_FLOAT_IS_DET:
case UNITY_FLOAT_IS_NOT_DET:
if ( (actual != actual) || ((1.0f / f_zero) == actual) || ((-1.0f / f_zero) == actual) )
is_trait = 0;
else
is_trait = 1;
break;
default:
;
}
if (is_trait != should_be_trait)
{
UnityTestResultsFailBegin(lineNumber);
UnityPrint(UnityStrExpected);
if (!should_be_trait)
UnityPrint(UnityStrNot);
UnityPrint(trait_names[trait_index]);
UnityPrint(UnityStrWas);
#ifdef UNITY_FLOAT_VERBOSE
UnityPrintFloat(actual);
#else
if (should_be_trait)
UnityPrint(UnityStrNot);
UnityPrint(trait_names[trait_index]);
#endif
UnityAddMsgIfSpecified(msg);
UNITY_FAIL_AND_BAIL;
}
}
if (elements == 0)
{
UnityTestResultsFailBegin(lineNumber);
UnityPrint(UnityStrPointless);
UnityAddMsgIfSpecified(msg);
UNITY_FAIL_AND_BAIL;
}
if (UnityCheckArraysForNull((UNITY_PTR_ATTRIBUTE void*)expected, (UNITY_PTR_ATTRIBUTE void*)actual, lineNumber, msg) == 1)
return;
while (elements--)
{
diff = *ptr_expected - *ptr_actual;
if (diff < 0.0)
diff = 0.0 - diff;
tol = UNITY_DOUBLE_PRECISION * *ptr_expected;
if (tol < 0.0)
tol = 0.0 - tol;
//This first part of this condition will catch any NaN or Infinite values
if ((diff * 0.0 != 0.0) || (diff > tol))
{
UnityTestResultsFailBegin(lineNumber);
UnityPrint(UnityStrElement);
UnityPrintNumberByStyle((num_elements - elements - 1), UNITY_DISPLAY_STYLE_UINT);
#ifdef UNITY_DOUBLE_VERBOSE
UnityPrint(UnityStrExpected);
UnityPrintFloat((float)(*ptr_expected));
UnityPrint(UnityStrWas);
UnityPrintFloat((float)(*ptr_actual));
#else
UnityPrint(UnityStrDelta);
#endif
UnityAddMsgIfSpecified(msg);
UNITY_FAIL_AND_BAIL;
}
ptr_expected++;
ptr_actual++;
}
}
//This first part of this condition will catch any NaN or Infinite values
if ((diff * 0.0 != 0.0) || (pos_delta < diff))
{
UnityTestResultsFailBegin(lineNumber);
#ifdef UNITY_DOUBLE_VERBOSE
UnityPrint(UnityStrExpected);
UnityPrintFloat((float)expected);
UnityPrint(UnityStrWas);
UnityPrintFloat((float)actual);
#else
UnityPrint(UnityStrDelta);
#endif
UnityAddMsgIfSpecified(msg);
UNITY_FAIL_AND_BAIL;
}
}
switch(style)
{
//To determine Inf / Neg Inf, we compare to an Inf / Neg Inf value we create on the fly
//We are using a variable to hold the zero value because some compilers complain about dividing by zero otherwise
case UNITY_FLOAT_IS_INF:
case UNITY_FLOAT_IS_NOT_INF:
is_trait = ((1.0 / d_zero) == actual) ? 1 : 0;
break;
case UNITY_FLOAT_IS_NEG_INF:
case UNITY_FLOAT_IS_NOT_NEG_INF:
is_trait = ((-1.0 / d_zero) == actual) ? 1 : 0;
break;
//NaN is the only floating point value that does NOT equal itself. Therefore if Actual == Actual, then it is NOT NaN.
case UNITY_FLOAT_IS_NAN:
case UNITY_FLOAT_IS_NOT_NAN:
is_trait = (actual == actual) ? 0 : 1;
break;
//A determinate number is non infinite and not NaN. (therefore the opposite of the two above)
case UNITY_FLOAT_IS_DET:
case UNITY_FLOAT_IS_NOT_DET:
if ( (actual != actual) || ((1.0 / d_zero) == actual) || ((-1.0 / d_zero) == actual) )
is_trait = 0;
else
is_trait = 1;
break;
default:
;
}
if (is_trait != should_be_trait)
{
UnityTestResultsFailBegin(lineNumber);
UnityPrint(UnityStrExpected);
if (!should_be_trait)
UnityPrint(UnityStrNot);
UnityPrint(trait_names[trait_index]);
UnityPrint(UnityStrWas);
#ifdef UNITY_DOUBLE_VERBOSE
UnityPrintFloat(actual);
#else
if (should_be_trait)
UnityPrint(UnityStrNot);
UnityPrint(trait_names[trait_index]);
#endif
UnityAddMsgIfSpecified(msg);
UNITY_FAIL_AND_BAIL;
}
}
// if both pointers not null compare the strings
if (expected && actual)
{
for (i = 0; expected[i] || actual[i]; i++)
{
if (expected[i] != actual[i])
{
Unity.CurrentTestFailed = 1;
break;
}
}
}
else
{ // handle case of one pointers being null (if both null, test should pass)
if (expected != actual)
{
Unity.CurrentTestFailed = 1;
}
}
if (Unity.CurrentTestFailed)
{
UnityTestResultsFailBegin(lineNumber);
UnityPrintExpectedAndActualStrings(expected, actual);
UnityAddMsgIfSpecified(msg);
UNITY_FAIL_AND_BAIL;
}
}
// if no elements, it's an error
if (num_elements == 0)
{
UnityTestResultsFailBegin(lineNumber);
UnityPrint(UnityStrPointless);
UnityAddMsgIfSpecified(msg);
UNITY_FAIL_AND_BAIL;
}
if (UnityCheckArraysForNull((UNITY_PTR_ATTRIBUTE void*)expected, (UNITY_PTR_ATTRIBUTE void*)actual, lineNumber, msg) == 1)
return;
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((j), UNITY_DISPLAY_STYLE_UINT);
}
UnityPrintExpectedAndActualStrings((const char*)(expected[j]), (const char*)(actual[j]));
UnityAddMsgIfSpecified(msg);
UNITY_FAIL_AND_BAIL;
}
} while (++j < num_elements);
}