dtype.py 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. #!/usr/bin/env python
  2. # Copyright Jim Bosch & Ankit Daftery 2010-2012.
  3. # Distributed under the Boost Software License, Version 1.0.
  4. # (See accompanying file LICENSE_1_0.txt or copy at
  5. # http://www.boost.org/LICENSE_1_0.txt)
  6. import dtype_ext
  7. import unittest
  8. import numpy
  9. import sys
  10. if (sys.version_info.major >= 3):
  11. long = int
  12. class DtypeTestCase(unittest.TestCase):
  13. def assertEquivalent(self, a, b):
  14. return self.assert_(dtype_ext.equivalent(a, b), "%r is not equivalent to %r")
  15. def testIntegers(self):
  16. for bits in (8, 16, 32, 64):
  17. s = getattr(numpy, "int%d" % bits)
  18. u = getattr(numpy, "uint%d" % bits)
  19. fs = getattr(dtype_ext, "accept_int%d" % bits)
  20. fu = getattr(dtype_ext, "accept_uint%d" % bits)
  21. self.assertEquivalent(fs(s(1)), numpy.dtype(s))
  22. self.assertEquivalent(fu(u(1)), numpy.dtype(u))
  23. # these should just use the regular Boost.Python converters
  24. self.assertEquivalent(fs(True), numpy.dtype(s))
  25. self.assertEquivalent(fu(True), numpy.dtype(u))
  26. self.assertEquivalent(fs(int(1)), numpy.dtype(s))
  27. self.assertEquivalent(fu(int(1)), numpy.dtype(u))
  28. self.assertEquivalent(fs(long(1)), numpy.dtype(s))
  29. self.assertEquivalent(fu(long(1)), numpy.dtype(u))
  30. for name in ("bool_", "byte", "ubyte", "short", "ushort", "intc", "uintc"):
  31. t = getattr(numpy, name)
  32. ft = getattr(dtype_ext, "accept_%s" % name)
  33. self.assertEquivalent(ft(t(1)), numpy.dtype(t))
  34. # these should just use the regular Boost.Python converters
  35. self.assertEquivalent(ft(True), numpy.dtype(t))
  36. if name != "bool_":
  37. self.assertEquivalent(ft(int(1)), numpy.dtype(t))
  38. self.assertEquivalent(ft(long(1)), numpy.dtype(t))
  39. def testFloats(self):
  40. f = numpy.float32
  41. c = numpy.complex64
  42. self.assertEquivalent(dtype_ext.accept_float32(f(numpy.pi)), numpy.dtype(f))
  43. self.assertEquivalent(dtype_ext.accept_complex64(c(1+2j)), numpy.dtype(c))
  44. f = numpy.float64
  45. c = numpy.complex128
  46. self.assertEquivalent(dtype_ext.accept_float64(f(numpy.pi)), numpy.dtype(f))
  47. self.assertEquivalent(dtype_ext.accept_complex128(c(1+2j)), numpy.dtype(c))
  48. if hasattr(numpy, "longdouble") and hasattr(dtype_ext, "accept_longdouble"):
  49. f = numpy.longdouble
  50. c = numpy.clongdouble
  51. self.assertEquivalent(dtype_ext.accept_longdouble(f(numpy.pi)), numpy.dtype(f))
  52. self.assertEquivalent(dtype_ext.accept_clongdouble(c(1+2j)), numpy.dtype(c))
  53. if __name__=="__main__":
  54. unittest.main()