File: test-taquin.vala

package info (click to toggle)
gnome-taquin 3.38.1-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 7,184 kB
  • sloc: xml: 81; python: 11; makefile: 6
file content (107 lines) | stat: -rw-r--r-- 6,272 bytes parent folder | download | duplicates (2)
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
/* -*- Mode: vala; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-

   This file is part of GNOME Taquin.

   Copyright (C) 2019 – Arnaud Bonatti <arnaud.bonatti@gmail.com>

   GNOME Taquin 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 3 of the License, or
   (at your option) any later version.

   GNOME Taquin 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 GNOME Taquin.  If not, see <https://www.gnu.org/licenses/>.
*/

private class TestTaquin : Object
{
    private static int main (string [] args)
    {
        Test.init (ref args);
        Test.add_func ("/Taquin/test tests",
                                test_tests);
        Test.add_func ("/Taquin/test a Fifteen 2 by 2 validity",
                                test_fifteen_validity);
        return Test.run ();
    }

    private static void test_tests ()
    {
        assert_true (1 + 1 == 2);
    }

    /*\
    * * test the Fifteen two by two
    \*/

    /*
                VALID                              INVALID
    ┌───┬───┐ ┌───┬───┐ ┌───┬───┐       ┌───┬───┐ ┌───┬───┐ ┌───┬───┐
    │[0]│ 2 │ │ 3 │ 2 │ │ 3 │ 2 │       │   │ 2 │ │ 4 │ 2 │ │ 4 │ 2 │
    ├───┼───┤ ├───┼───┤ ├───┼───┤       ├───┼───┤ ├───┼───┤ ├───┼───┤
    │ 3 │ 4 │ │   │ 4 │ │ 4 │   │       │ 4 │ 3 │ │   │ 3 │ │ 3 │   │
    └───┴───┘ └───┴───┘ └───┴───┘       └───┴───┘ └───┴───┘ └───┴───┘
    ┌───┬───┐ ┌───┬───┐ ┌───┬───┐       ┌───┬───┐ ┌───┬───┐ ┌───┬───┐
    │ 3 │   │ │   │ 3 │ │ 4 │ 3 │       │ 4 │   │ │   │ 4 │ │ 3 │ 4 │
    ├───┼───┤ ├───┼───┤ ├───┼───┤       ├───┼───┤ ├───┼───┤ ├───┼───┤
    │ 4 │ 2 │ │ 4 │ 2 │ │   │ 2 │       │ 3 │ 2 │ │ 3 │ 2 │ │   │ 2 │
    └───┴───┘ └───┴───┘ └───┴───┘       └───┴───┘ └───┴───┘ └───┴───┘
    ┌───┬───┐ ┌───┬───┐ ┌───┬───┐       ┌───┬───┐ ┌───┬───┐ ┌───┬───┐
    │ 4 │ 3 │ │ 4 │   │ │   │ 4 │       │ 3 │ 4 │ │ 3 │   │ │   │ 3 │
    ├───┼───┤ ├───┼───┤ ├───┼───┤       ├───┼───┤ ├───┼───┤ ├───┼───┤
    │ 2 │   │ │ 2 │ 3 │ │ 2 │ 3 │       │ 2 │   │ │ 2 │ 4 │ │ 2 │ 4 │
    └───┴───┘ └───┴───┘ └───┴───┘       └───┴───┘ └───┴───┘ └───┴───┘
    ┌───┬───┐ ┌───┬───┐ ┌───┬───┐       ┌───┬───┐ ┌───┬───┐ ┌───┬───┐
    │ 2 │ 4 │ │ 2 │ 4 │ │ 2 │   │       │ 2 │ 3 │ │ 2 │ 3 │ │ 2 │   │
    ├───┼───┤ ├───┼───┤ ├───┼───┤       ├───┼───┤ ├───┼───┤ ├───┼───┤
    │   │ 3 │ │ 3 │   │ │ 3 │ 4 │       │   │ 4 │ │ 4 │   │ │ 4 │ 3 │
    └───┴───┘ └───┴───┘ └───┴───┘       └───┴───┘ └───┴───┘ └───┴───┘
    */

    /* This test is a bit abstract: it tests for all the valid places,
       but Taquin shows the empty tile at the top-left corner, always. */
    private static void test_fifteen_validity ()
    {
        for (uint i = 0; i < 100; i++) // should be good enough
        {
            Game game = new Game (GameType.FIFTEEN, 2); // TODO use GameType.SIXTEEN instead?
            print (@"\ntest: $game");

            if (compare_value (ref game, 1, 0, 2))
            {
                assert_true (compare_value (ref game, 0, 0, 3) || compare_value (ref game, 0, 1, 3));
                assert_true (compare_value (ref game, 1, 1, 4) || compare_value (ref game, 0, 1, 4));
            }
            else if (compare_value (ref game, 1, 1, 2))
            {
                assert_true (compare_value (ref game, 1, 0, 3) || compare_value (ref game, 0, 0, 3));
                assert_true (compare_value (ref game, 0, 1, 4) || compare_value (ref game, 0, 0, 4));
            }
            else if (compare_value (ref game, 0, 1, 2))
            {
                assert_true (compare_value (ref game, 1, 1, 3) || compare_value (ref game, 1, 0, 3));
                assert_true (compare_value (ref game, 0, 0, 4) || compare_value (ref game, 1, 0, 4));
            }
            else if (compare_value (ref game, 0, 0, 2))
            {
                assert_true (compare_value (ref game, 0, 1, 3) || compare_value (ref game, 1, 1, 3));
                assert_true (compare_value (ref game, 1, 0, 4) || compare_value (ref game, 1, 1, 4));
            }
            else
                Test.fail ();
        }
    }
    private static bool compare_value (ref Game game, int8 x, int8 y, int8 k)
        requires (x >= 0)
        requires (x < game.size)
        requires (y >= 0)
        requires (y < game.size)
    {
        return game.get_tile_value (x, y) + 1 == k;
    }
}