~ubuntu-branches/ubuntu/precise/gnome-games/precise-proposed

« back to all changes in this revision

Viewing changes to glchess/src/test-chess-pgn.vala

  • Committer: Package Import Robot
  • Author(s): Rodrigo Moya
  • Date: 2011-05-30 13:32:04 UTC
  • mfrom: (1.3.4)
  • mto: (163.1.3 precise)
  • mto: This revision was merged to the branch mainline in revision 143.
  • Revision ID: package-import@ubuntu.com-20110530133204-celaq1v1dsxc48q1
Tags: upstream-3.0.2
ImportĀ upstreamĀ versionĀ 3.0.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
class GlChess
 
2
{
 
3
    static int test_count = 0;
 
4
    static int failure_count = 0;
 
5
 
 
6
    private static void test_pgn_file (string data, string moves)
 
7
    {
 
8
        test_count++;
 
9
 
 
10
        PGN file;
 
11
        try
 
12
        {
 
13
            file = new PGN.from_string (data);
 
14
        }
 
15
        catch (PGNError e)
 
16
        {
 
17
            stderr.printf ("%d. FAIL %s\n", test_count, e.message);
 
18
            failure_count++;
 
19
            return;
 
20
        }
 
21
 
 
22
        var game = file.games.nth_data (0);
 
23
        var move_string = "";
 
24
        foreach (var move in game.moves)
 
25
            move_string += "%s ".printf (move);
 
26
        move_string = move_string.strip ();
 
27
 
 
28
        if (move_string == moves)
 
29
            stderr.printf ("%d. PASS\n", test_count);
 
30
        else
 
31
        {
 
32
            failure_count++;
 
33
            stderr.printf ("%d. FAIL got moves '%s', expected '%s'\n", test_count, move_string, moves);
 
34
        }
 
35
    }
 
36
 
 
37
    public static int main (string[] args)
 
38
    {
 
39
        /* Simple file in export format */
 
40
        test_pgn_file ("[Event \"?\"]\n" +
 
41
                       "[Site \"?\"]\n" +
 
42
                       "[Date \"????.??.??\"]\n" +
 
43
                       "[Round \"?\"]\n" +
 
44
                       "[White \"\"]\n" +
 
45
                       "[Black \"\"]\n" +
 
46
                       "[Result \"*\"]\n" +
 
47
                       "\n" +
 
48
                       "1. *\n",
 
49
                       "");
 
50
 
 
51
        /* No tags */
 
52
        test_pgn_file ("1. e1 *\n", "e1");
 
53
 
 
54
        /* No move numbers */
 
55
        test_pgn_file ("e1 *\n", "e1");
 
56
 
 
57
        /* No move result */
 
58
        test_pgn_file ("e1\n", "e1");
 
59
 
 
60
        /* No trailing newline */
 
61
        test_pgn_file ("e1", "e1");
 
62
 
 
63
        /* Carriage returns instead of newlines */
 
64
        test_pgn_file ("[Event \"?\"]\r" +
 
65
                       "\r" +
 
66
                       "1. d4 *\r",
 
67
                       "d4");
 
68
 
 
69
        /* Comments */
 
70
        test_pgn_file ("; Line comment 1\n" +
 
71
                       "[Event \"?\"]\n" +
 
72
                       "; Line comment 2\n" +
 
73
                       "\n" +
 
74
                       "1. e4 {First Move} e5 {Multi\n" +
 
75
                       "line\n" +
 
76
                       "comment} 2. Nc3 {More comments} * {Comment about game end}\n",
 
77
                       "e4 e5 Nc3");
 
78
 
 
79
        /* Format used by Yahoo Chess */
 
80
        test_pgn_file (";Title: Yahoo! Chess Game\n" +
 
81
                       ";White: roovis\n" +
 
82
                       ";Black: ladyjones96\n" +
 
83
                       ";Date: Fri Oct 19 12:51:54 GMT 2007\n" +
 
84
                       "\n" +
 
85
                       "1. e2-e4 e7-e5\n",
 
86
                       "e2-e4 e7-e5");
 
87
 
 
88
        /* Recursive Annotation Variation */
 
89
        test_pgn_file ("1.Ra8+ (1.Bxd6+ Kb7 2.Rc7+ Kb8 (2...Kb6 3.Ra6#) 3.Rd7+ Kc8 4.Rc1# (4.Ra8#))",
 
90
                       "Ra8+");
 
91
 
 
92
        /* Numeric Annotation Glyph */
 
93
        test_pgn_file ("e4 e5 $1 Nc3 $2",
 
94
                       "e4 e5 Nc3");
 
95
 
 
96
        stdout.printf ("%d/%d tests successful\n", test_count - failure_count, test_count);
 
97
 
 
98
        return failure_count;
 
99
    }
 
100
}