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: HexagonGridTest.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.List; 35 import java.util.Vector; 36 import jtge.util.grid.Coordinate; 37 import jtge.util.grid.Grid; 38 import jtge.util.grid.IGrid; 39 import jtge.util.grid.directiongroup.FlatHexagonDirectionGroup; 40 import jtge.util.grid.directiongroup.FlatShiftedHexagonDirectionGroup; 41 import jtge.util.grid.directiongroup.PeakHexagonDirectionGroup; 42 import jtge.util.grid.directiongroup.PeakShiftedHexagonDirectionGroup; 43 import junit.framework.TestCase; 44 45 /*** 46 * @author Jean-Laurent Fabre de Morlhon 47 * @version $Id: HexagonGridTest.java,v 1.2 2005/09/09 18:24:28 mat007 Exp $ 48 */ 49 public class HexagonGridTest extends TestCase 50 { 51 private IGrid grid; 52 53 public void testFlatGridLinearIterator() 54 { 55 grid = new Grid( 3, 3, new FlatHexagonDirectionGroup() ); 56 int count = 0; 57 Iterator iterator = grid.linearIterator(); 58 while( iterator.hasNext() ) 59 { 60 count++; 61 iterator.next(); 62 } 63 assertEquals( 9, count ); 64 } 65 66 public void testFlatGridAdjacentIterator() 67 { 68 List list; 69 grid = new Grid( 3, 3, new FlatHexagonDirectionGroup() ); 70 list = buildMeAListOutOfAdjacentCoordinates( new Coordinate( 0, 0 ) ); 71 assertEquals( 2, list.size() ); 72 assertEquals( list.get( 0 ), new Coordinate( 1, 0 ) ); 73 assertEquals( list.get( 1 ), new Coordinate( 0, 1 ) ); 74 list = buildMeAListOutOfAdjacentCoordinates( new Coordinate( 1, 1 ) ); 75 assertEquals( 6, list.size() ); 76 assertEquals( list.get( 0 ), new Coordinate( 1, 0 ) ); 77 assertEquals( list.get( 1 ), new Coordinate( 2, 1 ) ); 78 assertEquals( list.get( 2 ), new Coordinate( 2, 2 ) ); 79 assertEquals( list.get( 3 ), new Coordinate( 1, 2 ) ); 80 assertEquals( list.get( 4 ), new Coordinate( 0, 2 ) ); 81 assertEquals( list.get( 5 ), new Coordinate( 0, 1 ) ); 82 list = buildMeAListOutOfAdjacentCoordinates( new Coordinate( 2, 1 ) ); 83 assertEquals( 4, list.size() ); 84 assertEquals( list.get( 0 ), new Coordinate( 2, 0 ) ); 85 assertEquals( list.get( 1 ), new Coordinate( 2, 2 ) ); 86 assertEquals( list.get( 2 ), new Coordinate( 1, 1 ) ); 87 assertEquals( list.get( 3 ), new Coordinate( 1, 0 ) ); 88 } 89 90 public void testFlatShiftedGridAdjacentIterator() 91 { 92 List list; 93 grid = new Grid( 3, 3, new FlatShiftedHexagonDirectionGroup() ); 94 list = buildMeAListOutOfAdjacentCoordinates( new Coordinate( 0, 0 ) ); 95 assertEquals( 3, list.size() ); 96 assertEquals( list.get( 0 ), new Coordinate( 1, 0 ) ); 97 assertEquals( list.get( 1 ), new Coordinate( 1, 1 ) ); 98 assertEquals( list.get( 2 ), new Coordinate( 0, 1 ) ); 99 list = buildMeAListOutOfAdjacentCoordinates( new Coordinate( 1, 2 ) ); 100 assertEquals( 5, list.size() ); 101 assertEquals( list.get( 0 ), new Coordinate( 1, 1 ) ); 102 assertEquals( list.get( 1 ), new Coordinate( 2, 1 ) ); 103 assertEquals( list.get( 2 ), new Coordinate( 2, 2 ) ); 104 assertEquals( list.get( 3 ), new Coordinate( 0, 2 ) ); 105 assertEquals( list.get( 4 ), new Coordinate( 0, 1 ) ); 106 } 107 108 public void testPeakGridAdjacentIterator() 109 { 110 List list; 111 grid = new Grid( 3, 3, new PeakHexagonDirectionGroup() ); 112 list = buildMeAListOutOfAdjacentCoordinates( new Coordinate( 0, 0 ) ); 113 assertEquals( 2, list.size() ); 114 assertEquals( list.get( 0 ), new Coordinate( 1, 0 ) ); 115 assertEquals( list.get( 1 ), new Coordinate( 0, 1 ) ); 116 } 117 118 public void testPeakShiftedGridAdjacentIterator() 119 { 120 grid = new Grid( 3, 3, new PeakShiftedHexagonDirectionGroup() ); 121 final List list = buildMeAListOutOfAdjacentCoordinates( new Coordinate( 0, 0 ) ); 122 assertEquals( 3, list.size() ); 123 assertEquals( list.get( 0 ), new Coordinate( 1, 0 ) ); 124 assertEquals( list.get( 1 ), new Coordinate( 1, 1 ) ); 125 assertEquals( list.get( 2 ), new Coordinate( 0, 1 ) ); 126 } 127 128 public void testPeakShiftedGridAdjacentIteratorOutsideGrid() 129 { 130 grid = new Grid( 10, 10, new PeakShiftedHexagonDirectionGroup() ); 131 final List list = buildMeAListOutOfAdjacentCoordinates( new Coordinate( 10, 9 ) ); 132 assertEquals( 2, list.size() ); 133 assertEquals( list.get( 0 ), new Coordinate( 9, 9 ) ); 134 assertEquals( list.get( 1 ), new Coordinate( 9, 8 ) ); 135 } 136 137 private List buildMeAListOutOfAdjacentCoordinates( Coordinate coordinate ) 138 { 139 Vector result = new Vector(); 140 Iterator iterator = grid.adjacentIterator( coordinate ); 141 while( iterator.hasNext() ) 142 result.add( iterator.next() ); 143 return result; 144 } 145 }