Back to index

python3.2  3.2.2
Public Member Functions
ctypes.test.test_byteswap.Test Class Reference

List of all members.

Public Member Functions

def X_test
def test_endian_short
def test_endian_int
def test_endian_longlong
def test_endian_float
def test_endian_double
def test_endian_other
def test_struct_fields_1
def test_struct_struct
def test_struct_fields_2
def test_unaligned_nonnative_struct_fields
def test_unaligned_native_struct_fields

Detailed Description

Definition at line 16 of file test_byteswap.py.


Member Function Documentation

Definition at line 125 of file test_byteswap.py.

00125 
00126     def test_endian_double(self):
00127         if sys.byteorder == "little":
00128             self.assertTrue(c_double.__ctype_le__ is c_double)
00129             self.assertTrue(c_double.__ctype_be__.__ctype_le__ is c_double)
00130         else:
00131             self.assertTrue(c_double.__ctype_be__ is c_double)
00132             self.assertTrue(c_double.__ctype_le__.__ctype_be__ is c_double)
00133         s = c_double(math.pi)
00134         self.assertEqual(s.value, math.pi)
00135         self.assertEqual(bin(struct.pack("d", math.pi)), bin(s))
00136         s = c_double.__ctype_le__(math.pi)
00137         self.assertEqual(s.value, math.pi)
00138         self.assertEqual(bin(struct.pack("<d", math.pi)), bin(s))
00139         s = c_double.__ctype_be__(math.pi)
00140         self.assertEqual(s.value, math.pi)
00141         self.assertEqual(bin(struct.pack(">d", math.pi)), bin(s))

Here is the call graph for this function:

Definition at line 107 of file test_byteswap.py.

00107 
00108     def test_endian_float(self):
00109         if sys.byteorder == "little":
00110             self.assertTrue(c_float.__ctype_le__ is c_float)
00111             self.assertTrue(c_float.__ctype_be__.__ctype_le__ is c_float)
00112         else:
00113             self.assertTrue(c_float.__ctype_be__ is c_float)
00114             self.assertTrue(c_float.__ctype_le__.__ctype_be__ is c_float)
00115         s = c_float(math.pi)
00116         self.assertEqual(bin(struct.pack("f", math.pi)), bin(s))
00117         # Hm, what's the precision of a float compared to a double?
00118         self.assertAlmostEqual(s.value, math.pi, places=6)
00119         s = c_float.__ctype_le__(math.pi)
00120         self.assertAlmostEqual(s.value, math.pi, places=6)
00121         self.assertEqual(bin(struct.pack("<f", math.pi)), bin(s))
00122         s = c_float.__ctype_be__(math.pi)
00123         self.assertAlmostEqual(s.value, math.pi, places=6)
00124         self.assertEqual(bin(struct.pack(">f", math.pi)), bin(s))

Here is the call graph for this function:

Definition at line 51 of file test_byteswap.py.

00051 
00052     def test_endian_int(self):
00053         if sys.byteorder == "little":
00054             self.assertTrue(c_int.__ctype_le__ is c_int)
00055             self.assertTrue(c_int.__ctype_be__.__ctype_le__ is c_int)
00056         else:
00057             self.assertTrue(c_int.__ctype_be__ is c_int)
00058             self.assertTrue(c_int.__ctype_le__.__ctype_be__ is c_int)
00059 
00060         s = c_int.__ctype_be__(0x12345678)
00061         self.assertEqual(bin(struct.pack(">i", 0x12345678)), "12345678")
00062         self.assertEqual(bin(s), "12345678")
00063         self.assertEqual(s.value, 0x12345678)
00064 
00065         s = c_int.__ctype_le__(0x12345678)
00066         self.assertEqual(bin(struct.pack("<i", 0x12345678)), "78563412")
00067         self.assertEqual(bin(s), "78563412")
00068         self.assertEqual(s.value, 0x12345678)
00069 
00070         s = c_uint.__ctype_be__(0x12345678)
00071         self.assertEqual(bin(struct.pack(">I", 0x12345678)), "12345678")
00072         self.assertEqual(bin(s), "12345678")
00073         self.assertEqual(s.value, 0x12345678)
00074 
00075         s = c_uint.__ctype_le__(0x12345678)
00076         self.assertEqual(bin(struct.pack("<I", 0x12345678)), "78563412")
00077         self.assertEqual(bin(s), "78563412")
00078         self.assertEqual(s.value, 0x12345678)

Here is the call graph for this function:

Definition at line 79 of file test_byteswap.py.

