Ejemplo n.º 1
0
    def test_addNeighbor(self):
        tst = search.Node()

        nxt = search.Node()
        mir = search.Node()

        # no mirror
        tst.addNeighbor(nxt,4.2,False)

        self.assertIs(tst.edges[0].left, tst)
        self.assertIs(tst.edges[0].right, nxt)
        self.assertEquals(tst.edges[0].cost, 4.2)
        self.assertEquals(len(tst.edges), 1)
        self.assertEquals(len(nxt.edges), 0)

        # mirror now
        tst.addNeighbor(mir)

        self.assertIs(tst.edges[1].left, tst)
        self.assertIs(tst.edges[1].right, mir)
        self.assertEquals(tst.edges[1].cost, 1.0)
        self.assertEquals(len(tst.edges), 2)

        self.assertIs(mir.edges[0].left, mir)
        self.assertIs(mir.edges[0].right, tst)
        self.assertEquals(mir.edges[0].cost, 1.0)
        self.assertEquals(len(mir.edges), 1)
Ejemplo n.º 2
0
    def test_hasNeighbor(self):
        tst = search.Node()
        nbr = search.Node()
        nop = search.Node()

        tst.addNeighbor(nbr, 1.0, False)
        
        self.assertTrue(tst.hasNeighbor(nbr))
        self.assertFalse(tst.hasNeighbor(nop))
Ejemplo n.º 3
0
    def test_constructor_args(self):
        l = search.Node()
        r = search.Node()
        
        tst = search.Edge(l, r, 1.0)

        self.assertIs(tst.left, l)
        self.assertIs(tst.right, r)
        self.assertEqual(tst.cost, 1.0)
Ejemplo n.º 4
0
    def test_getConnectingEdge(self):
        tst = search.Node()
        nbr = search.Node()
        nop = search.Node()

        tst.addNeighbor(nbr, 1.0, False)

        shouldBe = tst.edges[0]

        self.assertIs(tst.getConnectingEdge(nbr), shouldBe)
        self.assertIs(tst.getConnectingEdge(nop), None)
Ejemplo n.º 5
0
    def test_constructor_default(self):
        node = search.Node()
        tst  = search.Path(node)

        self.assertEquals(tst.cost, 0)
        self.assertIs(tst.start, node)
        self.assertIs(tst.end, node)
        self.assertEquals(len(tst.nodes), 1)
        self.assertIs(tst.nodes[0], node)
        self.assertFalse(tst.failed)
Ejemplo n.º 6
0
    def test_addFlag(self):
        tst = search.Node()

        self.assertEquals(len(tst.flags), 0)
        
        tst.addFlag("test")
        self.assertEquals(len(tst.flags), 1)
        self.assertEquals(tst.flags[0], "test")

        tst.addFlag("test")
        self.assertEquals(len(tst.flags), 1)
Ejemplo n.º 7
0
    def test_traverse(self):
        # setup.
        l   = search.Node()
        r   = search.Node()
        err = search.Node()

        swp = search.Edge(l, r, 6.18)

        # left-to-right
        tst = swp.traverse(l)
        self.assertIs(tst[0], r)
        self.assertEqual(tst[1], 6.18)

        # right to left
        tst = swp.traverse(r)
        self.assertIs(tst[0], l)
        self.assertEqual(tst[1], 6.18)

        # error
        self.assertRaises(ValueError, swp.traverse, err)
Ejemplo n.º 8
0
    def test_removeNeighbor(self):
        tst_a = search.Node()
        tst_b = search.Node()
        tst_c = search.Node()
        tst_d = search.Node()

        tst_a.addNeighbor(tst_b)
        tst_c.addNeighbor(tst_d)


        # actual test - mirrored
        tst_a.removeNeighbor(tst_b)

        self.assertFalse(tst_a.hasNeighbor(tst_b))
        self.assertFalse(tst_b.hasNeighbor(tst_b))

        # actual test - non-mirrored
        tst_c.removeNeighbor(tst_d, False)

        self.assertFalse(tst_c.hasNeighbor(tst_d))
        self.assertTrue(tst_d.hasNeighbor(tst_c))
