Back to index

python3.2  3.2.2
Classes | Functions | Variables
ctypes.test.test_numbers Namespace Reference

Classes

class  NumberTestCase
class  c_int_S

Functions

def valid_ranges
def test_from_param
 def test_valid_ranges(self):

invalid values of the correct type

raise ValueError (not OverflowError)

for t, (l, h) in zip(unsigned_types, unsigned_ranges): self.assertRaises(ValueError, t, l-1) self.assertRaises(ValueError, t, h+1)
def test_byref
def test_floats
def test_integers
def test_sizes
def test_alignments
def test_int_from_address
def test_float_from_address
def test_char_from_address
def test_init
def run_test
def check_perf

Variables

tuple ArgType = type(byref(c_int(0)))
list unsigned_types = [c_ubyte, c_ushort, c_uint, c_ulong]
list signed_types = [c_byte, c_short, c_int, c_long, c_longlong]
list bool_types = []
list float_types = [c_double, c_float]
tuple unsigned_ranges = valid_ranges(*unsigned_types)
tuple signed_ranges = valid_ranges(*signed_types)
list bool_values = [True, False, 0, 1, -1, 5000, 'test', [], [1]]
tuple items = range(rep)
 items = [None] * rep
tuple start = clock()
tuple stop = clock()

Function Documentation

Definition at line 244 of file test_numbers.py.

00244 
00245 def check_perf():
00246     # Construct 5 objects
00247     from ctypes import c_int
00248 
00249     REP = 200000
00250 
00251     run_test(REP, "int()", int)
00252     run_test(REP, "int(999)", int)
00253     run_test(REP, "c_int()", c_int)
00254     run_test(REP, "c_int(999)", c_int)
00255     run_test(REP, "c_int_S()", c_int_S)
00256     run_test(REP, "c_int_S(999)", c_int_S)
00257 
00258 # Python 2.3 -OO, win2k, P4 700 MHz:
00259 #
00260 #          int(): 0.87 us
00261 #       int(999): 0.87 us
00262 #        c_int(): 3.35 us
00263 #     c_int(999): 3.34 us
00264 #      c_int_S(): 3.23 us
00265 #   c_int_S(999): 3.24 us
00266 
00267 # Python 2.2 -OO, win2k, P4 700 MHz:
00268 #
00269 #          int(): 0.89 us
00270 #       int(999): 0.89 us
00271 #        c_int(): 9.99 us
00272 #     c_int(999): 10.02 us
00273 #      c_int_S(): 9.87 us
00274 #   c_int_S(999): 9.85 us

Here is the call graph for this function:

def ctypes.test.test_numbers.run_test (   rep,
  msg,
  func,
  arg = None 
)

Definition at line 228 of file test_numbers.py.

00228 
def run_test(rep, msg, func, arg=None):

Here is the caller graph for this function:

Definition at line 145 of file test_numbers.py.

00145 
00146     def test_alignments(self):
00147         for t in signed_types + unsigned_types + float_types:
00148             code = t._type_ # the typecode
00149             align = struct.calcsize("c%c" % code) - struct.calcsize(code)
00150 
00151             # alignment of the type...
00152             self.assertEqual((code, alignment(t)),
00153                                  (code, align))
00154             # and alignment of an instance
00155             self.assertEqual((code, alignment(t())),
00156                                  (code, align))

Definition at line 98 of file test_numbers.py.

00098 
00099     def test_byref(self):
00100         # calling byref returns also a PyCArgObject instance
00101         for t in signed_types + unsigned_types + float_types + bool_types:
00102             parm = byref(t())
00103             self.assertEqual(ArgType, type(parm))
00104 

Here is the call graph for this function:

Definition at line 189 of file test_numbers.py.

00189 
00190     def test_char_from_address(self):
00191         from ctypes import c_char
00192         from array import array
00193 
00194         a = array('b', [0])
00195         a[0] = ord('x')
00196         v = c_char.from_address(a.buffer_info()[0])
00197         self.assertEqual(v.value, b'x')
00198         self.assertTrue(type(v) is c_char)
00199 
00200         a[0] = ord('?')
00201         self.assertEqual(v.value, b'?')

Here is the call graph for this function:

Definition at line 178 of file test_numbers.py.

00178 
00179     def test_float_from_address(self):
00180         from array import array
00181         for t in float_types:
00182             a = array(t._type_, [3.14])
00183             v = t.from_address(a.buffer_info()[0])
00184             self.assertEqual(v.value, a[0])
00185             self.assertTrue(type(v) is t)
00186             a[0] = 2.3456e17
00187             self.assertEqual(v.value, a[0])
00188             self.assertTrue(type(v) is t)

Definition at line 105 of file test_numbers.py.

