c - assert macro that prints the values which are passed in -


in our c codebase have assertion macros such as:

assert3(x, ==, y)      // x=0, y=1 results in "main.c:45: 'x == y' (0 == 1) untrue" assert(x == y)         // x=0, y=1 results in "main.c:45: 'x == y' untrue" 

clearly, assert3 form more helpful when you're trying debug after failure because tells values of variables were.

however, whenever need more complex assertion (especially including || since can split ones && multiple assertions), assert(x == y || y != 0 || x == 2), can no longer take advantage of awesome assert3 format. clearly, could build macro assert11(x, ==, y, ||, y, !=, 0, ||, x, ==, 2), ideally i'd create single macro can handle variable number of arguments , figure out print on own. this, think i'd need macro filter out arguments logical operators doesn't try print values -- there way that?

i've had feeling , thought along lines, turns out asserts things should never happen, doesn't make sense have fancy output them. you're looking debugging message that's printed when conditions not met, should code way:

if (!condition) {     debug(whatever); /* or assert() macro prints */     panic();         /* or assert() macro aborts */ } 

in case, think code easier write, far easier read, , more reliable complicated assert statement you're going end with. not mention once assert gets complicated, surely there's other important relevant information around isn't in assert statement, anyway.


Comments