Adding actual failure values

Cher B
2011-09-30
2012-10-25
  • Cher B
    Cher B
    2011-09-30

    I am a past user of CppUnit, so I was excited to find a tool like this for
    coding in C. I however missed the tests showing the actual values of a
    variable when it failed. Therefore, I added these changes for my team. Feel
    free to use them and add them to your CUnit.h. It outputs the "actual: xx,
    expected: xx" values.

    / Internal Add - Asserts that (double) actual == (double) expected.
    If actual does not equal expected, then the actual values are output.
    Reports failure with no other action.
    */

    define CU_ASSERT_DOUBLES_EQUAL(actual, expected) \

    { char condition; \
    sprintf(condition, "CU_ASSERT_DOUBLES_EQUAL(actual: %d, expected: %d)",
    actual, expected); \
    CU_assertImplementation((actual == expected), LINE, condition, FILE,
    "", CU_FALSE); }

    / Internal Add - Asserts that (double) actual == (double) expected.
    If actual does not equal expected, then the actual values are output.
    Reports failure and causes test to abort.
    */

    define CU_ASSERT_DOUBLES_EQUAL_FATAL(actual, expected) \

    { char condition; \
    sprintf(condition, "CU_ASSERT_DOUBLES_EQUAL_FATAL(actual: %d, expected: %d)",
    actual, expected); \
    CU_assertImplementation((actual == expected), LINE, condition, FILE,
    "", CU_TRUE); }

    / Internal Add - Asserts that (char) actual == (char) expected.
    If actual does not equal expected, then the actual values are output.
    Reports failure with no other action.
    */

    define CU_ASSERT_CHARS_EQUAL(actual, expected) \

    { char condition; \
    sprintf(condition, "CU_ASSERT_CHARS_EQUAL(actual: \"%c\", expected: \"%c\")",
    actual, expected); \
    CU_assertImplementation((actual == expected), LINE, condition, FILE,
    "", CU_FALSE); }

    / Internal Add - Asserts that (char) actual == (char) expected.
    If actual does not equal expected, then the actual values are output.
    Reports failure and causes test to abort.
    */

    define CU_ASSERT_CHARS_EQUAL_FATAL(actual, expected) \

    { char condition; \
    sprintf(condition, "CU_ASSERT_CHARS_EQUAL_FATAL(actual: \"%c\", expected:
    \"%c\")", actual, expected); \
    CU_assertImplementation((actual == expected), LINE, condition, FILE,
    "", CU_TRUE); }

    / Internal Add - Asserts that (string) actual == (string) expected.
    If actual does not equal expected, then the actual values are output.
    Reports failure with no other action.
    */

    define CU_ASSERT_STRINGS_EQUAL(actual, expected) \

    { int size = strlen(actual) + strlen(expected); \
    char condition; \
    sprintf(condition, "CU_ASSERT_STRINGS_EQUAL(actual: \"%s\", expected:
    \"%s\")", actual, expected); \
    CU_assertImplementation((actual == expected), LINE, condition, FILE,
    "", CU_FALSE); }

    / Internal Add - Asserts that (string) actual == (string) expected.
    If actual does not equal expected, then the actual values are output.
    Reports failure with no other action.
    */

    define CU_ASSERT_STRINGS_EQUAL_FATAL(actual, expected) \

    { int size = strlen(actual) + strlen(expected); \
    char condition; \
    sprintf(condition, "CU_ASSERT_STRINGS_EQUAL_FATAL(actual: \"%s\", expected:
    \"%s\")", actual, expected); \
    CU_assertImplementation((actual == expected), LINE, condition, FILE,
    "", CU_TRUE); }