예제 #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)
예제 #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: []}
예제 #3
0
 def setUp(self):
     self.node = Node('x')
     self.agent1 = Agent(1)
     self.agent2 = Agent(2)
     self.y = Node('y')
     self.colorY = 'red'
     self.z = Node('z')
     self.colorZ = 'green'
예제 #4
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))
예제 #5
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)
예제 #6
0
 def setUp(self):
     self.graph = Graph()
     self.x = Node('x')
     self.y = Node('y')
     self.agent = Agent(1)
예제 #7
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())
예제 #8
0
class TestNode(TestCase):
    def setUp(self):
        self.node = Node('x')
        self.agent1 = Agent(1)
        self.agent2 = Agent(2)
        self.y = Node('y')
        self.colorY = 'red'
        self.z = Node('z')
        self.colorZ = 'green'

    def test_init(self):
        self.assertEqual('x', self.node.id)
        self.assertSetEqual(set(), self.node.agents)
        self.assertDictEqual({}, self.node.neighbors)

    def test_addAgentsGetAgents(self):
        self.node.addAgents([self.agent1])
        self.assertSetEqual({self.agent1}, self.node.getAgents())

    def test_removeAgents(self):
        self.node.addAgents([self.agent1, self.agent2])
        self.node.removeAgents([self.agent1])
        self.assertSetEqual({self.agent2}, self.node.getAgents())

    def test_addEdgesGetEdgeColorsGetNeighbor(self):
        self.node.addEdges({self.colorY: self.y, self.colorZ: self.z})
        self.assertItemsEqual([self.colorY, self.colorZ], self.node.getEdgeColors())
        self.assertEqual(self.y, self.node.getNeighbor(self.colorY))
        self.assertEqual(self.z, self.node.getNeighbor(self.colorZ))

    def test_removeEdges(self):
        self.node.addEdges({self.colorY: self.y, self.colorZ: self.z})
        self.node.removeEdges([self.colorY])
        self.assertItemsEqual([self.colorZ], self.node.getEdgeColors())
        self.assertEqual(self.z, self.node.getNeighbor(self.colorZ))