Update codebase to remove clang warnings (and a couple of legit errors
[typhoon.git] / src / testfen.c
1 /**
2
3 Copyright (c) Scott Gasch
4
5 Module Name:
6
7     testfen.c
8
9 Abstract:
10
11     Test the fen parser.
12
13 Author:
14
15     Scott Gasch ([email protected]) 11 May 2004
16
17 Revision History:
18
19     $Id: testfen.c 345 2007-12-02 22:56:42Z scott $
20
21 **/
22
23 #include "chess.h"
24
25 char *_szPieceAbbrevs = "pPnNbBrRqQkK";
26
27 #ifdef TEST
28 CHAR *
29 GenerateRandomLegalFenString(void)
30 /**
31
32 Routine description:
33
34 Parameters:
35
36     void
37
38 Return value:
39
40     CHAR
41
42 **/
43 {
44     static CHAR buf[MEDIUM_STRING_LEN_CHAR];
45     CHAR *pch = buf;              
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;
53     COOR c;
54     COOR cKingLoc[2];
55     PIECE p;
56
57     memset(buf, 0, sizeof(buf));
58
59     //
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.
64     //
65     cKingLoc[WHITE] = RANDOM_COOR;
66     do
67     {
68         cKingLoc[BLACK] = RANDOM_COOR;
69     }
70     while(cKingLoc[WHITE] == cKingLoc[BLACK]);
71     ASSERT(IS_ON_BOARD(cKingLoc[WHITE]));
72     ASSERT(IS_ON_BOARD(cKingLoc[BLACK]));
73     
74     FOREACH_SQUARE(c)
75     {
76         if (!IS_ON_BOARD(c)) 
77         {
78             if (c % 8 == 0) *pch++ = '/';
79             continue;
80         }
81         else
82         {
83             if (c == cKingLoc[WHITE])
84             {
85                 *pch = 'K';
86             }
87             else if (c == cKingLoc[BLACK])
88             {
89                 *pch = 'k';
90             }
91             else
92             {
93                 if ((rand() & 1) &&
94                     (uNumPiecesOnBoard < 32))
95                 {
96                     do
97                     {
98                         p = RANDOM_PIECE;
99                         ASSERT(IS_VALID_PIECE(p));
100                     }
101                     while(uPieceCounts[p] >= uPieceLimits[p]);
102                     uPieceCounts[p]++;
103                     uNumPiecesOnBoard++;
104                     *pch = *(_szPieceAbbrevs + (p - 2));
105                 }
106                 else
107                 {
108                     *pch = '-';
109                 }
110             }
111         }
112         pch++;
113     }
114     *pch++ = ' ';
115
116     //
117     // Do side on move
118     //
119     *pch = 'w';
120     if (rand() & 1)
121     {
122         *pch = 'b';
123     }
124     pch++;
125     *pch++ = ' ';
126
127     //
128     // Do castling permissions
129     //
130     *pch++ = '-';
131     *pch++ = ' ';
132     
133     //
134     // Do the rest
135     //
136     strcat(buf, "- 0 1");
137     
138     return(buf);
139 }
140
141
142 void
143 TestFenCode(void)
144 {
145     char *p;
146     char *q;
147     UINT u = 0;
148     POSITION pos1, pos2;
149     
150     Trace("Testing FEN translation code...\n");
151     do
152     {
153         //
154         // Generate a random legal FEN string and parse it into pos1.
155         //
156         p = GenerateRandomLegalFenString();
157         if (FALSE == FenToPosition(&pos1, p))
158         {
159             UtilPanic(TESTCASE_FAILURE,
160                       NULL, p, NULL, NULL,
161                       __FILE__, __LINE__);
162         }
163
164         //
165         // Set q to be the FEN code of the position at pos1.
166         //
167         q = PositionToFen(&pos1);
168         if (NULL == q)
169         {
170             UtilPanic(TESTCASE_FAILURE,
171                       &pos1, "q <- PositionToFen", NULL, NULL,
172                       __FILE__, __LINE__);
173         }
174
175         //
176         // Set pos2 to be the position represented in q. 
177         //
178         if (FALSE == FenToPosition(&pos2, q))
179         {
180             UtilPanic(TESTCASE_FAILURE,
181                       NULL, q, NULL, NULL,
182                       __FILE__, __LINE__);
183         }
184
185         //
186         // Pos1 and pos2 should be identical
187         //
188         if (FALSE == PositionsAreEquivalent(&pos1, &pos2))
189         {
190             UtilPanic(TESTCASE_FAILURE,
191                       NULL,
192                       "One initial FEN string has generated two "
193                       "different positions",
194                       &pos1,
195                       &pos2,
196                       __FILE__, __LINE__);
197         }
198         SystemFreeMemory(q);
199         u++;
200     }
201     while(u < 10000);
202 }
203 #endif