001/*
002 * Gridarta MMORPG map editor for Crossfire, Daimonin and similar games.
003 * Copyright (C) 2000-2011 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.mapmodel;
021
022import java.awt.Point;
023import java.util.Iterator;
024import net.sf.gridarta.model.archetype.TestArchetype;
025import net.sf.gridarta.model.gameobject.TestGameObject;
026import net.sf.gridarta.model.maparchobject.TestMapArchObject;
027import org.jetbrains.annotations.NotNull;
028import org.junit.Assert;
029import org.junit.Test;
030
031/**
032 * Regression tests for {@link TopLevelGameObjectIterator}.
033 * @author Andreas Kirschbaum
034 */
035public class TopLevelGameObjectIteratorTest {
036
037    /**
038     * Checks the forward iterator on an empty map.
039     */
040    @Test
041    public void testIteratorForwardEmpty() {
042        final TestMapModelCreator mapModelCreator = new TestMapModelCreator(false);
043        final MapModel<TestGameObject, TestMapArchObject, TestArchetype> mapModel = mapModelCreator.newMapModel(3, 2);
044        final Iterator<TestGameObject> it = new TopLevelGameObjectIterator<TestGameObject, TestMapArchObject, TestArchetype>(mapModel, new Point(1, 1), +1, false);
045        Assert.assertFalse(it.hasNext());
046    }
047
048    /**
049     * Checks the backward iterator on an empty map.
050     */
051    @Test
052    public void testIteratorBackwardEmpty() {
053        final TestMapModelCreator mapModelCreator = new TestMapModelCreator(false);
054        final MapModel<TestGameObject, TestMapArchObject, TestArchetype> mapModel = mapModelCreator.newMapModel(3, 2);
055        final Iterator<TestGameObject> it = new TopLevelGameObjectIterator<TestGameObject, TestMapArchObject, TestArchetype>(mapModel, new Point(1, 1), -1, false);
056        Assert.assertFalse(it.hasNext());
057    }
058
059    /**
060     * Checks the forward iterator.
061     */
062    @Test
063    public void testIteratorForward() {
064        final MapModel<TestGameObject, TestMapArchObject, TestArchetype> mapModel = createMap();
065        final Iterator<TestGameObject> it = new TopLevelGameObjectIterator<TestGameObject, TestMapArchObject, TestArchetype>(mapModel, new Point(1, 1), +1, false);
066        Assert.assertTrue(it.hasNext());
067        Assert.assertEquals("n4", it.next().getBestName());
068        Assert.assertTrue(it.hasNext());
069        Assert.assertEquals("n5", it.next().getBestName());
070        Assert.assertTrue(it.hasNext());
071        Assert.assertEquals("n1", it.next().getBestName());
072        Assert.assertTrue(it.hasNext());
073        Assert.assertEquals("n2", it.next().getBestName());
074        Assert.assertTrue(it.hasNext());
075        Assert.assertEquals("n3", it.next().getBestName());
076        Assert.assertFalse(it.hasNext());
077    }
078
079    /**
080     * Checks the backward iterator.
081     */
082    @Test
083    public void testIteratorBackward() {
084        final MapModel<TestGameObject, TestMapArchObject, TestArchetype> mapModel = createMap();
085        final Iterator<TestGameObject> it = new TopLevelGameObjectIterator<TestGameObject, TestMapArchObject, TestArchetype>(mapModel, new Point(1, 1), -1, false);
086        Assert.assertTrue(it.hasNext());
087        Assert.assertEquals("n4", it.next().getBestName());
088        Assert.assertTrue(it.hasNext());
089        Assert.assertEquals("n3", it.next().getBestName());
090        Assert.assertTrue(it.hasNext());
091        Assert.assertEquals("n1", it.next().getBestName());
092        Assert.assertTrue(it.hasNext());
093        Assert.assertEquals("n2", it.next().getBestName());
094        Assert.assertTrue(it.hasNext());
095        Assert.assertEquals("n5", it.next().getBestName());
096        Assert.assertFalse(it.hasNext());
097    }
098
099    /**
100     * Checks the forward iterator with skipped first element.
101     */
102    @Test
103    public void testIteratorForwardSkip() {
104        final MapModel<TestGameObject, TestMapArchObject, TestArchetype> mapModel = createMap();
105        final Iterator<TestGameObject> it = new TopLevelGameObjectIterator<TestGameObject, TestMapArchObject, TestArchetype>(mapModel, new Point(1, 1), +1, true);
106        Assert.assertTrue(it.hasNext());
107        Assert.assertEquals("n5", it.next().getBestName());
108        Assert.assertTrue(it.hasNext());
109        Assert.assertEquals("n1", it.next().getBestName());
110        Assert.assertTrue(it.hasNext());
111        Assert.assertEquals("n2", it.next().getBestName());
112        Assert.assertTrue(it.hasNext());
113        Assert.assertEquals("n3", it.next().getBestName());
114        Assert.assertTrue(it.hasNext());
115        Assert.assertEquals("n4", it.next().getBestName());
116        Assert.assertFalse(it.hasNext());
117    }
118
119    /**
120     * Checks the backward iterator with skipped first element.
121     */
122    @Test
123    public void testIteratorBackwardSkip() {
124        final MapModel<TestGameObject, TestMapArchObject, TestArchetype> mapModel = createMap();
125        final Iterator<TestGameObject> it = new TopLevelGameObjectIterator<TestGameObject, TestMapArchObject, TestArchetype>(mapModel, new Point(1, 1), -1, true);
126        Assert.assertTrue(it.hasNext());
127        Assert.assertEquals("n3", it.next().getBestName());
128        Assert.assertTrue(it.hasNext());
129        Assert.assertEquals("n1", it.next().getBestName());
130        Assert.assertTrue(it.hasNext());
131        Assert.assertEquals("n2", it.next().getBestName());
132        Assert.assertTrue(it.hasNext());
133        Assert.assertEquals("n5", it.next().getBestName());
134        Assert.assertTrue(it.hasNext());
135        Assert.assertEquals("n4", it.next().getBestName());
136        Assert.assertFalse(it.hasNext());
137    }
138
139    /**
140     * Creates a new {@link MapModel} instance filled with game objects.
141     * @return the new map model instance
142     */
143    @NotNull
144    private static MapModel<TestGameObject, TestMapArchObject, TestArchetype> createMap() {
145        final TestMapModelCreator mapModelCreator = new TestMapModelCreator(false);
146        final MapModel<TestGameObject, TestMapArchObject, TestArchetype> mapModel = mapModelCreator.newMapModel(3, 2);
147        final InsertionMode<TestGameObject, TestMapArchObject, TestArchetype> insertionMode = mapModelCreator.getTopmostInsertionMode();
148        mapModelCreator.addGameObjectToMap(mapModel, "arch", "n1", 0, 0, insertionMode);
149        mapModelCreator.addGameObjectToMap(mapModel, "arch", "n2", 0, 0, insertionMode);
150        mapModelCreator.addGameObjectToMap(mapModel, "arch", "n3", 2, 0, insertionMode);
151        mapModelCreator.addGameObjectToMap(mapModel, "arch", "n4", 1, 1, insertionMode);
152        mapModelCreator.addGameObjectToMap(mapModel, "arch", "n5", 2, 1, insertionMode);
153
154        //noinspection ConstantConditions
155        mapModelCreator.insertGameObject(mapModel.getMapSquare(new Point(2, 0)).getFirst(), "arch", "i1");
156
157        return mapModel;
158    }
159
160}