1   /***
2    * Redistribution  and use  in source  and binary  forms, with  or without
3    * modification, are permitted provided  that the following conditions are
4    * met :
5    *
6    * . Redistributions  of  source  code  must  retain  the  above copyright
7    *   notice, this list of conditions and the following disclaimer.
8    *
9    * . Redistributions in  binary form  must reproduce  the above  copyright
10   *   notice, this list of conditions  and the following disclaimer in  the
11   *   documentation and/or other materials provided with the distribution.
12   *
13   * . The name of the author may not be used to endorse or promote products
14   *   derived from this software without specific prior written permission.
15   *
16   * THIS SOFTWARE IS  PROVIDED BY THE  AUTHOR ``AS IS''  AND ANY EXPRESS  OR
17   * IMPLIED  WARRANTIES,  INCLUDING,  BUT   NOT  LIMITED  TO,  THE   IMPLIED
18   * WARRANTIES OF MERCHANTABILITY AND  FITNESS FOR A PARTICULAR  PURPOSE ARE
19   * DISCLAIMED.  IN NO  EVENT SHALL  THE AUTHOR  BE LIABLE  FOR ANY  DIRECT,
20   * INDIRECT,  INCIDENTAL,  SPECIAL,  EXEMPLARY,  OR  CONSEQUENTIAL  DAMAGES
21   * (INCLUDING,  BUT  NOT LIMITED  TO,  PROCUREMENT OF  SUBSTITUTE  GOODS OR
22   * SERVICES;  LOSS  OF USE,  DATA,  OR PROFITS;  OR  BUSINESS INTERRUPTION)
23   * HOWEVER CAUSED  AND ON  ANY THEORY  OF LIABILITY,  WHETHER IN  CONTRACT,
24   * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
25   * ANY  WAY  OUT OF  THE  USE OF  THIS  SOFTWARE, EVEN  IF  ADVISED OF  THE
26   * POSSIBILITY OF SUCH DAMAGE.
27   *
28   * $Id: AdjacentTileIteratorTest.java,v 1.2 2005/09/09 18:24:28 mat007 Exp $
29   */
30  
31  package jtge.util.grid;
32  
33  import java.util.Iterator;
34  import java.util.NoSuchElementException;
35  import jtge.util.grid.Coordinate;
36  import jtge.util.grid.Grid;
37  import jtge.util.grid.IGrid;
38  import jtge.util.grid.directiongroup.EightDirectionGroup;
39  import junit.framework.TestCase;
40  
41  /***
42   * @author Jean-Laurent
43   * @version $Id: AdjacentTileIteratorTest.java,v 1.2 2005/09/09 18:24:28 mat007 Exp $
44   */
45  public class AdjacentTileIteratorTest extends TestCase
46  {
47      /***
48       * Tested object.
49       */
50      private IGrid grid;
51  
52      protected void setUp()
53      {
54          grid = new Grid( 3, 3, new EightDirectionGroup() );
55      }
56  
57      public void testEmptyGrid()
58      {
59          final Iterator iterator = grid.adjacentTileIterator( new Coordinate( 1, 1 ) );
60          assertFalse( iterator.hasNext() );
61          try
62          {
63              iterator.next();
64          }
65          catch( NoSuchElementException nsee )
66          {
67              return;
68          }
69          fail( "exception should have been thrown" );
70      }
71  
72      public void testPartialGrid()
73      {
74          grid.setTile( new TestTile( 1 ), new Coordinate( 1, 0 ) );
75          grid.setTile( new TestTile( 2 ), new Coordinate( 2, 0 ) );
76          grid.setTile( new TestTile( 3 ), new Coordinate( 2, 2 ) ); // clockwise !
77          grid.setTile( new TestTile( 4 ), new Coordinate( 1, 2 ) );
78          Iterator iterator = grid.adjacentTileIterator( new Coordinate( 1, 1 ) );
79          assertTrue( iterator.hasNext() );
80          assertEquals( 1, ((TestTile)iterator.next()).number );
81          assertTrue( iterator.hasNext() );
82          assertEquals( 2, ((TestTile)iterator.next()).number );
83          assertTrue( iterator.hasNext() );
84          assertEquals( 3, ((TestTile)iterator.next()).number );
85          assertTrue( iterator.hasNext() );
86          assertEquals( 4, ((TestTile)iterator.next()).number );
87          assertFalse( iterator.hasNext() );
88          try
89          {
90              iterator.next();
91          }
92          catch( NoSuchElementException nsee )
93          {
94              return;
95          }
96          fail( "exception should have been thrown" );
97      }
98  }