001 package aima.test.search.eightpuzzle; 002 003 import java.util.ArrayList; 004 import java.util.List; 005 006 import junit.framework.TestCase; 007 import aima.basic.XYLocation; 008 import aima.search.eightpuzzle.EightPuzzleBoard; 009 010 /** 011 * @author Ravi Mohan 012 * 013 */ 014 015 public class EightPuzzleBoardTest extends TestCase { 016 EightPuzzleBoard board; 017 018 @Override 019 public void setUp() { 020 board = new EightPuzzleBoard(); 021 } 022 023 public void testGetBoard() { 024 int[] expected = new int[] { 5, 4, 0, 6, 1, 8, 7, 3, 2 }; 025 int[] boardRepr = board.getBoard(); 026 assertEquals(expected[0], boardRepr[0]); 027 assertEquals(expected[1], boardRepr[1]); 028 assertEquals(expected[2], boardRepr[2]); 029 assertEquals(expected[3], boardRepr[3]); 030 assertEquals(expected[4], boardRepr[4]); 031 assertEquals(expected[5], boardRepr[5]); 032 assertEquals(expected[6], boardRepr[6]); 033 assertEquals(expected[7], boardRepr[7]); 034 assertEquals(expected[8], boardRepr[8]); 035 } 036 037 public void testGetLocation() { 038 assertEquals(new XYLocation(0, 2), board.getLocationOf(0)); 039 assertEquals(new XYLocation(1, 1), board.getLocationOf(1)); 040 assertEquals(new XYLocation(2, 2), board.getLocationOf(2)); 041 assertEquals(new XYLocation(2, 1), board.getLocationOf(3)); 042 assertEquals(new XYLocation(0, 1), board.getLocationOf(4)); 043 assertEquals(new XYLocation(0, 0), board.getLocationOf(5)); 044 assertEquals(new XYLocation(1, 0), board.getLocationOf(6)); 045 assertEquals(new XYLocation(2, 0), board.getLocationOf(7)); 046 assertEquals(new XYLocation(1, 2), board.getLocationOf(8)); 047 } 048 049 public void testGetValueAt() { 050 assertEquals(5, board.getValueAt(new XYLocation(0, 0))); 051 assertEquals(4, board.getValueAt(new XYLocation(0, 1))); 052 assertEquals(0, board.getValueAt(new XYLocation(0, 2))); 053 assertEquals(6, board.getValueAt(new XYLocation(1, 0))); 054 assertEquals(1, board.getValueAt(new XYLocation(1, 1))); 055 assertEquals(8, board.getValueAt(new XYLocation(1, 2))); 056 assertEquals(7, board.getValueAt(new XYLocation(2, 0))); 057 assertEquals(3, board.getValueAt(new XYLocation(2, 1))); 058 assertEquals(2, board.getValueAt(new XYLocation(2, 2))); 059 } 060 061 public void testGetPositions() { 062 List<XYLocation> expected = new ArrayList<XYLocation>(); 063 expected.add(new XYLocation(0, 2)); 064 expected.add(new XYLocation(1, 1)); 065 expected.add(new XYLocation(2, 2)); 066 expected.add(new XYLocation(2, 1)); 067 expected.add(new XYLocation(0, 1)); 068 expected.add(new XYLocation(0, 0)); 069 expected.add(new XYLocation(1, 0)); 070 expected.add(new XYLocation(2, 0)); 071 expected.add(new XYLocation(1, 2)); 072 073 List actual = board.getPositions(); 074 assertEquals(expected, actual); 075 } 076 077 public void testSetBoard() { 078 List<XYLocation> passedIn = new ArrayList<XYLocation>(); 079 passedIn.add(new XYLocation(1, 1)); 080 passedIn.add(new XYLocation(0, 2)); 081 passedIn.add(new XYLocation(2, 2)); 082 passedIn.add(new XYLocation(2, 1)); 083 passedIn.add(new XYLocation(0, 1)); 084 passedIn.add(new XYLocation(0, 0)); 085 passedIn.add(new XYLocation(1, 0)); 086 passedIn.add(new XYLocation(2, 0)); 087 passedIn.add(new XYLocation(1, 2)); 088 board.setBoard(passedIn); 089 assertEquals(new XYLocation(1, 1), board.getLocationOf(0)); 090 assertEquals(new XYLocation(0, 2), board.getLocationOf(1)); 091 } 092 093 }