开发者

Assertion debug type independent?

I have written my own assertion debug.

#define ASSERT_EQUALS(a,b) \
do { \
    if ((a)!=(b)) \
开发者_如何学Go    { \
        printf(". ASSERT_EQUALS (%s:%d) %d!=%d\n",__FUNCTION__,__LINE__,a,b); \
    } \
} while (0)     

However its only compatible with integer types. Is there a way I can change this so I can support float/double types as well?

Thanks.


Maybe you should just print them as floats.

#define ASSERT_EQUALS(a, b) \
  do { \
    if ((a)!=(b)) { \
      printf(". ASSERT_EQUALS (%s:%d) %f!=%f\n",__FUNCTION__,__LINE__,(float)(a),(float)(b)); \
    } \
  } while (0)

It looks bad with integers, for example 1 will show up as 1.00000, but it will work for both types.


Print them as strings; this works for all types and prints the actual text of the expressions, not just their values:

#define ASSERT_EQUALS(a,b) \
do { \
    if ((a)!=(b)) \
    { \
        printf(". ASSERT_EQUALS (%s:%d) %s!=%s\n",__FUNCTION__,__LINE__, #a,#b); \
    } \
} while (0)     


Use a variadic macro and vprintf. Look at the assert.h in your C library

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