00105 
00106     def test_floats(self):
00107         # c_float and c_double can be created from
00108         # Python int, long and float
00109         class FloatLike(object):
00110             def __float__(self):
00111                 return 2.0
00112         f = FloatLike()
00113         for t in float_types:
00114             self.assertEqual(t(2.0).value, 2.0)
00115             self.assertEqual(t(2).value, 2.0)
00116             self.assertEqual(t(2).value, 2.0)
00117             self.assertEqual(t(f).value, 2.0)

def test_valid_ranges(self):

invalid values of the correct type

raise ValueError (not OverflowError)

for t, (l, h) in zip(unsigned_types, unsigned_ranges): self.assertRaises(ValueError, t, l-1) self.assertRaises(ValueError, t, h+1)

Definition at line 92 of file test_numbers.py.

00092 
00093     def test_from_param(self):
00094         # the from_param class method attribute always
00095         # returns PyCArgObject instances
00096         for t in signed_types + unsigned_types + float_types:
00097             self.assertEqual(ArgType, type(t.from_param(0)))

Definition at line 214 of file test_numbers.py.

00214 
00215     def test_init(self):
00216         # c_int() can be initialized from Python's int, and c_int.
00217         # Not from c_long or so, which seems strange, abd should
00218         # probably be changed:
00219         self.assertRaises(TypeError, c_int, c_long(42))

Definition at line 157 of file test_numbers.py.

00157 
00158     def test_int_from_address(self):
00159         from array import array
00160         for t in signed_types + unsigned_types:
00161             # the array module doesn't support all format codes
00162             # (no 'q' or 'Q')
00163             try:
00164                 array(t._type_)
00165             except ValueError:
00166                 continue
00167             a = array(t._type_, [100])
00168 
00169             # v now is an integer at an 'external' memory location
00170             v = t.from_address(a.buffer_info()[0])
00171             self.assertEqual(v.value, a[0])
00172             self.assertEqual(type(v), t)
00173 
00174             # changing the value at the memory location changes v's value also
00175             a[0] = 42
00176             self.assertEqual(v.value, a[0])
00177 

Definition at line 118 of file test_numbers.py.

00118 
00119     def test_integers(self):
00120         class FloatLike(object):
00121             def __float__(self):
00122                 return 2.0
00123         f = FloatLike()
00124         class IntLike(object):
00125             def __int__(self):
00126                 return 2
00127         i = IntLike()
00128         # integers cannot be constructed from floats,
00129         # but from integer-like objects
00130         for t in signed_types + unsigned_types:
00131             self.assertRaises(TypeError, t, 3.14)
00132             self.assertRaises(TypeError, t, f)
00133             self.assertEqual(t(i).value, 2)

Definition at line 134 of file test_numbers.py.

00134 
00135     def test_sizes(self):
00136         for t in signed_types + unsigned_types + float_types + bool_types:
00137             try:
00138                 size = struct.calcsize(t._type_)
00139             except struct.error:
00140                 continue
00141             # sizeof of the type...
00142             self.assertEqual(sizeof(t), size)
00143             # and sizeof of an instance
00144             self.assertEqual(sizeof(t()), size)

Definition at line 5 of file test_numbers.py.

00005 
00006 def valid_ranges(*types):
00007     # given a sequence of numeric types, collect their _type_
00008     # attribute, which is a single format character compatible with
00009     # the struct module, use the struct module to calculate the
00010     # minimum and maximum value allowed for this format.
00011     # Returns a list of (min, max) values.
00012     result = []
00013     for t in types:
00014         fmt = t._type_
00015         size = struct.calcsize(fmt)
00016         a = struct.unpack(fmt, (b"\x00"*32)[:size])[0]
00017         b = struct.unpack(fmt, (b"\xFF"*32)[:size])[0]
00018         c = struct.unpack(fmt, (b"\x7F"+b"\x00"*32)[:size])[0]
00019         d = struct.unpack(fmt, (b"\x80"+b"\xFF"*32)[:size])[0]
00020         result.append((min(a, b, c, d), max(a, b, c, d)))
00021     return result


Variable Documentation

Definition at line 22 of file test_numbers.py.

Definition at line 27 of file test_numbers.py.

list ctypes.test.test_numbers.bool_values = [True, False, 0, 1, -1, 5000, 'test', [], [1]]

Definition at line 49 of file test_numbers.py.

Definition at line 29 of file test_numbers.py.

tuple ctypes.test.test_numbers.items = range(rep)

items = [None] * rep

Definition at line 230 of file test_numbers.py.

Definition at line 48 of file test_numbers.py.

Definition at line 25 of file test_numbers.py.

Definition at line 233 of file test_numbers.py.

Definition at line 236 of file test_numbers.py.

Definition at line 47 of file test_numbers.py.

Definition at line 24 of file test_numbers.py.