예제 #1
0
#!/usr/bin/python
# -*- coding: utf-8 -*-


import unittest
import test.util as util
from algorithm.datastructure.heap import Heap


logger = util.getLogger('test.datastructure')


class TestHeap(util.PyAlgorithmTestCase):

    def setUp(self):
        super(TestHeap, self).setUp(logger)

    def testSize(self):
        u"""测试堆的获取尺寸方法"""

        heap = Heap(range(5))
        self.assertEqual(5, heap.size())

    def testBubbleDown(self):
        u"""测试堆的向下冒泡调整操作"""

        heap = Heap()
        heap._heap = [3, 2, 4, 5, 1]

        # 调整第2个元素
        heap._bubbleDown(1)
예제 #2
0
#!/usr/bin/python
# -*- coding: utf-8 -*-

import unittest
import test.util as util
from algorithm.graph.dijkstra_shotest_path import *

logger = util.getLogger('test.graph.dijkstra')


class TestDijkstra(util.PyAlgorithmTestCase):
    TEST_CASE_FORMAT = '''
    Input: graph = %s
    Input: start vertex = %s
    Result: %s
    '''

    def setUp(self):
        super(TestDijkstra, self).setUp(logger)
        self.cases = util.getInputAnswerCases('data', 'dijkstra')

    def testBaseCase(self):
        u"""测试正常情况"""

        graph = {1: [(2, 1), (3, 4)], 2: [(3, 2), (4, 6)], 3: [(4, 3)]}
        start = 1
        self.correct = {1: 0, 2: 1, 3: 3, 4: 6}
        self.output = dijkstraShortestPath(graph, start)

        logger.debug(TestDijkstra.TEST_CASE_FORMAT, graph, start, self.output)
        self.assertEqual(self.correct, self.output)
예제 #3
0
#!/usr/bin/python
# -*- coding: utf-8 -*-

import random
import unittest
import test.util as util

logger = util.getLogger('test.sort')


class TestSorting(util.PyAlgorithmTestCase):
    TEST_CASE_FORMAT = '''
    Input: list = %s
    Result: %s
    '''

    def setUp(self):
        super(TestSorting, self).setUp(logger)
        self.cases = util.getInputAnswerCases('data', 'sort')

    def _testBaseCase(self, sortFunc):
        self.input = range(10)
        random.shuffle(self.input)
        self.correct = range(10)
        self.output = sortFunc(self.input)

        logger.debug(TestSorting.TEST_CASE_FORMAT, self.input, self.output)
        self.assertEqual(self.correct, self.output)

    def _testSortingCase(self, sortFunc):
        def standardizeInput(content):
예제 #4
0
#!/usr/bin/python
# -*- coding: utf-8 -*-


import unittest
import test.util as util
from algorithm.datastructure.heap import Heap


logger = util.getLogger("test.datastructure")


class TestHeap(util.PyAlgorithmTestCase):
    def setUp(self):
        super(TestHeap, self).setUp(logger)

    def testSize(self):
        u"""测试堆的获取尺寸方法"""

        heap = Heap(range(5))
        self.assertEqual(5, heap.size())

    def testBubbleDown(self):
        u"""测试堆的向下冒泡调整操作"""

        heap = Heap()
        heap._heap = [3, 2, 4, 5, 1]

        # 调整第2个元素
        heap._bubbleDown(1)
        self.assertEqual([3, 1, 4, 5, 2], heap._heap)
#!/usr/bin/python
# -*- coding: utf-8 -*-

import unittest
import test.util as util
from algorithm.graph.dijkstra_shotest_path import *

logger = util.getLogger('test.graph.dijkstra')


class TestDijkstra(util.PyAlgorithmTestCase):
    TEST_CASE_FORMAT = '''
    Input: graph = %s
    Input: start vertex = %s
    Result: %s
    '''

    def setUp(self):
        super(TestDijkstra, self).setUp(logger)
        self.cases = util.getInputAnswerCases('data', 'dijkstra')

    def testBaseCase(self):
        u"""测试正常情况"""

        graph = {
            1: [(2, 1), (3, 4)],
            2: [(3, 2), (4, 6)],
            3: [(4, 3)]
        }
        start = 1
        self.correct = {