Debian Codegraph @

tools/swig/test/testFarray.py

raw
1 #!/usr/bin/env python3
2 import os
3 import sys
4 import unittest
5 from distutils.util import get_platform
6
7 import numpy as np
8
9 major, minor = [int(d) for d in np.__version__.split(".")[:2]]
10 if major == 0:
11 BadListError = TypeError
12 else:
13 BadListError = ValueError
14
15 # Add the distutils-generated build directory to the python search path and then
16 # import the extension module
17 libDir = f"lib.{get_platform()}-{sys.version_info[0]}.{sys.version_info[1]}"
18 sys.path.insert(0, os.path.join("build", libDir))
19 import Farray
20
21 ######################################################################
22
23 class FarrayTestCase(unittest.TestCase):
24
25 def setUp(self):
26 self.nrows = 5
27 self.ncols = 4
28 self.array = Farray.Farray(self.nrows, self.ncols)
29
30 def testConstructor1(self):
31 "Test Farray size constructor"
32 self.assertTrue(isinstance(self.array, Farray.Farray))
33
34 def testConstructor2(self):
35 "Test Farray copy constructor"
36 for i in range(self.nrows):
37 for j in range(self.ncols):
38 self.array[i, j] = i + j
39 arrayCopy = Farray.Farray(self.array)
40 self.assertTrue(arrayCopy == self.array)
41
42 def testConstructorBad1(self):
43 "Test Farray size constructor, negative nrows"
44 self.assertRaises(ValueError, Farray.Farray, -4, 4)
45
46 def testConstructorBad2(self):
47 "Test Farray size constructor, negative ncols"
48 self.assertRaises(ValueError, Farray.Farray, 4, -4)
49
50 def testNrows(self):
51 "Test Farray nrows method"
52 self.assertTrue(self.array.nrows() == self.nrows)
53
54 def testNcols(self):
55 "Test Farray ncols method"
56 self.assertTrue(self.array.ncols() == self.ncols)
57
58 def testLen(self):
59 "Test Farray __len__ method"
60 self.assertTrue(len(self.array) == self.nrows * self.ncols)
61
62 def testSetGet(self):
63 "Test Farray __setitem__, __getitem__ methods"
64 m = self.nrows
65 n = self.ncols
66 for i in range(m):
67 for j in range(n):
68 self.array[i, j] = i * j
69 for i in range(m):
70 for j in range(n):
71 self.assertTrue(self.array[i, j] == i * j)
72
73 def testSetBad1(self):
74 "Test Farray __setitem__ method, negative row"
75 self.assertRaises(IndexError, self.array.__setitem__, (-1, 3), 0)
76
77 def testSetBad2(self):
78 "Test Farray __setitem__ method, negative col"
79 self.assertRaises(IndexError, self.array.__setitem__, (1, -3), 0)
80
81 def testSetBad3(self):
82 "Test Farray __setitem__ method, out-of-range row"
83 self.assertRaises(IndexError, self.array.__setitem__, (self.nrows + 1, 0), 0)
84
85 def testSetBad4(self):
86 "Test Farray __setitem__ method, out-of-range col"
87 self.assertRaises(IndexError, self.array.__setitem__, (0, self.ncols + 1), 0)
88
89 def testGetBad1(self):
90 "Test Farray __getitem__ method, negative row"
91 self.assertRaises(IndexError, self.array.__getitem__, (-1, 3))
92
93 def testGetBad2(self):
94 "Test Farray __getitem__ method, negative col"
95 self.assertRaises(IndexError, self.array.__getitem__, (1, -3))
96
97 def testGetBad3(self):
98 "Test Farray __getitem__ method, out-of-range row"
99 self.assertRaises(IndexError, self.array.__getitem__, (self.nrows + 1, 0))
100
101 def testGetBad4(self):
102 "Test Farray __getitem__ method, out-of-range col"
103 self.assertRaises(IndexError, self.array.__getitem__, (0, self.ncols + 1))
104
105 def testAsString(self):
106 "Test Farray asString method"
107 result = """\
108 [ [ 0, 1, 2, 3 ],
109 [ 1, 2, 3, 4 ],
110 [ 2, 3, 4, 5 ],
111 [ 3, 4, 5, 6 ],
112 [ 4, 5, 6, 7 ] ]
113 """
114 for i in range(self.nrows):
115 for j in range(self.ncols):
116 self.array[i, j] = i + j
117 self.assertTrue(self.array.asString() == result)
118
119 def testStr(self):
120 "Test Farray __str__ method"
121 result = """\
122 [ [ 0, -1, -2, -3 ],
123 [ 1, 0, -1, -2 ],
124 [ 2, 1, 0, -1 ],
125 [ 3, 2, 1, 0 ],
126 [ 4, 3, 2, 1 ] ]
127 """
128 for i in range(self.nrows):
129 for j in range(self.ncols):
130 self.array[i, j] = i - j
131 self.assertTrue(str(self.array) == result)
132
133 def testView(self):
134 "Test Farray view method"
135 for i in range(self.nrows):
136 for j in range(self.ncols):
137 self.array[i, j] = i + j
138 a = self.array.view()
139 self.assertTrue(isinstance(a, np.ndarray))
140 self.assertTrue(a.flags.f_contiguous)
141 for i in range(self.nrows):
142 for j in range(self.ncols):
143 self.assertTrue(a[i, j] == i + j)
144
145 ######################################################################
146
147
148 if __name__ == "__main__":
149
150 # Build the test suite
151 suite = unittest.TestSuite()
152 suite.addTest(unittest.TestLoader().loadTestsFromTestCase(FarrayTestCase))
153
154 # Execute the test suite
155 print("Testing Classes of Module Farray")
156 print("NumPy version", np.__version__)
157 print()
158 result = unittest.TextTestRunner(verbosity=2).run(suite)
159 sys.exit(bool(result.errors + result.failures))