quoridor/src/test/java/cz/xelfi/quoridor/BoardCase.java
author Jaroslav Tulach <jtulach@netbeans.org>
Thu, 10 Sep 2009 23:19:40 +0200
changeset 75 6802034b7a6f
parent 40 e45bc8ad2eaf
child 92 de3dd5710a5c
permissions -rw-r--r--
Support for giving up the game
jtulach@8
     1
/*
jtulach@8
     2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
jtulach@8
     3
 *
jtulach@8
     4
 * The contents of this file are subject to the terms of either the GNU
jtulach@8
     5
 * General Public License Version 2 only ("GPL") or the Common
jtulach@8
     6
 * Development and Distribution License("CDDL") (collectively, the
jtulach@8
     7
 * "License"). You may not use this file except in compliance with the
jtulach@8
     8
 * License. You can obtain a copy of the License at
jtulach@8
     9
 * http://www.netbeans.org/cddl-gplv2.html
jtulach@8
    10
 * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
jtulach@8
    11
 * specific language governing permissions and limitations under the
jtulach@8
    12
 * License.  When distributing the software, include this License Header
jtulach@8
    13
 * Notice in each file and include the License file at
jtulach@8
    14
 * nbbuild/licenses/CDDL-GPL-2-CP.  Sun designates this
jtulach@8
    15
 * particular file as subject to the "Classpath" exception as provided
jtulach@8
    16
 * by Sun in the GPL Version 2 section of the License file that
jtulach@8
    17
 * accompanied this code. If applicable, add the following below the
jtulach@8
    18
 * License Header, with the fields enclosed by brackets [] replaced by
jtulach@8
    19
 * your own identifying information:
jtulach@8
    20
 * "Portions Copyrighted [year] [name of copyright owner]"
jtulach@8
    21
 *
jtulach@8
    22
 * Contributor(s):
jtulach@8
    23
 *
jtulach@8
    24
 * Portions Copyrighted 2009 Jaroslav Tulach
jtulach@8
    25
 */
jtulach@0
    26
package cz.xelfi.quoridor;
jtulach@0
    27
jtulach@23
    28
import cz.xelfi.quoridor.Fence.Orientation;
jtulach@30
    29
import cz.xelfi.quoridor.Player.Direction;
jtulach@5
    30
import java.util.List;
jtulach@6
    31
import junit.framework.TestCase;
jtulach@0
    32
jtulach@6
    33
/**
jtulach@0
    34
 *
jtulach@6
    35
 * @author Jaroslav Tulach <jtulach@netbeans.org>
jtulach@0
    36
 */
jtulach@6
    37
