4
from distutils.util import get_platform
11
# Add the distutils-generated build directory to the python search path and then
12
# import the extension module
13
libDir = "lib.%s-%s" % (get_platform(), sys.version[:3])
14
sys.path.insert(0,os.path.join("build", libDir))
17
######################################################################
19
class SeriesTestCase(unittest.TestCase):
21
########################################################
22
### Test functions that take 1D arrays of type SHORT ###
23
def testShortSum(self):
24
"Test the shortSum function"
25
self.assertEquals(Series.shortSum([1,2,3,4]), 10)
27
def testShortProd(self):
28
"Test the shortProd function"
29
self.assertEquals(Series.shortProd([1,2,3,4]), 24)
31
######################################################
32
### Test functions that take 1D arrays of type INT ###
34
"Test the intSum function"
35
self.assertEquals(Series.intSum([1,2,3,4]), 10)
37
def testIntSumBadContainer(self):
38
"Test the intSum function with an invalid list"
39
self.assertRaises(TypeError, Series.intSum, [1,2,"junk"])
41
def testIntProd(self):
42
"Test the intProd function"
43
self.assertEquals(Series.intProd([1,2,3,4]), 24)
45
def testIntProdNonContainer(self):
46
"Test the intProd function with None"
47
self.assertRaises(TypeError, Series.intProd, None)
49
def testIntZeros(self):
50
"Test the intZeros function"
51
myArray = N.ones(5,'i')
52
Series.intZeros(myArray)
53
N.testing.assert_array_equal(myArray, N.array([0,0,0,0,0]))
55
def testIntZerosNonArray(self):
56
"Test the intZeros function with an integer"
57
self.assertRaises(TypeError, Series.intZeros, 5)
59
def testIntOnes(self):
60
"Test the intOnes function"
61
myArray = N.zeros(5,'i')
62
Series.intOnes(myArray)
63
N.testing.assert_array_equal(myArray, N.array([1,1,1,1,1]))
65
def testIntNegate(self):
66
"Test the intNegate function"
67
myArray = N.arange(5,dtype='i')
68
Series.intNegate(myArray)
69
N.testing.assert_array_equal(myArray, N.array([0,-1,-2,-3,-4]))
71
#######################################################
72
### Test functions that take 1D arrays of type LONG ###
73
def testLongSum(self):
74
"Test the longSum function"
75
self.assertEquals(Series.longSum([1,2,3,4]), 10)
77
def testLongSumMultiDimensional(self):
78
"Test the longSum function with multi-dimensional array"
79
self.assertRaises(TypeError, Series.longSum, [[1,2,3],[9,8,7]])
81
def testLongProd(self):
82
"Test the longProd function"
83
self.assertEquals(Series.longProd([1,2,3,4]), 24)
85
########################################################
86
### Test functions that take 1D arrays of type FLOAT ###
87
def testFloatSum(self):
88
"Test the floatSum function (to 5 decimal places)"
89
self.assertAlmostEquals(Series.floatSum([1,2,3.14,4]), 10.14, 5)
91
def testFloatSumBadContainer(self):
92
"Test the floatSum function with a dictionary"
93
self.assertRaises(TypeError, Series.floatSum, {"key":"value"})
95
def testFloatProd(self):
96
"Test the floatProd function (to 5 decimal places)"
97
self.assertAlmostEquals(Series.floatProd((1,2.718,3,4)), 32.616, 5)
99
def testFloatProdBadContainer(self):
100
"Test the floatProd function with an invalid list"
101
self.assertRaises(TypeError, Series.floatProd, [3.14, "pi"])
103
#########################################################
104
### Test functions that take 1D arrays of type DOUBLE ###
105
def testDoubleSum(self):
106
"Test the doubleSum function"
107
self.assertEquals(Series.doubleSum([1,2,3.14,4]), 10.14)
109
def testDoubleSumNonContainer(self):
110
"Test the doubleSum function with None"
111
self.assertRaises(TypeError, Series.doubleSum, None)
113
def testDoubleProd(self):
114
"Test the doubleProd function"
115
self.assertEquals(Series.doubleProd((1,2.718,3,4)), 32.616)
117
def testDoubleProdBadContainer(self):
118
"Test the doubleProd function with an invalid list"
119
self.assertRaises(TypeError, Series.doubleProd, [3.14, "pi"])
121
def testDoubleZeros(self):
122
"Test the doubleZeros function"
123
myArray = N.ones(5,'d')
124
Series.doubleZeros(myArray)
125
N.testing.assert_array_equal(myArray, N.array([0.,0.,0.,0.,0.]))
127
def testDoubleOnes(self):
128
"Test the doubleOnes function"
129
myArray = N.zeros(5,'d')
130
Series.doubleOnes(myArray)
131
N.testing.assert_array_equal(myArray, N.array([1.,1.,1.,1.,1.]))
133
def testDoubleOnesNonArray(self):
134
"Test the doubleOnes function with a list"
135
self.assertRaises(TypeError, Series.doubleOnes, [True, 0, 2.718, "pi"])
137
def testDoubleNegate(self):
138
"Test the doubleNegate function"
139
myArray = N.arange(5) * 1.0
140
Series.doubleNegate(myArray)
141
N.testing.assert_array_equal(myArray, N.array([0.,-1.,-2.,-3.,-4.]))
143
#########################################################
144
### Test functions that take 2D arrays of type DOUBLE ###
145
def testIntMax(self):
146
"Test the intMax function"
147
matrix = [[6,-5,4],[-3,2,-1]]
148
self.assertEquals(Series.intMax(matrix), 6)
150
def testIntMaxNonContainer(self):
151
"Test the intMax function with None"
152
self.assertRaises(TypeError, Series.intMax, None)
154
def testIntFloor(self):
155
"Test the intFloor function"
156
matrix = N.array([[10,-2],[-6,7]])
157
Series.intFloor(matrix,0)
158
N.testing.assert_array_equal(matrix, N.array([[10,0],[0,7]]))
160
def testDoubleMax(self):
161
"Test the doubleMax function"
162
matrix = [[-6,5,-4],[3.14,-2.718,1]]
163
self.assertEquals(Series.doubleMax(matrix), 5.0)
165
def testDoubleMaxWrongDim(self):
166
"Test the doubleMax function with a 1D array"
167
self.assertRaises(TypeError, Series.doubleMax, [0.0, -1, 2.718, -3.14])
169
def testDoubleFloor(self):
170
"Test the doubleFloor function"
171
matrix = N.array([[10,-2.718],[-6,3.14]])
172
Series.doubleFloor(matrix,5.0)
173
N.testing.assert_array_equal(matrix, N.array([[10.0,0],[0,0]]))
175
######################################################################
177
if __name__ == "__main__":
179
# Build the test suite
180
suite = unittest.TestSuite()
181
suite.addTest(unittest.makeSuite(SeriesTestCase))
183
# Execute the test suite
184
print "Testing Module Series\n"
185
result = unittest.TextTestRunner(verbosity=2).run(suite)
186
sys.exit(len(result.errors) + len(result.failures))