[iagno] Add a couple simple tests



commit df0f0e613e608549e261cd0ab9b0e357effcfb90
Author: Michael Catanzaro <mcatanzaro gnome org>
Date:   Fri Sep 27 23:09:53 2013 -0500

    Add a couple simple tests

 configure.ac             |    2 +
 src/Makefile.am          |   17 +++++-
 src/computer-player.vala |    2 +-
 src/test-iagno.vala      |  157 ++++++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 175 insertions(+), 3 deletions(-)
---
diff --git a/configure.ac b/configure.ac
index fc1d95d..a69428a 100644
--- a/configure.ac
+++ b/configure.ac
@@ -23,6 +23,8 @@ PKG_CHECK_MODULES(IAGNO, [
   libcanberra-gtk3 >= $CANBERRA_GTK_REQUIRED
 ])
 
+AC_PATH_PROG(GTESTER, gtester)
+
 dnl ###########################################################################
 dnl Internationalization
 dnl ###########################################################################
diff --git a/src/Makefile.am b/src/Makefile.am
index 5088809..4ddb956 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -1,4 +1,5 @@
 bin_PROGRAMS = iagno
+check_PROGRAMS = test-iagno
 
 iagno_SOURCES = \
        config.vapi \
@@ -23,8 +24,20 @@ iagno_VALAFLAGS = \
        --pkg libcanberra \
        --pkg libcanberra-gtk
 
-iagno_LDADD = \
-       $(IAGNO_LIBS)
+iagno_LDADD = $(IAGNO_LIBS)
+
+test_iagno_SOURCES = \
+       test-iagno.vala \
+       computer-player.vala \
+       game.vala \
+       player.vala
+
+test_iagno_CFLAGS = $(IAGNO_CFLAGS)
+
+test_iagno_LDADD = $(IAGNO_LIBS)
+
+check-local: $(check_PROGRAMS)
+       $(GTESTER) --keep-going --verbose $(check_PROGRAMS)
 
 CLEANFILES = \
        $(patsubst %.vala,%.c,$(filter %.vala, $(SOURCES))) \
diff --git a/src/computer-player.vala b/src/computer-player.vala
index 7ac1150..57d4199 100644
--- a/src/computer-player.vala
+++ b/src/computer-player.vala
@@ -50,7 +50,7 @@ public class ComputerPlayer : Object
         65,  -3, 6, 4, 4, 6,  -3, 65
     };
 
