PageRenderTime 52ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/doc/swig/test/testMatrix.py

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