[gnome-2048] Add a test.



commit 30c7bd1ba81129dd34dfcfc3e892060a8b4de00e
Author: Arnaud Bonatti <arnaud bonatti gmail com>
Date:   Fri Feb 8 18:53:05 2019 +0100

    Add a test.

 meson.build             |  1 +
 src/game-headerbar.vala |  2 +-
 src/game-window.vala    | 28 ----------------------------
 src/grid.vala           | 47 ++++++++++++++++++++++++++++++++++++++++++++---
 src/meson.build         |  6 +++++-
 src/test-tw12ht.vala    | 29 ++++++++++++++++++++++++++++-
 6 files changed, 79 insertions(+), 34 deletions(-)
---
diff --git a/meson.build b/meson.build
index d95ebc5..f8532d3 100644
--- a/meson.build
+++ b/meson.build
@@ -27,6 +27,7 @@ podir = join_paths(meson.current_source_dir(), 'po')
 posix_dependency = valac.find_library('posix')
 libm_dependency = cc.find_library('m', required: false) # some platforms do not have libm separated from libc
 gio_dependency = dependency('gio-2.0', version: '>= 2.40.0')
+glib_dependency = dependency('glib-2.0', version: '>= 2.40.0')
 gtk_dependency = dependency('gtk+-3.0', version: '>= 3.12.0')
 clutter_dependency = dependency('clutter-1.0', version: '>= 1.12.0')
 clutter_gtk_dependency = dependency('clutter-gtk-1.0', version: '>= 1.6.0')
diff --git a/src/game-headerbar.vala b/src/game-headerbar.vala
index bc076e5..c0498af 100644
--- a/src/game-headerbar.vala
+++ b/src/game-headerbar.vala
@@ -156,7 +156,7 @@ private class GameHeaderBar : HeaderBar
                            ref menu);
 
         bool is_square = rows == cols;
-        bool disallowed_grid = GameWindow.is_disallowed_grid_size (ref rows, ref cols);
+        bool disallowed_grid = Grid.is_disallowed_grid_size (ref rows, ref cols);
         if (disallowed_grid && !is_square)
             /* Translators: command-line warning displayed if the user manually sets a invalid grid size */
             warning (_("Grids of size 1 by 2 are disallowed."));
diff --git a/src/game-window.vala b/src/game-window.vala
index 3621aa2..3e16866 100644
--- a/src/game-window.vala
+++ b/src/game-window.vala
@@ -412,15 +412,6 @@ private class GameWindow : ApplicationWindow
             });
     }
 
-    internal static bool is_disallowed_grid_size (ref int rows, ref int cols)
-        requires (rows >= 1)
-        requires (rows <= 9)
-        requires (cols >= 1)
-        requires (cols <= 9)
-    {
-        return (rows == 1 && cols == 1) || (rows == 1 && cols == 2) || (rows == 2 && cols == 1);
-    }
-
     /*\
     * * gesture
     \*/
@@ -476,22 +467,3 @@ private class GameWindow : ApplicationWindow
         _game.move (request);
     }
 }
-
-private enum MoveRequest {
-    UP,
-    RIGHT,
-    DOWN,
-    LEFT;
-
-    internal static string debug_string (MoveRequest request)
-    {
-        switch (request)
-        {
-            case UP:    return "move up";
-            case RIGHT: return "move right";
-            case DOWN:  return "move down";
-            case LEFT:  return "move left";
-            default:    assert_not_reached ();
-        }
-    }
-}
diff --git a/src/grid.vala b/src/grid.vala
index 773bbd7..b130164 100644
--- a/src/grid.vala
+++ b/src/grid.vala
@@ -19,7 +19,7 @@
 
 private class Grid : Object
 {
-    private uint8 [,] _grid;
+    protected uint8 [,] _grid;
 
     [CCode (notify = false)] public int rows { internal get; protected construct; }
     [CCode (notify = false)] public int cols { internal get; protected construct; }
@@ -404,7 +404,7 @@ private class Grid : Object
         return true;
     }
 
