assert

Discussion of chess software programming and technical issues.

Moderators: hgm, Rebel, chrisw

User avatar
lucasart
Posts: 3232
Joined: Mon May 31, 2010 1:29 pm
Full name: lucasart

Re: assert

Post by lucasart »

Harald wrote:If you want to test only parts of your code then you can do things like this:

In an header like basics.h

Code: Select all

// Which tests should be performed
#ifdef _DEBUG
#define ASSERT_BASICS(c,t)              { if ( !(c) )  throw (t); }
#define ASSERT_PRINCIPAL_VARIATION(c,t) { if ( !(c) )  throw (t); }
#define ASSERT_MOVE(c,t)                { if ( !(c) )  throw (t); }
...
#else
#define ASSERT_BASICS(c,t)
#define ASSERT_PRINCIPAL_VARIATION(c,t)
#define ASSERT_MOVE(c,t)
...
#endif
And in a file like move.cpp

Code: Select all

void Move::set( ColorType color, SquareType from, SquareType to,
                PieceType piece, PieceType capture,
                PromotionType promotion
              )
{
    ASSERT_MOVE( color == White || color == Black, "Error: bad color in Move::set()" )
    ASSERT_MOVE&#40; from >= 0 && from < 64, "Error&#58; bad from in Move&#58;&#58;set&#40;)" )
    ASSERT_MOVE&#40; to   >= 0 && to   < 64, "Error&#58; bad to in Move&#58;&#58;set&#40;)" )
    ASSERT_MOVE&#40; piece >= Pawn && piece < 8, "Error&#58; bad piece in Move&#58;&#58;set&#40;)" )
    ASSERT_MOVE&#40; capture >= NoPiece && capture != King && capture < 8, "Error&#58; bad capture in Move&#58;&#58;set&#40;)" )
    ASSERT_MOVE&#40; promotion <= PromoteToKnight, "Error&#58; bad promotion in Move&#58;&#58;set&#40;)" )
    move_ = from | &#40;to << 6&#41; | &#40;promotion << 12&#41; | &#40;piece << 14&#41; | &#40;capture << 17&#41; | &#40;color << 20&#41;;
&#125;
This is pointless. Just clutters the code for no reason.

Assert already prints the source file name and line when it fires.
Theory and practice sometimes clash. And when that happens, theory loses. Every single time.
AlvaroBegue
Posts: 931
Joined: Tue Mar 09, 2010 3:46 pm
Location: New York
Full name: Álvaro Begué (RuyDos)

Re: assert

Post by AlvaroBegue »

lucasart wrote:
Harald wrote:If you want to test only parts of your code then you can do things like this:

In an header like basics.h

Code: Select all

// Which tests should be performed
#ifdef _DEBUG
#define ASSERT_BASICS&#40;c,t&#41;              &#123; if ( !&#40;c&#41; )  throw &#40;t&#41;; &#125;
#define ASSERT_PRINCIPAL_VARIATION&#40;c,t&#41; &#123; if ( !&#40;c&#41; )  throw &#40;t&#41;; &#125;
#define ASSERT_MOVE&#40;c,t&#41;                &#123; if ( !&#40;c&#41; )  throw &#40;t&#41;; &#125;
...
#else
#define ASSERT_BASICS&#40;c,t&#41;
#define ASSERT_PRINCIPAL_VARIATION&#40;c,t&#41;
#define ASSERT_MOVE&#40;c,t&#41;
...
#endif
And in a file like move.cpp

Code: Select all

void Move&#58;&#58;set&#40; ColorType color, SquareType from, SquareType to,
                PieceType piece, PieceType capture,
                PromotionType promotion
              )
&#123;
    ASSERT_MOVE&#40; color == White || color == Black, "Error&#58; bad color in Move&#58;&#58;set&#40;)" )
    ASSERT_MOVE&#40; from >= 0 && from < 64, "Error&#58; bad from in Move&#58;&#58;set&#40;)" )
    ASSERT_MOVE&#40; to   >= 0 && to   < 64, "Error&#58; bad to in Move&#58;&#58;set&#40;)" )
    ASSERT_MOVE&#40; piece >= Pawn && piece < 8, "Error&#58; bad piece in Move&#58;&#58;set&#40;)" )
    ASSERT_MOVE&#40; capture >= NoPiece && capture != King && capture < 8, "Error&#58; bad capture in Move&#58;&#58;set&#40;)" )
    ASSERT_MOVE&#40; promotion <= PromoteToKnight, "Error&#58; bad promotion in Move&#58;&#58;set&#40;)" )
    move_ = from | &#40;to << 6&#41; | &#40;promotion << 12&#41; | &#40;piece << 14&#41; | &#40;capture << 17&#41; | &#40;color << 20&#41;;
&#125;
This is pointless. Just clutters the code for no reason.

Assert already prints the source file name and line when it fires.
Those are pretty bad examples, but sometimes you do want to have a message to go with the assert. I have seen this trick used before:

Code: Select all

    assert&#40;a1 >= 0.0f && "negative values not yet supported");
When the condition is not satisfied you get a message like this:

Code: Select all

Assertion failed&#58; &#40;a1 >= 0.0f && "negative values not yet supported"), function main, file kk.cpp, line 44.
Harald
Posts: 318
Joined: Thu Mar 09, 2006 1:07 am

Re: assert

Post by Harald »

This is pointless. Just clutters the code for no reason.
The point is that you can choose which portions of your program you want to debug today without making the whole program slower.
Henk
Posts: 7220
Joined: Mon May 27, 2013 10:31 am

Re: assert

Post by Henk »

They also say you should avoid using primitive types. If you define a more specific type you can add more checks and make less mistakes.