Exemplo n.º 1
0
class TestGhettoBitStream(unittest.TestCase):
    def setUp(self):
        self.bs = GhettoBitStream(tree_data)

    def test_pop_byte(self):
        length = len(self.bs.bytes)
        first = self.bs.pop_byte()
        self.assertEqual(first, 5)
        self.assertEqual(len(self.bs.bytes), length - 1)

    def test_as_iterator(self):
        next(self.bs)
        self.assertTrue(list(self.bs))

    def test_bit_buffer(self):
        byte_count = len(self.bs.bytes)
        # empty buffer
        self.assertEqual(len(self.bs._bit_buffer), 0)

        bit = self.bs.next()
        # one byte decoded, then one bit consumed
        self.assertEqual(len(self.bs._bit_buffer), 7)
        # one byte removed
        self.assertEqual(len(self.bs.bytes), byte_count - 1)

        bit = self.bs.next()
        # another bit consumed
        self.assertEqual(len(self.bs._bit_buffer), 6)
        # remaining bytes still stand
        self.assertEqual(len(self.bs.bytes), byte_count - 1)

    def test_byte_to_bits(self):
        # just spot-checking
        self.assertEqual(self.bs._byte_to_bits(0), '00000000')
        self.assertEqual(self.bs._byte_to_bits(6), '00000110')
        self.assertEqual(self.bs._byte_to_bits(213), '11010101')

    def test_bin_backport(self):
        # just spot-checking
        self.assertEqual(self.bs._bin_backport(0), '00000000')
        self.assertEqual(self.bs._bin_backport(6), '00000110')
        self.assertEqual(self.bs._bin_backport(213), '11010101')

    def test_combine_bytes(self):
        # just spot-checking
        self.assertEqual(self.bs.combine_bytes([1, 3]), 259)
        self.assertEqual(self.bs.combine_bytes([3]), 3)
        self.assertEqual(self.bs.combine_bytes([1, 1, 3]), 65795)
Exemplo n.º 2
0
 def setUp(self):
     self.bs = GhettoBitStream(tree_data)
Exemplo n.º 3
0
 def test_get_node_count_medium(self):
     bs = GhettoBitStream([])
     # count bigger than 127, only need 1 byte to represent it
     bs.bytes = deque([129, 150])
     ret = PathTree._get_node_count(bs)
     self.assertEqual(ret, 150)
Exemplo n.º 4
0
 def test_get_node_count_big(self):
     bs = GhettoBitStream([])
     # count bigger than 127, need next 2 bytes to represent it
     bs.bytes = deque([130, 1, 17])
     ret = PathTree._get_node_count(bs)
     self.assertEqual(ret, 273)
Exemplo n.º 5
0
 def test_get_node_count_small(self):
     bs = GhettoBitStream([])
     bs.bytes = deque([6])
     ret = PathTree._get_node_count(bs)
     self.assertEqual(ret, 6)