00079 
00080     def test_endian_longlong(self):
00081         if sys.byteorder == "little":
00082             self.assertTrue(c_longlong.__ctype_le__ is c_longlong)
00083             self.assertTrue(c_longlong.__ctype_be__.__ctype_le__ is c_longlong)
00084         else:
00085             self.assertTrue(c_longlong.__ctype_be__ is c_longlong)
00086             self.assertTrue(c_longlong.__ctype_le__.__ctype_be__ is c_longlong)
00087 
00088         s = c_longlong.__ctype_be__(0x1234567890ABCDEF)
00089         self.assertEqual(bin(struct.pack(">q", 0x1234567890ABCDEF)), "1234567890ABCDEF")
00090         self.assertEqual(bin(s), "1234567890ABCDEF")
00091         self.assertEqual(s.value, 0x1234567890ABCDEF)
00092 
00093         s = c_longlong.__ctype_le__(0x1234567890ABCDEF)
00094         self.assertEqual(bin(struct.pack("<q", 0x1234567890ABCDEF)), "EFCDAB9078563412")
00095         self.assertEqual(bin(s), "EFCDAB9078563412")
00096         self.assertEqual(s.value, 0x1234567890ABCDEF)
00097 
00098         s = c_ulonglong.__ctype_be__(0x1234567890ABCDEF)
00099         self.assertEqual(bin(struct.pack(">Q", 0x1234567890ABCDEF)), "1234567890ABCDEF")
00100         self.assertEqual(bin(s), "1234567890ABCDEF")
00101         self.assertEqual(s.value, 0x1234567890ABCDEF)
00102 
00103         s = c_ulonglong.__ctype_le__(0x1234567890ABCDEF)
00104         self.assertEqual(bin(struct.pack("<Q", 0x1234567890ABCDEF)), "EFCDAB9078563412")
00105         self.assertEqual(bin(s), "EFCDAB9078563412")
00106         self.assertEqual(s.value, 0x1234567890ABCDEF)

Here is the call graph for this function:

Definition at line 142 of file test_byteswap.py.

00142 
00143     def test_endian_other(self):
00144         self.assertTrue(c_byte.__ctype_le__ is c_byte)
00145         self.assertTrue(c_byte.__ctype_be__ is c_byte)
00146 
00147         self.assertTrue(c_ubyte.__ctype_le__ is c_ubyte)
00148         self.assertTrue(c_ubyte.__ctype_be__ is c_ubyte)
00149 
00150         self.assertTrue(c_char.__ctype_le__ is c_char)
00151         self.assertTrue(c_char.__ctype_be__ is c_char)

Here is the call graph for this function:

Definition at line 24 of file test_byteswap.py.

00024 
00025     def test_endian_short(self):
00026         if sys.byteorder == "little":
00027             self.assertTrue(c_short.__ctype_le__ is c_short)
00028             self.assertTrue(c_short.__ctype_be__.__ctype_le__ is c_short)
00029         else:
00030             self.assertTrue(c_short.__ctype_be__ is c_short)
00031             self.assertTrue(c_short.__ctype_le__.__ctype_be__ is c_short)
00032         s = c_short.__ctype_be__(0x1234)
00033         self.assertEqual(bin(struct.pack(">h", 0x1234)), "1234")
00034         self.assertEqual(bin(s), "1234")
00035         self.assertEqual(s.value, 0x1234)
00036 
00037         s = c_short.__ctype_le__(0x1234)
00038         self.assertEqual(bin(struct.pack("<h", 0x1234)), "3412")
00039         self.assertEqual(bin(s), "3412")
00040         self.assertEqual(s.value, 0x1234)
00041 
00042         s = c_ushort.__ctype_be__(0x1234)
00043         self.assertEqual(bin(struct.pack(">h", 0x1234)), "1234")
00044         self.assertEqual(bin(s), "1234")
00045         self.assertEqual(s.value, 0x1234)
00046 
00047         s = c_ushort.__ctype_le__(0x1234)
00048         self.assertEqual(bin(struct.pack("<h", 0x1234)), "3412")
00049         self.assertEqual(bin(s), "3412")
00050         self.assertEqual(s.value, 0x1234)

Here is the call graph for this function:

Definition at line 152 of file test_byteswap.py.

00152 
00153     def test_struct_fields_1(self):
00154         if sys.byteorder == "little":
00155             base = BigEndianStructure
00156         else:
00157             base = LittleEndianStructure
00158 
00159         class T(base):
00160             pass
00161         _fields_ = [("a", c_ubyte),
00162                     ("b", c_byte),
00163                     ("c", c_short),
00164                     ("d", c_ushort),
00165                     ("e", c_int),
00166                     ("f", c_uint),
00167                     ("g", c_long),
00168                     ("h", c_ulong),
00169                     ("i", c_longlong),
00170                     ("k", c_ulonglong),
00171                     ("l", c_float),
00172                     ("m", c_double),
00173                     ("n", c_char),
00174 
00175                     ("b1", c_byte, 3),
00176                     ("b2", c_byte, 3),
00177                     ("b3", c_byte, 2),
00178                     ("a", c_int * 3 * 3 * 3)]
00179         T._fields_ = _fields_
00180 
00181         # these fields do not support different byte order:
00182         for typ in c_wchar, c_void_p, POINTER(c_int):
00183             _fields_.append(("x", typ))
00184             class T(base):
00185                 pass
00186             self.assertRaises(TypeError, setattr, T, "_fields_", [("x", typ)])

