-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathCSearchSuite.cc
177 lines (152 loc) · 4.63 KB
/
CSearchSuite.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
#include <sstream>
#include <iostream>
#include <fstream>
#include <stdlib.h>
#include "CSearchSuite.h"
/***************************************************************
* open
***************************************************************/
void CSearchSuite::open(const char *fileName)
{
m_testSuiteFile.open(fileName);
std::string logFileName(fileName);
logFileName += ".log";
m_testSuiteLogFile.open(logFileName.c_str(), std::ofstream::trunc);
} // end of open
/***************************************************************
* close
***************************************************************/
void CSearchSuite::close()
{
m_testSuiteFile.close();
m_testSuiteLogFile.close();
} // end of close
/***************************************************************
* operator <<
***************************************************************/
std::ostream& operator <<(std::ostream &os, const CSearchSuite &rhs)
{
return os << rhs.ToString();
}
/***************************************************************
* ToString
***************************************************************/
std::string CSearchSuite::ToString() const
{
std::stringstream ss;
ss << m_passCount << " test cases passed!" << std::endl;
ss << m_failCount << " test cases failed!" << std::endl;
return ss.str();
} // end of ToString
/***************************************************************
* DoTest
* Returns true on any errors.
***************************************************************/
bool CSearchSuite::DoTest()
{
if (!m_testSuiteFile.is_open())
{
std::cerr << "Could not open file. " << std::endl;
return true;
}
while (true)
{
std::string line;
getline(m_testSuiteFile, line);
if (m_testSuiteFile.eof())
{
return false;
}
if (line[0] == '#')
continue; // Skip comments
const char *p;
if (m_board.read_from_fen(line.c_str(), &p))
{
std::cerr << "Error reading from FEN" << std::endl;
return false;
}
// Read command
while (*p == ' ')
++p;
std::string id;
CMoveList expBestMoves;
CMoveList avoidMoves;
std::cout << m_board;
// Parse input line
while (*p)
{
if (p[0] == 'i' and p[1] == 'd') {
p += 2;
while (*p == ' ')
++p;
if (*p == '"')
{
++p;
const char *p2 = p;
while (*p2 != '"')
p2++;
id = std::string(p, p2);
p=p2+1;
}
}
else if (p[0] == 'b' and p[1] == 'm') {
p += 2;
while (*p == ' ')
{
++p;
CMove move = m_board.readMove(p, &p);
if (!move.Valid())
{
std::cout << "Invalid move" << std::endl;
exit(-1);
}
expBestMoves.push_back(move);
}
}
else if (p[0] == 'a' and p[1] == 'm') {
p += 2;
while (*p == ' ')
{
++p;
CMove move = m_board.readMove(p, &p);
if (!move.Valid())
{
std::cout << "Invalid move" << std::endl;
exit(-1);
}
avoidMoves.push_back(move);
}
}
else if (p[0] == 'c') {
p++;
while (*p != ';')
{
++p;
}
}
if (*p == ';')
{
++p;
while (*p == ' ')
++p;
}
} // while (*p)
CMove best_move = m_ai.find_best_move(60*1000, 60*1000, 1); // Search for one minute
m_testSuiteLogFile << id << " : " << best_move << " : ";
if (avoidMoves.is_in(best_move))
{
m_testSuiteLogFile << "Wrong! " << "Should be avoided" << std::endl;
m_failCount++;
}
else if (expBestMoves.is_in(best_move))
{
m_testSuiteLogFile << "Correct!" << std::endl;
m_passCount++;
}
else
{
m_testSuiteLogFile << "Wrong! Expected " << expBestMoves << std::endl;
m_failCount++;
}
} // end of while
} // end of DoTest