Skip to content

Commit

Permalink
Fixed binary conversion bug, and added corresponding tests
Browse files Browse the repository at this point in the history
  • Loading branch information
radarhere committed Mar 1, 2015
1 parent aeb1e61 commit f644adb
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 4 deletions.
8 changes: 4 additions & 4 deletions PIL/_binary.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def i16le(c, o=0):
c: string containing bytes to convert
o: offset of bytes to convert in string
"""
return unpack("<H", c[o:o+2])
return unpack("<H", c[o:o+2])[0]


def i32le(c, o=0):
Expand All @@ -46,15 +46,15 @@ def i32le(c, o=0):
c: string containing bytes to convert
o: offset of bytes to convert in string
"""
return unpack("<I", c[o:o+4])
return unpack("<I", c[o:o+4])[0]


def i16be(c, o=0):
return unpack(">H", c[o:o+2])
return unpack(">H", c[o:o+2])[0]


def i32be(c, o=0):
return unpack(">I", c[o:o+4])
return unpack(">I", c[o:o+4])[0]


# Output, le = little endian, be = big endian
Expand Down
28 changes: 28 additions & 0 deletions Tests/test_binary.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
from helper import unittest, PillowTestCase

from PIL import _binary

class TestBinary(PillowTestCase):

def test_standard(self):
self.assertEqual(_binary.i8(b'*'), 42)
self.assertEqual(_binary.o8(42), b'*')

def test_little_endian(self):
self.assertEqual(_binary.i16le(b'\xff\xff\x00\x00'), 65535)
self.assertEqual(_binary.i32le(b'\xff\xff\x00\x00'), 65535)

self.assertEqual(_binary.o16le(65535), b'\xff\xff')
self.assertEqual(_binary.o32le(65535), b'\xff\xff\x00\x00')

def test_big_endian(self):
self.assertEqual(_binary.i16be(b'\x00\x00\xff\xff'), 0)
self.assertEqual(_binary.i32be(b'\x00\x00\xff\xff'), 65535)

self.assertEqual(_binary.o16be(65535), b'\xff\xff')
self.assertEqual(_binary.o32be(65535), b'\x00\x00\xff\xff')

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

# End of file

0 comments on commit f644adb

Please sign in to comment.