Ejemplo n.º 9
0
    def test_moveto(self):
        start = search.Node()
        end   = search.Node()
        nope  = search.Node()

        start.addNeighbor(end, 2.3)

        tst = search.Path(start)

        self.assertTrue(tst.moveto(end))

        self.assertEquals(tst.cost, 2.3)
        self.assertEquals(len(tst.nodes), 2)
        self.assertIs(tst.end, end)
        self.assertIs(tst.start, start)
        self.assertFalse(tst.failed)

        self.assertFalse(tst.moveto(nope))

        self.assertEquals(tst.cost, 2.3)
        self.assertEquals(len(tst.nodes), 2)
        self.assertIs(tst.end, end)
        self.assertIs(tst.start, start)
        self.assertTrue(tst.failed)
Ejemplo n.º 10
0
    def test_resetFlags(self):
        tst = search.Node()

        tst.addFlag("test")
        tst.resetFlags()
        self.assertFalse(tst.hasFlag("test"))
Ejemplo n.º 11
0
    def test_removeFlag(self):
        tst = search.Node()

        tst.addFlag("test")
        tst.removeFlag("test")
        self.assertFalse(tst.hasFlag("test"))
Ejemplo n.º 12
0
    def test_hasFlag(self):
        tst = search.Node()

        self.assertFalse(tst.hasFlag("test"))
        tst.addFlag("test")
        self.assertTrue(tst.hasFlag("test"))
Ejemplo n.º 13
0
    def test_constructor_default(self):
        tst  = search.Node()

        self.assertEqual([], tst.flags)
        self.assertEqual([], tst.edges)
        self.assertEqual(0,  tst.cost)
Ejemplo n.º 14
0
    def build_common_graph(self):
        """Builds a graph I use for multiple tests.

        The graph forms a triangular shape, such that:
          A
         B C
        D E F

        And costs:
        A <-> B = 1
        A <-> C = 2
        B <-> C = 3
        B <-> D = 5
        B <-> E = 3
        C <-> F = 1
        D <-> E = 1
        E <-> F = 2

        There's no significance to the costs chosen.

        Shortest routes (defined as lowest cost, fewest nodes)
         S | E | Cost | Route
        ---+---+------+------------------
         A | A |    2 | A, B, A
         A | B |    1 | A, B
         A | C |    2 | A, C
         A | D |    4 | A, C, E, D
         A | E |    3 | A, C, E
         A | F |    3 | A, C, F
         B | A |    1 | B, A
         B | B |    2 | B, A, B
         B | C |    3 | B, C
         B | D |    4 | B, E, D
         B | E |    3 | B, E
         B | F |    4 | B, C, F
         ...

        Shortest route visiting all points exactly once:
         S | E | Cost | Route
        ---+---+------+------------------
         A | B |   11 | A, C, F, E, D, B
        ...


        """
        ret =   search.Graph()

        nodeA = search.Node()
        nodeB = search.Node()
        nodeC = search.Node()
        nodeD = search.Node()
        nodeE = search.Node()
        nodeF = search.Node()

        nodeA.addNeighbor(nodeB, 1)
        nodeA.addNeighbor(nodeC, 2)
        nodeB.addNeighbor(nodeC, 3)
        nodeB.addNeighbor(nodeD, 5)
        nodeB.addNeighbor(nodeE, 3)
        nodeC.addNeighbor(nodeF, 1)
        nodeD.addNeighbor(nodeE, 1)
        nodeE.addNeighbor(nodeF, 2)

        ret.contents.append(nodeA)
        ret.contents.append(nodeB)
        ret.contents.append(nodeC)
        ret.contents.append(nodeD)
        ret.contents.append(nodeE)
        ret.contents.append(nodeF)

        return ret