Exemplo n.º 1
0
    def test_put_bitstream_copy_self(self):
        """
        Test using the put_bitstream_copy method with the same BitStream object 
        as origin and destination.
        """
        bitstream = BitStream()

        # Generate a random string of bits, ie: ('0' | '1')*
        num_bits = 50
        bits = ""
        for i in range(0, num_bits):
            bits += random.choice(
                ('0', '1'))  # inefficient, but ok for a test.

        # Put those bits in the BitStream...
        bitstream.put_bit_dump_string(bits)

        # ... copy the bitstream into itself at any point:
        bitstream.seek(random.randint(0, 50))
        bitstream.put_bitstream_copy(bitstream)

        # Check that the bitstream was unchanged by the previous operation:
        # (overwriting data with the same data is the same as doing nothing,
        # except that the current position is changed to the end of the stream)
        self.assertEquals(bitstream.get_length(), num_bits)
        self.assertEquals(bitstream.get_current_pos(), num_bits)
        bitstream.seek(0)
        read_bits = bitstream.get_bit_dump_string(bitstream.get_length())
        self.assertEqual(read_bits, bits)
Exemplo n.º 2
0
 def test_put_bitstream_copy_self(self):
     """
     Test using the put_bitstream_copy method with the same BitStream object 
     as origin and destination.
     """
     bitstream = BitStream()
     
     # Generate a random string of bits, ie: ('0' | '1')*
     num_bits = 50        
     bits = ""
     for i in range(0,num_bits):
         bits += random.choice(('0','1')) # inefficient, but ok for a test.
         
     # Put those bits in the BitStream...
     bitstream.put_bit_dump_string(bits)
     
     # ... copy the bitstream into itself at any point:
     bitstream.seek(random.randint(0,50))
     bitstream.put_bitstream_copy(bitstream)
     
     # Check that the bitstream was unchanged by the previous operation:
     # (overwriting data with the same data is the same as doing nothing,
     # except that the current position is changed to the end of the stream)
     self.assertEquals(bitstream.get_length(),num_bits)
     self.assertEquals(bitstream.get_current_pos(),num_bits)
     bitstream.seek(0)
     read_bits = bitstream.get_bit_dump_string(bitstream.get_length())
     self.assertEqual(read_bits, bits)
Exemplo n.º 3
0
    def test_get_bit_dump_string_zero_bits(self):
        """
        Test that reading zero bits from the stream as a bit dump string 
        results in getting the empty string: \"\".
        """
        bitstream = BitStream()
        self.assertEquals(bitstream.get_bit_dump_string(0), "")

        # Store some bits in the stream.
        bitstream.put_bit_dump_string("0001110101")  # 10 bits

        bitstream.seek(0)
        self.assertEquals(bitstream.get_bit_dump_string(0), "")
Exemplo n.º 4
0
 def test_get_bit_dump_string_zero_bits(self):
     """
     Test that reading zero bits from the stream as a bit dump string 
     results in getting the empty string: \"\".
     """
     bitstream = BitStream()
     self.assertEquals(bitstream.get_bit_dump_string(0), "")
     
     # Store some bits in the stream.
     bitstream.put_bit_dump_string("0001110101") # 10 bits
     
     bitstream.seek(0)
     self.assertEquals(bitstream.get_bit_dump_string(0), "")
Exemplo n.º 5
0
    def test_get_bit_dump_string_beyond_eos(self):
        """
        Test that trying to read beyond the end of the stream raises an 
        exception when calling get_bit_dump_string(...).
        """
        bitstream = BitStream()

        self.assertRaises(NotEnoughBitsInStreamError,
                          bitstream.get_bit_dump_string, 1)
        # Current position should not have been changed
        self.assertEquals(bitstream.get_current_pos(), 0)

        bitstream.put_bit_dump_string("0001110101")  # 10 bits
        bitstream.seek(0)
        # Read beyond EOS
        self.assertRaises(NotEnoughBitsInStreamError,
                          bitstream.get_bit_dump_string, 11)
        # Current position should not have been changed
        self.assertEquals(bitstream.get_current_pos(), 0)
Exemplo n.º 6
0
 def test_get_bit_dump_string_beyond_eos(self):
     """
     Test that trying to read beyond the end of the stream raises an 
     exception when calling get_bit_dump_string(...).
     """
     bitstream = BitStream()
     
     self.assertRaises(NotEnoughBitsInStreamError, 
                       bitstream.get_bit_dump_string, 1)
     # Current position should not have been changed
     self.assertEquals(bitstream.get_current_pos(),0)
     
     bitstream.put_bit_dump_string("0001110101") # 10 bits
     bitstream.seek(0)
     # Read beyond EOS
     self.assertRaises(NotEnoughBitsInStreamError, 
                       bitstream.get_bit_dump_string, 11)
     # Current position should not have been changed
     self.assertEquals(bitstream.get_current_pos(),0)
Exemplo n.º 7
0
 def test_bit_dump_string_basic(self):
     """
     This method tests put_bit_dump_string's and get_bit_dump_string's 
     basic functionality.
     """
     bitstream = BitStream()
     
     # Generate a random string of bits, ie: ('0' | '1')*
     num_bits = 50        
     bits = ""
     for i in range(0,num_bits):
         bits += random.choice(('0','1')) # inefficient, but ok for a test.
         
     # Put those bits in the BitStream...
     bitstream.put_bit_dump_string(bits)
     
     # ...and get them back
     bitstream.seek(0)
     read_bits = bitstream.get_bit_dump_string(len(bits))
     
     # Check that the bits were recovered correctly
     self.assertEqual(read_bits, bits)
Exemplo n.º 8
0
    def test_bit_dump_string_basic(self):
        """
        This method tests put_bit_dump_string's and get_bit_dump_string's 
        basic functionality.
        """
        bitstream = BitStream()

        # Generate a random string of bits, ie: ('0' | '1')*
        num_bits = 50
        bits = ""
        for i in range(0, num_bits):
            bits += random.choice(
                ('0', '1'))  # inefficient, but ok for a test.

        # Put those bits in the BitStream...
        bitstream.put_bit_dump_string(bits)

        # ...and get them back
        bitstream.seek(0)
        read_bits = bitstream.get_bit_dump_string(len(bits))

        # Check that the bits were recovered correctly
        self.assertEqual(read_bits, bits)