/apps/autodock_mgl.bak/autodock_mgl_1.125_i686-pc-linux-gnu/test/lib/python2.5/ctypes/test/test_numbers.py

https://github.com/jackygrahamez/DrugDiscovery-Home · Python · 230 lines · 135 code · 39 blank · 56 comment · 23 complexity · f5290851676669629ac4305768cedaeb MD5 · raw file

  1. from ctypes import *
  2. import unittest
  3. import sys, struct
  4. def valid_ranges(*types):
  5. # given a sequence of numeric types, collect their _type_
  6. # attribute, which is a single format character compatible with
  7. # the struct module, use the struct module to calculate the
  8. # minimum and maximum value allowed for this format.
  9. # Returns a list of (min, max) values.
  10. result = []
  11. for t in types:
  12. fmt = t._type_
  13. size = struct.calcsize(fmt)
  14. a = struct.unpack(fmt, ("\x00"*32)[:size])[0]
  15. b = struct.unpack(fmt, ("\xFF"*32)[:size])[0]
  16. c = struct.unpack(fmt, ("\x7F"+"\x00"*32)[:size])[0]
  17. d = struct.unpack(fmt, ("\x80"+"\xFF"*32)[:size])[0]
  18. result.append((min(a, b, c, d), max(a, b, c, d)))
  19. return result
  20. ArgType = type(byref(c_int(0)))
  21. unsigned_types = [c_ubyte, c_ushort, c_uint, c_ulong]
  22. signed_types = [c_byte, c_short, c_int, c_long, c_longlong]
  23. float_types = [c_double, c_float]
  24. try:
  25. c_ulonglong
  26. c_longlong
  27. except NameError:
  28. pass
  29. else:
  30. unsigned_types.append(c_ulonglong)
  31. signed_types.append(c_longlong)
  32. unsigned_ranges = valid_ranges(*unsigned_types)
  33. signed_ranges = valid_ranges(*signed_types)
  34. ################################################################
  35. class NumberTestCase(unittest.TestCase):
  36. def test_default_init(self):
  37. # default values are set to zero
  38. for t in signed_types + unsigned_types + float_types:
  39. self.failUnlessEqual(t().value, 0)
  40. def test_unsigned_values(self):
  41. # the value given to the constructor is available
  42. # as the 'value' attribute
  43. for t, (l, h) in zip(unsigned_types, unsigned_ranges):
  44. self.failUnlessEqual(t(l).value, l)
  45. self.failUnlessEqual(t(h).value, h)
  46. def test_signed_values(self):
  47. # see above
  48. for t, (l, h) in zip(signed_types, signed_ranges):
  49. self.failUnlessEqual(t(l).value, l)
  50. self.failUnlessEqual(t(h).value, h)
  51. def test_typeerror(self):
  52. # Only numbers are allowed in the contructor,
  53. # otherwise TypeError is raised
  54. for t in signed_types + unsigned_types + float_types:
  55. self.assertRaises(TypeError, t, "")
  56. self.assertRaises(TypeError, t, None)
  57. ## def test_valid_ranges(self):
  58. ## # invalid values of the correct type
  59. ## # raise ValueError (not OverflowError)
  60. ## for t, (l, h) in zip(unsigned_types, unsigned_ranges):
  61. ## self.assertRaises(ValueError, t, l-1)
  62. ## self.assertRaises(ValueError, t, h+1)
  63. def test_from_param(self):
  64. # the from_param class method attribute always
  65. # returns PyCArgObject instances
  66. for t in signed_types + unsigned_types + float_types:
  67. self.failUnlessEqual(ArgType, type(t.from_param(0)))
  68. def test_byref(self):
  69. # calling byref returns also a PyCArgObject instance
  70. for t in signed_types + unsigned_types + float_types:
  71. parm = byref(t())
  72. self.failUnlessEqual(ArgType, type(parm))
  73. def test_floats(self):
  74. # c_float and c_double can be created from
  75. # Python int, long and float
  76. for t in float_types:
  77. self.failUnlessEqual(t(2.0).value, 2.0)
  78. self.failUnlessEqual(t(2).value, 2.0)
  79. self.failUnlessEqual(t(2L).value, 2.0)
  80. def test_integers(self):
  81. # integers cannot be constructed from floats
  82. for t in signed_types + unsigned_types:
  83. self.assertRaises(TypeError, t, 3.14)
  84. def test_sizes(self):
  85. for t in signed_types + unsigned_types + float_types:
  86. size = struct.calcsize(t._type_)
  87. # sizeof of the type...
  88. self.failUnlessEqual(sizeof(t), size)
  89. # and sizeof of an instance
  90. self.failUnlessEqual(sizeof(t()), size)
  91. def test_alignments(self):
  92. for t in signed_types + unsigned_types + float_types:
  93. code = t._type_ # the typecode
  94. align = struct.calcsize("c%c" % code) - struct.calcsize(code)
  95. # alignment of the type...
  96. self.failUnlessEqual((code, alignment(t)),
  97. (code, align))
  98. # and alignment of an instance
  99. self.failUnlessEqual((code, alignment(t())),
  100. (code, align))
  101. def test_int_from_address(self):
  102. from array import array
  103. for t in signed_types + unsigned_types:
  104. # the array module doesn't suppport all format codes
  105. # (no 'q' or 'Q')
  106. try:
  107. array(t._type_)
  108. except ValueError:
  109. continue
  110. a = array(t._type_, [100])
  111. # v now is an integer at an 'external' memory location
  112. v = t.from_address(a.buffer_info()[0])
  113. self.failUnlessEqual(v.value, a[0])
  114. self.failUnlessEqual(type(v), t)
  115. # changing the value at the memory location changes v's value also
  116. a[0] = 42
  117. self.failUnlessEqual(v.value, a[0])
  118. def test_float_from_address(self):
  119. from array import array
  120. for t in float_types:
  121. a = array(t._type_, [3.14])
  122. v = t.from_address(a.buffer_info()[0])
  123. self.failUnlessEqual(v.value, a[0])
  124. self.failUnless(type(v) is t)
  125. a[0] = 2.3456e17
  126. self.failUnlessEqual(v.value, a[0])
  127. self.failUnless(type(v) is t)
  128. def test_char_from_address(self):
  129. from ctypes import c_char
  130. from array import array
  131. a = array('c', 'x')
  132. v = c_char.from_address(a.buffer_info()[0])
  133. self.failUnlessEqual(v.value, a[0])
  134. self.failUnless(type(v) is c_char)
  135. a[0] = '?'
  136. self.failUnlessEqual(v.value, a[0])
  137. def test_init(self):
  138. # c_int() can be initialized from Python's int, and c_int.
  139. # Not from c_long or so, which seems strange, abd should
  140. # probably be changed:
  141. self.assertRaises(TypeError, c_int, c_long(42))
  142. ## def test_perf(self):
  143. ## check_perf()
  144. from ctypes import _SimpleCData
  145. class c_int_S(_SimpleCData):
  146. _type_ = "i"
  147. __slots__ = []
  148. def run_test(rep, msg, func, arg=None):
  149. ## items = [None] * rep
  150. items = range(rep)
  151. from time import clock
  152. if arg is not None:
  153. start = clock()
  154. for i in items:
  155. func(arg); func(arg); func(arg); func(arg); func(arg)
  156. stop = clock()
  157. else:
  158. start = clock()
  159. for i in items:
  160. func(); func(); func(); func(); func()
  161. stop = clock()
  162. print "%15s: %.2f us" % (msg, ((stop-start)*1e6/5/rep))
  163. def check_perf():
  164. # Construct 5 objects
  165. from ctypes import c_int
  166. REP = 200000
  167. run_test(REP, "int()", int)
  168. run_test(REP, "int(999)", int)
  169. run_test(REP, "c_int()", c_int)
  170. run_test(REP, "c_int(999)", c_int)
  171. run_test(REP, "c_int_S()", c_int_S)
  172. run_test(REP, "c_int_S(999)", c_int_S)
  173. # Python 2.3 -OO, win2k, P4 700 MHz:
  174. #
  175. # int(): 0.87 us
  176. # int(999): 0.87 us
  177. # c_int(): 3.35 us
  178. # c_int(999): 3.34 us
  179. # c_int_S(): 3.23 us
  180. # c_int_S(999): 3.24 us
  181. # Python 2.2 -OO, win2k, P4 700 MHz:
  182. #
  183. # int(): 0.89 us
  184. # int(999): 0.89 us
  185. # c_int(): 9.99 us
  186. # c_int(999): 10.02 us
  187. # c_int_S(): 9.87 us
  188. # c_int_S(999): 9.85 us
  189. if __name__ == '__main__':
  190. ## check_perf()
  191. unittest.main()