Definition at line 215 of file test_byteswap.py.

00215 
00216     def test_struct_fields_2(self):
00217         # standard packing in struct uses no alignment.
00218         # So, we have to align using pad bytes.
00219         #
00220         # Unaligned accesses will crash Python (on those platforms that
00221         # don't allow it, like sparc solaris).
00222         if sys.byteorder == "little":
00223             base = BigEndianStructure
00224             fmt = ">bxhid"
00225         else:
00226             base = LittleEndianStructure
00227             fmt = "<bxhid"
00228 
00229         class S(base):
00230             _fields_ = [("b", c_byte),
00231                         ("h", c_short),
00232                         ("i", c_int),
00233                         ("d", c_double)]
00234 
00235         s1 = S(0x12, 0x1234, 0x12345678, 3.14)
00236         s2 = struct.pack(fmt, 0x12, 0x1234, 0x12345678, 3.14)
00237         self.assertEqual(bin(s1), bin(s2))

Here is the call graph for this function:

Definition at line 187 of file test_byteswap.py.

00187 
00188     def test_struct_struct(self):
00189         # nested structures with different byteorders
00190 
00191         # create nested structures with given byteorders and set memory to data
00192 
00193         for nested, data in (
00194             (BigEndianStructure, b'\0\0\0\1\0\0\0\2'),
00195             (LittleEndianStructure, b'\1\0\0\0\2\0\0\0'),
00196         ):
00197             for parent in (
00198                 BigEndianStructure,
00199                 LittleEndianStructure,
00200                 Structure,
00201             ):
00202                 class NestedStructure(nested):
00203                     _fields_ = [("x", c_uint32),
00204                                 ("y", c_uint32)]
00205 
00206                 class TestStructure(parent):
00207                     _fields_ = [("point", NestedStructure)]
00208 
00209                 self.assertEqual(len(data), sizeof(TestStructure))
00210                 ptr = POINTER(TestStructure)
00211                 s = cast(data, ptr)[0]
00212                 del ctypes._pointer_type_cache[TestStructure]
00213                 self.assertEqual(s.point.x, 1)
00214                 self.assertEqual(s.point.y, 2)

Here is the call graph for this function:

Definition at line 266 of file test_byteswap.py.

00266 
00267     def test_unaligned_native_struct_fields(self):
00268         if sys.byteorder == "little":
00269             fmt = "<b h xi xd"
00270         else:
00271             base = LittleEndianStructure
00272             fmt = ">b h xi xd"
00273 
00274         class S(Structure):
00275             _pack_ = 1
00276             _fields_ = [("b", c_byte),
00277 
00278                         ("h", c_short),
00279 
00280                         ("_1", c_byte),
00281                         ("i", c_int),
00282 
00283                         ("_2", c_byte),
00284                         ("d", c_double)]
00285 
00286         s1 = S()
00287         s1.b = 0x12
00288         s1.h = 0x1234
00289         s1.i = 0x12345678
00290         s1.d = 3.14
00291         s2 = struct.pack(fmt, 0x12, 0x1234, 0x12345678, 3.14)
00292         self.assertEqual(bin(s1), bin(s2))

Here is the call graph for this function:

Definition at line 238 of file test_byteswap.py.

00238 
00239     def test_unaligned_nonnative_struct_fields(self):
00240         if sys.byteorder == "little":
00241             base = BigEndianStructure
00242             fmt = ">b h xi xd"
00243         else:
00244             base = LittleEndianStructure
00245             fmt = "<b h xi xd"
00246 
00247         class S(base):
00248             _pack_ = 1
00249             _fields_ = [("b", c_byte),
00250 
00251                         ("h", c_short),
00252 
00253                         ("_1", c_byte),
00254                         ("i", c_int),
00255 
00256                         ("_2", c_byte),
00257                         ("d", c_double)]
00258 
00259         s1 = S()
00260         s1.b = 0x12
00261         s1.h = 0x1234
00262         s1.i = 0x12345678
00263         s1.d = 3.14
00264         s2 = struct.pack(fmt, 0x12, 0x1234, 0x12345678, 3.14)
00265         self.assertEqual(bin(s1), bin(s2))

Here is the call graph for this function:

Definition at line 17 of file test_byteswap.py.

00017 
00018     def X_test(self):
00019         print(sys.byteorder, file=sys.stderr)
00020         for i in range(32):
00021             bits = BITS()
00022             setattr(bits, "i%s" % i, 1)
00023             dump(bits)

Here is the call graph for this function:


The documentation for this class was generated from the following file: