1 | #!/usr/bin/env python |
---|
2 | # |
---|
3 | |
---|
4 | import unittest |
---|
5 | from Numeric import zeros, array, allclose |
---|
6 | from math import sqrt, pi |
---|
7 | |
---|
8 | from util import * |
---|
9 | from config import epsilon |
---|
10 | |
---|
11 | class TestCase(unittest.TestCase): |
---|
12 | def setUp(self): |
---|
13 | pass |
---|
14 | |
---|
15 | def tearDown(self): |
---|
16 | pass |
---|
17 | |
---|
18 | |
---|
19 | def test_gradient(self): |
---|
20 | x0 = 0.0; y0 = 0.0; z0 = 0.0 |
---|
21 | x1 = 1.0; y1 = 0.0; z1 = -1.0 |
---|
22 | x2 = 0.0; y2 = 1.0; z2 = 0.0 |
---|
23 | |
---|
24 | zx, zy = gradient(x0, y0, x1, y1, x2, y2, array([z0]), array([z1]), array([z2])) |
---|
25 | |
---|
26 | assert zx == -1.0 |
---|
27 | assert zy == 0.0 |
---|
28 | |
---|
29 | |
---|
30 | def test_rotate(self): |
---|
31 | normal = array([0.0,-1.0]) |
---|
32 | |
---|
33 | q = array([1.0,2.0,3.0]) |
---|
34 | |
---|
35 | r = rotate(q, normal, direction = 1) |
---|
36 | assert r[0] == 1 |
---|
37 | assert r[1] == -3 |
---|
38 | assert r[2] == 2 |
---|
39 | |
---|
40 | w = rotate(r, normal, direction = -1) |
---|
41 | assert allclose(w, q) |
---|
42 | |
---|
43 | |
---|
44 | |
---|
45 | def test_gradient2(self): |
---|
46 | |
---|
47 | from util import gradient |
---|
48 | x0 = 2.0/3; y0 = 2.0/3 |
---|
49 | x1= 8.0/3; y1 = 2.0/3 |
---|
50 | x2 = 2.0/3; y2 = 8.0/3 |
---|
51 | |
---|
52 | q0 = array([2.0+2.0/3]) |
---|
53 | q1 = array([8.0+2.0/3]) |
---|
54 | q2 = array([2.0+8.0/3]) |
---|
55 | |
---|
56 | #Gradient of fitted pwl surface |
---|
57 | a, b = gradient(x0, y0, x1, y1, x2, y2, q0, q1, q2) |
---|
58 | |
---|
59 | assert abs(a[0] - 3.0) < epsilon |
---|
60 | assert abs(b[0] - 1.0) < epsilon |
---|
61 | |
---|
62 | |
---|
63 | |
---|
64 | def test_that_C_extension_compiles(self): |
---|
65 | FN = 'util_ext.c' |
---|
66 | try: |
---|
67 | import util_ext |
---|
68 | except: |
---|
69 | from pytools import compile |
---|
70 | |
---|
71 | try: |
---|
72 | compile(FN) |
---|
73 | except: |
---|
74 | raise 'Could not compile %s' %FN |
---|
75 | else: |
---|
76 | import util_ext |
---|
77 | |
---|
78 | |
---|
79 | def test_gradient_C_extension(self): |
---|
80 | from util_ext import gradient as gradient_c |
---|
81 | |
---|
82 | x0 = 2.0/3; y0 = 2.0/3 |
---|
83 | x1= 8.0/3; y1 = 2.0/3 |
---|
84 | x2 = 2.0/3; y2 = 8.0/3 |
---|
85 | |
---|
86 | q0 = array([2.0+2.0/3]) |
---|
87 | q1 = array([8.0+2.0/3]) |
---|
88 | q2 = array([2.0+8.0/3]) |
---|
89 | |
---|
90 | #Gradient of fitted pwl surface |
---|
91 | a, b = gradient_c(x0, y0, x1, y1, x2, y2, q0, q1, q2) |
---|
92 | |
---|
93 | assert abs(a[0] - 3.0) < epsilon |
---|
94 | assert abs(b[0] - 1.0) < epsilon |
---|
95 | |
---|
96 | def test_gradient_C_extension_scalars(self): |
---|
97 | from util_ext import gradient as gradient_c |
---|
98 | |
---|
99 | from RandomArray import uniform, seed |
---|
100 | seed(17, 53) |
---|
101 | |
---|
102 | x0, x1, x2, y0, y1, y2 = uniform(0.0,3.0,6) |
---|
103 | |
---|
104 | q0 = 10.0 |
---|
105 | q1 = 3.0 |
---|
106 | q2 = 7.0 |
---|
107 | |
---|
108 | |
---|
109 | #Gradient of fitted pwl surface |
---|
110 | from util import gradient_python |
---|
111 | a_ref, b_ref = gradient_python(x0, y0, x1, y1, x2, y2, q0, q1, q2) |
---|
112 | |
---|
113 | #print a_ref, b_ref |
---|
114 | try: |
---|
115 | a, b = gradient_c(x0, y0, x1, y1, x2, y2, q0, q1, q2) |
---|
116 | except TypeError: |
---|
117 | pass |
---|
118 | else: |
---|
119 | raise 'gradient c-extension should throw exception when q''s are scalars' |
---|
120 | |
---|
121 | |
---|
122 | def test_gradient_C_extension3(self): |
---|
123 | from util_ext import gradient as gradient_c |
---|
124 | |
---|
125 | from RandomArray import uniform, seed |
---|
126 | seed(17, 53) |
---|
127 | |
---|
128 | x0, x1, x2, y0, y1, y2 = uniform(0.0,3.0,6) |
---|
129 | |
---|
130 | q0 = uniform(0.0, 10.0, 4) |
---|
131 | q1 = uniform(1.0, 3.0, 4) |
---|
132 | q2 = uniform(7.0, 20.0, 4) |
---|
133 | |
---|
134 | |
---|
135 | #Gradient of fitted pwl surface |
---|
136 | from util import gradient_python |
---|
137 | a_ref, b_ref = gradient(x0, y0, x1, y1, x2, y2, q0, q1, q2) |
---|
138 | |
---|
139 | #print a_ref, b_ref |
---|
140 | a, b = gradient_c(x0, y0, x1, y1, x2, y2, q0, q1, q2) |
---|
141 | |
---|
142 | #print a, a_ref, b, b_ref |
---|
143 | for i in range(4): |
---|
144 | assert abs(a[i] - a_ref[i]) < epsilon |
---|
145 | assert abs(b[i] - b_ref[i]) < epsilon |
---|
146 | |
---|
147 | |
---|
148 | |
---|
149 | #Geometric |
---|
150 | def test_distance(self): |
---|
151 | from util import distance |
---|
152 | |
---|
153 | self.failUnless( distance([4,2],[7,6]) == 5.0, |
---|
154 | 'Distance is wrong!') |
---|
155 | self.failUnless( allclose(distance([7,6],[9,8]), 2.82842712475), |
---|
156 | 'distance is wrong!') |
---|
157 | self.failUnless( allclose(distance([9,8],[4,2]), 7.81024967591), |
---|
158 | 'distance is wrong!') |
---|
159 | |
---|
160 | self.failUnless( distance([9,8],[4,2]) == distance([4,2],[9,8]), |
---|
161 | 'distance is wrong!') |
---|
162 | |
---|
163 | def test_angle(self): |
---|
164 | from util import angle |
---|
165 | |
---|
166 | assert allclose(angle([1.0, 1.0])/pi*180, 45.0) |
---|
167 | |
---|
168 | |
---|
169 | def test_anglediff(self): |
---|
170 | from util import anglediff |
---|
171 | |
---|
172 | assert allclose(anglediff([0.0, 1.], [1.0, 1.0])/pi*180, 45.0) |
---|
173 | |
---|
174 | |
---|
175 | |
---|
176 | |
---|
177 | #------------------------------------------------------------- |
---|
178 | if __name__ == "__main__": |
---|
179 | suite = unittest.makeSuite(TestCase,'test') |
---|
180 | runner = unittest.TextTestRunner() |
---|
181 | runner.run(suite) |
---|
182 | |
---|
183 | |
---|
184 | |
---|
185 | |
---|
186 | |
---|