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
|
class GlChess
{
static int test_count = 0;
static int failure_count = 0;
private static void test_pgn_file (string data, string moves)
{
test_count++;
PGN file;
try
{
file = new PGN.from_string (data);
}
catch (PGNError e)
{
stderr.printf ("%d. FAIL %s\n", test_count, e.message);
failure_count++;
return;
}
var game = file.games.nth_data (0);
var move_string = "";
foreach (var move in game.moves)
move_string += "%s ".printf (move);
move_string = move_string.strip ();
if (move_string == moves)
stderr.printf ("%d. PASS\n", test_count);
else
{
failure_count++;
stderr.printf ("%d. FAIL got moves '%s', expected '%s'\n", test_count, move_string, moves);
}
}
public static int main (string[] args)
{
/* Simple file in export format */
test_pgn_file ("[Event \"?\"]\n" +
"[Site \"?\"]\n" +
"[Date \"????.??.??\"]\n" +
"[Round \"?\"]\n" +
"[White \"\"]\n" +
"[Black \"\"]\n" +
"[Result \"*\"]\n" +
"\n" +
"1. *\n",
"");
/* No tags */
test_pgn_file ("1. e1 *\n", "e1");
/* No move numbers */
test_pgn_file ("e1 *\n", "e1");
/* No move result */
test_pgn_file ("e1\n", "e1");
/* No trailing newline */
test_pgn_file ("e1", "e1");
/* Carriage returns instead of newlines */
test_pgn_file ("[Event \"?\"]\r" +
"\r" +
"1. d4 *\r",
"d4");
/* Comments */
test_pgn_file ("; Line comment 1\n" +
"[Event \"?\"]\n" +
"; Line comment 2\n" +
"\n" +
"1. e4 {First Move} e5 {Multi\n" +
"line\n" +
"comment} 2. Nc3 {More comments} * {Comment about game end}\n",
"e4 e5 Nc3");
/* Format used by Yahoo Chess */
test_pgn_file (";Title: Yahoo! Chess Game\n" +
";White: roovis\n" +
";Black: ladyjones96\n" +
";Date: Fri Oct 19 12:51:54 GMT 2007\n" +
"\n" +
"1. e2-e4 e7-e5\n",
"e2-e4 e7-e5");
/* Recursive Annotation Variation */
test_pgn_file ("1.Ra8+ (1.Bxd6+ Kb7 2.Rc7+ Kb8 (2...Kb6 3.Ra6#) 3.Rd7+ Kc8 4.Rc1# (4.Ra8#))",
"Ra8+");
/* Numeric Annotation Glyph */
test_pgn_file ("e4 e5 $1 Nc3 $2",
"e4 e5 Nc3");
stdout.printf ("%d/%d tests successful\n", test_count - failure_count, test_count);
return failure_count;
}
}
|