dtype.py
Go to the documentation of this file.
00001 #!/usr/bin/env python
00002 
00003 # Copyright Jim Bosch & Ankit Daftery 2010-2012.
00004 # Distributed under the Boost Software License, Version 1.0.
00005 #    (See accompanying file LICENSE_1_0.txt or copy at
00006 #          http://www.boost.org/LICENSE_1_0.txt)
00007 
00008 import dtype_mod
00009 import unittest
00010 import numpy
00011 
00012 class DtypeTestCase(unittest.TestCase):
00013     
00014     def assertEquivalent(self, a, b):
00015         return self.assert_(dtype_mod.equivalent(a, b), "%r is not equivalent to %r")
00016 
00017     def testIntegers(self):
00018         for bits in (8, 16, 32, 64):
00019             s = getattr(numpy, "int%d" % bits)
00020             u = getattr(numpy, "uint%d" % bits)
00021             fs = getattr(dtype_mod, "accept_int%d" % bits)
00022             fu = getattr(dtype_mod, "accept_uint%d" % bits)
00023             self.assertEquivalent(fs(s(1)), numpy.dtype(s))
00024             self.assertEquivalent(fu(u(1)), numpy.dtype(u))
00025             # these should just use the regular Boost.Python converters
00026             self.assertEquivalent(fs(True), numpy.dtype(s))
00027             self.assertEquivalent(fu(True), numpy.dtype(u))
00028             self.assertEquivalent(fs(int(1)), numpy.dtype(s))
00029             self.assertEquivalent(fu(int(1)), numpy.dtype(u))
00030             self.assertEquivalent(fs(long(1)), numpy.dtype(s))
00031             self.assertEquivalent(fu(long(1)), numpy.dtype(u))
00032         for name in ("bool_", "byte", "ubyte", "short", "ushort", "intc", "uintc"):
00033             t = getattr(numpy, name)
00034             ft = getattr(dtype_mod, "accept_%s" % name)
00035             self.assertEquivalent(ft(t(1)), numpy.dtype(t))
00036             # these should just use the regular Boost.Python converters
00037             self.assertEquivalent(ft(True), numpy.dtype(t))
00038             if name != "bool_":
00039                 self.assertEquivalent(ft(int(1)), numpy.dtype(t))
00040                 self.assertEquivalent(ft(long(1)), numpy.dtype(t))
00041 
00042 
00043     def testFloats(self):
00044         f = numpy.float32
00045         c = numpy.complex64
00046         self.assertEquivalent(dtype_mod.accept_float32(f(numpy.pi)), numpy.dtype(f))
00047         self.assertEquivalent(dtype_mod.accept_complex64(c(1+2j)), numpy.dtype(c))
00048         f = numpy.float64
00049         c = numpy.complex128
00050         self.assertEquivalent(dtype_mod.accept_float64(f(numpy.pi)), numpy.dtype(f))
00051         self.assertEquivalent(dtype_mod.accept_complex128(c(1+2j)), numpy.dtype(c))
00052         if hasattr(numpy, "longdouble"):
00053             f = numpy.longdouble
00054             c = numpy.clongdouble
00055             self.assertEquivalent(dtype_mod.accept_longdouble(f(numpy.pi)), numpy.dtype(f))
00056             self.assertEquivalent(dtype_mod.accept_clongdouble(c(1+2j)), numpy.dtype(c))
00057             
00058 
00059 if __name__=="__main__":
00060     unittest.main()


boost_numpy
Author(s): Jim Bosch, Ankit Daftery
autogenerated on Fri Aug 28 2015 10:10:40