-    public ComputerPlayer (Game game, int level)
+    public ComputerPlayer (Game game, int level = 1)
     {
         this.game = game;
         this.level = level;
diff --git a/src/test-iagno.vala b/src/test-iagno.vala
new file mode 100644
index 0000000..f961faf
--- /dev/null
+++ b/src/test-iagno.vala
@@ -0,0 +1,157 @@
+/*
+ * Copyright (C) 2013 Michael Catanzaro
+ *
+ * This file is part of Iagno.
+ *
+ * Iagno is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * Iagno is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with Iagno.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+public class TestIagno : Object
+{
+    private static void test_undo_after_pass ()
+    {
+        string[] board = {"    LLLL",
+                          "   LLLLD",
+                          "  LLLLD ",
+                          "  LLLDLL",
+                          " LLLDLLL",
+                          " LLDDLLL",
+                          "LLLLLLLL",
+                          "LLLLLLLL"};
+        Game game = new Game.from_strings (board, Player.DARK);
+        assert (game.place_tile (7, 2) > 0);
+        assert (!game.can_move (Player.LIGHT));
+        assert (game.can_undo ());
+        game.pass ();
+        assert (game.can_undo ());
+        game.undo (2);
+        assert (game.to_string ().strip () == string.joinv ("\n", board).strip ());
+        assert (game.place_tile (7, 2) > 0);
+        assert (!game.can_move (Player.LIGHT));
+        assert (game.can_undo ());
+        game.undo (1);
+        assert (game.to_string ().strip () == string.joinv ("\n", board).strip ());
+    }
+
+    private static void test_current_color_after_pass ()
+    {
+        string[] board = {"L LLLLLL",
+                          "LLLLLLLL",
+                          "L LLLLLL",
+                          "LDLLLLLL",
+                          "LDDLDLLL",
+                          "LDLDLLLL",
+                          "DDDDDLLL",
+                          "DDDDDDDD"};
+        Game game = new Game.from_strings (board, Player.DARK);
+        assert (game.current_color == Player.DARK);
+        assert (game.place_tile (1, 2) > 0);
+        assert (game.current_color == Player.LIGHT);
+        assert (!game.can_move (Player.LIGHT));
+        game.pass ();
+        assert (game.current_color == Player.DARK);
+    }
+
+    private static void test_ai_search_1 ()
+    {
+        string[] board = {"L  LLLLL",
+                          "LLDDDDDD",
+                          "DDDDDLDD",
+                          "LDLLLLLL",
+                          "LLDLDDLL",
+                          "LLDDLLLL",
+                          "LLLLLLLL",
+                          "LLLLLLLL"};
+        Game game = new Game.from_strings (board, Player.LIGHT);
+        ComputerPlayer ai = new ComputerPlayer (game);
+        ai.move ();
+        /* didn't crash */
+    }
+
+    private static void test_ai_search_2 ()
+    {
+        string[] board = {"        ",
+                          "        ",
+                          "   D    ",
+                          "   DD   ",
+                          "   DLL  ",
+                          "  DDD   ",
+                          "   D    ",
+                          "  D     "};
+        Game game = new Game.from_strings (board, Player.LIGHT);
+        ComputerPlayer ai = new ComputerPlayer (game);
+        ai.move ();
+        /* didn't crash */
+    }
+
+    private static void test_ai_search_3 ()
+    {
+        string[] board = {"DL DDDDD",
+                          "DLDDDDDD",
+                          "DDLDLDDD",
+                          "DLDDLDDD",
+                          "DDLDLLLD",
+                          "DLDDLDLD",
+                          "LLLLLDDD",
+                          "DDDDDDDD"};
+        Game game = new Game.from_strings (board, Player.LIGHT);
+        ComputerPlayer ai = new ComputerPlayer (game);
+        ai.move ();
+        assert (game.get_owner (2, 0) == Player.LIGHT);
+    }
+
+    private static void test_ai_search_4 ()
+    {
+        string[] board = {"  LDDDDD",
+                          "DLDLDLDD",
+                          "DDDLLDLD",
+                          "DDLLDLDD",
+                          "DLLLDDDD",
+                          "DLDLDLDD",
+                          "DDDLLDDD",
+                          "DDDLDDDD"};
+        Game game = new Game.from_strings (board, Player.LIGHT);
+        ComputerPlayer ai = new ComputerPlayer (game);
+        ai.move ();
+        assert (game.get_owner (1, 0) == Player.LIGHT);
+    }
+
+    private static void test_ai_search_5 ()
+    {
+        string[] board = {"     L  ",
+                          "    LL  ",
+                          "  LLLL  ",
+                          "  LLLL  ",
+                          " DDDLLLL",
+                          " L DLLLL",
+                          " LLLDLLL",
+                          "  LLLLLL"};
+        Game game = new Game.from_strings (board, Player.LIGHT);
+        ComputerPlayer ai = new ComputerPlayer (game);
+        ai.move ();
+        /* didn't crash */
+    }
+
+    public static int main (string[] args) {
+        Test.init (ref args);
+        Test.add_func ("/Iagno/Pass then Undo", test_undo_after_pass);
+        Test.add_func ("/Iagno/Current Color after Pass", test_current_color_after_pass);
+        Test.add_func ("/Iagno/AI Search 1", test_ai_search_1);
+        Test.add_func ("/Iagno/AI Search 2", test_ai_search_2);
+        Test.add_func ("/Iagno/AI Search 3", test_ai_search_3);
+        Test.add_func ("/Iagno/AI Search 4", test_ai_search_4);
+        Test.add_func ("/Iagno/AI Search 5", test_ai_search_5);
+        return Test.run ();
+    }
+}


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]