PageRenderTime 94ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/doc/swig/test/testArray.py

https://github.com/WeatherGod/numpy
Python | 284 lines | 227 code | 47 blank | 10 comment | 18 complexity | 53447ba96b50051f02c913ac134f74b1 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:
  12. BadListError = TypeError
  13. else:
  14. BadListError = ValueError
  15. import Array
  16. ######################################################################
  17. class Array1TestCase(unittest.TestCase):
  18. def setUp(self):
  19. self.length = 5
  20. self.array1 = Array.Array1(self.length)
  21. def testConstructor0(self):
  22. "Test Array1 default constructor"
  23. a = Array.Array1()
  24. self.failUnless(isinstance(a, Array.Array1))
  25. self.failUnless(len(a) == 0)
  26. def testConstructor1(self):
  27. "Test Array1 length constructor"
  28. self.failUnless(isinstance(self.array1, Array.Array1))
  29. def testConstructor2(self):
  30. "Test Array1 array constructor"
  31. na = np.arange(self.length)
  32. aa = Array.Array1(na)
  33. self.failUnless(isinstance(aa, Array.Array1))
  34. def testConstructor3(self):
  35. "Test Array1 copy constructor"
  36. for i in range(self.array1.length()): self.array1[i] = i
  37. arrayCopy = Array.Array1(self.array1)
  38. self.failUnless(arrayCopy == self.array1)
  39. def testConstructorBad(self):
  40. "Test Array1 length constructor, negative"
  41. self.assertRaises(ValueError, Array.Array1, -4)
  42. def testLength(self):
  43. "Test Array1 length method"
  44. self.failUnless(self.array1.length() == self.length)
  45. def testLen(self):
  46. "Test Array1 __len__ method"
  47. self.failUnless(len(self.array1) == self.length)
  48. def testResize0(self):
  49. "Test Array1 resize method, length"
  50. newLen = 2 * self.length
  51. self.array1.resize(newLen)
  52. self.failUnless(len(self.array1) == newLen)
  53. def testResize1(self):
  54. "Test Array1 resize method, array"
  55. a = np.zeros((2*self.length,), dtype='l')
  56. self.array1.resize(a)
  57. self.failUnless(len(self.array1) == len(a))
  58. def testResizeBad(self):
  59. "Test Array1 resize method, negative length"
  60. self.assertRaises(ValueError, self.array1.resize, -5)
  61. def testSetGet(self):
  62. "Test Array1 __setitem__, __getitem__ methods"
  63. n = self.length
  64. for i in range(n):
  65. self.array1[i] = i*i
  66. for i in range(n):
  67. self.failUnless(self.array1[i] == i*i)
  68. def testSetBad1(self):
  69. "Test Array1 __setitem__ method, negative index"
  70. self.assertRaises(IndexError, self.array1.__setitem__, -1, 0)
  71. def testSetBad2(self):
  72. "Test Array1 __setitem__ method, out-of-range index"
  73. self.assertRaises(IndexError, self.array1.__setitem__, self.length+1, 0)
  74. def testGetBad1(self):
  75. "Test Array1 __getitem__ method, negative index"
  76. self.assertRaises(IndexError, self.array1.__getitem__, -1)
  77. def testGetBad2(self):
  78. "Test Array1 __getitem__ method, out-of-range index"
  79. self.assertRaises(IndexError, self.array1.__getitem__, self.length+1)
  80. def testAsString(self):
  81. "Test Array1 asString method"
  82. for i in range(self.array1.length()): self.array1[i] = i+1
  83. self.failUnless(self.array1.asString() == "[ 1, 2, 3, 4, 5 ]")
  84. def testStr(self):
  85. "Test Array1 __str__ method"
  86. for i in range(self.array1.length()): self.array1[i] = i-2
  87. self.failUnless(str(self.array1) == "[ -2, -1, 0, 1, 2 ]")
  88. def testView(self):
  89. "Test Array1 view method"
  90. for i in range(self.array1.length()): self.array1[i] = i+1
  91. a = self.array1.view()
  92. self.failUnless(isinstance(a, np.ndarray))
  93. self.failUnless(len(a) == self.length)
  94. self.failUnless((a == [1,2,3,4,5]).all())
  95. ######################################################################
  96. class Array2TestCase(unittest.TestCase):
  97. def setUp(self):
  98. self.nrows = 5
  99. self.ncols = 4
  100. self.array2 = Array.Array2(self.nrows, self.ncols)
  101. def testConstructor0(self):
  102. "Test Array2 default constructor"
  103. a = Array.Array2()
  104. self.failUnless(isinstance(a, Array.Array2))
  105. self.failUnless(len(a) == 0)
  106. def testConstructor1(self):
  107. "Test Array2 nrows, ncols constructor"
  108. self.failUnless(isinstance(self.array2, Array.Array2))
  109. def testConstructor2(self):
  110. "Test Array2 array constructor"
  111. na = np.zeros((3,4), dtype="l")
  112. aa = Array.Array2(na)
  113. self.failUnless(isinstance(aa, Array.Array2))
  114. def testConstructor3(self):
  115. "Test Array2 copy constructor"
  116. for i in range(self.nrows):
  117. for j in range(self.ncols):
  118. self.array2[i][j] = i * j
  119. arrayCopy = Array.Array2(self.array2)
  120. self.failUnless(arrayCopy == self.array2)
  121. def testConstructorBad1(self):
  122. "Test Array2 nrows, ncols constructor, negative nrows"
  123. self.assertRaises(ValueError, Array.Array2, -4, 4)
  124. def testConstructorBad2(self):
  125. "Test Array2 nrows, ncols constructor, negative ncols"
  126. self.assertRaises(ValueError, Array.Array2, 4, -4)
  127. def testNrows(self):
  128. "Test Array2 nrows method"
  129. self.failUnless(self.array2.nrows() == self.nrows)
  130. def testNcols(self):
  131. "Test Array2 ncols method"
  132. self.failUnless(self.array2.ncols() == self.ncols)
  133. def testLen(self):
  134. "Test Array2 __len__ method"
  135. self.failUnless(len(self.array2) == self.nrows*self.ncols)
  136. def testResize0(self):
  137. "Test Array2 resize method, size"
  138. newRows = 2 * self.nrows
  139. newCols = 2 * self.ncols
  140. self.array2.resize(newRows, newCols)
  141. self.failUnless(len(self.array2) == newRows * newCols)
  142. #def testResize1(self):
  143. # "Test Array2 resize method, array"
  144. # a = np.zeros((2*self.nrows, 2*self.ncols), dtype='l')
  145. # self.array2.resize(a)
  146. # self.failUnless(len(self.array2) == len(a))
  147. def testResizeBad1(self):
  148. "Test Array2 resize method, negative nrows"
  149. self.assertRaises(ValueError, self.array2.resize, -5, 5)
  150. def testResizeBad2(self):
  151. "Test Array2 resize method, negative ncols"
  152. self.assertRaises(ValueError, self.array2.resize, 5, -5)
  153. def testSetGet1(self):
  154. "Test Array2 __setitem__, __getitem__ methods"
  155. m = self.nrows
  156. n = self.ncols
  157. array1 = [ ]
  158. a = np.arange(n, dtype="l")
  159. for i in range(m):
  160. array1.append(Array.Array1(i*a))
  161. for i in range(m):
  162. self.array2[i] = array1[i]
  163. for i in range(m):
  164. self.failUnless(self.array2[i] == array1[i])
  165. def testSetGet2(self):
  166. "Test Array2 chained __setitem__, __getitem__ methods"
  167. m = self.nrows
  168. n = self.ncols
  169. for i in range(m):
  170. for j in range(n):
  171. self.array2[i][j] = i*j
  172. for i in range(m):
  173. for j in range(n):
  174. self.failUnless(self.array2[i][j] == i*j)
  175. def testSetBad1(self):
  176. "Test Array2 __setitem__ method, negative index"
  177. a = Array.Array1(self.ncols)
  178. self.assertRaises(IndexError, self.array2.__setitem__, -1, a)
  179. def testSetBad2(self):
  180. "Test Array2 __setitem__ method, out-of-range index"
  181. a = Array.Array1(self.ncols)
  182. self.assertRaises(IndexError, self.array2.__setitem__, self.nrows+1, a)
  183. def testGetBad1(self):
  184. "Test Array2 __getitem__ method, negative index"
  185. self.assertRaises(IndexError, self.array2.__getitem__, -1)
  186. def testGetBad2(self):
  187. "Test Array2 __getitem__ method, out-of-range index"
  188. self.assertRaises(IndexError, self.array2.__getitem__, self.nrows+1)
  189. def testAsString(self):
  190. "Test Array2 asString method"
  191. result = """\
  192. [ [ 0, 1, 2, 3 ],
  193. [ 1, 2, 3, 4 ],
  194. [ 2, 3, 4, 5 ],
  195. [ 3, 4, 5, 6 ],
  196. [ 4, 5, 6, 7 ] ]
  197. """
  198. for i in range(self.nrows):
  199. for j in range(self.ncols):
  200. self.array2[i][j] = i+j
  201. self.failUnless(self.array2.asString() == result)
  202. def testStr(self):
  203. "Test Array2 __str__ method"
  204. result = """\
  205. [ [ 0, -1, -2, -3 ],
  206. [ 1, 0, -1, -2 ],
  207. [ 2, 1, 0, -1 ],
  208. [ 3, 2, 1, 0 ],
  209. [ 4, 3, 2, 1 ] ]
  210. """
  211. for i in range(self.nrows):
  212. for j in range(self.ncols):
  213. self.array2[i][j] = i-j
  214. self.failUnless(str(self.array2) == result)
  215. def testView(self):
  216. "Test Array2 view method"
  217. a = self.array2.view()
  218. self.failUnless(isinstance(a, np.ndarray))
  219. self.failUnless(len(a) == self.nrows)
  220. ######################################################################
  221. if __name__ == "__main__":
  222. # Build the test suite
  223. suite = unittest.TestSuite()
  224. suite.addTest(unittest.makeSuite(Array1TestCase))
  225. suite.addTest(unittest.makeSuite(Array2TestCase))
  226. # Execute the test suite
  227. print("Testing Classes of Module Array")
  228. print("NumPy version", np.__version__)
  229. print()
  230. result = unittest.TextTestRunner(verbosity=2).run(suite)
  231. sys.exit(len(result.errors) + len(result.failures))