1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381
|
#!/usr/bin/env python3
# System imports
import sys
import unittest
# Import NumPy
import numpy as np
major, minor = [ int(d) for d in np.__version__.split(".")[:2] ]
if major == 0:
BadListError = TypeError
else:
BadListError = ValueError
import Array
######################################################################
class Array1TestCase(unittest.TestCase):
def setUp(self):
self.length = 5
self.array1 = Array.Array1(self.length)
def testConstructor0(self):
"Test Array1 default constructor"
a = Array.Array1()
self.assertTrue(isinstance(a, Array.Array1))
self.assertTrue(len(a) == 0)
def testConstructor1(self):
"Test Array1 length constructor"
self.assertTrue(isinstance(self.array1, Array.Array1))
def testConstructor2(self):
"Test Array1 array constructor"
na = np.arange(self.length)
aa = Array.Array1(na)
self.assertTrue(isinstance(aa, Array.Array1))
def testConstructor3(self):
"Test Array1 copy constructor"
for i in range(self.array1.length()): self.array1[i] = i
arrayCopy = Array.Array1(self.array1)
self.assertTrue(arrayCopy == self.array1)
def testConstructorBad(self):
"Test Array1 length constructor, negative"
self.assertRaises(ValueError, Array.Array1, -4)
def testLength(self):
"Test Array1 length method"
self.assertTrue(self.array1.length() == self.length)
def testLen(self):
"Test Array1 __len__ method"
self.assertTrue(len(self.array1) == self.length)
def testResize0(self):
"Test Array1 resize method, length"
newLen = 2 * self.length
self.array1.resize(newLen)
self.assertTrue(len(self.array1) == newLen)
def testResize1(self):
"Test Array1 resize method, array"
a = np.zeros((2*self.length,), dtype='l')
self.array1.resize(a)
self.assertTrue(len(self.array1) == a.size)
def testResizeBad(self):
"Test Array1 resize method, negative length"
self.assertRaises(ValueError, self.array1.resize, -5)
def testSetGet(self):
"Test Array1 __setitem__, __getitem__ methods"
n = self.length
for i in range(n):
self.array1[i] = i*i
for i in range(n):
self.assertTrue(self.array1[i] == i*i)
def testSetBad1(self):
"Test Array1 __setitem__ method, negative index"
self.assertRaises(IndexError, self.array1.__setitem__, -1, 0)
def testSetBad2(self):
"Test Array1 __setitem__ method, out-of-range index"
self.assertRaises(IndexError, self.array1.__setitem__, self.length+1, 0)
def testGetBad1(self):
"Test Array1 __getitem__ method, negative index"
self.assertRaises(IndexError, self.array1.__getitem__, -1)
def testGetBad2(self):
"Test Array1 __getitem__ method, out-of-range index"
self.assertRaises(IndexError, self.array1.__getitem__, self.length+1)
def testAsString(self):
"Test Array1 asString method"
for i in range(self.array1.length()): self.array1[i] = i+1
self.assertTrue(self.array1.asString() == "[ 1, 2, 3, 4, 5 ]")
def testStr(self):
"Test Array1 __str__ method"
for i in range(self.array1.length()): self.array1[i] = i-2
self.assertTrue(str(self.array1) == "[ -2, -1, 0, 1, 2 ]")
def testView(self):
"Test Array1 view method"
for i in range(self.array1.length()): self.array1[i] = i+1
a = self.array1.view()
self.assertTrue(isinstance(a, np.ndarray))
self.assertTrue(len(a) == self.length)
self.assertTrue((a == [1, 2, 3, 4, 5]).all())
######################################################################
class Array2TestCase(unittest.TestCase):
def setUp(self):
self.nrows = 5
self.ncols = 4
self.array2 = Array.Array2(self.nrows, self.ncols)
def testConstructor0(self):
"Test Array2 default constructor"
a = Array.Array2()
self.assertTrue(isinstance(a, Array.Array2))
self.assertTrue(len(a) == 0)
def testConstructor1(self):
"Test Array2 nrows, ncols constructor"
self.assertTrue(isinstance(self.array2, Array.Array2))
def testConstructor2(self):
"Test Array2 array constructor"
na = np.zeros((3, 4), dtype="l")
aa = Array.Array2(na)
self.assertTrue(isinstance(aa, Array.Array2))
def testConstructor3(self):
"Test Array2 copy constructor"
for i in range(self.nrows):
for j in range(self.ncols):
self.array2[i][j] = i * j
arrayCopy = Array.Array2(self.array2)
self.assertTrue(arrayCopy == self.array2)
def testConstructorBad1(self):
"Test Array2 nrows, ncols constructor, negative nrows"
self.assertRaises(ValueError, Array.Array2, -4, 4)
def testConstructorBad2(self):
"Test Array2 nrows, ncols constructor, negative ncols"
self.assertRaises(ValueError, Array.Array2, 4, -4)
def testNrows(self):
"Test Array2 nrows method"
self.assertTrue(self.array2.nrows() == self.nrows)
def testNcols(self):
"Test Array2 ncols method"
self.assertTrue(self.array2.ncols() == self.ncols)
def testLen(self):
"Test Array2 __len__ method"
self.assertTrue(len(self.array2) == self.nrows*self.ncols)
def testResize0(self):
"Test Array2 resize method, size"
newRows = 2 * self.nrows
newCols = 2 * self.ncols
self.array2.resize(newRows, newCols)
self.assertTrue(len(self.array2) == newRows * newCols)
def testResize1(self):
"Test Array2 resize method, array"
a = np.zeros((2*self.nrows, 2*self.ncols), dtype='l')
self.array2.resize(a)
self.assertTrue(len(self.array2) == a.size)
def testResizeBad1(self):
"Test Array2 resize method, negative nrows"
self.assertRaises(ValueError, self.array2.resize, -5, 5)
def testResizeBad2(self):
"Test Array2 resize method, negative ncols"
self.assertRaises(ValueError, self.array2.resize, 5, -5)
def testSetGet1(self):
"Test Array2 __setitem__, __getitem__ methods"
m = self.nrows
n = self.ncols
array1 = [ ]
a = np.arange(n, dtype="l")
for i in range(m):
array1.append(Array.Array1(i*a))
for i in range(m):
self.array2[i] = array1[i]
for i in range(m):
self.assertTrue(self.array2[i] == array1[i])
def testSetGet2(self):
"Test Array2 chained __setitem__, __getitem__ methods"
m = self.nrows
n = self.ncols
for i in range(m):
for j in range(n):
self.array2[i][j] = i*j
for i in range(m):
for j in range(n):
self.assertTrue(self.array2[i][j] == i*j)
def testSetBad1(self):
"Test Array2 __setitem__ method, negative index"
a = Array.Array1(self.ncols)
self.assertRaises(IndexError, self.array2.__setitem__, -1, a)
def testSetBad2(self):
"Test Array2 __setitem__ method, out-of-range index"
a = Array.Array1(self.ncols)
self.assertRaises(IndexError, self.array2.__setitem__, self.nrows+1, a)
def testGetBad1(self):
"Test Array2 __getitem__ method, negative index"
self.assertRaises(IndexError, self.array2.__getitem__, -1)
def testGetBad2(self):
"Test Array2 __getitem__ method, out-of-range index"
self.assertRaises(IndexError, self.array2.__getitem__, self.nrows+1)
def testAsString(self):
"Test Array2 asString method"
result = """\
[ [ 0, 1, 2, 3 ],
[ 1, 2, 3, 4 ],
[ 2, 3, 4, 5 ],
[ 3, 4, 5, 6 ],
[ 4, 5, 6, 7 ] ]
"""
for i in range(self.nrows):
for j in range(self.ncols):
self.array2[i][j] = i+j
self.assertTrue(self.array2.asString() == result)
def testStr(self):
"Test Array2 __str__ method"
result = """\
[ [ 0, -1, -2, -3 ],
[ 1, 0, -1, -2 ],
[ 2, 1, 0, -1 ],
[ 3, 2, 1, 0 ],
[ 4, 3, 2, 1 ] ]
"""
for i in range(self.nrows):
for j in range(self.ncols):
self.array2[i][j] = i-j
self.assertTrue(str(self.array2) == result)
def testView(self):
"Test Array2 view method"
a = self.array2.view()
self.assertTrue(isinstance(a, np.ndarray))
self.assertTrue(len(a) == self.nrows)
######################################################################
class ArrayZTestCase(unittest.TestCase):
def setUp(self):
self.length = 5
self.array3 = Array.ArrayZ(self.length)
def testConstructor0(self):
"Test ArrayZ default constructor"
a = Array.ArrayZ()
self.assertTrue(isinstance(a, Array.ArrayZ))
self.assertTrue(len(a) == 0)
def testConstructor1(self):
"Test ArrayZ length constructor"
self.assertTrue(isinstance(self.array3, Array.ArrayZ))
def testConstructor2(self):
"Test ArrayZ array constructor"
na = np.arange(self.length, dtype=np.complex128)
aa = Array.ArrayZ(na)
self.assertTrue(isinstance(aa, Array.ArrayZ))
def testConstructor3(self):
"Test ArrayZ copy constructor"
for i in range(self.array3.length()): self.array3[i] = complex(i,-i)
arrayCopy = Array.ArrayZ(self.array3)
self.assertTrue(arrayCopy == self.array3)
def testConstructorBad(self):
"Test ArrayZ length constructor, negative"
self.assertRaises(ValueError, Array.ArrayZ, -4)
def testLength(self):
"Test ArrayZ length method"
self.assertTrue(self.array3.length() == self.length)
def testLen(self):
"Test ArrayZ __len__ method"
self.assertTrue(len(self.array3) == self.length)
def testResize0(self):
"Test ArrayZ resize method, length"
newLen = 2 * self.length
self.array3.resize(newLen)
self.assertTrue(len(self.array3) == newLen)
def testResize1(self):
"Test ArrayZ resize method, array"
a = np.zeros((2*self.length,), dtype=np.complex128)
self.array3.resize(a)
self.assertTrue(len(self.array3) == a.size)
def testResizeBad(self):
"Test ArrayZ resize method, negative length"
self.assertRaises(ValueError, self.array3.resize, -5)
def testSetGet(self):
"Test ArrayZ __setitem__, __getitem__ methods"
n = self.length
for i in range(n):
self.array3[i] = i*i
for i in range(n):
self.assertTrue(self.array3[i] == i*i)
def testSetBad1(self):
"Test ArrayZ __setitem__ method, negative index"
self.assertRaises(IndexError, self.array3.__setitem__, -1, 0)
def testSetBad2(self):
"Test ArrayZ __setitem__ method, out-of-range index"
self.assertRaises(IndexError, self.array3.__setitem__, self.length+1, 0)
def testGetBad1(self):
"Test ArrayZ __getitem__ method, negative index"
self.assertRaises(IndexError, self.array3.__getitem__, -1)
def testGetBad2(self):
"Test ArrayZ __getitem__ method, out-of-range index"
self.assertRaises(IndexError, self.array3.__getitem__, self.length+1)
def testAsString(self):
"Test ArrayZ asString method"
for i in range(self.array3.length()): self.array3[i] = complex(i+1,-i-1)
self.assertTrue(self.array3.asString() == "[ (1,-1), (2,-2), (3,-3), (4,-4), (5,-5) ]")
def testStr(self):
"Test ArrayZ __str__ method"
for i in range(self.array3.length()): self.array3[i] = complex(i-2,(i-2)*2)
self.assertTrue(str(self.array3) == "[ (-2,-4), (-1,-2), (0,0), (1,2), (2,4) ]")
def testView(self):
"Test ArrayZ view method"
for i in range(self.array3.length()): self.array3[i] = complex(i+1,i+2)
a = self.array3.view()
self.assertTrue(isinstance(a, np.ndarray))
self.assertTrue(len(a) == self.length)
self.assertTrue((a == [1+2j, 2+3j, 3+4j, 4+5j, 5+6j]).all())
######################################################################
if __name__ == "__main__":
# Build the test suite
suite = unittest.TestSuite()
suite.addTest(unittest.makeSuite(Array1TestCase))
suite.addTest(unittest.makeSuite(Array2TestCase))
suite.addTest(unittest.makeSuite(ArrayZTestCase))
# Execute the test suite
print("Testing Classes of Module Array")
print("NumPy version", np.__version__)
print()
result = unittest.TextTestRunner(verbosity=2).run(suite)
sys.exit(bool(result.errors + result.failures))
|