001/*
002 * Gridarta MMORPG map editor for Crossfire, Daimonin and similar games.
003 * Copyright (C) 2000-2010 The Gridarta Developers.
004 *
005 * This program is free software; you can redistribute it and/or modify
006 * it under the terms of the GNU General Public License as published by
007 * the Free Software Foundation; either version 2 of the License, or
008 * (at your option) any later version.
009 *
010 * This program is distributed in the hope that it will be useful,
011 * but WITHOUT ANY WARRANTY; without even the implied warranty of
012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
013 * GNU General Public License for more details.
014 *
015 * You should have received a copy of the GNU General Public License along
016 * with this program; if not, write to the Free Software Foundation, Inc.,
017 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
018 */
019
020package net.sf.gridarta.model.mapgrid;
021
022import java.awt.Point;
023import net.sf.gridarta.utils.Size2D;
024import org.junit.Assert;
025import org.junit.Test;
026
027/**
028 * Test for {@link MapGrid}.
029 * @author Andreas Kirschbaum
030 */
031public class MapGridTest {
032
033    /**
034     * Test case for selection border updates.
035     */
036    @Test
037    public void testSelectionBorderUpdates() {
038        final MapGrid mapGrid = new MapGrid(new Size2D(4, 3));
039        checkSelectionBorder(mapGrid, "0000" + "0000" + "0000");
040
041        mapGrid.select(new Point(1, 1), SelectionMode.ADD);
042        checkSelectionBorder(mapGrid, "0000" + "0f00" + "0000");
043
044        mapGrid.select(new Point(2, 1), SelectionMode.ADD);
045        checkSelectionBorder(mapGrid, "0000" + "0d70" + "0000");
046
047        mapGrid.select(new Point(3, 1), SelectionMode.ADD);
048        checkSelectionBorder(mapGrid, "0000" + "0d57" + "0000");
049
050        mapGrid.select(new Point(3, 1), SelectionMode.SUB);
051        checkSelectionBorder(mapGrid, "0000" + "0d70" + "0000");
052
053        mapGrid.selectArea(new Point(0, 0), new Point(3, 2), SelectionMode.FLIP);
054        checkSelectionBorder(mapGrid, "9553" + "a00a" + "c556");
055
056        mapGrid.select(new Point(1, 1), SelectionMode.FLIP);
057        checkSelectionBorder(mapGrid, "9153" + "820a" + "c456");
058
059        mapGrid.select(new Point(1, 1), SelectionMode.SUB);
060        checkSelectionBorder(mapGrid, "9553" + "a00a" + "c556");
061
062        mapGrid.selectArea(new Point(0, 0), new Point(3, 2), SelectionMode.ADD);
063        checkSelectionBorder(mapGrid, "9113" + "8002" + "c446");
064
065        mapGrid.resize(new Size2D(2, 1));
066        checkSelectionBorder(mapGrid, "d7");
067
068        mapGrid.resize(new Size2D(3, 4));
069        checkSelectionBorder(mapGrid, "d700" + "0000" + "0000");
070
071        mapGrid.selectArea(new Point(0, 0), new Point(2, 3), SelectionMode.ADD);
072        checkSelectionBorder(mapGrid, "913" + "802" + "802" + "c46");
073
074        mapGrid.resize(new Size2D(4, 3));
075        checkSelectionBorder(mapGrid, "9130" + "8020" + "c460");
076    }
077
078    /**
079     * Checks that the map border selection flags of a {@link MapGrid} instance
080     * is as expected. The expected border selection is represented as
081     * <code>String</code>. Each square is represented as one hex digit
082     * character; 1=north, 2=east, 4=south, 8=east border is set.
083     * @param mapGrid the map grid
084     * @param expectedBorder the expected border selection
085     */
086    private static void checkSelectionBorder(final MapGrid mapGrid, final String expectedBorder) {
087        final StringBuilder sb = new StringBuilder();
088        final Size2D size = mapGrid.getSize();
089        for (int y = 0; y < size.getHeight(); y++) {
090            for (int x = 0; x < size.getWidth(); x++) {
091                final int flags = mapGrid.getFlags(x, y);
092                int value = 0;
093                if ((flags & MapGrid.GRID_FLAG_SELECTION_NORTH) != 0) {
094                    value |= 1;
095                }
096                if ((flags & MapGrid.GRID_FLAG_SELECTION_EAST) != 0) {
097                    value |= 2;
098                }
099                if ((flags & MapGrid.GRID_FLAG_SELECTION_SOUTH) != 0) {
100                    value |= 4;
101                }
102                if ((flags & MapGrid.GRID_FLAG_SELECTION_WEST) != 0) {
103                    value |= 8;
104                }
105                sb.append(Integer.toHexString(value));
106            }
107        }
108        Assert.assertEquals(expectedBorder, sb.toString());
109    }
110
111}