001    package aima.test.coretest;
002    
003    import java.util.ArrayList;
004    
005    import junit.framework.TestCase;
006    import aima.basic.Agent;
007    import aima.basic.MockAgent;
008    import aima.basic.Wall;
009    import aima.basic.XYEnvironment;
010    import aima.basic.XYLocation;
011    
012    /**
013     * @author Ravi Mohan
014     * 
015     */
016    public class XYEnvironmentTest extends TestCase {
017            XYEnvironment env;
018    
019            Agent a;
020    
021            private static String LOCATION = XYEnvironment.LOCATION;
022    
023            public XYEnvironmentTest(String name) {
024                    super(name);
025            }
026    
027            @Override
028            public void setUp() {
029                    env = new XYEnvironment(10, 12);
030                    a = new MockAgent();
031                    env.addAgent(a, new XYLocation(3, 4));
032            }
033    
034            public void testAddObject() {
035    
036                    assertEquals(1, env.getAgents().size());
037                    assertEquals(new XYLocation(3, 4), a.getAttribute(LOCATION));
038            }
039    
040            public void testAddObject2() {
041                    env.addObject(new Wall(), new XYLocation(9, 9));
042                    assertEquals(1, env.getAgents().size());
043                    assertEquals(1, env.getObjects().size());
044                    assertEquals(1, env.getObjectsAt(new XYLocation(9, 9)).size());
045            }
046    
047            public void testAddObjectTwice() {
048                    assertEquals(1, env.getAgents().size());
049                    XYLocation loc = new XYLocation(5, 5);
050                    XYLocation loc2 = new XYLocation(6, 6);
051                    Agent b = new MockAgent();
052                    env.addAgent(b, loc);
053                    assertEquals(2, env.getAgents().size());
054    
055                    assertEquals(loc, b.getAttribute(LOCATION));
056            }
057    
058            public void testAbsoluteMoveObject() {
059                    XYLocation loc = new XYLocation(5, 5);
060                    env.moveObjectToAbsoluteLocation(a, loc);
061                    assertEquals(new XYLocation(5, 5), a.getAttribute(LOCATION));
062    
063            }
064    
065            public void testMoveObject() {
066                    XYLocation loc = new XYLocation(5, 5);
067                    env.moveObjectToAbsoluteLocation(a, loc);
068                    assertEquals(new XYLocation(5, 5), a.getAttribute(LOCATION));
069                    env.moveObject(a, "North");
070                    assertEquals(new XYLocation(5, 4), a.getAttribute(LOCATION));
071                    env.moveObject(a, "East");
072                    assertEquals(new XYLocation(6, 4), a.getAttribute(LOCATION));
073                    env.moveObject(a, "South");
074                    assertEquals(new XYLocation(6, 5), a.getAttribute(LOCATION));
075                    env.moveObject(a, "West");
076                    assertEquals(new XYLocation(5, 5), a.getAttribute(LOCATION));
077            }
078    
079            public void testIsBlocked() {
080                    XYLocation loc = new XYLocation(5, 5);
081                    assertEquals(0, env.getObjectsAt(loc).size());
082                    assertEquals(false, env.isBlocked(loc));
083                    env.addObject(new Wall(), loc);
084                    assertEquals(1, env.getObjectsAt(loc).size());
085                    assertEquals(true, env.isBlocked(loc));
086            }
087    
088            public void testMoveWithBlockingWalls() {
089                    XYLocation loc = new XYLocation(5, 5);
090                    env.moveObjectToAbsoluteLocation(a, loc);
091                    XYLocation northLoc = new XYLocation(5, 6);
092                    XYLocation southLoc = new XYLocation(5, 4);
093                    XYLocation westLoc = new XYLocation(4, 5);
094    
095                    env.addObject(new Wall(), northLoc); // wall to the north of object
096                    assertTrue(env.isBlocked(northLoc));
097                    env.addObject(new Wall(), southLoc); // wall to the south of object
098                    env.addObject(new Wall(), westLoc); // wall to the west of object
099                    assertEquals(3, env.getObjects().size());
100    
101                    env.moveObject(a, "North"); // should not move
102                    env.moveObject(a, "South"); // should not move
103                    env.moveObject(a, "West"); // should not move
104                    env.moveObject(a, "East"); // SHOULD move
105                    assertEquals(new XYLocation(6, 5), a.getAttribute(LOCATION));
106    
107            }
108    
109            public void testGetObjectsAt() {
110                    XYLocation loc = new XYLocation(5, 7);
111                    env.moveObjectToAbsoluteLocation(a, loc);
112                    assertEquals(1, env.getObjectsAt(loc).size());
113                    Agent b = new MockAgent();
114                    env.addAgent(b, loc);
115                    assertEquals(2, env.getObjectsAt(loc).size());
116            }
117    
118            public void testGetObjectsNear() {
119                    XYLocation loc = new XYLocation(5, 5);
120                    env.moveObjectToAbsoluteLocation(a, loc);
121                    Agent b = new MockAgent();
122                    Agent c = new MockAgent();
123                    Wall w1 = new Wall();
124    
125                    env.addAgent(b, new XYLocation(7, 4));
126                    env.addAgent(c, new XYLocation(5, 7));
127                    env.addObject(w1, new XYLocation(3, 10));
128    
129                    // at this point agent A shouldbe able to see B and C but not the wall
130                    // with a "vision radius" of 3
131                    ArrayList visibleToA = env.getObjectsNear(a, 3);
132                    assertEquals(2, visibleToA.size());
133                    // agent B shouldbe able to see A only
134                    ArrayList visibleToB = env.getObjectsNear(b, 3);
135                    assertEquals(1, visibleToB.size());
136    
137                    // move B north and west
138                    env.moveObject(b, "North");
139                    env.moveObject(b, "West");
140                    // at this point both a and c should be visible to b
141                    // TODO fix this
142                    visibleToB = env.getObjectsNear(b, 3);
143                    assertEquals(1, visibleToB.size());
144                    // move c near the wall
145                    env.moveObjectToAbsoluteLocation(c, new XYLocation(3, 11));
146                    // only the wall should be visible
147                    ArrayList visibleToC = env.getObjectsNear(b, 3);
148                    assertEquals(1, visibleToC.size());
149    
150                    //
151    
152            }
153    
154            public void testMakePerimeter() {
155                    env.makePerimeter();
156                    assertTrue(env.isBlocked(new XYLocation(0, 0)));
157                    assertTrue(env.isBlocked(new XYLocation(0, 6)));
158                    assertTrue(env.isBlocked(new XYLocation(0, 11)));
159                    assertTrue(env.isBlocked(new XYLocation(6, 0)));
160                    assertTrue(env.isBlocked(new XYLocation(9, 0)));
161                    assertTrue(env.isBlocked(new XYLocation(9, 6)));
162                    assertTrue(env.isBlocked(new XYLocation(9, 11)));
163                    assertTrue(env.isBlocked(new XYLocation(6, 11)));
164    
165            }
166    
167    }