[1] | 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, z0, z1, z2) |
---|
| 25 | |
---|
| 26 | assert zx == -1.0 |
---|
| 27 | assert zy == 0.0 |
---|
| 28 | |
---|
| 29 | |
---|
| 30 | |
---|
| 31 | |
---|
| 32 | def test_gradient2(self): |
---|
| 33 | |
---|
| 34 | from util import gradient |
---|
| 35 | x0 = 2.0/3; y0 = 2.0/3 |
---|
| 36 | x1= 8.0/3; y1 = 2.0/3 |
---|
| 37 | x2 = 2.0/3; y2 = 8.0/3 |
---|
| 38 | |
---|
| 39 | q0 = 2.0+2.0/3 |
---|
| 40 | q1 = 8.0+2.0/3 |
---|
| 41 | q2 = 2.0+8.0/3 |
---|
| 42 | |
---|
| 43 | #Gradient of fitted pwl surface |
---|
| 44 | a, b = gradient(x0, y0, x1, y1, x2, y2, q0, q1, q2) |
---|
| 45 | |
---|
| 46 | assert abs(a - 3.0) < epsilon |
---|
| 47 | assert abs(b - 1.0) < epsilon |
---|
| 48 | |
---|
| 49 | |
---|
| 50 | |
---|
| 51 | def test_that_C_extension_compiles(self): |
---|
| 52 | FN = 'util_ext.c' |
---|
| 53 | try: |
---|
| 54 | import util_ext |
---|
| 55 | except: |
---|
| 56 | from compile import compile |
---|
| 57 | |
---|
| 58 | try: |
---|
| 59 | compile(FN) |
---|
| 60 | except: |
---|
| 61 | raise 'Could not compile %s' %FN |
---|
| 62 | else: |
---|
| 63 | import util_ext |
---|
| 64 | |
---|
| 65 | |
---|
| 66 | def test_gradient_C_extension(self): |
---|
| 67 | from util_ext import gradient as gradient_c |
---|
| 68 | |
---|
| 69 | x0 = 2.0/3; y0 = 2.0/3 |
---|
| 70 | x1= 8.0/3; y1 = 2.0/3 |
---|
| 71 | x2 = 2.0/3; y2 = 8.0/3 |
---|
| 72 | |
---|
| 73 | q0 = 2.0+2.0/3 |
---|
| 74 | q1 = 8.0+2.0/3 |
---|
| 75 | q2 = 2.0+8.0/3 |
---|
| 76 | |
---|
| 77 | #Gradient of fitted pwl surface |
---|
| 78 | a, b = gradient_c(x0, y0, x1, y1, x2, y2, q0, q1, q2) |
---|
| 79 | |
---|
| 80 | assert abs(a - 3.0) < epsilon |
---|
| 81 | assert abs(b - 1.0) < epsilon |
---|
| 82 | |
---|
| 83 | |
---|
| 84 | def test_gradient_C_extension3(self): |
---|
| 85 | from util_ext import gradient as gradient_c |
---|
| 86 | |
---|
| 87 | from RandomArray import uniform, seed |
---|
| 88 | seed(17, 53) |
---|
| 89 | |
---|
| 90 | x0, x1, x2, y0, y1, y2 = uniform(0.0,3.0,6) |
---|
| 91 | |
---|
| 92 | q0 = uniform(0.0, 10.0, 4) |
---|
| 93 | q1 = uniform(1.0, 3.0, 4) |
---|
| 94 | q2 = uniform(7.0, 20.0, 4) |
---|
| 95 | |
---|
| 96 | |
---|
| 97 | for i in range(4): |
---|
| 98 | #Gradient of fitted pwl surface |
---|
| 99 | from util import gradient_python |
---|
| 100 | a_ref, b_ref = gradient(x0, y0, x1, y1, x2, y2, |
---|
| 101 | q0[i], q1[i], q2[i]) |
---|
| 102 | |
---|
| 103 | #print a_ref, b_ref |
---|
| 104 | a, b = gradient_c(x0, y0, x1, y1, x2, y2, |
---|
| 105 | q0[i], q1[i], q2[i]) |
---|
| 106 | |
---|
| 107 | #print a, a_ref, b, b_ref |
---|
| 108 | assert abs(a - a_ref) < epsilon |
---|
| 109 | assert abs(b - b_ref) < epsilon |
---|
| 110 | |
---|
| 111 | |
---|
| 112 | |
---|
| 113 | #Geometric |
---|
| 114 | #def test_distance(self): |
---|
| 115 | # from util import distance# |
---|
| 116 | # |
---|
| 117 | # self.failUnless( distance([4,2],[7,6]) == 5.0, |
---|
| 118 | # 'Distance is wrong!') |
---|
| 119 | # self.failUnless( allclose(distance([7,6],[9,8]), 2.82842712475), |
---|
| 120 | # 'distance is wrong!') |
---|
| 121 | # self.failUnless( allclose(distance([9,8],[4,2]), 7.81024967591), |
---|
| 122 | # 'distance is wrong!') |
---|
| 123 | # |
---|
| 124 | # self.failUnless( distance([9,8],[4,2]) == distance([4,2],[9,8]), |
---|
| 125 | # 'distance is wrong!') |
---|
| 126 | |
---|
| 127 | def test_angle(self): |
---|
| 128 | from util import angle |
---|
| 129 | |
---|
| 130 | assert allclose(angle([1.0, 1.0])/pi*180, 45.0) |
---|
| 131 | |
---|
| 132 | |
---|
| 133 | def test_anglediff(self): |
---|
| 134 | from util import anglediff |
---|
| 135 | |
---|
| 136 | assert allclose(anglediff([0.0, 1.], [1.0, 1.0])/pi*180, 45.0) |
---|
| 137 | |
---|
| 138 | |
---|
| 139 | |
---|
| 140 | |
---|
| 141 | #------------------------------------------------------------- |
---|
| 142 | if __name__ == "__main__": |
---|
| 143 | suite = unittest.makeSuite(TestCase,'test') |
---|
| 144 | runner = unittest.TextTestRunner() |
---|
| 145 | runner.run(suite) |
---|
| 146 | |
---|
| 147 | |
---|
| 148 | |
---|
| 149 | |
---|
| 150 | |
---|