-    private static bool _grid_is_full (ref uint8 [,] grid)
+    protected static bool _grid_is_full (ref uint8 [,] grid)
     {
         uint rows = grid.length [0];
         uint cols = grid.length [1];
@@ -475,6 +475,15 @@ private class Grid : Object
         return _grid [row, col];
     }
 
+    internal static bool is_disallowed_grid_size (ref int rows, ref int cols)
+        requires (rows >= 1)
+        requires (rows <= 9)
+        requires (cols >= 1)
+        requires (cols <= 9)
+    {
+        return (rows == 1 && cols == 1) || (rows == 1 && cols == 2) || (rows == 2 && cols == 1);
+    }
+
     /*\
     * * saving
     \*/
@@ -557,7 +566,7 @@ private class Grid : Object
             return false;
         int cols = (int) number_64;
 
-        if (GameWindow.is_disallowed_grid_size (ref rows, ref cols))
+        if (is_disallowed_grid_size (ref rows, ref cols))
             return false;
         // number of rows + 1 for size + 1 for score; maybe an empty line at end
         if (lines.length < rows + 2)
@@ -602,6 +611,19 @@ private class Grid : Object
     }
 }
 
+private class TestGrid : Grid
+{
+    internal TestGrid (int rows, int cols)
+    {
+        Object (rows: rows, cols: cols);
+    }
+
+    internal bool grid_is_full ()
+    {
+        return _grid_is_full (ref _grid);
+    }
+}
+
 private struct GridPosition
 {
     public int row;
@@ -624,3 +646,22 @@ private struct Tile
     public GridPosition pos;
     public uint8 val;
 }
+
+private enum MoveRequest {
+    UP,
+    RIGHT,
+    DOWN,
+    LEFT;
+
+    internal static string debug_string (MoveRequest request)
+    {
+        switch (request)
+        {
+            case UP:    return "move up";
+            case RIGHT: return "move right";
+            case DOWN:  return "move down";
+            case LEFT:  return "move left";
+            default:    assert_not_reached ();
+        }
+    }
+}
diff --git a/src/meson.build b/src/meson.build
index 83780b1..b362553 100644
--- a/src/meson.build
+++ b/src/meson.build
@@ -2,10 +2,14 @@
 tw12ht_tests = executable(
     'tw12ht_tests',
     [
+        'grid.vala',
         'test-tw12ht.vala'
     ],
     dependencies : [
-        gio_dependency
+        gee_dependency,
+        gio_dependency,
+        glib_dependency,
+        libm_dependency
     ]
 )
 test('tw12ht-tests', tw12ht_tests)
diff --git a/src/test-tw12ht.vala b/src/test-tw12ht.vala
index d8b03c0..cf25da0 100644
--- a/src/test-tw12ht.vala
+++ b/src/test-tw12ht.vala
@@ -23,6 +23,8 @@ private class TestTw12ht : Object
         Test.init (ref args);
         Test.add_func ("/Tw12ht/test tests",
                                 test_tests);
+        Test.add_func ("/Tw12ht/test full game",
+                                test_full_game);
         return Test.run ();
     }
 
@@ -32,6 +34,31 @@ private class TestTw12ht : Object
     }
 
     /*\
-    * * tests
+    * * test full game
     \*/
+
+    private static void test_full_game ()
+    {
+        test_full_grid (3, 3);
+        test_full_grid (4, 4);
+        test_full_grid (5, 5);
+        test_full_grid (3, 5);
+        test_full_grid (4, 3);
+    }
+
+    private static void test_full_grid (int rows, int cols)
+    {
+        TestGrid grid = new TestGrid (rows, cols);
+        for (int i = 0; i < rows * cols; i++)
+        {
+            Tile unused;
+            grid.new_tile (out unused);
+        }
+
+        assert_true (grid.grid_is_full ());
+
+        for (int i = 0; i < rows; i++)
+            for (int j = 0; j < cols; j++)
+                assert_true (grid [i, j] == 1);
+    }
 }


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