Subject: assert
From: "Henrik Grubbström (Lysator) @ Pike (-) importmöte för mailinglistan" <6341@lyskom.lysator.liu.se>
Date: Sat, 4 May 2013 AD 07:00:01 -0400

>Does anyone happen to have any boilerplate code they use to handle 
>assertions in pike? I know that it's pretty easy to put one together, I 
>just figured if there was a popular version floating around, I'd use that 
>rather than rolling my own.

In some Roxen related modules we use macros like:

| #ifdef DEBUG
| #define ASSERT_IF_DEBUG(TEST, ARGS...) do {                             \
|     if (!(TEST)) error ("Assertion failed: " #TEST "\n", ARGS);         \
|   } while (0)
| #else
| #define ASSERT_IF_DEBUG(TEST, ARGS...) do {} while (0)
| #endif

These can then be used like:

| ASSERT_IF_DEBUG(foo->bar[baz /* %O */] /* %O */ <= foo->barmax /* %O */,
|                 baz, foo->bar[baz], foo->barmax);

Also in case nobody has noticed:
In Pike 7.9 the static_assert() macro from C11 was added. It can be
used to perform compile-time assertions. cf:
http://pike.lysator.liu.se/generated/manual/modref/ex/predef_3A_3A/_Static_assert.html

Return to results