Created
December 9, 2014 18:11
-
-
Save ironpythonbot/c120f535b61fd8e33513 to your computer and use it in GitHub Desktop.
CodePlex Issue #34682 Plain Text Attachments
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import unittest | |
import struct | |
import array | |
class StructTest(unittest.TestCase): | |
def test_unpack(self): | |
# test string format string | |
result = struct.pack(">HH", 1, 2) | |
self.assertEqual(result, b"\x00\x01\x00\x02") | |
# test bytes format string | |
result = struct.pack(b">HH", 1, 2) | |
self.assertEqual(result, b"\x00\x01\x00\x02") | |
def test_unpack(self): | |
# works python 2.x | |
# test string/string combination | |
a,b = struct.unpack(">HH", "\x00\x01\x00\x02") | |
self.assertEqual(a, 1) | |
self.assertEqual(b, 2) | |
# test bytes/string combination | |
a,b = struct.unpack(b">HH", "\x00\x01\x00\x02") | |
self.assertEqual(a, 1) | |
self.assertEqual(b, 2) | |
# test bytes/string combination | |
a,b = struct.unpack(b">HH", b"\x00\x01\x00\x02") | |
self.assertEqual(a, 1) | |
self.assertEqual(b, 2) | |
# test string/bytes combination | |
a,b = struct.unpack(">HH", b"\x00\x01\x00\x02") | |
self.assertEqual(a, 1) | |
self.assertEqual(b, 2) | |
def test_unpack_from(self): | |
# test string format string | |
a, = struct.unpack_from('>H', buffer("\x00\x01")) | |
self.assertEqual(a, 1) | |
# test bytes format string | |
a, = struct.unpack_from(b'>H', buffer("\x00\x01")) | |
self.assertEqual(a, 1) | |
def test_pack_into(self): | |
# test string format string | |
result = array.array('b', [0, 0]) | |
struct.pack_into('>H', result, 0, 0xABCD ) | |
self.assertSequenceEqual(result, array.array('b', b"\xAB\xCD")) | |
# test bytes format string | |
result = array.array('b', [0, 0]) | |
struct.pack_into(b'>H', result, 0, 0xABCD ) | |
self.assertSequenceEqual(result, array.array('b', b"\xAB\xCD")) | |
if __name__ == '__main__': | |
unittest.main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment