aboutsummaryrefslogtreecommitdiff
path: root/circuitpython/tools/huffman/tests/test_codes.py
blob: c3160460e93cdcb7a2693c884fb9157b87c85ad6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
from __future__ import absolute_import, print_function

import collections
import unittest

import huffman

class TestCodebookGeneration(unittest.TestCase):

    def test_basic(self):
        output = huffman.codebook([('A', 2), ('B', 4), ('C', 1), ('D', 1)])
        expected = {'A': '10', 'B': '0', 'C': '110', 'D': '111'}

        self.assertEqual(output, expected)

    def test_counter(self):
        input_ = sorted(collections.Counter('man the stand banana man').items())

        output = huffman.codebook(input_)
        expected = {
            ' ': '111',
            'a': '10',
            'b': '0101',
            'd': '0110',
            'e': '11000',
            'h': '0100',
            'm': '0111',
            'n': '00',
            's': '11001',
            't': '1101',
         }

        self.assertEqual(output, expected)