Ejemplo n.º 1
0
    def testReadCode(self):
        """
        Tests that the interpretation of bit values performed by
        ``_readCode()`` as a contiguous bit-stream works as intended.
        """
        inputs = bytearray(range(256))
        d__ = LZWCodec.Decoder(inputs)
        exp_output_stream = "".join(intToBitstring(b__) for b__ in inputs)
        curr = 0
        # TODO:  this deserves to be an assignment expression.
        code = d__._readCode()

        while code != -1:
            if curr + d__.bitspercode >= len(exp_output_stream):
                exp_output = exp_output_stream[curr:] + "0" * (
                    (curr + d__.bitspercode) - len(exp_output_stream)
                )
            else:
                exp_output = exp_output_stream[curr : curr + d__.bitspercode]

            self.assertEqual(
                exp_output,
                intToBitstring(code, d__.bitspercode),
                msg="(curr, code) = (%d, %d)" % (curr, code),
            )

            curr += d__.bitspercode
            code = d__._readCode()
Ejemplo n.º 2
0
    def testIntToBitstringToInt(self):
        """
        Ensures that bitstringToInt(intToBitsring(input)) == input.
        """
        inputs = range(2**12 + 1)

        for i in inputs:
            self.assertEqual(i, bitstringToInt(intToBitstring(i)))
Ejemplo n.º 3
0
    def testWriteCode(self):
        """
        Tests that the memorization of bit values performed by ``_writeCode()``
        as a contiguous bit-stream works as intended.
        """
        self.maxDiff = None
        e__ = LZWCodec.Encoder("")
        e__.output = list()

        inputs = range(2 ** 8, 2 ** 12 - 1)
        e__.bitspercode = int(floor(log(inputs[0], 2))) + 1
        exp_output = "".join(intToBitstring(n__, floor(log(n__, 2))) for n__ in inputs)

        for i in inputs:
            if floor(log(i, 2)) + 1 > e__.bitspercode:
                e__.bitspercode += 1

            e__._writeCode(i)

        self.assertEqual(
            exp_output, "".join(intToBitstring(n__) for n__ in e__.output)[: e__.bitpos]
        )
Ejemplo n.º 4
0
    def testReadCode(self):
        """
        Tests that the interpretation of bit values performed by
        ``_readCode()`` as a contiguous bit-stream works as intended.
        """
        inputs = bytearray(range(256))
        d = LZWCodec.Decoder(inputs)
        expOutputStream = "".join(intToBitstring(b) for b in inputs)
        curr = 0
        code = d._readCode()

        while code != -1:
            if curr + d.bitspercode >= len(expOutputStream):
                expOutput = expOutputStream[curr:]\
                      + "0" * ((curr + d.bitspercode) - len(expOutputStream))
            else:
                expOutput = expOutputStream[curr:curr + d.bitspercode]

            self.assertEqual(expOutput,
                             intToBitstring(code, d.bitspercode),
                             msg="(curr, code) = (%d, %d)" % (curr, code))

            curr += d.bitspercode
            code = d._readCode()