Beispiel #1
0
 def test_next_bit_length_returns_the_bits_needed_to_encode_the_next_symbol(
         self):
     generator = SymbolGenerator(1, 1)
     self.assertEqual(1, generator.next_bit_length
                      )  # the next symbol will be the initial value
     self.assertEqual(Symbol("1"), next(generator))
     self.assertEqual(2, generator.next_bit_length)
     self.assertEqual(Symbol("10"), next(generator))
     self.assertEqual(2, generator.next_bit_length)
     self.assertEqual(Symbol("11"), next(generator))
     self.assertEqual(3, generator.next_bit_length)
     self.assertEqual(Symbol("100"), next(generator))
Beispiel #2
0
    def test_encode_wikipedia_example(self):
        uncompressed_input = "TOBEORNOTTOBEORTOBEORNOT"

        encoded = self.encoder.encode(uncompressed_input)

        expected_encoded_raw = (
            "10100011110001000101011111001000111000111101010001"
            "1011011101011111100100011110100000100010"
        )
        expected_encoded = Symbol(expected_encoded_raw)
        original_encoding = Symbol.from_dict(self.alphabet, uncompressed_input)

        self.assertLess(len(expected_encoded), len(original_encoding))
        self.assertEqual(expected_encoded, encoded)
Beispiel #3
0
 def test_decode_wikipedia_example(self):
     encoded_raw = (
         "10100011110001000101011111001000111000111101010001101101110"
         "1011111100100011110100000100010")
     encoded_input = Symbol(encoded_raw)
     decoded = self.decoder.decode(encoded_input)
     self.assertEqual("TOBEORNOTTOBEORTOBEORNOT", decoded)
Beispiel #4
0
    def test_encode_empty_string(self):
        uncompressed_input = ""

        encoded = self.encoder.encode(uncompressed_input)

        self.assertEqual(Symbol(), encoded)
Beispiel #5
0
 def test_start_from_provided_number_of_bits_and_value(self):
     generator = SymbolGenerator(0, 4)
     self.assertEqual(Symbol("0000"), next(generator))
     self.assertEqual(4, generator.current_bit_length)
Beispiel #6
0
 def test_linear_progression_when_needs_1_bit_more(self):
     generator = SymbolGenerator(7, 3)
     self.assertEqual(3, generator.current_bit_length)
     next(generator)  # yields the initial value
     self.assertEqual(Symbol("1000"), next(generator))
     self.assertEqual(4, generator.current_bit_length)
Beispiel #7
0
 def test_linear_progression(self):
     generator = SymbolGenerator(4, 3)
     next(generator)
     next(generator)
     next(generator)
     self.assertEqual(Symbol("111"), next(generator))
Beispiel #8
0
 def test_decode_empty_input(self):
     encoded_input = Symbol()
     decoded = self.decoder.decode(encoded_input)
     self.assertEqual("", decoded)