debug checks - ProkopHapala/FireCore GitHub Wiki

Asserts

  • Normally in C++ there is this assert(condition) from <cassert>
#include <cassert>; 
assert(!std::isnan(value) && !std::isinf(value) && value >= 0.0);
  • This assert is compiled only in DEBUG mode, in optimized code as no runtime cost.
  • see mistral

However we want to make our own which provides a bit more information - i.e. it can execute code prividing more detailed output. We did it here:

#ifdef DEBUGBUILD
#define _assert( cond, action ) \
    if( !(cond) ){ \
        printf("Assertion failed: %s, line: %d  function: %s file: %s \n", #cond, __LINE__, __FUNCTION__, __FILE__ ); \
        printf("  => execute action: %s", #action ); \
        {action;} \
    }
#else
#define _assert( cond, action )
#endif

can be used like this: _assert(0>1, double a=2; printf("HELLO a(%g)+2= %g \n", a, a+2 ); exit(0); );

or more complex example printing whole vector if one component is strange:

double dot(int n, double a, double b){
    double sum=0;
    for(int i=0;i<n;i++){
        sum += a[i]*b[i];
        // if invalid results print both vectors 
        _assert( isnan(ci)||isinf(ci) , for(int i=0;i<n;i++){printf("[%i] %g  %g \n", a[i], b[i] );} );
    }
    return sum;
}
⚠️ **GitHub.com Fallback** ⚠️