public abstract class BoardCase extends TestCase {
jtulach@6
    38
    protected Board board;
jtulach@6
    39
jtulach@6
    40
    protected BoardCase(String n) {
jtulach@6
    41
        super(n);
jtulach@16
    42
        board = Board.empty();
jtulach@0
    43
    }
jtulach@0
    44
jtulach@15
    45
    protected abstract Board move(Board b, int player, Player.Direction... where)
jtulach@6
    46
    throws IllegalPositionException;
jtulach@6
    47
jtulach@15
    48
    protected abstract Board fence(Board b, int player, char x, int y, Fence.Orientation orie)
jtulach@6
    49
    throws IllegalPositionException;
jtulach@6
    50
jtulach@13
    51
    protected abstract Board apply(Board b, Move move) throws IllegalPositionException;
jtulach@13
    52
jtulach@0
    53
jtulach@75
    54
    public void testResignWhite() throws IllegalPositionException {
jtulach@75
    55
        List<Player> list = board.getPlayers();
jtulach@75
    56
        assertEquals ("Two", 2, list.size ());
jtulach@75
    57
        Board b = board.apply(Move.RESIGN);
jtulach@75
    58
        assertEquals(b.getPlayers().get(1), b.getWinner());
jtulach@75
    59
        try {
jtulach@75
    60
            b.apply(Move.EAST);
jtulach@75
    61
            fail("No more moves allowed, the player resigned");
jtulach@75
    62
        } catch (IllegalPositionException ex) {
jtulach@75
    63
            // OK
jtulach@75
    64
        }
jtulach@75
    65
        assertNull("No player", b.getCurrentPlayer());
jtulach@75
    66
    }
jtulach@75
    67
    public void testResignBlack() throws IllegalPositionException {
jtulach@75
    68
        List<Player> list = board.getPlayers();
jtulach@75
    69
        assertEquals ("Two", 2, list.size ());
jtulach@75
    70
        Board b = board.apply(Move.NORTH).apply(Move.RESIGN);
jtulach@75
    71
        assertEquals(b.getPlayers().get(0), b.getWinner());
jtulach@75
    72
        try {
jtulach@75
    73
            b.apply(Move.EAST);
jtulach@75
    74
            fail("No more moves allowed, the player resigned");
jtulach@75
    75
        } catch (IllegalPositionException ex) {
jtulach@75
    76
            // OK
jtulach@75
    77
        }
jtulach@75
    78
        assertNull("No player", b.getCurrentPlayer());
jtulach@75
    79
    }
jtulach@0
    80
    public void testTwoPlayers () {
jtulach@15
    81
        List<Player> list = board.getPlayers();
jtulach@0
    82
        assertEquals ("Two", 2, list.size ());
jtulach@0
    83
        assertFalse ("Both are non-null", list.contains (null));
jtulach@0
    84
        try {
jtulach@0
    85
            list.add (null);
jtulach@0
    86
            fail ("Modifications are not allowed");
jtulach@0
    87
        } catch (UnsupportedOperationException ex) {
jtulach@0
    88
            // ok
jtulach@0
    89
        }
jtulach@0
    90
        try {
jtulach@0
    91
            list.remove (0);
jtulach@0
    92
            fail ("Modifications are not allowed");
jtulach@0
    93
        } catch (UnsupportedOperationException ex) {
jtulach@0
    94
            // ok
jtulach@0
    95
        }
jtulach@0
    96
        
jtulach@0
    97
        
jtulach@26
    98
        assertEquals (8, list.get (0).getXInternal());
jtulach@26
    99
        assertEquals (0, list.get (0).getYInternal());
jtulach@0
   100
        assertEquals (10, list.get (0).getFences ());
jtulach@26
   101
        assertEquals (8, list.get (1).getXInternal());
jtulach@26
   102
        assertEquals (16, list.get (1).getYInternal());
jtulach@0
   103
        assertEquals (10, list.get (1).getFences ());
jtulach@0
   104
    }
jtulach@0
   105
    
jtulach@23
   106
    public void testFences () throws IllegalPositionException {
jtulach@0
   107
        assertEquals ("No on board", 0, board.getFences ().size ());
jtulach@0
   108
        try {
jtulach@0
   109
            board.getFences ().add (null);
jtulach@0
   110
            fail ("Should be unmodifiable");
jtulach@0
   111
        } catch (java.lang.UnsupportedOperationException ex) {
jtulach@0
   112
            // ok
jtulach@0
   113
        }
jtulach@23
   114
jtulach@23
   115
        {
jtulach@23
   116
            Board b = board.apply(Move.fence('A', 1, Orientation.HORIZONTAL));
jtulach@23
   117
            assertEquals("One fence placed: ", 1, b.getFences().size());
jtulach@23
   118
            Fence f = b.getFences().iterator().next();
jtulach@23
   119
            assertEquals("Row", 1, f.getRow());
jtulach@23
   120
            assertEquals("Column", 'A', f.getColumn());
jtulach@23
   121
        }
jtulach@23
   122
jtulach@23
   123
        {
jtulach@23
   124
            Board b = board.apply(Move.fence('A', 1, Orientation.VERTICAL));
jtulach@23
   125
            assertEquals("One fence placed: ", 1, b.getFences().size());
jtulach@23
   126
            Fence f = b.getFences().iterator().next();
jtulach@23
   127
            assertEquals("Row", 1, f.getRow());
jtulach@23
   128
            assertEquals("Column", 'A', f.getColumn());
jtulach@23
   129
        }
jtulach@23
   130
jtulach@23
   131
        {
jtulach@23
   132
            Board b = board.apply(Move.fence('H', 8, Orientation.HORIZONTAL));
jtulach@23
   133
            assertEquals("One fence placed: ", 1, b.getFences().size());
jtulach@23
   134
            Fence f = b.getFences().iterator().next();
jtulach@23
   135
            assertEquals("Row", 8, f.getRow());
jtulach@23
   136
            assertEquals("Column", 'H', f.getColumn());
jtulach@23
   137
        }
jtulach@23
   138
jtulach@23
   139
        {
jtulach@23
   140
            Board b = board.apply(Move.fence('H', 8, Orientation.VERTICAL));
jtulach@23
   141
            assertEquals("One fence placed: ", 1, b.getFences().size());
jtulach@23
   142
            Fence f = b.getFences().iterator().next();
jtulach@23
   143
            assertEquals("Row", 8, f.getRow());
jtulach@23
   144
            assertEquals("Column", 'H', f.getColumn());
jtulach@23
   145
        }
jtulach@0
   146
    }
jtulach@0
   147
    
jtulach@0
   148
    public void testFourTimesInAgainstEachOtherResultsInInvalidPosition () throws Exception {
jtulach@0
   149
        Board b = board;
jtulach@0
   150
        
jtulach@0
   151
        for (int i = 0; i < 3; i++) {
jtulach@15
   152
            b = move(b, 0, Player.Direction.NORTH);
jtulach@15
   153
            b = move(b, 1, Player.Direction.SOUTH);
jtulach@0
   154
        }
jtulach@0
   155
        
jtulach@15
   156
        b = move(b, 0, Player.Direction.NORTH);
jtulach@0
   157
        try {
jtulach@15
   158
            b = move(b, 1, Player.Direction.SOUTH);
jtulach@0
   159
            fail ("Now the positions of two players are supposed to be the same, this results in exception");
jtulach@0
   160
        } catch (IllegalPositionException ex) {
jtulach@0
   161
            // ok
jtulach@0
   162
        }
jtulach@0
   163
        
jtulach@0
   164
    }
jtulach@0
   165
jtulach@0
   166
    public void testCrossFences () throws Exception {
jtulach@15
   167
        Board b1 = board.fence (board.getPlayers ().get(0), 'A', 1, Fence.Orientation.HORIZONTAL);
jtulach@0
   168
        
jtulach@0
   169
        try {
jtulach@15
   170
            b1.fence (b1.getPlayers ().get(1), 'A', 1, Fence.Orientation.VERTICAL);
jtulach@0
   171
            fail ("This must fail, as the fences overlap");
jtulach@0
   172
        } catch (IllegalPositionException ex) {
jtulach@0
   173
            // ok
jtulach@0
   174
        }
jtulach@0
   175
    }
jtulach@0
   176
    
jtulach@0
   177
    public void testPawnsCanJumpOverEachOther () throws Exception {
jtulach@0
   178
        Board b = board;
jtulach@0
   179
        
jtulach@0
   180
        for (int i = 0; i < 3; i++) {
jtulach@15
   181
            b = move(b, 0, Player.Direction.NORTH);
jtulach@15
   182
            b = move(b, 1, Player.Direction.SOUTH);
jtulach@0
   183
        }
jtulach@0
   184
        
jtulach@15
   185
        b = move(b, 0, Player.Direction.NORTH);
jtulach@0
   186
        
jtulach@0
   187
        // jump over
jtulach@15
   188
        b = move(b, 1, Player.Direction.SOUTH, Player.Direction.SOUTH);
jtulach@0
   189
    }
jtulach@0
   190
jtulach@0
   191
    public void testCannotJumpOverFence () throws Exception {
jtulach@15
   192
        Board b = fence (board, 0, 'D', 8, Fence.Orientation.HORIZONTAL);
jtulach@21
   193
        assertEquals("One fence is present", 1, b.getFences().size());
jtulach@21
   194
        final Fence f = b.getFences().iterator().next();
jtulach@21
   195
        assertEquals("Row is 8", 8, f.getRow());
jtulach@21
   196
        assertEquals("Column is D", 'D', f.getColumn());
jtulach@0
   197
        try {
jtulach@15
   198
            move(b, 1, Player.Direction.SOUTH);
jtulach@0
   199
            fail ("This shall not be allowed, as there is the fence");
jtulach@0
   200
        } catch (IllegalPositionException ex) {
jtulach@0
   201
            // ok
jtulach@0
   202
        }
jtulach@0
   203
    }
jtulach@0
   204
    
jtulach@0
   205
jtulach@0
   206
    public void testSideJumpsNotAllowedWhenThereIsNoFence () throws Exception {
jtulach@0
   207
        Board b = board;
jtulach@0
   208
        
jtulach@0
   209
        for (int i = 0; i < 3; i++) {
jtulach@15
   210
            b = move(b, 0, Player.Direction.NORTH);
jtulach@15
   211
            b = move(b, 1, Player.Direction.SOUTH);
jtulach@0
   212
        }
jtulach@0
   213
        
jtulach@15
   214
        b = move(b, 0, Player.Direction.NORTH);
jtulach@0
   215
        
jtulach@0
   216
        try {
jtulach@15
   217
            b = move(b, 1, Player.Direction.SOUTH, Player.Direction.WEST);
jtulach@0
   218
            fail ("Cannot just jump aside");
jtulach@0
   219
        } catch (IllegalPositionException ex) {
jtulach@0
   220
            // ok
jtulach@0
   221
        }
jtulach@0
   222
    }
jtulach@0
   223
    
jtulach@0
   224
    public void testSideJumpsAllowedWhenThereAFence () throws Exception {
jtulach@0
   225
        Board b = board;
jtulach@0
   226
        
jtulach@0
   227
        for (int i = 0; i < 3; i++) {
jtulach@15
   228
            b = move(b, 0, Player.Direction.NORTH);
jtulach@15
   229
            b = move(b, 1, Player.Direction.SOUTH);
jtulach@0
   230
        }
jtulach@0
   231
        
jtulach@15
   232
        b = move(b, 0, Player.Direction.NORTH);
jtulach@0
   233
        
jtulach@26
   234
        assertEquals (8, b.getPlayers ().get (0).getXInternal());
jtulach@26
   235
        assertEquals (8, b.getPlayers ().get (0).getYInternal());
jtulach@0
   236
        
jtulach@15
   237
        b = fence(b, 0, 'D', 4, Fence.Orientation.HORIZONTAL);
jtulach@0
   238
        
jtulach@0
   239
        // we can over jump to west
jtulach@15
   240
        move(b, 1, Player.Direction.SOUTH, Player.Direction.WEST);
jtulach@0
   241
        // as well as east
jtulach@15
   242
        move(b, 1, Player.Direction.SOUTH, Player.Direction.EAST);
jtulach@0
   243
    }
jtulach@11
   244
jtulach@11
   245
    public void testPlaceAllFences() throws Exception {
jtulach@11
   246
        doPlaceAllFences(0);
jtulach@11
   247
    }
jtulach@11
   248
    public void testPlaceAllFences2() throws Exception {
jtulach@11
   249
        doPlaceAllFences(1);
jtulach@11
   250
    }
jtulach@11
   251
jtulach@11
   252
    private void doPlaceAllFences(int player) throws Exception {
jtulach@11
   253
        Board b = board;
jtulach@11
   254
jtulach@11
   255
        int cnt = 10;
jtulach@11
   256
        for (int i = 1; i <= 5; i++) {
jtulach@15
   257
            b = fence(b, player, 'A', i, Fence.Orientation.HORIZONTAL);
jtulach@15
   258
            b = fence(b, player, 'D', i, Fence.Orientation.HORIZONTAL);
jtulach@11
   259
            cnt -= 2;
jtulach@11
   260
            assertEquals("Two less" + i, cnt, b.getPlayers().get(player).getFences());
jtulach@11
   261
        }
jtulach@11
   262
jtulach@11
   263
        try {
jtulach@15
   264
            fence(b, player, 'F', 7, Fence.Orientation.VERTICAL);
jtulach@11
   265
            fail("We shall run out of fences");
jtulach@11
   266
        } catch (IllegalPositionException ex) {
jtulach@11
   267
            // OK
jtulach@11
   268
        }
jtulach@11
   269
    }
jtulach@0
   270
    
jtulach@0
   271
    public void testAlwaysHasToHaveAccessToEndLine () throws Exception {
jtulach@0
   272
        Board b = board;
jtulach@0
   273
        
jtulach@15
   274
        b = b.fence (b.getPlayers ().get (0), 'E', 1, Fence.Orientation.HORIZONTAL);
jtulach@15
   275
        b = b.fence (b.getPlayers ().get (0), 'F', 1, Fence.Orientation.VERTICAL);
jtulach@0
   276
jtulach@0
   277
        try {
jtulach@15
   278
            b = b.fence (b.getPlayers ().get (0), 'D', 1, Fence.Orientation.VERTICAL);
jtulach@0
   279
            fail ("This is not allowed as player 0 cannot now reach the final line");
jtulach@0
   280
        } catch (IllegalPositionException ex) {
jtulach@0
   281
            // ok
jtulach@0
   282
        }
jtulach@0
   283
        
jtulach@0
   284
    }
jtulach@0
   285
    
jtulach@0
   286
    public void testEqualsOfPlayers () throws Exception {
jtulach@15
   287
        Player p1 = new Player (1, 1, 10, Player.Direction.EAST);
jtulach@15
   288
        Player p2 = new Player (1, 1, 10, Player.Direction.EAST);
jtulach@15
   289
        Player p3 = new Player (2, 1, 10, Player.Direction.EAST);
jtulach@15
   290
        Player p4 = new Player (1, 1, 10, Player.Direction.WEST);
jtulach@15
   291
        Player p5 = new Player (1, 2, 10, Player.Direction.EAST);
jtulach@15
   292
        Player p6 = new Player (1, 1, 5, Player.Direction.EAST);
jtulach@0
   293
        
jtulach@0
   294
        assertEquals ("p1 == p2", p1, p2);
jtulach@0
   295
        if (p2.equals (p3)) fail ();
jtulach@0
   296
        if (p2.equals (p4)) fail ();
jtulach@0
   297
        if (p2.equals (p5)) fail ();
jtulach@0
   298
        if (p2.equals (p6)) fail ();
jtulach@0
   299
        if (p3.equals (p6)) fail ();
jtulach@0
   300
        if (p4.equals (p3)) fail ();
jtulach@0
   301
        if (p6.equals (p3)) fail ();
jtulach@0
   302
        if (p5.equals (p3)) fail ();
jtulach@0
   303
        if (p5.equals (p3)) fail ();
jtulach@0
   304
        if (p4.equals (p3)) fail ();
jtulach@0
   305
        if (p3.equals (p4)) fail ();
jtulach@0
   306
        if (p3.equals (p5)) fail ();
jtulach@0
   307
        if (p4.equals (p5)) fail ();
jtulach@0
   308
        if (p5.equals (p4)) fail ();
jtulach@0
   309
    }
jtulach@0
   310
    
jtulach@0
   311
    public void testEqualsOfFences () throws Exception {
jtulach@15
   312
        Fence f1 = new Fence (1, 1, Fence.Orientation.HORIZONTAL);
jtulach@15
   313
        Fence f2 = new Fence (1, 1, Fence.Orientation.HORIZONTAL);
jtulach@15
   314
        Fence f3 = new Fence (3, 1, Fence.Orientation.HORIZONTAL);
jtulach@15
   315
        Fence f4 = new Fence (1, 3, Fence.Orientation.HORIZONTAL);
jtulach@15
   316
        Fence f5 = new Fence (1, 1, Fence.Orientation.VERTICAL);
jtulach@0
   317
        
jtulach@0
   318
        assertEquals ("f1 == f2", f1, f2);
jtulach@0
   319
        if (f1.equals (f3)) fail ();
jtulach@0
   320
        if (f3.equals (f1)) fail ();
jtulach@0
   321
        if (f5.equals (f1)) fail ();
jtulach@0
   322
        if (f1.equals (f5)) fail ();
jtulach@0
   323
        if (f4.equals (f1)) fail ();
jtulach@0
   324
        if (f1.equals (f4)) fail ();
jtulach@0
   325
    }
jtulach@0
   326
    
jtulach@0
   327
    public void testEqualsOfBoards1 () throws Exception {
jtulach@15
   328
        Board b1 = board.move (board.getPlayers ().get (0), Player.Direction.NORTH);
jtulach@15
   329
        Board b2 = board.move (board.getPlayers ().get (0), Player.Direction.NORTH);
jtulach@15
   330
        Board b3 = board.move (board.getPlayers ().get (0), Player.Direction.EAST);
jtulach@0
   331
        
jtulach@0
   332
        if (b1.equals (b3)) fail ();
jtulach@0
   333
        if (b3.equals (b1)) fail ();
jtulach@0
   334
        
jtulach@0
   335
        assertEquals ("b1 == b2", b1, b2);
jtulach@0
   336
    }
jtulach@0
   337
    public void testEqualsOfBoards2 () throws Exception {
jtulach@15
   338
        Board b1 = board.fence (board.getPlayers ().get (0), 'E', 3, Fence.Orientation.HORIZONTAL);
jtulach@15
   339
        Board b2 = board.fence (board.getPlayers ().get (0), 'E', 3, Fence.Orientation.HORIZONTAL);
jtulach@15
   340
        Board b3 = board.fence (board.getPlayers ().get (0), 'D', 3, Fence.Orientation.HORIZONTAL);
jtulach@15
   341
        Board b4 = board.fence (board.getPlayers ().get (0), 'E', 4, Fence.Orientation.HORIZONTAL);
jtulach@15
   342
        Board b5 = board.fence (board.getPlayers ().get (0), 'E', 3, Fence.Orientation.VERTICAL);
jtulach@0
   343
        
jtulach@0
   344
        if (b1.equals (b3)) fail ();
jtulach@0
   345
        if (b3.equals (b1)) fail ();
jtulach@0
   346
        if (b1.equals (b4)) fail ();
jtulach@0
   347
        if (b1.equals (b5)) fail ();
jtulach@0
   348
        if (b5.equals (b1)) fail ();
jtulach@0
   349
        if (b4.equals (b1)) fail ();
jtulach@0
   350
        
jtulach@0
   351
        assertEquals ("b1 == b2", b1, b2);
jtulach@0
   352
    }
jtulach@0
   353
    public void testEqualsOfBoardsWhenJumpOver () throws Exception {
jtulach@0
   354
        Board b = board;
jtulach@0
   355
        
jtulach@0
   356
        for (int i = 0; i < 3; i++) {
jtulach@15
   357
            b = move(b, 0, Player.Direction.NORTH);
jtulach@15
   358
            b = move(b, 1, Player.Direction.SOUTH);
jtulach@0
   359
        }
jtulach@0
   360
        
jtulach@15
   361
        Board b1 = move(b, 0, Player.Direction.NORTH);
jtulach@0
   362
        
jtulach@15
   363
        Board b2 = move(b, 1, Player.Direction.SOUTH);
jtulach@15
   364
        b2 = b2.move (b2.getPlayers ().get (0), Player.Direction.NORTH, Player.Direction.NORTH);
jtulach@0
   365
        
jtulach@0
   366
        if (b1.equals (b2)) fail ("Not the same, pawns are reverted");
jtulach@0
   367
        if (b2.equals (b1)) fail ("Not the same, pawns are reverted");
jtulach@0
   368
    }
jtulach@11
   369
jtulach@13
   370
    public void testMoveAltersCurrentPlayer() throws Exception {
jtulach@13
   371
        assertEquals("First player ready", board.getCurrentPlayer(), board.getPlayers().get(0));
jtulach@13
   372
        Board b = apply(board, Move.EAST);
jtulach@13
   373
jtulach@14
   374
        for (int i = 0; i < 7; i++) {
jtulach@13
   375
            assertEquals("Snd player ready", b.getCurrentPlayer(), b.getPlayers().get(1));
jtulach@13
   376
            b = apply(b, Move.SOUTH);
jtulach@13
   377
            assertEquals("First player ready", b.getCurrentPlayer(), b.getPlayers().get(0));
jtulach@13
   378
            b = apply(b, Move.NORTH);
jtulach@13
   379
        }
jtulach@14
   380
jtulach@14
   381
        Board fin = b.apply(Move.NORTH).apply(Move.NORTH);
jtulach@14
   382
        assertNotNull("There is a winner", fin.getWinner());
jtulach@14
   383
        assertEquals("And the winner is", fin.getPlayers().get(0), fin.getWinner());
jtulach@14
   384
jtulach@17
   385
        assertFalse("No next move can be applied", fin.isApplicable(Move.fence('D', 3, Fence.Orientation.HORIZONTAL)));
jtulach@17
   386
jtulach@14
   387
        try {
jtulach@14
   388
            fin.apply(Move.EAST);
jtulach@14
   389
            fail("No moves allow when we are in final position");
jtulach@14
   390
        } catch (IllegalPositionException ex) {
jtulach@14
   391
            // OK
jtulach@14
   392
        }
jtulach@14
   393
jtulach@13
   394
    }
jtulach@30
   395
jtulach@31
   396
    public void testDetectInvalidFence() {
jtulach@31
   397
        try {
jtulach@31
   398
            Move m = Move.fence('D', 9, Orientation.HORIZONTAL);
jtulach@31
   399
            fail("Move shall not be allowed: " + m);
jtulach@31
   400
        } catch (IllegalPositionException ex) {
jtulach@31
   401
            // OK
jtulach@31
   402
        }
jtulach@31
   403
    }
jtulach@31
   404
jtulach@30
   405
    public void testEqualityOfMoves() throws Exception {
jtulach@30
   406
jtulach@30
   407
        for (Direction m1 : Direction.values()) {
jtulach@30
   408
            for (Direction m2 : Direction.values()) {
jtulach@30
   409
                Move both1 = Move.jump(m1, m2);
jtulach@30
   410
                Move both2 = Move.jump(m1, m2);
jtulach@30
   411
                Move opposite = Move.jump(m2, m1);
jtulach@30
   412
jtulach@30
   413
                assertTrue("Both boths are equal", both1.equals(both2));
jtulach@30
   414
                assertFalse("Not north", Move.NORTH.equals(both1));
jtulach@30
   415
                assertFalse("Not east", Move.EAST.equals(both1));
jtulach@30
   416
                assertFalse("Not west", Move.WEST.equals(both1));
jtulach@30
   417
                assertFalse("Not south", Move.SOUTH.equals(both1));
jtulach@30
   418
                if (m1 == m2) {
jtulach@30
   419
                    continue;
jtulach@30
   420
                }
jtulach@30
   421
                assertFalse("Not equal to opposite", both1.equals(opposite));
jtulach@30
   422
            }
jtulach@30
   423
        }
jtulach@30
   424
jtulach@30
   425
        Move f1 = Move.fence('D', 6, Orientation.HORIZONTAL);
jtulach@30
   426
        Move f2 = Move.fence('D', 6, Orientation.HORIZONTAL);
jtulach@30
   427
        Move f3 = Move.fence('D', 6, Orientation.VERTICAL);
jtulach@30
   428
        Move f4 = Move.fence('E', 6, Orientation.VERTICAL);
jtulach@30
   429
        Move f5 = Move.fence('E', 5, Orientation.VERTICAL);
jtulach@30
   430
jtulach@30
   431
        assertEquals(f1, f2);
jtulach@30
   432
        assertFalse(f1.equals(f3));
jtulach@30
   433
        assertFalse(f4.equals(f5));
jtulach@30
   434
        assertFalse(f3.equals(f5));
jtulach@30
   435
    }
jtulach@0
   436
}