Gridarta Editor
TopLevelGameObjectIteratorTest.java
Go to the documentation of this file.
1 /*
2  * Gridarta MMORPG map editor for Crossfire, Daimonin and similar games.
3  * Copyright (C) 2000-2015 The Gridarta Developers.
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License along
16  * with this program; if not, write to the Free Software Foundation, Inc.,
17  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18  */
19 
20 package net.sf.gridarta.model.mapmodel;
21 
22 import java.awt.Point;
23 import java.util.Iterator;
27 import org.jetbrains.annotations.NotNull;
28 import org.junit.Assert;
29 import org.junit.Test;
30 
36 
40  @Test
41  public void testIteratorForwardEmpty() {
42  final TestMapModelCreator mapModelCreator = new TestMapModelCreator(false);
43  final MapModel<TestGameObject, TestMapArchObject, TestArchetype> mapModel = mapModelCreator.newMapModel(3, 2);
44  final Iterator<TestGameObject> it = new TopLevelGameObjectIterator<>(mapModel, new Point(1, 1), +1, false);
45  Assert.assertFalse(it.hasNext());
46  }
47 
51  @Test
52  public void testIteratorBackwardEmpty() {
53  final TestMapModelCreator mapModelCreator = new TestMapModelCreator(false);
54  final MapModel<TestGameObject, TestMapArchObject, TestArchetype> mapModel = mapModelCreator.newMapModel(3, 2);
55  final Iterator<TestGameObject> it = new TopLevelGameObjectIterator<>(mapModel, new Point(1, 1), -1, false);
56  Assert.assertFalse(it.hasNext());
57  }
58 
62  @Test
63  public void testIteratorForward() {
65  final Iterator<TestGameObject> it = new TopLevelGameObjectIterator<>(mapModel, new Point(1, 1), +1, false);
66  Assert.assertTrue(it.hasNext());
67  Assert.assertEquals("n4", it.next().getBestName());
68  Assert.assertTrue(it.hasNext());
69  Assert.assertEquals("n5", it.next().getBestName());
70  Assert.assertTrue(it.hasNext());
71  Assert.assertEquals("n1", it.next().getBestName());
72  Assert.assertTrue(it.hasNext());
73  Assert.assertEquals("n2", it.next().getBestName());
74  Assert.assertTrue(it.hasNext());
75  Assert.assertEquals("n3", it.next().getBestName());
76  Assert.assertFalse(it.hasNext());
77  }
78 
82  @Test
83  public void testIteratorBackward() {
85  final Iterator<TestGameObject> it = new TopLevelGameObjectIterator<>(mapModel, new Point(1, 1), -1, false);
86  Assert.assertTrue(it.hasNext());
87  Assert.assertEquals("n4", it.next().getBestName());
88  Assert.assertTrue(it.hasNext());
89  Assert.assertEquals("n3", it.next().getBestName());
90  Assert.assertTrue(it.hasNext());
91  Assert.assertEquals("n1", it.next().getBestName());
92  Assert.assertTrue(it.hasNext());
93  Assert.assertEquals("n2", it.next().getBestName());
94  Assert.assertTrue(it.hasNext());
95  Assert.assertEquals("n5", it.next().getBestName());
96  Assert.assertFalse(it.hasNext());
97  }
98 
102  @Test
103  public void testIteratorForwardSkip() {
105  final Iterator<TestGameObject> it = new TopLevelGameObjectIterator<>(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 
122  @Test
123  public void testIteratorBackwardSkip() {
125  final Iterator<TestGameObject> it = new TopLevelGameObjectIterator<>(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 
143  @NotNull
145  final TestMapModelCreator mapModelCreator = new TestMapModelCreator(false);
146  final MapModel<TestGameObject, TestMapArchObject, TestArchetype> mapModel = mapModelCreator.newMapModel(3, 2);
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 }
MapModel< TestGameObject, TestMapArchObject, TestArchetype > newMapModel(final int w, final int h)
Creates a new MapModel instance.
void testIteratorForwardEmpty()
Checks the forward iterator on an empty map.
A MapModel reflects the data of a map.
Definition: MapModel.java:75
void insertGameObject(@NotNull final GameObject< TestGameObject, TestMapArchObject, TestArchetype > gameObject, @NotNull final String archetypeName, @NotNull final String name)
Inserts a game object into the inventory of another game object.
Helper class for regression tests to create MapModel instances.
Iterator for iterating over top-level game object of a map model.
void testIteratorForwardSkip()
Checks the forward iterator with skipped first element.
static MapModel< TestGameObject, TestMapArchObject, TestArchetype > createMap()
Creates a new MapModel instance filled with game objects.
A MapArchObject implementation for testing purposes.
Base package of all Gridarta classes.
MapSquare< G, A, R > getMapSquare(@NotNull Point pos)
Get the square at a specified location.
GameObjects are the objects based on Archetypes found on maps.
G getFirst(@NotNull final GameObjectMatcher gameObjectMatcher)
Returns the first occurrence of a matching game object.
Definition: MapSquare.java:226
void testIteratorBackwardSkip()
Checks the backward iterator with skipped first element.
void addGameObjectToMap(@NotNull final MapModel< TestGameObject, TestMapArchObject, TestArchetype > mapModel, @NotNull final String archetypeName, @NotNull final String name, final int x, final int y, @NotNull final InsertionMode< TestGameObject, TestMapArchObject, TestArchetype > insertionMode)
Inserts a game object into a map.
void testIteratorBackwardEmpty()
Checks the backward iterator on an empty map.
An Archetype implementation for testing purposes.
A GameObject implementation for testing purposes.
InsertionMode< TestGameObject, TestMapArchObject, TestArchetype > getTopmostInsertionMode()
Returns the "topmost" insertion mode.