Skip to content

Commit

Permalink
Test cases of bad format string type and wrong float size.
Browse files Browse the repository at this point in the history
  • Loading branch information
eerimoq committed Oct 14, 2017
1 parent 63c0ae5 commit f94f5ef
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions tests/test_bitstruct.py
Original file line number Diff line number Diff line change
Expand Up @@ -466,6 +466,40 @@ def test_unsigned_integer(self):
self.assertEqual(pack(fmt, value), packed)
self.assertEqual(unpack(fmt, packed), (value, ))

def test_bad_float_size(self):
"""Test of bad float size.
"""

with self.assertRaises(ValueError) as cm:
pack('f31', 1.0)

self.assertEqual(str(cm.exception),
'expected float size of 32 of 64 bits (got 31)')

with self.assertRaises(ValueError) as cm:
unpack('f33', 8 * b'\x00')

self.assertEqual(str(cm.exception),
'expected float size of 32 of 64 bits (got 33)')

def test_bad_format_type(self):
"""Test of bad format type.
"""

cf = bitstruct.compile('g1')

with self.assertRaises(ValueError) as cm:
cf.pack(1.0)

self.assertEqual(str(cm.exception), "bad type 'g' in format")

with self.assertRaises(ValueError) as cm:
cf.unpack(b'\x00')

self.assertEqual(str(cm.exception), "bad type 'g' in format")


if __name__ == '__main__':
unittest.main()

0 comments on commit f94f5ef

Please sign in to comment.