Ejemplo n.º 1
0
    def test_averageMessageLengths(self):

        for i in range(10):

            weights = [random.randrange(2*20) for x in range(1000)]

            for D in range(2, 15):
                huff_codes = huffman(weights, D)
                gdm_codes = GDM(weights, D)

                huff_avg = averageMessageLength(weights, huff_codes)
                gdm_avg = averageMessageLength(weights, gdm_codes)
                self.assertEqual(huff_avg, gdm_avg)
Ejemplo n.º 2
0
 def test_paperExample(self):
     elements = [
         0.20, 0.18, 0.10, 0.10, 0.10, 0.06, 0.06, 0.04, 0.04, 0.04, 0.04,
         0.03, 0.01
     ]
     result = huffman(elements, 2)
     elements.sort()
     result.sort(reverse=True)
     avgLength = 0
     for i in range(len(elements)):
         avgLength += result[i] * elements[i]
     expected = 3.42
     self.assertAlmostEqual(
         avgLength, expected)  #test by optimal average message length
Ejemplo n.º 3
0
 def test_paperExample(self):
     elements = [0.22, 0.20, 0.18, 0.15, 0.10, 0.08, 0.05, 0.02]
     result = compressByRunLengths(huffman(elements, 4))
     expected = [(3, 2), (2, 3), (1, 3)]
     self.assertEqual(result, expected)
Ejemplo n.º 4
0
 def test_fourTernary(self):
     elements = [4, 3, 6, 8]
     result = compressByRunLengths(huffman(elements, 3))
     expected = [(2, 2), (1, 2)]
     self.assertEqual(result, expected)
Ejemplo n.º 5
0
 def test_sameWeigths(self):
     for d in range(2, 15):
         elements = [1] * d
         result = compressByRunLengths(huffman(elements, d))
         expected = [(1, d)]
         self.assertEqual(result, expected)
Ejemplo n.º 6
0
 def test_empty(self):
     elements = []
     expected = []
     for d in xrange(2, 12):
         result = (huffman(elements, d))
         self.assertEqual(result, expected)
Ejemplo n.º 7
0
 def test_fourElements(self):
     elements = [4, 3, 6, 8]
     result = compressByRunLengths(huffman(elements, 2))
     expected = [(3, 2), (2, 1), (1, 1)]
     self.assertEqual(result, expected)
Ejemplo n.º 8
0
 def test_twoElements(self):
     elements = [4, 2]
     result = compressByRunLengths(huffman(elements, 2))
     expected = [(1, 2)]
     self.assertEqual(result, expected)
Ejemplo n.º 9
0
 def test_lowOutputAlphabet(self):
     with self.assertRaises(Exception) as context:
         huffman([], 1)
Ejemplo n.º 10
0
 def test_unusualWeights(self):
     elements = [-4, 0, 18.3, 9]
     result = compressByRunLengths(huffman(elements, 2))
     expected = [(3, 2), (2, 1), (1, 1)]
     self.assertEqual(result, expected)
Ejemplo n.º 11
0
 def test_oneElement(self):
     elements = [4]
     expected = [0]
     for d in xrange(2, 10):
         result = (huffman(elements, d))
         self.assertEqual(result, expected)