/tools/swig/test/testMatrix.py

https://github.com/njsmith/numpy · Python · 362 lines · 255 code · 61 blank · 46 comment · 4 complexity · 731c8615680c4e9fc950b503910a06f3 MD5 · raw file

  1. #! /usr/bin/env python
  2. from __future__ import division, absolute_import, print_function
  3. # System imports
  4. from distutils.util import get_platform
  5. import os
  6. import sys
  7. import unittest
  8. # Import NumPy
  9. import numpy as np
  10. major, minor = [ int(d) for d in np.__version__.split(".")[:2] ]
  11. if major == 0: BadListError = TypeError
  12. else: BadListError = ValueError
  13. import Matrix
  14. ######################################################################
  15. class MatrixTestCase(unittest.TestCase):
  16. def __init__(self, methodName="runTests"):
  17. unittest.TestCase.__init__(self, methodName)
  18. self.typeStr = "double"
  19. self.typeCode = "d"
  20. # Test (type IN_ARRAY2[ANY][ANY]) typemap
  21. def testDet(self):
  22. "Test det function"
  23. print(self.typeStr, "... ", end=' ', file=sys.stderr)
  24. det = Matrix.__dict__[self.typeStr + "Det"]
  25. matrix = [[8, 7], [6, 9]]
  26. self.assertEquals(det(matrix), 30)
  27. # Test (type IN_ARRAY2[ANY][ANY]) typemap
  28. def testDetBadList(self):
  29. "Test det function with bad list"
  30. print(self.typeStr, "... ", end=' ', file=sys.stderr)
  31. det = Matrix.__dict__[self.typeStr + "Det"]
  32. matrix = [[8, 7], ["e", "pi"]]
  33. self.assertRaises(BadListError, det, matrix)
  34. # Test (type IN_ARRAY2[ANY][ANY]) typemap
  35. def testDetWrongDim(self):
  36. "Test det function with wrong dimensions"
  37. print(self.typeStr, "... ", end=' ', file=sys.stderr)
  38. det = Matrix.__dict__[self.typeStr + "Det"]
  39. matrix = [8, 7]
  40. self.assertRaises(TypeError, det, matrix)
  41. # Test (type IN_ARRAY2[ANY][ANY]) typemap
  42. def testDetWrongSize(self):
  43. "Test det function with wrong size"
  44. print(self.typeStr, "... ", end=' ', file=sys.stderr)
  45. det = Matrix.__dict__[self.typeStr + "Det"]
  46. matrix = [[8, 7, 6], [5, 4, 3], [2, 1, 0]]
  47. self.assertRaises(TypeError, det, matrix)
  48. # Test (type IN_ARRAY2[ANY][ANY]) typemap
  49. def testDetNonContainer(self):
  50. "Test det function with non-container"
  51. print(self.typeStr, "... ", end=' ', file=sys.stderr)
  52. det = Matrix.__dict__[self.typeStr + "Det"]
  53. self.assertRaises(TypeError, det, None)
  54. # Test (type* IN_ARRAY2, int DIM1, int DIM2) typemap
  55. def testMax(self):
  56. "Test max function"
  57. print(self.typeStr, "... ", end=' ', file=sys.stderr)
  58. max = Matrix.__dict__[self.typeStr + "Max"]
  59. matrix = [[6, 5, 4], [3, 2, 1]]
  60. self.assertEquals(max(matrix), 6)
  61. # Test (type* IN_ARRAY2, int DIM1, int DIM2) typemap
  62. def testMaxBadList(self):
  63. "Test max function with bad list"
  64. print(self.typeStr, "... ", end=' ', file=sys.stderr)
  65. max = Matrix.__dict__[self.typeStr + "Max"]
  66. matrix = [[6, "five", 4], ["three", 2, "one"]]
  67. self.assertRaises(BadListError, max, matrix)
  68. # Test (type* IN_ARRAY2, int DIM1, int DIM2) typemap
  69. def testMaxNonContainer(self):
  70. "Test max function with non-container"
  71. print(self.typeStr, "... ", end=' ', file=sys.stderr)
  72. max = Matrix.__dict__[self.typeStr + "Max"]
  73. self.assertRaises(TypeError, max, None)
  74. # Test (type* IN_ARRAY2, int DIM1, int DIM2) typemap
  75. def testMaxWrongDim(self):
  76. "Test max function with wrong dimensions"
  77. print(self.typeStr, "... ", end=' ', file=sys.stderr)
  78. max = Matrix.__dict__[self.typeStr + "Max"]
  79. self.assertRaises(TypeError, max, [0, 1, 2, 3])
  80. # Test (int DIM1, int DIM2, type* IN_ARRAY2) typemap
  81. def testMin(self):
  82. "Test min function"
  83. print(self.typeStr, "... ", end=' ', file=sys.stderr)
  84. min = Matrix.__dict__[self.typeStr + "Min"]
  85. matrix = [[9, 8], [7, 6], [5, 4]]
  86. self.assertEquals(min(matrix), 4)
  87. # Test (int DIM1, int DIM2, type* IN_ARRAY2) typemap
  88. def testMinBadList(self):
  89. "Test min function with bad list"
  90. print(self.typeStr, "... ", end=' ', file=sys.stderr)
  91. min = Matrix.__dict__[self.typeStr + "Min"]
  92. matrix = [["nine", "eight"], ["seven", "six"]]
  93. self.assertRaises(BadListError, min, matrix)
  94. # Test (int DIM1, int DIM2, type* IN_ARRAY2) typemap
  95. def testMinWrongDim(self):
  96. "Test min function with wrong dimensions"
  97. print(self.typeStr, "... ", end=' ', file=sys.stderr)
  98. min = Matrix.__dict__[self.typeStr + "Min"]
  99. self.assertRaises(TypeError, min, [1, 3, 5, 7, 9])
  100. # Test (int DIM1, int DIM2, type* IN_ARRAY2) typemap
  101. def testMinNonContainer(self):
  102. "Test min function with non-container"
  103. print(self.typeStr, "... ", end=' ', file=sys.stderr)
  104. min = Matrix.__dict__[self.typeStr + "Min"]
  105. self.assertRaises(TypeError, min, False)
  106. # Test (type INPLACE_ARRAY2[ANY][ANY]) typemap
  107. def testScale(self):
  108. "Test scale function"
  109. print(self.typeStr, "... ", end=' ', file=sys.stderr)
  110. scale = Matrix.__dict__[self.typeStr + "Scale"]
  111. matrix = np.array([[1, 2, 3], [2, 1, 2], [3, 2, 1]], self.typeCode)
  112. scale(matrix, 4)
  113. self.assertEquals((matrix == [[4, 8, 12], [8, 4, 8], [12, 8, 4]]).all(), True)
  114. # Test (type INPLACE_ARRAY2[ANY][ANY]) typemap
  115. def testScaleWrongDim(self):
  116. "Test scale function with wrong dimensions"
  117. print(self.typeStr, "... ", end=' ', file=sys.stderr)
  118. scale = Matrix.__dict__[self.typeStr + "Scale"]
  119. matrix = np.array([1, 2, 2, 1], self.typeCode)
  120. self.assertRaises(TypeError, scale, matrix)
  121. # Test (type INPLACE_ARRAY2[ANY][ANY]) typemap
  122. def testScaleWrongSize(self):
  123. "Test scale function with wrong size"
  124. print(self.typeStr, "... ", end=' ', file=sys.stderr)
  125. scale = Matrix.__dict__[self.typeStr + "Scale"]
  126. matrix = np.array([[1, 2], [2, 1]], self.typeCode)
  127. self.assertRaises(TypeError, scale, matrix)
  128. # Test (type INPLACE_ARRAY2[ANY][ANY]) typemap
  129. def testScaleWrongType(self):
  130. "Test scale function with wrong type"
  131. print(self.typeStr, "... ", end=' ', file=sys.stderr)
  132. scale = Matrix.__dict__[self.typeStr + "Scale"]
  133. matrix = np.array([[1, 2, 3], [2, 1, 2], [3, 2, 1]], 'c')
  134. self.assertRaises(TypeError, scale, matrix)
  135. # Test (type INPLACE_ARRAY2[ANY][ANY]) typemap
  136. def testScaleNonArray(self):
  137. "Test scale function with non-array"
  138. print(self.typeStr, "... ", end=' ', file=sys.stderr)
  139. scale = Matrix.__dict__[self.typeStr + "Scale"]
  140. matrix = [[1, 2, 3], [2, 1, 2], [3, 2, 1]]
  141. self.assertRaises(TypeError, scale, matrix)
  142. # Test (type* INPLACE_ARRAY2, int DIM1, int DIM2) typemap
  143. def testFloor(self):
  144. "Test floor function"
  145. print(self.typeStr, "... ", end=' ', file=sys.stderr)
  146. floor = Matrix.__dict__[self.typeStr + "Floor"]
  147. matrix = np.array([[6, 7], [8, 9]], self.typeCode)
  148. floor(matrix, 7)
  149. np.testing.assert_array_equal(matrix, np.array([[7, 7], [8, 9]]))
  150. # Test (type* INPLACE_ARRAY2, int DIM1, int DIM2) typemap
  151. def testFloorWrongDim(self):
  152. "Test floor function with wrong dimensions"
  153. print(self.typeStr, "... ", end=' ', file=sys.stderr)
  154. floor = Matrix.__dict__[self.typeStr + "Floor"]
  155. matrix = np.array([6, 7, 8, 9], self.typeCode)
  156. self.assertRaises(TypeError, floor, matrix)
  157. # Test (type* INPLACE_ARRAY2, int DIM1, int DIM2) typemap
  158. def testFloorWrongType(self):
  159. "Test floor function with wrong type"
  160. print(self.typeStr, "... ", end=' ', file=sys.stderr)
  161. floor = Matrix.__dict__[self.typeStr + "Floor"]
  162. matrix = np.array([[6, 7], [8, 9]], 'c')
  163. self.assertRaises(TypeError, floor, matrix)
  164. # Test (type* INPLACE_ARRAY2, int DIM1, int DIM2) typemap
  165. def testFloorNonArray(self):
  166. "Test floor function with non-array"
  167. print(self.typeStr, "... ", end=' ', file=sys.stderr)
  168. floor = Matrix.__dict__[self.typeStr + "Floor"]
  169. matrix = [[6, 7], [8, 9]]
  170. self.assertRaises(TypeError, floor, matrix)
  171. # Test (int DIM1, int DIM2, type* INPLACE_ARRAY2) typemap
  172. def testCeil(self):
  173. "Test ceil function"
  174. print(self.typeStr, "... ", end=' ', file=sys.stderr)
  175. ceil = Matrix.__dict__[self.typeStr + "Ceil"]
  176. matrix = np.array([[1, 2], [3, 4]], self.typeCode)
  177. ceil(matrix, 3)
  178. np.testing.assert_array_equal(matrix, np.array([[1, 2], [3, 3]]))
  179. # Test (int DIM1, int DIM2, type* INPLACE_ARRAY2) typemap
  180. def testCeilWrongDim(self):
  181. "Test ceil function with wrong dimensions"
  182. print(self.typeStr, "... ", end=' ', file=sys.stderr)
  183. ceil = Matrix.__dict__[self.typeStr + "Ceil"]
  184. matrix = np.array([1, 2, 3, 4], self.typeCode)
  185. self.assertRaises(TypeError, ceil, matrix)
  186. # Test (int DIM1, int DIM2, type* INPLACE_ARRAY2) typemap
  187. def testCeilWrongType(self):
  188. "Test ceil function with wrong dimensions"
  189. print(self.typeStr, "... ", end=' ', file=sys.stderr)
  190. ceil = Matrix.__dict__[self.typeStr + "Ceil"]
  191. matrix = np.array([[1, 2], [3, 4]], 'c')
  192. self.assertRaises(TypeError, ceil, matrix)
  193. # Test (int DIM1, int DIM2, type* INPLACE_ARRAY2) typemap
  194. def testCeilNonArray(self):
  195. "Test ceil function with non-array"
  196. print(self.typeStr, "... ", end=' ', file=sys.stderr)
  197. ceil = Matrix.__dict__[self.typeStr + "Ceil"]
  198. matrix = [[1, 2], [3, 4]]
  199. self.assertRaises(TypeError, ceil, matrix)
  200. # Test (type ARGOUT_ARRAY2[ANY][ANY]) typemap
  201. def testLUSplit(self):
  202. "Test luSplit function"
  203. print(self.typeStr, "... ", end=' ', file=sys.stderr)
  204. luSplit = Matrix.__dict__[self.typeStr + "LUSplit"]
  205. lower, upper = luSplit([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
  206. self.assertEquals((lower == [[1, 0, 0], [4, 5, 0], [7, 8, 9]]).all(), True)
  207. self.assertEquals((upper == [[0, 2, 3], [0, 0, 6], [0, 0, 0]]).all(), True)
  208. ######################################################################
  209. class scharTestCase(MatrixTestCase):
  210. def __init__(self, methodName="runTest"):
  211. MatrixTestCase.__init__(self, methodName)
  212. self.typeStr = "schar"
  213. self.typeCode = "b"
  214. ######################################################################
  215. class ucharTestCase(MatrixTestCase):
  216. def __init__(self, methodName="runTest"):
  217. MatrixTestCase.__init__(self, methodName)
  218. self.typeStr = "uchar"
  219. self.typeCode = "B"
  220. ######################################################################
  221. class shortTestCase(MatrixTestCase):
  222. def __init__(self, methodName="runTest"):
  223. MatrixTestCase.__init__(self, methodName)
  224. self.typeStr = "short"
  225. self.typeCode = "h"
  226. ######################################################################
  227. class ushortTestCase(MatrixTestCase):
  228. def __init__(self, methodName="runTest"):
  229. MatrixTestCase.__init__(self, methodName)
  230. self.typeStr = "ushort"
  231. self.typeCode = "H"
  232. ######################################################################
  233. class intTestCase(MatrixTestCase):
  234. def __init__(self, methodName="runTest"):
  235. MatrixTestCase.__init__(self, methodName)
  236. self.typeStr = "int"
  237. self.typeCode = "i"
  238. ######################################################################
  239. class uintTestCase(MatrixTestCase):
  240. def __init__(self, methodName="runTest"):
  241. MatrixTestCase.__init__(self, methodName)
  242. self.typeStr = "uint"
  243. self.typeCode = "I"
  244. ######################################################################
  245. class longTestCase(MatrixTestCase):
  246. def __init__(self, methodName="runTest"):
  247. MatrixTestCase.__init__(self, methodName)
  248. self.typeStr = "long"
  249. self.typeCode = "l"
  250. ######################################################################
  251. class ulongTestCase(MatrixTestCase):
  252. def __init__(self, methodName="runTest"):
  253. MatrixTestCase.__init__(self, methodName)
  254. self.typeStr = "ulong"
  255. self.typeCode = "L"
  256. ######################################################################
  257. class longLongTestCase(MatrixTestCase):
  258. def __init__(self, methodName="runTest"):
  259. MatrixTestCase.__init__(self, methodName)
  260. self.typeStr = "longLong"
  261. self.typeCode = "q"
  262. ######################################################################
  263. class ulongLongTestCase(MatrixTestCase):
  264. def __init__(self, methodName="runTest"):
  265. MatrixTestCase.__init__(self, methodName)
  266. self.typeStr = "ulongLong"
  267. self.typeCode = "Q"
  268. ######################################################################
  269. class floatTestCase(MatrixTestCase):
  270. def __init__(self, methodName="runTest"):
  271. MatrixTestCase.__init__(self, methodName)
  272. self.typeStr = "float"
  273. self.typeCode = "f"
  274. ######################################################################
  275. class doubleTestCase(MatrixTestCase):
  276. def __init__(self, methodName="runTest"):
  277. MatrixTestCase.__init__(self, methodName)
  278. self.typeStr = "double"
  279. self.typeCode = "d"
  280. ######################################################################
  281. if __name__ == "__main__":
  282. # Build the test suite
  283. suite = unittest.TestSuite()
  284. suite.addTest(unittest.makeSuite( scharTestCase))
  285. suite.addTest(unittest.makeSuite( ucharTestCase))
  286. suite.addTest(unittest.makeSuite( shortTestCase))
  287. suite.addTest(unittest.makeSuite( ushortTestCase))
  288. suite.addTest(unittest.makeSuite( intTestCase))
  289. suite.addTest(unittest.makeSuite( uintTestCase))
  290. suite.addTest(unittest.makeSuite( longTestCase))
  291. suite.addTest(unittest.makeSuite( ulongTestCase))
  292. suite.addTest(unittest.makeSuite( longLongTestCase))
  293. suite.addTest(unittest.makeSuite(ulongLongTestCase))
  294. suite.addTest(unittest.makeSuite( floatTestCase))
  295. suite.addTest(unittest.makeSuite( doubleTestCase))
  296. # Execute the test suite
  297. print("Testing 2D Functions of Module Matrix")
  298. print("NumPy version", np.__version__)
  299. print()
  300. result = unittest.TextTestRunner(verbosity=2).run(suite)
  301. sys.exit(bool(result.errors + result.failures))