source: inundation/utilities/test_numerical_tools.py @ 2309

Last change on this file since 2309 was 1910, checked in by ole, 19 years ago

Made utilities a Python package
Added numerical_tools (and test) containing ensure_numeric from the old util module.
Added test_polygon.py

File size: 1.8 KB
RevLine 
[1910]1#!/usr/bin/env python
2
3
4import unittest
5from Numeric import zeros, array, allclose
6from math import sqrt, pi
7
8from numerical_tools import *
9
10def test_function(x, y):
11    return x+y
12
13class Test_Numerical_Tools(unittest.TestCase):
14    def setUp(self):
15        pass
16
17    def tearDown(self):
18        pass
19
20
21
22       
23    def test_ensure_numeric(self):
24        from numerical_tools import ensure_numeric
25        from Numeric import ArrayType, Float, array
26
27        A = [1,2,3,4]
28        B = ensure_numeric(A)
29        assert type(B) == ArrayType
30        assert B.typecode() == 'l'
31        assert B[0] == 1 and B[1] == 2 and B[2] == 3 and B[3] == 4
32
33
34        A = [1,2,3.14,4]
35        B = ensure_numeric(A)
36        assert type(B) == ArrayType
37        assert B.typecode() == 'd'
38        assert B[0] == 1 and B[1] == 2 and B[2] == 3.14 and B[3] == 4
39
40
41        A = [1,2,3,4]
42        B = ensure_numeric(A, Float)
43        assert type(B) == ArrayType
44        assert B.typecode() == 'd'
45        assert B[0] == 1.0 and B[1] == 2.0 and B[2] == 3.0 and B[3] == 4.0
46
47
48        A = [1,2,3,4]
49        B = ensure_numeric(A, Float)
50        assert type(B) == ArrayType
51        assert B.typecode() == 'd'
52        assert B[0] == 1.0 and B[1] == 2.0 and B[2] == 3.0 and B[3] == 4.0
53
54
55        A = array([1,2,3,4])
56        B = ensure_numeric(A)
57        assert type(B) == ArrayType
58        assert B.typecode() == 'l'       
59        assert A == B   
60        assert A is B   #Same object
61
62
63        A = array([1,2,3,4])
64        B = ensure_numeric(A, Float)
65        assert type(B) == ArrayType
66        assert B.typecode() == 'd'       
67        assert A == B   
68        assert A is not B   #Not the same object       
69
70
71#-------------------------------------------------------------
72if __name__ == "__main__":
73    suite = unittest.makeSuite(Test_Numerical_Tools,'test')
74    runner = unittest.TextTestRunner()
75    runner.run(suite)
Note: See TracBrowser for help on using the repository browser.