home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2010 November / maximum-cd-2010-11.iso / DiscContents / calibre-0.7.13.msi / file_354 (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2010-08-06  |  5.9 KB  |  154 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. import unittest
  5. from ctypes import *
  6. import _ctypes_test
  7.  
  8. class Callbacks(unittest.TestCase):
  9.     functype = CFUNCTYPE
  10.     
  11.     def callback(self, *args):
  12.         self.got_args = args
  13.         return args[-1]
  14.  
  15.     
  16.     def check_type(self, typ, arg):
  17.         PROTO = self.functype.im_func(typ, typ)
  18.         result = PROTO(self.callback)(arg)
  19.         if typ == c_float:
  20.             self.failUnlessAlmostEqual(result, arg, places = 5)
  21.         else:
  22.             self.failUnlessEqual(self.got_args, (arg,))
  23.             self.failUnlessEqual(result, arg)
  24.         PROTO = self.functype.im_func(typ, c_byte, typ)
  25.         result = PROTO(self.callback)(-3, arg)
  26.         if typ == c_float:
  27.             self.failUnlessAlmostEqual(result, arg, places = 5)
  28.         else:
  29.             self.failUnlessEqual(self.got_args, (-3, arg))
  30.             self.failUnlessEqual(result, arg)
  31.  
  32.     
  33.     def test_byte(self):
  34.         self.check_type(c_byte, 42)
  35.         self.check_type(c_byte, -42)
  36.  
  37.     
  38.     def test_ubyte(self):
  39.         self.check_type(c_ubyte, 42)
  40.  
  41.     
  42.     def test_short(self):
  43.         self.check_type(c_short, 42)
  44.         self.check_type(c_short, -42)
  45.  
  46.     
  47.     def test_ushort(self):
  48.         self.check_type(c_ushort, 42)
  49.  
  50.     
  51.     def test_int(self):
  52.         self.check_type(c_int, 42)
  53.         self.check_type(c_int, -42)
  54.  
  55.     
  56.     def test_uint(self):
  57.         self.check_type(c_uint, 42)
  58.  
  59.     
  60.     def test_long(self):
  61.         self.check_type(c_long, 42)
  62.         self.check_type(c_long, -42)
  63.  
  64.     
  65.     def test_ulong(self):
  66.         self.check_type(c_ulong, 42)
  67.  
  68.     
  69.     def test_longlong(self):
  70.         self.check_type(c_longlong, 42)
  71.         self.check_type(c_longlong, -42)
  72.  
  73.     
  74.     def test_ulonglong(self):
  75.         self.check_type(c_ulonglong, 42)
  76.  
  77.     
  78.     def test_float(self):
  79.         import math
  80.         self.check_type(c_float, math.e)
  81.         self.check_type(c_float, -(math.e))
  82.  
  83.     
  84.     def test_double(self):
  85.         self.check_type(c_double, 3.14)
  86.         self.check_type(c_double, -3.14)
  87.  
  88.     
  89.     def test_longdouble(self):
  90.         self.check_type(c_longdouble, 3.14)
  91.         self.check_type(c_longdouble, -3.14)
  92.  
  93.     
  94.     def test_char(self):
  95.         self.check_type(c_char, 'x')
  96.         self.check_type(c_char, 'a')
  97.  
  98.     
  99.     def test_pyobject(self):
  100.         o = ()
  101.         grc = getrefcount
  102.         import sys
  103.         for o in ((), [], object()):
  104.             initial = grc(o)
  105.             self.check_type(py_object, o)
  106.             before = grc(o)
  107.             self.check_type(py_object, o)
  108.             after = grc(o)
  109.             self.failUnlessEqual((after, o), (before, o))
  110.         
  111.  
  112.     
  113.     def test_unsupported_restype_1(self):
  114.         prototype = self.functype.im_func(POINTER(c_double))
  115.         self.assertRaises(TypeError, prototype, (lambda : pass))
  116.  
  117.     
  118.     def test_unsupported_restype_2(self):
  119.         prototype = self.functype.im_func(object)
  120.         self.assertRaises(TypeError, prototype, (lambda : pass))
  121.  
  122.  
  123.  
  124. try:
  125.     WINFUNCTYPE
  126. except NameError:
  127.     pass
  128.  
  129.  
  130. class StdcallCallbacks(Callbacks):
  131.     functype = WINFUNCTYPE
  132.  
  133.  
  134. class SampleCallbacksTestCase(unittest.TestCase):
  135.     
  136.     def test_integrate(self):
  137.         dll = CDLL(_ctypes_test.__file__)
  138.         CALLBACK = CFUNCTYPE(c_double, c_double)
  139.         integrate = dll.integrate
  140.         integrate.argtypes = (c_double, c_double, CALLBACK, c_long)
  141.         integrate.restype = c_double
  142.         
  143.         def func(x):
  144.             return x ** 2
  145.  
  146.         result = integrate(0, 1, CALLBACK(func), 10)
  147.         diff = abs(result - 1 / 3)
  148.         self.failUnless(diff < 0.01, '%s not less than 0.01' % diff)
  149.  
  150.  
  151. if __name__ == '__main__':
  152.     unittest.main()
  153.  
  154.