Example #1
0
class TestSimulation(TestCase):
    def setUp(self):
        self.x = Node('x')
        self.y = Node('y')
        self.x.addEdges({'red': self.y})
        self.y.addEdges({'red': self.x})

        self.graph = Graph()
        self.graph.addNodes([self.x, self.y])
        self.agent1 = Agent(1)
        self.agent2 = Agent(2)
        self.x.addAgents([self.agent1, self.agent2])
        self.agentMap = {self.agent1: self.x, self.agent2: self.x}

        self.simulation = Simulation(self.graph, self.agentMap)
        self.expectedEmptyMessages = {self.agent1: [], self.agent2: []}

    def test_init(self):
        self.assertEqual(self.simulation.graph, self.graph)
        self.assertEqual(self.simulation.agentMap, self.agentMap)
        self.assertEqual(self.simulation.messages, self.expectedEmptyMessages)

    def test_emptyMessages(self):
        self.assertDictEqual(self.simulation.emptyMessages(), self.expectedEmptyMessages)

    def test_moveAgent(self):
        self.simulation.moveAgent(self.agent1, self.y)

        self.assertEqual({self.agent2}, self.x.getAgents())
        self.assertEqual({self.agent1}, self.y.getAgents())

        self.assertEqual(self.simulation.agentMap[self.agent2], self.x)
        self.assertEqual(self.simulation.agentMap[self.agent1], self.y)
Example #2
0
    def setUp(self):
        self.x = Node('x')
        self.y = Node('y')
        self.x.addEdges({'red': self.y})
        self.y.addEdges({'red': self.x})

        self.graph = Graph()
        self.graph.addNodes([self.x, self.y])
        self.agent1 = Agent(1)
        self.agent2 = Agent(2)
        self.x.addAgents([self.agent1, self.agent2])
        self.agentMap = {self.agent1: self.x, self.agent2: self.x}

        self.simulation = Simulation(self.graph, self.agentMap)
        self.expectedEmptyMessages = {self.agent1: [], self.agent2: []}
Example #3
0
    def setUp(self):
        self.x = Node('x')
        self.y = Node('y')
        self.x.addEdges({'red': self.y})
        self.y.addEdges({'red': self.x})

        self.graph = Graph()
        self.graph.addNodes([self.x, self.y])


        self.agent1 = TestAgent(1, self.y)
        self.agent2 = TestAgent(2, self.x)
        self.x.addAgents([self.agent1])
        self.y.addAgents([self.agent2])
        self.agentMap = {self.agent1: self.x, self.agent2: self.y}

        self.agent1Message = BroadcastMoved(self.agent1, self.y)
        self.agent1.message = self.agent1Message
        self.agent2Message = BroadcastMoved(self.agent2, self.x)
        self.agent2.message = self.agent2Message

        self.simulation = Simulation(self.graph, self.agentMap)
        self.simRunner = SimulationRunner(self.simulation)
Example #4
0
 def setUp(self):
     self.graph = Graph()
     self.x = Node('x')
     self.y = Node('y')
     self.agent = Agent(1)
Example #5
0
class TestGraph(TestCase):
    def setUp(self):
        self.graph = Graph()
        self.x = Node('x')
        self.y = Node('y')
        self.agent = Agent(1)

    def test_addNodes(self):
        self.graph.addNodes([self.x, self.y, self.y])
        self.assertEqual({self.x, self.y}, self.graph.getNodes())

    def test_removeNodes(self):
        self.graph.addNodes([self.x, self.y])
        self.graph.removeNodes([self.x])
        self.assertEqual({self.y}, self.graph.getNodes())

    def test_getNodes(self):
        self.graph.addNodes([self.x, self.y])
        self.assertEqual({self.x, self.y}, self.graph.getNodes())

    def test_moveAgent(self):
        self.x.addAgents([self.agent])
        self.graph.moveAgent(self.agent, self.x, self.y)
        self.assertEqual(set(), self.x.getAgents())
        self.assertEqual({self.agent}, self.y.getAgents())

    def test_moveAgentThrowsExceptionOnBadMove(self):
        self.x.addAgents([self.agent])
        self.assertRaises(ValueError, lambda : self.graph.moveAgent(self.agent, self.y, self.x))
