diff --git a/ut_assert/inc/utassert.h b/ut_assert/inc/utassert.h index 195ed1412..366f0ac65 100644 --- a/ut_assert/inc/utassert.h +++ b/ut_assert/inc/utassert.h @@ -54,6 +54,15 @@ */ #define UTASSERT_STRINGBUF_NULL_TERM SIZE_MAX +/** + * Type for generic integer assert checks + * + * By using the C99 "ptrdiff_t" type, this should be large enough to also + * store pointer values on the target system, in addition to all normal + * integer values. + */ +typedef ptrdiff_t UT_IntCheck_t; + /** * Define various types of messages that can be generated by a test. * @@ -115,6 +124,7 @@ typedef enum typedef enum { UtAssert_Radix_DEFAULT = 0, /**< no preference, use default */ + UtAssert_Radix_BOOLEAN = 1, /**< log integers as logical true (1) / false (0) */ UtAssert_Radix_OCTAL = 8, /**< log integers as octal, base 8 */ UtAssert_Radix_DECIMAL = 10, /**< log integers as decimal, base 10 */ UtAssert_Radix_HEX = 16 /**< log integers as hexadecimal, base 16 */ @@ -240,6 +250,27 @@ typedef struct #define UtAssert_Type(Type, Expression, ...) \ UtAssertEx(Expression, UTASSERT_CASETYPE_##Type, __FILE__, __LINE__, __VA_ARGS__) +/** + * \brief Asserts on any integer value, in the context of the specified type + * + * \par Description + * The actual and reference values are interpreted as the specified data type, which + * may be any integer data type or typedef, signed or unsigned. + * + * "op" must be one of EQ, NEQ, LT, GT, LTEQ, or GTEQ (see #UtAssert_Compare_t) + * and indicates the type of logical comparison to perform + * + * "radix" must be one of DEFAULT, BOOLEAN, OCTAL, HEX, or DECIMAL (see #UtAssert_Radix_t) + * and is used as a hint to display the value in a more human-readable form + * + * \par Assumptions, External Events, and Notes: + * None + */ +#define UtAssert_INTVAL(type, actual, op, ref, radix) \ + UtAssert_GenericIntegerCompare(((type)(-1)) > ((type)(0)), (UT_IntCheck_t)(type)(actual), UtAssert_Compare_##op, \ + (UT_IntCheck_t)(type)(ref), __FILE__, __LINE__, UtAssert_Radix_##radix, #type, \ + #actual, #ref) + /** * \brief Asserts the expression/function evaluates as logically true * @@ -271,12 +302,99 @@ typedef struct UtAssert_GenericUnsignedCompare((bool)(expr), UtAssert_Compare_EQ, false, UtAssert_Radix_DECIMAL, __FILE__, \ __LINE__, "", #expr, "false") +/** + * \brief Compare two values for equality with an auto-generated description message + * + * This macro confirms that the given expression is equal to the reference value + * The generated log message will include the actual and reference values in decimal notation + * + * \param type The data type to use when doing the comparison + * \param actual The value from the unit under test + * \param ref The expected value from the unit under test + */ +#define UtAssert_EQ(type, actual, ref) \ + UtAssert_GenericIntegerCompare(((type)(-1)) > ((type)(0)), (type)(actual), UtAssert_Compare_EQ, (type)(ref), \ + __FILE__, __LINE__, UtAssert_Radix_DECIMAL, #type, #actual, #ref) + +/** + * \brief Compare two values for inequality with an auto-generated description message + * + * This macro confirms that the given expression is _not_ equal to the reference value + * The generated log message will include the actual and reference values in decimal notation + * + * \param type The data type to use when doing the comparison + * \param actual The value from the unit under test + * \param ref The expected value from the unit under test + */ +#define UtAssert_NEQ(type, actual, ref) \ + UtAssert_GenericIntegerCompare(((type)(-1)) > ((type)(0)), (type)(actual), UtAssert_Compare_NEQ, (type)(ref), \ + __FILE__, __LINE__, UtAssert_Radix_DECIMAL, #type, #actual, #ref) + +/** + * \brief Asserts the minimum value of a given function or expression + * + * This macro confirms that the given expression is at least the minimum value (inclusive) + * The generated log message will include the actual and reference values in decimal notation + * + * \param type The data type to use when doing the comparison + * \param actual The value from the unit under test + * \param ref The expected value from the unit under test + */ +#define UtAssert_GTEQ(type, actual, ref) \ + UtAssert_GenericIntegerCompare(((type)(-1)) > ((type)(0)), (type)(actual), UtAssert_Compare_GTEQ, (type)(ref), \ + __FILE__, __LINE__, UtAssert_Radix_DECIMAL, #type, #actual, #ref) + +/** + * \brief Asserts the maximum value of a given function or expression + * + * This macro confirms that the given expression is at most the maximum value (inclusive) + * The generated log message will include the actual and reference values in decimal notation + * + * \param type The data type to use when doing the comparison + * \param actual The value from the unit under test + * \param ref The expected value from the unit under test + */ +#define UtAssert_LTEQ(type, actual, ref) \ + UtAssert_GenericIntegerCompare(((type)(-1)) > ((type)(0)), (type)(actual), UtAssert_Compare_LTEQ, (type)(ref), \ + __FILE__, __LINE__, UtAssert_Radix_DECIMAL, #type, #actual, #ref) + +/** + * \brief Asserts the value of a given function or expression is less than the reference value + * + * This macro confirms that the given expression is less than the maximum value (exclusive) + * The generated log message will include the actual and reference values in decimal notation + * + * \param type The data type to use when doing the comparison + * \param actual The value from the unit under test + * \param ref The expected value from the unit under test + */ +#define UtAssert_LT(type, actual, ref) \ + UtAssert_GenericIntegerCompare(((type)(-1)) > ((type)(0)), (type)(actual), UtAssert_Compare_LT, (type)(ref), \ + __FILE__, __LINE__, UtAssert_Radix_DECIMAL, #type, #actual, #ref) + +/** + * \brief Asserts the value of a given function or expression is greater than the reference value + * + * This macro confirms that the given expression is greater than the minimum value (exclusive) + * The generated log message will include the actual and reference values in decimal notation + * + * \param type The data type to use when doing the comparison + * \param actual The value from the unit under test + * \param ref The expected value from the unit under test + */ +#define UtAssert_GT(type, actual, ref) \ + UtAssert_GenericIntegerCompare(((type)(-1)) > ((type)(0)), (type)(actual), UtAssert_Compare_GT, (type)(ref), \ + __FILE__, __LINE__, UtAssert_Radix_DECIMAL, #type, #actual, #ref) + /** * \brief Compare two values for equality with an auto-generated description message * * This macro confirms that the given expression is equal to the reference value * The generated log message will include the actual and reference values in decimal notation * Values will be compared in an "int32" type context. + * + * \param actual The value from the unit under test + * \param ref The expected value from the unit under test */ #define UtAssert_INT32_EQ(actual, ref) \ UtAssert_GenericSignedCompare((int32)(actual), UtAssert_Compare_EQ, (int32)(ref), UtAssert_Radix_DECIMAL, \ @@ -288,6 +406,9 @@ typedef struct * This macro confirms that the given expression is _not_ equal to the reference value * The generated log message will include the actual and reference values in decimal notation * Values will be compared in an "int32" type context. + * + * \param actual The value from the unit under test + * \param ref The expected value from the unit under test */ #define UtAssert_INT32_NEQ(actual, ref) \ UtAssert_GenericSignedCompare((int32)(actual), UtAssert_Compare_NEQ, (int32)(ref), UtAssert_Radix_DECIMAL, \ @@ -300,43 +421,54 @@ typedef struct * The generated log message will include the actual and reference values in decimal notation * Values will be compared in an "int32" type context. * - * \par Assumptions, External Events, and Notes: - * None - * + * \param actual The value from the unit under test + * \param ref The expected value from the unit under test */ -#define UtAssert_INT32_GTEQ(expr, ref) \ - UtAssert_GenericSignedCompare((int32)(expr), UtAssert_Compare_GTEQ, (int32)(ref), UtAssert_Radix_DECIMAL, \ - __FILE__, __LINE__, "", #expr, #ref) +#define UtAssert_INT32_GTEQ(actual, ref) \ + UtAssert_GenericSignedCompare((int32)(actual), UtAssert_Compare_GTEQ, (int32)(ref), UtAssert_Radix_DECIMAL, \ + __FILE__, __LINE__, "", #actual, #ref) /** * \brief Asserts the maximum value of a given function or expression * * This macro confirms that the given expression is at most the maximum value (inclusive) * The generated log message will include the actual and reference values in decimal notation + * Values will be compared in an "int32" type context. + * + * \param actual The value from the unit under test + * \param ref The expected value from the unit under test */ -#define UtAssert_INT32_LTEQ(expr, ref) \ - UtAssert_GenericSignedCompare((int32)(expr), UtAssert_Compare_LTEQ, (int32)(ref), UtAssert_Radix_DECIMAL, \ - __FILE__, __LINE__, "", #expr, #ref) +#define UtAssert_INT32_LTEQ(actual, ref) \ + UtAssert_GenericSignedCompare((int32)(actual), UtAssert_Compare_LTEQ, (int32)(ref), UtAssert_Radix_DECIMAL, \ + __FILE__, __LINE__, "", #actual, #ref) /** * \brief Asserts the value of a given function or expression is less than the reference value * * This macro confirms that the given expression is less than the maximum value (exclusive) * The generated log message will include the actual and reference values in decimal notation + * Values will be compared in an "int32" type context. + * + * \param actual The value from the unit under test + * \param ref The expected value from the unit under test */ -#define UtAssert_INT32_LT(expr, ref) \ - UtAssert_GenericSignedCompare((int32)(expr), UtAssert_Compare_LT, (int32)(ref), UtAssert_Radix_DECIMAL, __FILE__, \ - __LINE__, "", #expr, #ref) +#define UtAssert_INT32_LT(actual, ref) \ + UtAssert_GenericSignedCompare((int32)(actual), UtAssert_Compare_LT, (int32)(ref), UtAssert_Radix_DECIMAL, \ + __FILE__, __LINE__, "", #actual, #ref) /** * \brief Asserts the value of a given function or expression is greater than the reference value * * This macro confirms that the given expression is greater than the minimum value (exclusive) * The generated log message will include the actual and reference values in decimal notation + * Values will be compared in an "int32" type context. + * + * \param actual The value from the unit under test + * \param ref The expected value from the unit under test */ -#define UtAssert_INT32_GT(expr, ref) \ - UtAssert_GenericSignedCompare((int32)(expr), UtAssert_Compare_GT, (int32)(ref), UtAssert_Radix_DECIMAL, __FILE__, \ - __LINE__, "", #expr, #ref) +#define UtAssert_INT32_GT(actual, ref) \ + UtAssert_GenericSignedCompare((int32)(actual), UtAssert_Compare_GT, (int32)(ref), UtAssert_Radix_DECIMAL, \ + __FILE__, __LINE__, "", #actual, #ref) /** * \brief Compare two values for equality with an auto-generated description message @@ -344,6 +476,9 @@ typedef struct * This macro confirms that the given expression is equal to the reference value * The generated log message will include the actual and reference values in decimal notation * Values will be compared in an "uint32" type context. + * + * \param actual The value from the unit under test + * \param ref The expected value from the unit under test */ #define UtAssert_UINT32_EQ(actual, ref) \ UtAssert_GenericUnsignedCompare((uint32)(actual), UtAssert_Compare_EQ, (uint32)(ref), UtAssert_Radix_DECIMAL, \ @@ -355,6 +490,9 @@ typedef struct * This macro confirms that the given expression is _not_ equal to the reference value * The generated log message will include the actual and reference values in decimal notation * Values will be compared in an "uint32" type context. + * + * \param actual The value from the unit under test + * \param ref The expected value from the unit under test */ #define UtAssert_UINT32_NEQ(actual, ref) \ UtAssert_GenericUnsignedCompare((uint32)(actual), UtAssert_Compare_NEQ, (uint32)(ref), UtAssert_Radix_DECIMAL, \ @@ -363,58 +501,54 @@ typedef struct /** * \brief Asserts the minimum value of a given function or expression * - * \par Description - * This macro confirms that the given expression is at least the minimum value (inclusive) - * - * \par Assumptions, External Events, and Notes: - * None + * This macro confirms that the given expression is at least the minimum value (inclusive) + * Values will be compared in an "uint32" type context. * + * \param actual The value from the unit under test + * \param ref The expected value from the unit under test */ -#define UtAssert_UINT32_GTEQ(expr, ref) \ - UtAssert_GenericUnsignedCompare((uint32)(expr), UtAssert_Compare_GTEQ, (uint32)(ref), UtAssert_Radix_DECIMAL, \ - __FILE__, __LINE__, "", #expr, #ref) +#define UtAssert_UINT32_GTEQ(actual, ref) \ + UtAssert_GenericUnsignedCompare((uint32)(actual), UtAssert_Compare_GTEQ, (uint32)(ref), UtAssert_Radix_DECIMAL, \ + __FILE__, __LINE__, "", #actual, #ref) /** * \brief Asserts the maximum value of a given function or expression * - * \par Description - * This macro confirms that the given expression is at most the maximum value (inclusive) - * - * \par Assumptions, External Events, and Notes: - * None + * This macro confirms that the given expression is at most the maximum value (inclusive) + * Values will be compared in an "uint32" type context. * + * \param actual The value from the unit under test + * \param ref The expected value from the unit under test */ -#define UtAssert_UINT32_LTEQ(expr, ref) \ - UtAssert_GenericUnsignedCompare((uint32)(expr), UtAssert_Compare_LTEQ, (uint32)(ref), UtAssert_Radix_DECIMAL, \ - __FILE__, __LINE__, "", #expr, #ref) +#define UtAssert_UINT32_LTEQ(actual, ref) \ + UtAssert_GenericUnsignedCompare((uint32)(actual), UtAssert_Compare_LTEQ, (uint32)(ref), UtAssert_Radix_DECIMAL, \ + __FILE__, __LINE__, "", #actual, #ref) /** * \brief Asserts the value of a given function or expression is less than the reference value * - * \par Description - * This macro confirms that the given expression is less than the maximum value (exclusive) - * - * \par Assumptions, External Events, and Notes: - * None + * This macro confirms that the given expression is less than the maximum value (exclusive) + * Values will be compared in an "uint32" type context. * + * \param actual The value from the unit under test + * \param ref The expected value from the unit under test */ -#define UtAssert_UINT32_LT(expr, ref) \ - UtAssert_GenericUnsignedCompare((uint32)(expr), UtAssert_Compare_LT, (uint32)(ref), UtAssert_Radix_DECIMAL, \ - __FILE__, __LINE__, "", #expr, #ref) +#define UtAssert_UINT32_LT(actual, ref) \ + UtAssert_GenericUnsignedCompare((uint32)(actual), UtAssert_Compare_LT, (uint32)(ref), UtAssert_Radix_DECIMAL, \ + __FILE__, __LINE__, "", #actual, #ref) /** * \brief Asserts the value of a given function or expression is greater than the reference value * - * \par Description - * This macro confirms that the given expression is greater than the minimum value (exclusive) - * - * \par Assumptions, External Events, and Notes: - * None + * This macro confirms that the given expression is greater than the minimum value (exclusive) + * Values will be compared in an "uint32" type context. * + * \param actual The value from the unit under test + * \param ref The expected value from the unit under test */ -#define UtAssert_UINT32_GT(expr, ref) \ - UtAssert_GenericUnsignedCompare((uint32)(expr), UtAssert_Compare_GT, (uint32)(ref), UtAssert_Radix_DECIMAL, \ - __FILE__, __LINE__, "", #expr, #ref) +#define UtAssert_UINT32_GT(actual, ref) \ + UtAssert_GenericUnsignedCompare((uint32)(actual), UtAssert_Compare_GT, (uint32)(ref), UtAssert_Radix_DECIMAL, \ + __FILE__, __LINE__, "", #actual, #ref) /** * \brief Compare two 16-bit values for equality with an auto-generated description message @@ -422,10 +556,13 @@ typedef struct * This macro confirms that the given expression is equal to the reference value * The generated log message will include the actual and reference values in decimal notation * Values will be compared in an "uint16" type context. + * + * \param actual The value from the unit under test + * \param ref The expected value from the unit under test */ #define UtAssert_UINT16_EQ(actual, ref) \ UtAssert_GenericUnsignedCompare((uint16)(actual), UtAssert_Compare_EQ, (uint16)(ref), UtAssert_Radix_DECIMAL, \ - __FILE__, __LINE__, "Compare UINT16: ", #actual, #ref) + __FILE__, __LINE__, "uint16", #actual, #ref) /** * \brief Compare two 16-bit values for inequality with an auto-generated description message @@ -433,66 +570,65 @@ typedef struct * This macro confirms that the given expression is _not_ equal to the reference value * The generated log message will include the actual and reference values in decimal notation * Values will be compared in an "uint16" type context. + * + * \param actual The value from the unit under test + * \param ref The expected value from the unit under test */ #define UtAssert_UINT16_NEQ(actual, ref) \ UtAssert_GenericUnsignedCompare((uint16)(actual), UtAssert_Compare_NEQ, (uint16)(ref), UtAssert_Radix_DECIMAL, \ - __FILE__, __LINE__, "Compare UINT16: ", #actual, #ref) + __FILE__, __LINE__, "uint16", #actual, #ref) /** * \brief Asserts the minimum 16-bit value of a given function or expression * - * \par Description - * This macro confirms that the given expression is at least the minimum 16-bit value (inclusive) - * - * \par Assumptions, External Events, and Notes: - * None + * This macro confirms that the given expression is at least the minimum 16-bit value (inclusive) + * Values will be compared in an "uint16" type context. * + * \param actual The value from the unit under test + * \param ref The expected value from the unit under test */ -#define UtAssert_UINT16_GTEQ(expr, ref) \ - UtAssert_GenericUnsignedCompare((uint16)(expr), UtAssert_Compare_GTEQ, (uint16)(ref), UtAssert_Radix_DECIMAL, \ - __FILE__, __LINE__, "Compare UINT16: ", #expr, #ref) +#define UtAssert_UINT16_GTEQ(actual, ref) \ + UtAssert_GenericUnsignedCompare((uint16)(actual), UtAssert_Compare_GTEQ, (uint16)(ref), UtAssert_Radix_DECIMAL, \ + __FILE__, __LINE__, "uint16", #actual, #ref) /** * \brief Asserts the maximum 16-bit value of a given function or expression * - * \par Description - * This macro confirms that the given expression is at most the maximum 16-bit value (inclusive) - * - * \par Assumptions, External Events, and Notes: - * None + * This macro confirms that the given expression is at most the maximum 16-bit value (inclusive) + * Values will be compared in an "uint16" type context. * + * \param actual The value from the unit under test + * \param ref The expected value from the unit under test */ -#define UtAssert_UINT16_LTEQ(expr, ref) \ - UtAssert_GenericUnsignedCompare((uint16)(expr), UtAssert_Compare_LTEQ, (uint16)(ref), UtAssert_Radix_DECIMAL, \ - __FILE__, __LINE__, "Compare UINT16: ", #expr, #ref) +#define UtAssert_UINT16_LTEQ(actual, ref) \ + UtAssert_GenericUnsignedCompare((uint16)(actual), UtAssert_Compare_LTEQ, (uint16)(ref), UtAssert_Radix_DECIMAL, \ + __FILE__, __LINE__, "uint16", #actual, #ref) /** * \brief Asserts the 16-bit value of a given function or expression is less than the 16-bit reference value * - * \par Description - * This macro confirms that the given expression is less than the maximum 16-bit value (exclusive) - * - * \par Assumptions, External Events, and Notes: - * None + * This macro confirms that the given expression is less than the maximum 16-bit value (exclusive) + * Values will be compared in an "uint16" type context. * + * \param actual The value from the unit under test + * \param ref The expected value from the unit under test */ -#define UtAssert_UINT16_LT(expr, ref) \ - UtAssert_GenericUnsignedCompare((uint16)(expr), UtAssert_Compare_LT, (uint16)(ref), UtAssert_Radix_DECIMAL, \ - __FILE__, __LINE__, "Compare UINT16: ", #expr, #ref) +#define UtAssert_UINT16_LT(actual, ref) \ + UtAssert_GenericUnsignedCompare((uint16)(actual), UtAssert_Compare_LT, (uint16)(ref), UtAssert_Radix_DECIMAL, \ + __FILE__, __LINE__, "uint16", #actual, #ref) /** * \brief Asserts the 16-bit value of a given function or expression is greater than the 16-bit reference value * - * \par Description - * This macro confirms that the given expression is greater than the minimum 16-bit value (exclusive) - * - * \par Assumptions, External Events, and Notes: - * None + * This macro confirms that the given expression is greater than the minimum 16-bit value (exclusive) + * Values will be compared in an "uint16" type context. * + * \param actual The value from the unit under test + * \param ref The expected value from the unit under test */ -#define UtAssert_UINT16_GT(expr, ref) \ - UtAssert_GenericUnsignedCompare((uint16)(expr), UtAssert_Compare_GT, (uint16)(ref), UtAssert_Radix_DECIMAL, \ - __FILE__, __LINE__, "Compare UINT16: ", #expr, #ref) +#define UtAssert_UINT16_GT(actual, ref) \ + UtAssert_GenericUnsignedCompare((uint16)(actual), UtAssert_Compare_GT, (uint16)(ref), UtAssert_Radix_DECIMAL, \ + __FILE__, __LINE__, "uint16", #actual, #ref) /** * \brief Compare two 8-bit values for equality with an auto-generated description message @@ -500,10 +636,13 @@ typedef struct * This macro confirms that the given expression is equal to the reference value * The generated log message will include the actual and reference values in decimal notation * Values will be compared in an "uint8" type context. + * + * \param actual The value from the unit under test + * \param ref The expected value from the unit under test */ #define UtAssert_UINT8_EQ(actual, ref) \ UtAssert_GenericUnsignedCompare((uint8)(actual), UtAssert_Compare_EQ, (uint8)(ref), UtAssert_Radix_DECIMAL, \ - __FILE__, __LINE__, "Compare UINT8: ", #actual, #ref) + __FILE__, __LINE__, "uint8", #actual, #ref) /** * \brief Compare two 8-bit values for inequality with an auto-generated description message @@ -511,66 +650,65 @@ typedef struct * This macro confirms that the given expression is _not_ equal to the reference value * The generated log message will include the actual and reference values in decimal notation * Values will be compared in an "uint8" type context. + * + * \param actual The value from the unit under test + * \param ref The expected value from the unit under test */ #define UtAssert_UINT8_NEQ(actual, ref) \ UtAssert_GenericUnsignedCompare((uint8)(actual), UtAssert_Compare_NEQ, (uint8)(ref), UtAssert_Radix_DECIMAL, \ - __FILE__, __LINE__, "Compare UINT8: ", #actual, #ref) + __FILE__, __LINE__, "uint8", #actual, #ref) /** * \brief Asserts the minimum 8-bit value of a given function or expression * - * \par Description - * This macro confirms that the given expression is at least the minimum 8-bit value (inclusive) - * - * \par Assumptions, External Events, and Notes: - * None + * This macro confirms that the given expression is at least the minimum 8-bit value (inclusive) + * Values will be compared in an "uint8" type context. * + * \param actual The value from the unit under test + * \param ref The expected value from the unit under test */ -#define UtAssert_UINT8_GTEQ(expr, ref) \ - UtAssert_GenericUnsignedCompare((uint8)(expr), UtAssert_Compare_GTEQ, (uint8)(ref), UtAssert_Radix_DECIMAL, \ - __FILE__, __LINE__, "Compare UINT8: ", #expr, #ref) +#define UtAssert_UINT8_GTEQ(actual, ref) \ + UtAssert_GenericUnsignedCompare((uint8)(actual), UtAssert_Compare_GTEQ, (uint8)(ref), UtAssert_Radix_DECIMAL, \ + __FILE__, __LINE__, "uint8", #actual, #ref) /** * \brief Asserts the maximum 8-bit value of a given function or expression * - * \par Description - * This macro confirms that the given expression is at most the maximum 8-bit value (inclusive) - * - * \par Assumptions, External Events, and Notes: - * None + * This macro confirms that the given expression is at most the maximum 8-bit value (inclusive) + * Values will be compared in an "uint8" type context. * + * \param actual The value from the unit under test + * \param ref The expected value from the unit under test */ -#define UtAssert_UINT8_LTEQ(expr, ref) \ - UtAssert_GenericUnsignedCompare((uint8)(expr), UtAssert_Compare_LTEQ, (uint8)(ref), UtAssert_Radix_DECIMAL, \ - __FILE__, __LINE__, "Compare UINT8: ", #expr, #ref) +#define UtAssert_UINT8_LTEQ(actual, ref) \ + UtAssert_GenericUnsignedCompare((uint8)(actual), UtAssert_Compare_LTEQ, (uint8)(ref), UtAssert_Radix_DECIMAL, \ + __FILE__, __LINE__, "uint8", #actual, #ref) /** * \brief Asserts the 8-bit value of a given function or expression is less than the 8-bit reference value * - * \par Description - * This macro confirms that the given expression is less than the maximum 8-bit value (exclusive) - * - * \par Assumptions, External Events, and Notes: - * None + * This macro confirms that the given expression is less than the maximum 8-bit value (exclusive) + * Values will be compared in an "uint8" type context. * + * \param actual The value from the unit under test + * \param ref The expected value from the unit under test */ -#define UtAssert_UINT8_LT(expr, ref) \ - UtAssert_GenericUnsignedCompare((uint8)(expr), UtAssert_Compare_LT, (uint8)(ref), UtAssert_Radix_DECIMAL, \ - __FILE__, __LINE__, "Compare UINT8: ", #expr, #ref) +#define UtAssert_UINT8_LT(actual, ref) \ + UtAssert_GenericUnsignedCompare((uint8)(actual), UtAssert_Compare_LT, (uint8)(ref), UtAssert_Radix_DECIMAL, \ + __FILE__, __LINE__, "uint8", #actual, #ref) /** * \brief Asserts the 8-bit value of a given function or expression is greater than the 8-bit reference value * - * \par Description - * This macro confirms that the given expression is greater than the minimum 8-bit value (exclusive) - * - * \par Assumptions, External Events, and Notes: - * None + * This macro confirms that the given expression is greater than the minimum 8-bit value (exclusive) + * Values will be compared in an "uint8" type context. * + * \param actual The value from the unit under test + * \param ref The expected value from the unit under test */ -#define UtAssert_UINT8_GT(expr, ref) \ - UtAssert_GenericUnsignedCompare((uint8)(expr), UtAssert_Compare_GT, (uint8)(ref), UtAssert_Radix_DECIMAL, \ - __FILE__, __LINE__, "Compare UINT8: ", #expr, #ref) +#define UtAssert_UINT8_GT(actual, ref) \ + UtAssert_GenericUnsignedCompare((uint8)(actual), UtAssert_Compare_GT, (uint8)(ref), UtAssert_Radix_DECIMAL, \ + __FILE__, __LINE__, "uint8", #actual, #ref) /** * \brief Macro for checking that bits in a bit field are set @@ -666,7 +804,7 @@ typedef struct */ #define UtAssert_STUB_COUNT(stub, expected) \ UtAssert_GenericSignedCompare(UT_GetStubCount(UT_KEY(stub)), UtAssert_Compare_EQ, expected, \ - UtAssert_Radix_DECIMAL, __FILE__, __LINE__, "CallCount ", #stub "()", #expected) + UtAssert_Radix_DECIMAL, __FILE__, __LINE__, "CallCount", #stub "()", #expected) /* * Exported Functions @@ -866,6 +1004,37 @@ void UtAssert_DoTestSegmentReport(const char *SegmentName, const UtAssert_TestCo bool UtAssert_StringBufCompare(const char *String1, size_t String1Max, const char *String2, size_t String2Max, UtAssert_Compare_t CompareType, const char *File, uint32 Line); +/** + * \brief Helper function for generic integer value checks + * + * \par Description + * This helper function wraps the normal UtAssertEx() function, to compare two + * integer values in a signed or unsigned context. The comparison is performed as two + * "UT_IntCheck_t" integers and the numeric values are printed to the test log + * in the notation specified by RadixType. + * + * \par Assumptions, External Events, and Notes: + * None + * + * \param IsUnsigned Should be passed as true to check in an unsigned context, or false for a signed context + * \param ActualValue Integer Value from the unit under test + * \param CompareType Type of comparison to perform + * \param ReferenceValue Integer Value that was expected from the unit + * \param File File where test is taking place (typically __FILE__) + * \param Line Line number where test is taking place (typically __LINE__) + * \param RadixType How to display the result + * \param Typename Data type name as a string (for logging) + * \param ActualText Actual value as a string (for logging) + * \param ReferenceText Expected value as a string (for logging) + * + * \returns Test pass status, returns true if status was successful, false if it failed. + * + */ +bool UtAssert_GenericIntegerCompare(bool IsUnsigned, UT_IntCheck_t ActualValue, UtAssert_Compare_t CompareType, + UT_IntCheck_t ReferenceValue, const char *File, uint32 Line, + UtAssert_Radix_t RadixType, const char *Typename, const char *ActualText, + const char *ReferenceText); + /** * \brief Helper function for generic unsigned integer value checks * diff --git a/ut_assert/src/utassert.c b/ut_assert/src/utassert.c index be350d63c..ca5abcb85 100644 --- a/ut_assert/src/utassert.c +++ b/ut_assert/src/utassert.c @@ -27,6 +27,7 @@ */ #include #include +#include #include "common_types.h" #include "utassert.h" @@ -42,6 +43,14 @@ UtAssert_TestCounter_t UT_SegmentCounters = {0}; UtAssert_TestCounter_t UT_TotalCounters = {0}; static char CurrentSegment[64]; +typedef union +{ + intmax_t s; /**< If value is signed */ + uintmax_t u; /**< If value is unsigned */ +} UtAssert_IntBuf_t; + +#define UT_COMPARE_TYPE(t, s) (((t) << 1) | (s)) + /* * Function Definitions */ @@ -352,106 +361,204 @@ bool UtAssert_GenericUnsignedCompare(unsigned long ActualValue, UtAssert_Compare unsigned long ReferenceValue, UtAssert_Radix_t RadixType, const char *File, uint32 Line, const char *Desc, const char *ActualText, const char *ReferenceText) { - bool Result; - const char *FormatStr; + return UtAssert_GenericIntegerCompare(true, ActualValue, CompareType, ReferenceValue, File, Line, RadixType, Desc, + ActualText, ReferenceText); +} - switch (CompareType) +bool UtAssert_GenericSignedCompare(long ActualValue, UtAssert_Compare_t CompareType, long ReferenceValue, + UtAssert_Radix_t RadixType, const char *File, uint32 Line, const char *Desc, + const char *ActualText, const char *ReferenceText) +{ + return UtAssert_GenericIntegerCompare(false, ActualValue, CompareType, ReferenceValue, File, Line, RadixType, Desc, + ActualText, ReferenceText); +} + +static const char *UtAssert_GetValueText(char *TempBuf, size_t TempSz, UT_IntCheck_t InValue, bool IsUnsigned, + UtAssert_Radix_t RadixType) +{ + if (RadixType == UtAssert_Radix_BOOLEAN) { - case UtAssert_Compare_EQ: /* actual equals reference value */ - Result = (ActualValue == ReferenceValue); - break; - case UtAssert_Compare_NEQ: /* actual does not non equal reference value */ - Result = (ActualValue != ReferenceValue); - break; - case UtAssert_Compare_LT: /* actual less than reference (exclusive) */ - Result = (ActualValue < ReferenceValue); + if (InValue != 0) + { + snprintf(TempBuf, TempSz, "true"); + } + else + { + snprintf(TempBuf, TempSz, "false"); + } + } + else if (RadixType == UtAssert_Radix_OCTAL) + { + snprintf(TempBuf, TempSz, "0%lo", (unsigned long)InValue); + } + else if (RadixType == UtAssert_Radix_HEX) + { + snprintf(TempBuf, TempSz, "0x%lx", (unsigned long)InValue); + } + else if (IsUnsigned) + { + snprintf(TempBuf, TempSz, "%lu", (unsigned long)InValue); + } + else + { + snprintf(TempBuf, TempSz, "%ld", (long)InValue); + } + + return TempBuf; +} + +static bool UtAssert_DoCompare(intmax_t ActualValueIn, UtAssert_Compare_t CompareType, UT_IntCheck_t ReferenceValueIn, + bool IsUnsigned) +{ + bool Result; + UtAssert_IntBuf_t ActualValue; + UtAssert_IntBuf_t ReferenceValue; + + if (IsUnsigned) + { + ActualValue.u = ActualValueIn; + ReferenceValue.u = ReferenceValueIn; + } + else + { + ActualValue.s = ActualValueIn; + ReferenceValue.s = ReferenceValueIn; + } + + switch (UT_COMPARE_TYPE(CompareType, IsUnsigned)) + { + case UT_COMPARE_TYPE(UtAssert_Compare_EQ, true): /* actual equals reference value */ + Result = (ActualValue.u == ReferenceValue.u); break; - case UtAssert_Compare_GT: /* actual greater than reference (exclusive) */ - Result = (ActualValue > ReferenceValue); + case UT_COMPARE_TYPE(UtAssert_Compare_EQ, false): /* actual equals reference value */ + Result = (ActualValue.s == ReferenceValue.s); break; - case UtAssert_Compare_LTEQ: /* actual less than or equal to reference (inclusive) */ - Result = (ActualValue <= ReferenceValue); + case UT_COMPARE_TYPE(UtAssert_Compare_NEQ, true): /* actual does not non equal reference value */ + Result = (ActualValue.u != ReferenceValue.u); break; - case UtAssert_Compare_GTEQ: /* actual greater than reference (inclusive) */ - Result = (ActualValue >= ReferenceValue); + case UT_COMPARE_TYPE(UtAssert_Compare_NEQ, false): /* actual does not non equal reference value */ + Result = (ActualValue.s != ReferenceValue.s); break; - case UtAssert_Compare_BITMASK_SET: /* bit(s) in reference are set in actual */ - Result = (ActualValue & ReferenceValue) == ReferenceValue; + case UT_COMPARE_TYPE(UtAssert_Compare_LT, true): /* actual less than reference (exclusive) */ + Result = (ActualValue.u < ReferenceValue.u); break; - case UtAssert_Compare_BITMASK_UNSET: /* bit(s) in reference are not set in actual */ - Result = (ActualValue & ReferenceValue) == 0; + case UT_COMPARE_TYPE(UtAssert_Compare_LT, false): /* actual less than reference (exclusive) */ + Result = (ActualValue.s < ReferenceValue.s); break; - default: /* should never happen */ - Result = false; + case UT_COMPARE_TYPE(UtAssert_Compare_GT, true): /* actual greater than reference (exclusive) */ + Result = (ActualValue.u > ReferenceValue.u); break; - } - - switch (RadixType) - { - case UtAssert_Radix_OCTAL: - FormatStr = "%s%s (0%lo) %s %s (0%lo)"; + case UT_COMPARE_TYPE(UtAssert_Compare_GT, false): /* actual greater than reference (exclusive) */ + Result = (ActualValue.s > ReferenceValue.s); break; - case UtAssert_Radix_DECIMAL: - FormatStr = "%s%s (%lu) %s %s (%lu)"; + case UT_COMPARE_TYPE(UtAssert_Compare_LTEQ, true): /* actual less than reference (inclusive) */ + Result = (ActualValue.u <= ReferenceValue.u); break; - default: - /* for unsigned, default is hex */ - FormatStr = "%s%s (0x%lx) %s %s (0x%lx)"; + case UT_COMPARE_TYPE(UtAssert_Compare_LTEQ, false): /* actual less than reference (inclusive) */ + Result = (ActualValue.s <= ReferenceValue.s); break; - } - - return UtAssertEx(Result, UTASSERT_CASETYPE_FAILURE, File, Line, FormatStr, Desc, ActualText, ActualValue, - UtAssert_GetOpText(CompareType), ReferenceText, ReferenceValue); -} - -bool UtAssert_GenericSignedCompare(long ActualValue, UtAssert_Compare_t CompareType, long ReferenceValue, - UtAssert_Radix_t RadixType, const char *File, uint32 Line, const char *Desc, - const char *ActualText, const char *ReferenceText) -{ - bool Result; - const char *FormatStr; - - switch (CompareType) - { - case UtAssert_Compare_EQ: /* actual equals reference value */ - Result = (ActualValue == ReferenceValue); + case UT_COMPARE_TYPE(UtAssert_Compare_GTEQ, true): /* actual greater than reference (inclusive) */ + Result = (ActualValue.u >= ReferenceValue.u); break; - case UtAssert_Compare_NEQ: /* actual does not non equal reference value */ - Result = (ActualValue != ReferenceValue); + case UT_COMPARE_TYPE(UtAssert_Compare_GTEQ, false): /* actual greater than reference (inclusive) */ + Result = (ActualValue.s >= ReferenceValue.s); break; - case UtAssert_Compare_LT: /* actual less than reference (exclusive) */ - Result = (ActualValue < ReferenceValue); + case UT_COMPARE_TYPE(UtAssert_Compare_BITMASK_SET, true): /* bit(s) in reference are set in actual */ + Result = (ActualValue.u & ReferenceValue.u) == ReferenceValue.u; break; - case UtAssert_Compare_GT: /* actual greater than reference (exclusive) */ - Result = (ActualValue > ReferenceValue); + case UT_COMPARE_TYPE(UtAssert_Compare_BITMASK_SET, false): /* bit(s) in reference are set in actual */ + Result = (ActualValue.s & ReferenceValue.s) == ReferenceValue.s; break; - case UtAssert_Compare_LTEQ: /* actual less than or equal to reference (inclusive) */ - Result = (ActualValue <= ReferenceValue); + case UT_COMPARE_TYPE(UtAssert_Compare_BITMASK_UNSET, true): /* bit(s) in reference are not set in actual */ + Result = (ActualValue.u & ReferenceValue.u) == 0; break; - case UtAssert_Compare_GTEQ: /* actual greater than reference (inclusive) */ - Result = (ActualValue >= ReferenceValue); + case UT_COMPARE_TYPE(UtAssert_Compare_BITMASK_UNSET, false): /* bit(s) in reference are not set in actual */ + Result = (ActualValue.s & ReferenceValue.s) == 0; break; default: /* should never happen */ Result = false; break; } - switch (RadixType) + return Result; +} + +/* + * ------------------------------------------------------------------------------------- + * Implementation of UtAssert_GenericIntegerCompare() + * + * See declaration for full API information + * ------------------------------------------------------------------------------------- + */ +bool UtAssert_GenericIntegerCompare(bool IsUnsigned, UT_IntCheck_t ActualValue, UtAssert_Compare_t CompareType, + UT_IntCheck_t RefValue, const char *File, uint32 Line, UtAssert_Radix_t RadixType, + const char *Typename, const char *ActualText, const char *RefText) +{ + static const char UTASSERT_PREFIX[] = "UTASSERT_"; + + char ActualStr[32]; + char RefStr[32]; + char TagStr[32]; + int TagLen; + + /* If the radix type was not specified, then check if the typename appears to be a pointer - + * That is, it contains an asterisk. This is far from foolproof due to typedefs etc but + * it should catch most of them (note that "hiding" a pointer via typedef is discouraged by + * GSFC coding standards, so this shouldn't be too likely) + */ + if (Typename != NULL && *Typename != 0) { - case UtAssert_Radix_OCTAL: - FormatStr = "%s%s (0%lo) %s %s (0%lo)"; - break; - case UtAssert_Radix_HEX: - FormatStr = "%s%s (0x%lx) %s %s (0x%lx)"; - break; - default: - /* for signed, default is decimal */ - FormatStr = "%s%s (%ld) %s %s (%ld)"; - break; + TagLen = snprintf(TagStr, sizeof(TagStr), "%s", Typename); + if (TagLen < 0) + { + TagLen = 0; + } + else if (TagLen > (sizeof(TagStr) - 3)) + { + TagLen = sizeof(TagStr) - 3; + } + + while (TagLen > 0 && (isspace((unsigned char)TagStr[TagLen - 1]) || TagStr[TagLen - 1] == ':')) + { + --TagLen; + } + + if (TagLen > 0) + { + TagStr[TagLen] = ':'; + ++TagLen; + TagStr[TagLen] = ' '; + ++TagLen; + } + + TagStr[TagLen] = 0; + + if (RadixType == UtAssert_Radix_DEFAULT && strchr(Typename, '*') != NULL) + { + /* looks like a pointer type */ + RadixType = UtAssert_Radix_HEX; + } + } + else + { + TagStr[0] = 0; + } + + /* If either the actual text or the ref text starts with the "UTASSERT_" prefix, then strip it */ + if (strncmp(ActualText, UTASSERT_PREFIX, sizeof(UTASSERT_PREFIX) - 1) == 0) + { + ActualText += sizeof(UTASSERT_PREFIX) - 1; + } + if (strncmp(RefText, UTASSERT_PREFIX, sizeof(UTASSERT_PREFIX) - 1) == 0) + { + RefText += sizeof(UTASSERT_PREFIX) - 1; } - return UtAssertEx(Result, UTASSERT_CASETYPE_FAILURE, File, Line, FormatStr, Desc, ActualText, ActualValue, - UtAssert_GetOpText(CompareType), ReferenceText, ReferenceValue); + return UtAssertEx(UtAssert_DoCompare(ActualValue, CompareType, RefValue, IsUnsigned), UTASSERT_CASETYPE_FAILURE, + File, Line, "%s%s (%s) %s %s (%s)", TagStr, ActualText, + UtAssert_GetValueText(ActualStr, sizeof(ActualStr), ActualValue, IsUnsigned, RadixType), + UtAssert_GetOpText(CompareType), RefText, + UtAssert_GetValueText(RefStr, sizeof(RefStr), RefValue, IsUnsigned, RadixType)); } bool UtAssert_StringBufCompare(const char *String1, size_t String1Max, const char *String2, size_t String2Max,