3 Copyright (c) Scott Gasch
19 $Id: testfen.c 345 2007-12-02 22:56:42Z scott $
25 char *_szPieceAbbrevs = "pPnNbBrRqQkK";
29 GenerateRandomLegalFenString(void)
44 static CHAR buf[MEDIUM_STRING_LEN_CHAR];
46 static ULONG uPieceLimits[14] =
47 // - - p P n N b B r R q Q k K
48 {0, 0, 8, 8, 2, 2, 2, 2, 2, 2, 1, 1, 1, 1};
49 ULONG uPieceCounts[14] =
50 // - - p P n N b B r R q Q k K
51 {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1};
52 ULONG uNumPiecesOnBoard = 2;
57 memset(buf, 0, sizeof(buf));
60 // Do the board. Note: this can result in a position where one or
61 // both kings are en-prise, pawns on the 1st/8th etc. The purpose
62 // is just to test PositionToFen and FenToPosition, not to make
63 // legal chess position.
65 cKingLoc[WHITE] = RANDOM_COOR;
68 cKingLoc[BLACK] = RANDOM_COOR;
70 while(cKingLoc[WHITE] == cKingLoc[BLACK]);
71 ASSERT(IS_ON_BOARD(cKingLoc[WHITE]));
72 ASSERT(IS_ON_BOARD(cKingLoc[BLACK]));
78 if (c % 8 == 0) *pch++ = '/';
83 if (c == cKingLoc[WHITE])
87 else if (c == cKingLoc[BLACK])
94 (uNumPiecesOnBoard < 32))
99 ASSERT(IS_VALID_PIECE(p));
101 while(uPieceCounts[p] >= uPieceLimits[p]);
104 *pch = *(_szPieceAbbrevs + (p - 2));
128 // Do castling permissions
136 strcat(buf, "- 0 1");
150 Trace("Testing FEN translation code...\n");
154 // Generate a random legal FEN string and parse it into pos1.
156 p = GenerateRandomLegalFenString();
157 if (FALSE == FenToPosition(&pos1, p))
159 UtilPanic(TESTCASE_FAILURE,
165 // Set q to be the FEN code of the position at pos1.
167 q = PositionToFen(&pos1);
170 UtilPanic(TESTCASE_FAILURE,
171 &pos1, "q <- PositionToFen", NULL, NULL,
176 // Set pos2 to be the position represented in q.
178 if (FALSE == FenToPosition(&pos2, q))
180 UtilPanic(TESTCASE_FAILURE,
186 // Pos1 and pos2 should be identical
188 if (FALSE == PositionsAreEquivalent(&pos1, &pos2))
190 UtilPanic(TESTCASE_FAILURE,
192 "One initial FEN string has generated two "
193 "different positions",