Example #6
0
class TestSimulationRunner(TestCase):
    def setUp(self):
        self.x = Node('x')
        self.y = Node('y')
        self.x.addEdges({'red': self.y})
        self.y.addEdges({'red': self.x})

        self.graph = Graph()
        self.graph.addNodes([self.x, self.y])


        self.agent1 = TestAgent(1, self.y)
        self.agent2 = TestAgent(2, self.x)
        self.x.addAgents([self.agent1])
        self.y.addAgents([self.agent2])
        self.agentMap = {self.agent1: self.x, self.agent2: self.y}

        self.agent1Message = BroadcastMoved(self.agent1, self.y)
        self.agent1.message = self.agent1Message
        self.agent2Message = BroadcastMoved(self.agent2, self.x)
        self.agent2.message = self.agent2Message

        self.simulation = Simulation(self.graph, self.agentMap)
        self.simRunner = SimulationRunner(self.simulation)

    def test_groupMessagesBroadcastEmpty(self):
        initialMessage = BroadcastMoved(self.agent1, self.x)
        newMessage = BroadcastMoved(self.agent1, self.y)
        messages = {self.agent1: [initialMessage], self.agent2: []}
        expectedMessages = {self.agent1: [initialMessage],
                            self.agent2: [newMessage]}

        messages = self.simRunner.groupMessages(messages, newMessage)

        self.assertEqual(expectedMessages, messages)

    def test_groupMessageBroadcastNonEmpty(self):
        initialMessage = BroadcastMoved(self.agent1, self.x)
        newMessage = BroadcastMoved(self.agent2, self.y)
        messages = {self.agent1: [initialMessage], self.agent2: []}
        expectedMessages = {self.agent1: [initialMessage, newMessage],
                            self.agent2: []}

        messages = self.simRunner.groupMessages(messages, newMessage)

        self.assertEqual(expectedMessages, messages)

    def test_executeTick1(self):
        self.simRunner.executeTick()

        self.assertEqual([], self.agent1.messages)
        self.assertEqual(self.x, self.agent1.loc)
        self.assertEqual({self.agent1}, self.x.getAgents())
        self.assertEqual([], self.agent2.messages)
        self.assertEqual(self.y, self.agent2.loc)
        self.assertEqual({self.agent2}, self.y.getAgents())
        self.assertEqual(self.simulation.emptyMessages(), self.simulation.messages)

    def test_executeTick2(self):
        self.simRunner.executeTick()
        self.simRunner.executeTick()

        self.assertEqual([], self.agent1.messages)
        self.assertEqual(self.x, self.agent1.loc)
        self.assertEqual({self.agent1}, self.y.getAgents())
        self.assertEqual([], self.agent2.messages)
        self.assertEqual(self.y, self.agent2.loc)
        self.assertEqual({self.agent2}, self.x.getAgents())
        self.assertEqual(self.simulation.emptyMessages(), self.simulation.messages)

    def test_executeTick3(self):
        self.simRunner.executeTick()
        self.simRunner.executeTick()
        self.simRunner.executeTick()


        self.assertEqual([], self.agent1.messages)
        self.assertEqual(self.y, self.agent1.loc)
        self.assertEqual({self.agent1}, self.y.getAgents())
        self.assertEqual([], self.agent2.messages)
        self.assertEqual(self.x, self.agent2.loc)
        self.assertEqual({self.agent2}, self.x.getAgents())

        self.assertEqual([self.agent2Message], self.simulation.messages[self.agent1])
        self.assertEqual([self.agent1Message], self.simulation.messages[self.agent2])

    def test_executeTick4(self):
        self.simRunner.executeTick()
        self.simRunner.executeTick()
        self.simRunner.executeTick()
        self.simRunner.executeTick()

        self.checkSwapped()
        self.assertEqual([self.agent2Message], self.agent1.messages)
        self.assertEqual([self.agent1Message], self.agent2.messages)
        self.assertEqual(self.simulation.emptyMessages(), self.simulation.messages)
        self.assertEqual(self.simulation.emptyMessages(), self.simulation.messages)


    def checkSwapped(self):
        self.assertEqual(self.y, self.agent1.loc)
        self.assertEqual({self.agent1}, self.y.getAgents())
        self.assertEqual(self.x, self.agent2.loc)
        self.assertEqual({self.agent2}, self.x.getAgents())