~ubuntu-branches/ubuntu/feisty/python-numpy/feisty

« back to all changes in this revision

Viewing changes to numpy/doc/swig/testSeries.py

  • Committer: Bazaar Package Importer
  • Author(s): Matthias Klose
  • Date: 2006-07-12 10:00:24 UTC
  • Revision ID: james.westby@ubuntu.com-20060712100024-5lw9q2yczlisqcrt
Tags: upstream-0.9.8
ImportĀ upstreamĀ versionĀ 0.9.8

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#! /usr/bin/env python
 
2
 
 
3
# System imports
 
4
from   distutils.util import get_platform
 
5
import os
 
6
import sys
 
7
import unittest
 
8
 
 
9
import numpy as N
 
10
 
 
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))
 
15
import Series
 
16
 
 
17
######################################################################
 
18
 
 
19
class SeriesTestCase(unittest.TestCase):
 
20
 
 
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)
 
26
 
 
27
    def testShortProd(self):
 
28
        "Test the shortProd function"
 
29
        self.assertEquals(Series.shortProd([1,2,3,4]), 24)
 
30
 
 
31
    ######################################################
 
32
    ### Test functions that take 1D arrays of type INT ###
 
33
    def testIntSum(self):
 
34
        "Test the intSum function"
 
35
        self.assertEquals(Series.intSum([1,2,3,4]), 10)
 
36
 
 
37
    def testIntSumBadContainer(self):
 
38
        "Test the intSum function with an invalid list"
 
39
        self.assertRaises(TypeError, Series.intSum, [1,2,"junk"])
 
40
 
 
41
    def testIntProd(self):
 
42
        "Test the intProd function"
 
43
        self.assertEquals(Series.intProd([1,2,3,4]), 24)
 
44
 
 
45
    def testIntProdNonContainer(self):
 
46
        "Test the intProd function with None"
 
47
        self.assertRaises(TypeError, Series.intProd, None)
 
48
 
 
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]))
 
54
 
 
55
    def testIntZerosNonArray(self):
 
56
        "Test the intZeros function with an integer"
 
57
        self.assertRaises(TypeError, Series.intZeros, 5)
 
58
 
 
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]))
 
64
 
 
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]))
 
70
 
 
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)
 
76
 
 
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]])
 
80
 
 
81
    def testLongProd(self):
 
82
        "Test the longProd function"
 
83
        self.assertEquals(Series.longProd([1,2,3,4]), 24)
 
84
 
 
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)
 
90
 
 
91
    def testFloatSumBadContainer(self):
 
92
        "Test the floatSum function with a dictionary"
 
93
        self.assertRaises(TypeError, Series.floatSum, {"key":"value"})
 
94
 
 
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)
 
98
 
 
99
    def testFloatProdBadContainer(self):
 
100
        "Test the floatProd function with an invalid list"
 
101
        self.assertRaises(TypeError, Series.floatProd, [3.14, "pi"])
 
102
 
 
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)
 
108
 
 
109
    def testDoubleSumNonContainer(self):
 
110
        "Test the doubleSum function with None"
 
111
        self.assertRaises(TypeError, Series.doubleSum, None)
 
112
 
 
113
    def testDoubleProd(self):
 
114
        "Test the doubleProd function"
 
115
        self.assertEquals(Series.doubleProd((1,2.718,3,4)), 32.616)
 
116
 
 
117
    def testDoubleProdBadContainer(self):
 
118
        "Test the doubleProd function with an invalid list"
 
119
        self.assertRaises(TypeError, Series.doubleProd, [3.14, "pi"])
 
120
 
 
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.]))
 
126
 
 
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.]))
 
132
 
 
133
    def testDoubleOnesNonArray(self):
 
134
        "Test the doubleOnes function with a list"
 
135
        self.assertRaises(TypeError, Series.doubleOnes, [True, 0, 2.718, "pi"])
 
136
 
 
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.]))
 
142
 
 
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)
 
149
 
 
150
    def testIntMaxNonContainer(self):
 
151
        "Test the intMax function with None"
 
152
        self.assertRaises(TypeError, Series.intMax, None)
 
153
 
 
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]]))
 
159
 
 
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)
 
164
 
 
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])
 
168
 
 
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]]))
 
174
 
 
175
######################################################################
 
176
 
 
177
if __name__ == "__main__":
 
178
 
 
179
    # Build the test suite
 
180
    suite = unittest.TestSuite()
 
181
    suite.addTest(unittest.makeSuite(SeriesTestCase))
 
182
 
 
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))