From 85db6f930806aacd096415417e44d9547c124b52 Mon Sep 17 00:00:00 2001 From: tsutsumi <> Date: Sun, 14 Sep 2003 15:57:33 +0000 Subject: [PATCH] remade more type safe function --- src/ccunit/CCUnitAssert.c | 148 ++++++++++++++++++++++++++++------------------ src/ccunit/CCUnitAssert.h | 124 +++++++++++++++++++++++++++----------- src/ccunit/CCUnitConfig.h | 23 +++++++ 3 files changed, 203 insertions(+), 92 deletions(-) diff --git a/src/ccunit/CCUnitAssert.c b/src/ccunit/CCUnitAssert.c index 8f689e8..d9d62e3 100644 --- a/src/ccunit/CCUnitAssert.c +++ b/src/ccunit/CCUnitAssert.c @@ -30,6 +30,7 @@ #include #include #include +#include /** * @addtogroup CCUnitAssert @@ -48,62 +49,95 @@ void ccunit_assert (const char* file, unsigned int line, longjmp (_ccunit_runTest_env, (int)f); } -void ccunit_assert_test (const char* file, unsigned int line, - bool cond, const char* condstr, - const char* vfmt, ...) -{ - va_list args; - char* ex; - char* ac; - int require; - int capacity; - CCUnitTestFailure* f; - if (cond) - return; - ex = NULL; - ac = NULL; - require = 0; - capacity = 16 * 2; - for (ex = malloc (capacity); ex != NULL; ex = realloc (ex, capacity)) - { - va_start (args, vfmt); - require = vsnprintf (ex, capacity, vfmt, args); - va_end (args); - if (require < 0) - capacity *= 2; - else if (require < capacity) - break; - else - capacity = require + 1; - } - if (ex != NULL) - { - int exlen = strlen (ex); - char* dfmt = malloc (strlen (vfmt) * 2 + 2); - sprintf (dfmt, "%s;%s", vfmt, vfmt); - capacity = exlen * 2 + 2; - for (ex = realloc (ex, capacity); ex != NULL; ex = realloc (ex, capacity)) - { - va_start (args, vfmt); - require = vsnprintf (ex, capacity, dfmt, args); - va_end (args); - if (require < 0) - capacity *= 2; - else if (require < capacity) - break; - else - capacity = require + 1; - } - safe_free (dfmt); - if (ex != NULL) - { - ex[exlen] = '\0'; - ac = ex + exlen + 1; - } - } - f = ccunit_newTestFailure (file, line, condstr, ex, ac); - safe_free (ex); - longjmp (_ccunit_runTest_env, (int)f); -} +const char* _ccunit_assert_format_char = "%c"; +const char* _ccunit_assert_format_u_char = "%c"; +const char* _ccunit_assert_format_int = "%d"; +const char* _ccunit_assert_format_u_int = "%u"; +const char* _ccunit_assert_format_short = "%hd"; +const char* _ccunit_assert_format_u_short = "%hu"; +const char* _ccunit_assert_format_long = "%ld"; +const char* _ccunit_assert_format_u_long = "%lu"; +const char* _ccunit_assert_format_float = "%f"; +const char* _ccunit_assert_format_double = "%f"; +const char* _ccunit_assert_format__ccunit_str_t = "%s"; +const char* _ccunit_assert_format__ccunit_ptr_t = "%p"; +const char* _ccunit_assert_format_quad_t = "%lld"; +const char* _ccunit_assert_format_u_quad_t = "%llu"; + +#define DEF_VALUE_TO_STRING(TYP, CAPACITY) \ + static char* TYP ## _to_string (TYP value) \ + { \ + char* str = NULL; \ + int require = 0; \ + int capacity = CAPACITY; \ + for (str = malloc (capacity); \ + str != NULL; \ + str = realloc (str, capacity)) \ + { \ + require = snprintf (str, capacity, \ + _ccunit_assert_format_ ## TYP, \ + value); \ + if (require < 0) \ + capacity *= 2; \ + else if (require < capacity) \ + break; \ + else \ + capacity = require + 1; \ + } \ + return str; \ + } + +DEF_VALUE_TO_STRING(char, 6); +DEF_VALUE_TO_STRING(u_char, 6); +DEF_VALUE_TO_STRING(int, 12); +DEF_VALUE_TO_STRING(u_int, 12); +DEF_VALUE_TO_STRING(short, 8); +DEF_VALUE_TO_STRING(u_short, 8); +DEF_VALUE_TO_STRING(long, 12); +DEF_VALUE_TO_STRING(u_long, 12); +DEF_VALUE_TO_STRING(float, 24); +DEF_VALUE_TO_STRING(double, 24); +DEF_VALUE_TO_STRING(quad_t, 24); +DEF_VALUE_TO_STRING(u_quad_t, 24); +DEF_VALUE_TO_STRING(_ccunit_str_t, 32); +DEF_VALUE_TO_STRING(_ccunit_ptr_t, 24); + +#define DEF_CCUNIT_ASSERT_TEST_TYPE(TYP) \ + void ccunit_assert_test_ ## TYP (const char* file, \ + unsigned int line, \ + bool cond, \ + const char* condstr, \ + TYP expect, \ + TYP actual) \ + { \ + if (cond) \ + return; \ + else \ + { \ + const char* ex = TYP ## _to_string (expect); \ + const char* ac = TYP ## _to_string (actual); \ + CCUnitTestFailure* f; \ + f = ccunit_newTestFailure (file, line, condstr, ex, ac); \ + safe_free ((char*)ex); \ + safe_free ((char*)ac); \ + assert (f != NULL); \ + longjmp (_ccunit_runTest_env, (int)f); \ + } \ + } + +DEF_CCUNIT_ASSERT_TEST_TYPE(char); +DEF_CCUNIT_ASSERT_TEST_TYPE(u_char); +DEF_CCUNIT_ASSERT_TEST_TYPE(int); +DEF_CCUNIT_ASSERT_TEST_TYPE(u_int); +DEF_CCUNIT_ASSERT_TEST_TYPE(short); +DEF_CCUNIT_ASSERT_TEST_TYPE(u_short); +DEF_CCUNIT_ASSERT_TEST_TYPE(long); +DEF_CCUNIT_ASSERT_TEST_TYPE(u_long); +DEF_CCUNIT_ASSERT_TEST_TYPE(quad_t); +DEF_CCUNIT_ASSERT_TEST_TYPE(u_quad_t); +DEF_CCUNIT_ASSERT_TEST_TYPE(float); +DEF_CCUNIT_ASSERT_TEST_TYPE(double); +DEF_CCUNIT_ASSERT_TEST_TYPE(_ccunit_str_t); +DEF_CCUNIT_ASSERT_TEST_TYPE(_ccunit_ptr_t); /** @} */ diff --git a/src/ccunit/CCUnitAssert.h b/src/ccunit/CCUnitAssert.h index a8afa56..270e289 100644 --- a/src/ccunit/CCUnitAssert.h +++ b/src/ccunit/CCUnitAssert.h @@ -28,6 +28,9 @@ #define CCUNITASSERT_H #include +#if HAVE_SYS_TYPES_H +# include +#endif /** * @defgroup CCUnitAssert Assert @@ -56,18 +59,32 @@ extern void ccunit_assert (const char* file, unsigned int line, * Asserts that a condition is true. If it isn't it longjmp with * _ccunit_runTest_env. * - * @param file file name. - * @param line line number. - * @param cond assert condition. - * @param condstr assert condition as string. - * @param vfmt tested value format string for sprintf. - * @param ... expect value and actual value. - * + * @param TYP test value type * @throws _ccunit_runTest_env If condition is false. */ -extern void ccunit_assert_test (const char* file, unsigned int line, - bool cond, const char* condstr, - const char* vfmt, ...); +#define DCL_CCUNIT_ASSERT_TEST_TYPE(TYP) \ + extern void ccunit_assert_test_ ## TYP (const char* file, unsigned int line, \ + bool cond, const char* condstr, \ + TYP expect, TYP acutal) + +DCL_CCUNIT_ASSERT_TEST_TYPE(char); +DCL_CCUNIT_ASSERT_TEST_TYPE(u_char); +DCL_CCUNIT_ASSERT_TEST_TYPE(int); +DCL_CCUNIT_ASSERT_TEST_TYPE(u_int); +DCL_CCUNIT_ASSERT_TEST_TYPE(short); +DCL_CCUNIT_ASSERT_TEST_TYPE(u_short); +DCL_CCUNIT_ASSERT_TEST_TYPE(long); +DCL_CCUNIT_ASSERT_TEST_TYPE(u_long); +DCL_CCUNIT_ASSERT_TEST_TYPE(float); +DCL_CCUNIT_ASSERT_TEST_TYPE(double); +DCL_CCUNIT_ASSERT_TEST_TYPE(_ccunit_str_t); +DCL_CCUNIT_ASSERT_TEST_TYPE(_ccunit_ptr_t); +#if HAVE_QUAD_T +DCL_CCUNIT_ASSERT_TEST_TYPE(quad_t); +#endif +#if HAVE_U_QUAD_T +DCL_CCUNIT_ASSERT_TEST_TYPE(u_quad_t); +#endif /** * Asserts that a condition is true. If it isn't it longjmp with @@ -78,7 +95,7 @@ extern void ccunit_assert_test (const char* file, unsigned int line, */ #define CCUNIT_ASSERT(COND) ccunit_assert (__FILE__, __LINE__, COND, #COND) -/** @name CCUNIT_ASSERT_TEST +/** @name Assert Macros * The assert test macros. * @{ */ @@ -86,52 +103,59 @@ extern void ccunit_assert_test (const char* file, unsigned int line, * Asserts that a condition is true. If it isn't it longjmp with * _ccunit_runTest_env. * - * @param VFMT format string for expect/actual value. * @param TYP type of expect/actual value. * @param EXPECT expect value. * @param OP test operator. * @param ACTUAL actual value. * @throws _ccunit_runTest_env If condition is false. */ -#define CCUNIT_ASSERT_TEST(VFMT, TYP, EXPECT, OP, ACTUAL) \ - do { \ - TYP ex = (TYP)EXPECT; \ - TYP ac = (TYP)ACTUAL; \ - ccunit_assert_test (__FILE__, __LINE__, ex OP ac, \ - (#EXPECT " " #OP " " #ACTUAL), VFMT, ex, ac); \ +#define CCUNIT_ASSERT_TEST(TYP, EXPECT, OP, ACTUAL) \ + do { \ + const TYP ex = EXPECT; \ + const TYP ac = ACTUAL; \ + _CCUNIT_CONCAT(ccunit_assert_test_,TYP) (__FILE__, __LINE__, ex OP ac, \ + (#EXPECT " " #OP " " #ACTUAL), \ + ex, ac); \ } while (0) #define CCUNIT_ASSERT_TEST_INT(EXPECT, OP, ACTUAL) \ - CCUNIT_ASSERT_TEST ("%d", int, EXPECT, OP, ACTUAL) + CCUNIT_ASSERT_TEST (int, EXPECT, OP, ACTUAL) #define CCUNIT_ASSERT_TEST_UINT(EXPECT, OP, ACTUAL) \ - CCUNIT_ASSERT_TEST ("%u", unsigned, EXPECT, OP, ACTUAL) -#define CCUNIT_ASSERT_TEST_PTR(EXPECT, OP, ACTUAL) \ - CCUNIT_ASSERT_TEST ("%p", void*, EXPECT, OP, ACTUAL) + CCUNIT_ASSERT_TEST (u_int, EXPECT, OP, ACTUAL) #define CCUNIT_ASSERT_TEST_LONG(EXPECT, OP, ACTUAL) \ - CCUNIT_ASSERT_TEST ("%ld", long, EXPECT, OP, ACTUAL) + CCUNIT_ASSERT_TEST (long, EXPECT, OP, ACTUAL) #define CCUNIT_ASSERT_TEST_ULONG(EXPECT, OP, ACTUAL) \ - CCUNIT_ASSERT_TEST ("%lu", unsigned long, EXPECT, OP, ACTUAL) + CCUNIT_ASSERT_TEST (u_long, EXPECT, OP, ACTUAL) #define CCUNIT_ASSERT_TEST_SHORT(EXPECT, OP, ACTUAL) \ - CCUNIT_ASSERT_TEST ("%hd", short, EXPECT, OP, ACTUAL) + CCUNIT_ASSERT_TEST (short, EXPECT, OP, ACTUAL) #define CCUNIT_ASSERT_TEST_USHORT(EXPECT, OP, ACTUAL) \ - CCUNIT_ASSERT_TEST ("%hu", unsigned short, EXPECT, OP, ACTUAL) + CCUNIT_ASSERT_TEST (u_short, EXPECT, OP, ACTUAL) #define CCUNIT_ASSERT_TEST_DOUBLE(EXPECT, OP, ACTUAL) \ - CCUNIT_ASSERT_TEST ("%g", double, EXPECT, OP, ACTUAL) + CCUNIT_ASSERT_TEST (double, EXPECT, OP, ACTUAL) #define CCUNIT_ASSERT_TEST_FLOAT(EXPECT, OP, ACTUAL) \ - CCUNIT_ASSERT_TEST ("%f", float, EXPECT, OP, ACTUAL) - + CCUNIT_ASSERT_TEST (float, EXPECT, OP, ACTUAL) +#if HAVE_QUAD_T +#define CCUNIT_ASSERT_TEST_QUAD(EXPECT, OP, ACTUAL) \ + CCUNIT_ASSERT_TEST (quad_t, EXPECT, OP, ACTUAL) +#endif +#if HAVE_U_QUAD_T +#define CCUNIT_ASSERT_TEST_UQUAD(EXPECT, OP, ACTUAL) \ + CCUNIT_ASSERT_TEST (u_quad_t, EXPECT, OP, ACTUAL) +#endif +#define CCUNIT_ASSERT_TEST_PTR(EXPECT, OP, ACTUAL) \ + CCUNIT_ASSERT_TEST (ccunit_ptr_t, EXPECT, OP, ACTUAL) #define CCUNIT_ASSERT_TEST_STR(EXPECT, OP, ACTUAL) \ do { \ - const char* ex = EXPECT; \ - const char* ac = ACTUAL; \ - ccunit_assert_test (__FILE__, __LINE__, strcmp(ex, ac) OP 0, \ - "strcmp (" #EXPECT ", " #ACTUAL ") " #OP " 0", \ - "%s", ex, ac); \ + const ccunit_str_t ex = EXPECT; \ + const ccunit_str_t ac = ACTUAL; \ + const ccunit_str_t condstr = "strcmp (" #EXPECT ", " #ACTUAL ") " #OP " 0"; \ + ccunit_assert_test_ccunti_str_t (__FILE__, __LINE__, \ + strcmp(ex, ac) OP 0, condstr, ex, ac); \ } while (0) /** @} */ -/** @name CCUNIT_ASSERT_CONDITION +/** @name Assert Condition Macros * The assert condition macros. * @{ */ @@ -166,6 +190,36 @@ extern void ccunit_assert_test (const char* file, unsigned int line, #define CCUNIT_ASSERT_GE_UINT(EXPECT, ACTUAL) \ CCUNIT_ASSERT_TEST_UINT (EXPECT, >=, ACTUAL) +#if HAVE_QUAD_T +#define CCUNIT_ASSERT_EQ_QUAD(EXPECT, ACTUAL) \ + CCUNIT_ASSERT_TEST_QUAD (EXPECT, ==, ACTUAL) +#define CCUNIT_ASSERT_NE_QUAD(EXPECT, ACTUAL) \ + CCUNIT_ASSERT_TEST_QUAD (EXPECT, !=, ACTUAL) +#define CCUNIT_ASSERT_LT_QUAD(EXPECT, ACTUAL) \ + CCUNIT_ASSERT_TEST_QUAD (EXPECT, <, ACTUAL) +#define CCUNIT_ASSERT_LE_QUAD(EXPECT, ACTUAL) \ + CCUNIT_ASSERT_TEST_QUAD (EXPECT, <=, ACTUAL) +#define CCUNIT_ASSERT_GT_QUAD(EXPECT, ACTUAL) \ + CCUNIT_ASSERT_TEST_QUAD (EXPECT, >, ACTUAL) +#define CCUNIT_ASSERT_GE_QUAD(EXPECT, ACTUAL) \ + CCUNIT_ASSERT_TEST_QUAD (EXPECT, >=, ACTUAL) +#endif + +#if HAVE_U_QUAD_T +#define CCUNIT_ASSERT_EQ_UQUAD(EXPECT, ACTUAL) \ + CCUNIT_ASSERT_TEST_UQUAD (EXPECT, ==, ACTUAL) +#define CCUNIT_ASSERT_NE_UQUAD(EXPECT, ACTUAL) \ + CCUNIT_ASSERT_TEST_UQUAD (EXPECT, !=, ACTUAL) +#define CCUNIT_ASSERT_LT_UQUAD(EXPECT, ACTUAL) \ + CCUNIT_ASSERT_TEST_UQUAD (EXPECT, <, ACTUAL) +#define CCUNIT_ASSERT_LE_UQUAD(EXPECT, ACTUAL) \ + CCUNIT_ASSERT_TEST_UQUAD (EXPECT, <=, ACTUAL) +#define CCUNIT_ASSERT_GT_UQUAD(EXPECT, ACTUAL) \ + CCUNIT_ASSERT_TEST_UQUAD (EXPECT, >, ACTUAL) +#define CCUNIT_ASSERT_GE_UQUAD(EXPECT, ACTUAL) \ + CCUNIT_ASSERT_TEST_UQUAD (EXPECT, >=, ACTUAL) +#endif + #define CCUNIT_ASSERT_EQ_STR(EXPECT, ACTUAL) \ CCUNIT_ASSERT_TEST_STR (EXPECT, ==, ACTUAL) #define CCUNIT_ASSERT_NE_STR(EXPECT, ACTUAL) \ diff --git a/src/ccunit/CCUnitConfig.h b/src/ccunit/CCUnitConfig.h index aefda9e..9eabb66 100644 --- a/src/ccunit/CCUnitConfig.h +++ b/src/ccunit/CCUnitConfig.h @@ -42,12 +42,35 @@ #if HAVE_STDBOOL_H #include #elif defined (__cplusplus) +#elif HAVE__BOOL +#define bool _Bool /**< boolean */ +#define true ((bool)1) /**< true */ +#define false ((bool)0) /**< false */ #elif !defined (bool) #define bool int /**< boolean */ #define true (1) /**< true */ #define false (0) /**< false */ #endif +#ifndef HAVE_U_CHAR +typedef unsigned char u_char; +#endif +#ifndef HAVE_U_INT +typedef unsigned int u_int; +#endif +#ifndef HAVE_U_SHORT +typedef unsigned short u_short; +#endif +#ifndef HAVE_U_LONG +typedef unsigned long u_long; +#endif + +typedef char* _ccunit_str_t; +typedef void* _ccunit_ptr_t; + +#define _CCUNIT_CONCAT(A,B) A ## B +#define _CCUNIT_TOSTR(S) #S + /** * safty free memory. * Omits dmalloc's free(NULL) warning. -- 2.11.0