Gridarta Editor
MapSquareIteratorTest.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.junit.Assert;
28 import org.junit.Test;
29 
34 public class MapSquareIteratorTest {
35 
39  @Test
40  public void testIteratorForward() {
41  final TestMapModelCreator mapModelCreator = new TestMapModelCreator(false);
42  final MapModel<TestGameObject, TestMapArchObject, TestArchetype> mapModel = mapModelCreator.newMapModel(3, 2);
43  final Iterator<MapSquare<TestGameObject, TestMapArchObject, TestArchetype>> it = new MapSquareIterator<>(mapModel, null, +1, false);
44  Assert.assertTrue(it.hasNext());
45  Assert.assertSame(mapModel.getMapSquare(new Point(0, 0)), it.next());
46  Assert.assertTrue(it.hasNext());
47  Assert.assertSame(mapModel.getMapSquare(new Point(1, 0)), it.next());
48  Assert.assertTrue(it.hasNext());
49  Assert.assertSame(mapModel.getMapSquare(new Point(2, 0)), it.next());
50  Assert.assertTrue(it.hasNext());
51  Assert.assertSame(mapModel.getMapSquare(new Point(0, 1)), it.next());
52  Assert.assertTrue(it.hasNext());
53  Assert.assertSame(mapModel.getMapSquare(new Point(1, 1)), it.next());
54  Assert.assertTrue(it.hasNext());
55  Assert.assertSame(mapModel.getMapSquare(new Point(2, 1)), it.next());
56  Assert.assertFalse(it.hasNext());
57  }
58 
62  @Test
63  public void testIteratorBackward() {
64  final TestMapModelCreator mapModelCreator = new TestMapModelCreator(false);
65  final MapModel<TestGameObject, TestMapArchObject, TestArchetype> mapModel = mapModelCreator.newMapModel(3, 2);
66  final Iterator<MapSquare<TestGameObject, TestMapArchObject, TestArchetype>> it = new MapSquareIterator<>(mapModel, null, -1, false);
67  Assert.assertTrue(it.hasNext());
68  Assert.assertSame(mapModel.getMapSquare(new Point(2, 1)), it.next());
69  Assert.assertTrue(it.hasNext());
70  Assert.assertSame(mapModel.getMapSquare(new Point(1, 1)), it.next());
71  Assert.assertTrue(it.hasNext());
72  Assert.assertSame(mapModel.getMapSquare(new Point(0, 1)), it.next());
73  Assert.assertTrue(it.hasNext());
74  Assert.assertSame(mapModel.getMapSquare(new Point(2, 0)), it.next());
75  Assert.assertTrue(it.hasNext());
76  Assert.assertSame(mapModel.getMapSquare(new Point(1, 0)), it.next());
77  Assert.assertTrue(it.hasNext());
78  Assert.assertSame(mapModel.getMapSquare(new Point(0, 0)), it.next());
79  Assert.assertFalse(it.hasNext());
80  }
81 
85  @Test
86  public void testIteratorForwardStart() {
87  final TestMapModelCreator mapModelCreator = new TestMapModelCreator(false);
88  final MapModel<TestGameObject, TestMapArchObject, TestArchetype> mapModel = mapModelCreator.newMapModel(3, 2);
89  final Iterator<MapSquare<TestGameObject, TestMapArchObject, TestArchetype>> it = new MapSquareIterator<>(mapModel, new Point(0, 1), +1, false);
90  Assert.assertTrue(it.hasNext());
91  Assert.assertSame(mapModel.getMapSquare(new Point(0, 1)), it.next());
92  Assert.assertTrue(it.hasNext());
93  Assert.assertSame(mapModel.getMapSquare(new Point(1, 1)), it.next());
94  Assert.assertTrue(it.hasNext());
95  Assert.assertSame(mapModel.getMapSquare(new Point(2, 1)), it.next());
96  Assert.assertTrue(it.hasNext());
97  Assert.assertSame(mapModel.getMapSquare(new Point(0, 0)), it.next());
98  Assert.assertTrue(it.hasNext());
99  Assert.assertSame(mapModel.getMapSquare(new Point(1, 0)), it.next());
100  Assert.assertTrue(it.hasNext());
101  Assert.assertSame(mapModel.getMapSquare(new Point(2, 0)), it.next());
102  Assert.assertFalse(it.hasNext());
103  }
104 
108  @Test
110  final TestMapModelCreator mapModelCreator = new TestMapModelCreator(false);
111  final MapModel<TestGameObject, TestMapArchObject, TestArchetype> mapModel = mapModelCreator.newMapModel(3, 2);
112  final Iterator<MapSquare<TestGameObject, TestMapArchObject, TestArchetype>> it = new MapSquareIterator<>(mapModel, new Point(0, 1), -1, false);
113  Assert.assertTrue(it.hasNext());
114  Assert.assertSame(mapModel.getMapSquare(new Point(0, 1)), it.next());
115  Assert.assertTrue(it.hasNext());
116  Assert.assertSame(mapModel.getMapSquare(new Point(2, 0)), it.next());
117  Assert.assertTrue(it.hasNext());
118  Assert.assertSame(mapModel.getMapSquare(new Point(1, 0)), it.next());
119  Assert.assertTrue(it.hasNext());
120  Assert.assertSame(mapModel.getMapSquare(new Point(0, 0)), it.next());
121  Assert.assertTrue(it.hasNext());
122  Assert.assertSame(mapModel.getMapSquare(new Point(2, 1)), it.next());
123  Assert.assertTrue(it.hasNext());
124  Assert.assertSame(mapModel.getMapSquare(new Point(1, 1)), it.next());
125  Assert.assertFalse(it.hasNext());
126  }
127 
132  @Test
134  final TestMapModelCreator mapModelCreator = new TestMapModelCreator(false);
135  final MapModel<TestGameObject, TestMapArchObject, TestArchetype> mapModel = mapModelCreator.newMapModel(3, 2);
136  final Iterator<MapSquare<TestGameObject, TestMapArchObject, TestArchetype>> it = new MapSquareIterator<>(mapModel, new Point(0, 1), +1, true);
137  Assert.assertTrue(it.hasNext());
138  Assert.assertSame(mapModel.getMapSquare(new Point(1, 1)), it.next());
139  Assert.assertTrue(it.hasNext());
140  Assert.assertSame(mapModel.getMapSquare(new Point(2, 1)), it.next());
141  Assert.assertTrue(it.hasNext());
142  Assert.assertSame(mapModel.getMapSquare(new Point(0, 0)), it.next());
143  Assert.assertTrue(it.hasNext());
144  Assert.assertSame(mapModel.getMapSquare(new Point(1, 0)), it.next());
145  Assert.assertTrue(it.hasNext());
146  Assert.assertSame(mapModel.getMapSquare(new Point(2, 0)), it.next());
147  Assert.assertTrue(it.hasNext());
148  Assert.assertSame(mapModel.getMapSquare(new Point(0, 1)), it.next());
149  Assert.assertFalse(it.hasNext());
150  }
151 
156  @Test
158  final TestMapModelCreator mapModelCreator = new TestMapModelCreator(false);
159  final MapModel<TestGameObject, TestMapArchObject, TestArchetype> mapModel = mapModelCreator.newMapModel(3, 2);
160  final Iterator<MapSquare<TestGameObject, TestMapArchObject, TestArchetype>> it = new MapSquareIterator<>(mapModel, new Point(0, 1), -1, true);
161  Assert.assertTrue(it.hasNext());
162  Assert.assertSame(mapModel.getMapSquare(new Point(2, 0)), it.next());
163  Assert.assertTrue(it.hasNext());
164  Assert.assertSame(mapModel.getMapSquare(new Point(1, 0)), it.next());
165  Assert.assertTrue(it.hasNext());
166  Assert.assertSame(mapModel.getMapSquare(new Point(0, 0)), it.next());
167  Assert.assertTrue(it.hasNext());
168  Assert.assertSame(mapModel.getMapSquare(new Point(2, 1)), it.next());
169  Assert.assertTrue(it.hasNext());
170  Assert.assertSame(mapModel.getMapSquare(new Point(1, 1)), it.next());
171  Assert.assertTrue(it.hasNext());
172  Assert.assertSame(mapModel.getMapSquare(new Point(0, 1)), it.next());
173  Assert.assertFalse(it.hasNext());
174  }
175 
179  @Test(expected = IllegalArgumentException.class)
180  public void testIteratorDirection0() {
181  final TestMapModelCreator mapModelCreator = new TestMapModelCreator(false);
182  final MapModel<TestGameObject, TestMapArchObject, TestArchetype> mapModel = mapModelCreator.newMapModel(3, 2);
183  new MapSquareIterator<>(mapModel, null, 0, false);
184  }
185 
189  @Test(expected = IllegalArgumentException.class)
190  public void testIteratorDirection2() {
191  final TestMapModelCreator mapModelCreator = new TestMapModelCreator(false);
192  final MapModel<TestGameObject, TestMapArchObject, TestArchetype> mapModel = mapModelCreator.newMapModel(3, 2);
193  new MapSquareIterator<>(mapModel, null, 2, false);
194  }
195 
196 }
MapModel< TestGameObject, TestMapArchObject, TestArchetype > newMapModel(final int w, final int h)
Creates a new MapModel instance.
void testIteratorDirection0()
Checks that invalid directions are rejected.
A MapModel reflects the data of a map.
Definition: MapModel.java:75
Helper class for regression tests to create MapModel instances.
void testIteratorBackward()
Checks that the backward iterator returns all map squares.
void testIteratorBackwardStartSkip()
Checks that the backward iterator returns all map squares when skip is enabled.
A MapArchObject implementation for testing purposes.
void testIteratorForward()
Checks that the forward iterator returns all map squares.
Base package of all Gridarta classes.
void testIteratorForwardStartSkip()
Checks that the forward iterator returns all map squares when skip is enabled.
GameObjects are the objects based on Archetypes found on maps.
void testIteratorBackwardStart()
Checks that the backward iterator returns all map squares.
void testIteratorForwardStart()
Checks that the forward iterator returns all map squares.
An Archetype implementation for testing purposes.
A GameObject implementation for testing purposes.
void testIteratorDirection2()
Checks that invalid directions are rejected.
Iterator for iterating over all squares of a model.