Author: Russell Reagan
Date: 10:45:23 07/23/02
Go up one level in this thread
On July 23, 2002 at 08:58:04, Kim Roper Jensen wrote:
>Hi
>
>Im currently writing a little chess program in c++, and just wondered what is
>your favorite debug thingie..
>
>I doesnt mean gdb, but stuff you use for debugging your programs.
>
>With regards
>Kim
I use asserts, asserts, and more asserts. In debug mode my program is horribly
slow since it's double checking data and making sure things have legal values
and all kinds of other things. If it runs fine in debug mode for an extended
period of time, it's probably ok. I also do quite a bit of testing of each class
before I start using it. I test every method, and then I assert the output of
any of the other methods. For example, if I have a Piece class I would create a
Piece object, then verify that all of the "get" functions return what they're
supposed to. For example:
int main (int argc, char * argv[]) {
Piece p; // uninitialized to an 'empty' piece (like an empty square)
assert(!p.isWhite());
assert(!p.isBlack());
assert(!p.isPawn());
assert(!p.isKnight());
assert(!p.isBishop());
assert(!p.isRook());
assert(!p.isQueen());
assert(!p.isKing());
assert(!p.isWhitePawn());
// and then do all of the isWhiteKnight(), isWhiteBishop(), etc.
assert(!p.isBlackPawn());
// and then do all of the isBlackKnight(), etc.
assert(!p.isWhitePiece());
assert(!p.isBlackPiece());
}
After that you know your default initialization works. Now you would initialize
the piece in a different way, like:
Piece p('R'); // initialize to a white rook
and then remove the ! sign out of the assertion lines involving rooks and white
pieces, for example:
assert(p.isRook()); // make sure that it IS a rook
assert(p.isWhitePiece()); // make sure that it IS a white piece
Then you might test out your promotion routine, like:
Piece p('P');
p.promoteTo('Q');
assert(p.isQueen());
assert(p.isWhitePiece());
assert(p.isWhiteQueen());
// etc.
You need to do ALL of the functions each time you change something, not just the
ones that are related to the current piece. So in the last example, you not only
need to test that the piece is a queen and a white piece, but you need to make
sure that the piece is NOT a black bishop at the same time, because that might
cause you problems later on.
This is quite boring and it takes a while to sit down and hammer it all out, but
it's worth it, IMO anyway. After I get done I feel confident that my class works
like it's supposed to. You can go crazy with the asserts, because if you compile
in Release mode it will remove them automatically so there is no speed penalty.
Russell
This page took 0 seconds to execute
Last modified: Thu, 15 Apr 21 08:11:13 -0700
Current Computer Chess Club Forums at Talkchess. This site by Sean Mintz.