Update codebase to remove clang warnings (and a couple of legit errors
[typhoon.git] / src / testsearch.c
1 /**
2
3 Copyright (c) Scott Gasch
4
5 Module Name:
6
7     testsearch.c
8
9 Abstract:
10
11     This is a test module to sanity check the search code.  It
12     operates by generating a random legal chess position and
13     performing a two-ply search on the position.  It is expected that
14     the search will terminate and return a legal move.
15
16 Author:
17
18     Scott Gasch ([email protected]) 2 Jan 2005
19
20 Revision History:
21
22 **/
23
24 #include "chess.h"
25
26 #ifdef TEST
27 #define TIME_LIMIT 200.0
28
29 void
30 SetMoveTimerForTestingSearch(void)
31 {
32     g_MoveTimer.uNodeCheckMask = 0x10000 - 1;
33     g_MoveTimer.dStartTime = SystemTimeStamp();
34     g_MoveTimer.bvFlags = 0;
35     g_MoveTimer.dSoftTimeLimit = g_MoveTimer.dStartTime + TIME_LIMIT;
36     g_MoveTimer.dHardTimeLimit = g_MoveTimer.dStartTime + TIME_LIMIT;
37 }
38
39 FLAG
40 TestSearch(void)
41 {
42     SEARCHER_THREAD_CONTEXT *ctx;
43     POSITION pos;
44     ULONG u;
45     FLAG fOver;
46     FLAG fPost = g_Options.fShouldPost;
47     FLAG fRet = FALSE;
48
49     ctx = SystemAllocateMemory(sizeof(SEARCHER_THREAD_CONTEXT));
50     ASSERT(ctx);
51     g_Options.fShouldPost = FALSE;
52     Trace("Testing Search routine...\n");
53     for (u = 0; u < 20; u++)
54     {
55         GenerateRandomLegalPosition(&pos);
56         InitializeSearcherContext(&pos, ctx);
57     
58         g_MoveTimer.bvFlags = 0;
59         g_Options.fPondering = FALSE;
60         g_Options.fThinking = TRUE;
61         g_Options.fSuccessfulPonder = FALSE;
62         
63         MaintainDynamicMoveOrdering();
64         DirtyHashTable();
65         
66         //
67         // TODO: Any preEval?
68         //
69         
70         //
71         // Set a very long time limit on the search
72         //
73         SetMoveTimerForTestingSearch();
74         g_Options.uMaxDepth = 2;
75         
76         //
77         // TODO: Set draw value
78         //
79 #if (PERF_COUNTERS && MP)
80         ClearHelperThreadIdleness();
81 #endif
82         fOver = Iterate(ctx);
83
84         //
85         // How long did that take?
86         //
87         if (SystemTimeStamp() - g_MoveTimer.dStartTime > TIME_LIMIT)
88         {
89             UtilPanic(TESTCASE_FAILURE,
90                       NULL, "TestSearch", NULL, NULL,
91                       __FILE__, __LINE__);
92         }
93
94         //
95         // Did we get a sane move?
96         //
97         if (GAME_NOT_OVER == fOver)
98         {
99             if (FALSE == SanityCheckMove(&pos, ctx->mvRootMove))
100             {
101                 UtilPanic(TESTCASE_FAILURE,
102                           NULL, "TestSearch", NULL, NULL,
103                           __FILE__, __LINE__);
104             }
105         }
106 #ifdef DEBUG
107         else if (GAME_WHITE_WON == fOver)
108         {
109             ASSERT(InCheck(&pos, BLACK));
110         }
111         else if (GAME_BLACK_WON == fOver)
112         {
113             ASSERT(InCheck(&pos, WHITE));
114         }
115 #endif
116     }
117     fRet = TRUE;
118
119     g_Options.fShouldPost = fPost;
120     SystemFreeMemory(ctx);
121     return(fRet);
122 }
123 #endif