#!/usr/bin/env python # import unittest from Numeric import zeros, array, allclose from math import sqrt, pi from util import * from config import epsilon class TestCase(unittest.TestCase): def setUp(self): pass def tearDown(self): pass def test_gradient(self): x0 = 0.0; y0 = 0.0; z0 = 0.0 x1 = 1.0; y1 = 0.0; z1 = -1.0 x2 = 0.0; y2 = 1.0; z2 = 0.0 zx, zy = gradient(x0, y0, x1, y1, x2, y2, z0, z1, z2) assert zx == -1.0 assert zy == 0.0 def test_gradient2(self): from util import gradient x0 = 2.0/3; y0 = 2.0/3 x1= 8.0/3; y1 = 2.0/3 x2 = 2.0/3; y2 = 8.0/3 q0 = 2.0+2.0/3 q1 = 8.0+2.0/3 q2 = 2.0+8.0/3 #Gradient of fitted pwl surface a, b = gradient(x0, y0, x1, y1, x2, y2, q0, q1, q2) assert abs(a - 3.0) < epsilon assert abs(b - 1.0) < epsilon def test_that_C_extension_compiles(self): FN = 'util_ext.c' try: import util_ext except: from compile import compile try: compile(FN) except: raise 'Could not compile %s' %FN else: import util_ext def test_gradient_C_extension(self): from util_ext import gradient as gradient_c x0 = 2.0/3; y0 = 2.0/3 x1= 8.0/3; y1 = 2.0/3 x2 = 2.0/3; y2 = 8.0/3 q0 = 2.0+2.0/3 q1 = 8.0+2.0/3 q2 = 2.0+8.0/3 #Gradient of fitted pwl surface a, b = gradient_c(x0, y0, x1, y1, x2, y2, q0, q1, q2) assert abs(a - 3.0) < epsilon assert abs(b - 1.0) < epsilon def test_gradient_C_extension3(self): from util_ext import gradient as gradient_c from RandomArray import uniform, seed seed(17, 53) x0, x1, x2, y0, y1, y2 = uniform(0.0,3.0,6) q0 = uniform(0.0, 10.0, 4) q1 = uniform(1.0, 3.0, 4) q2 = uniform(7.0, 20.0, 4) for i in range(4): #Gradient of fitted pwl surface from util import gradient_python a_ref, b_ref = gradient(x0, y0, x1, y1, x2, y2, q0[i], q1[i], q2[i]) #print a_ref, b_ref a, b = gradient_c(x0, y0, x1, y1, x2, y2, q0[i], q1[i], q2[i]) #print a, a_ref, b, b_ref assert abs(a - a_ref) < epsilon assert abs(b - b_ref) < epsilon #Geometric #def test_distance(self): # from util import distance# # # self.failUnless( distance([4,2],[7,6]) == 5.0, # 'Distance is wrong!') # self.failUnless( allclose(distance([7,6],[9,8]), 2.82842712475), # 'distance is wrong!') # self.failUnless( allclose(distance([9,8],[4,2]), 7.81024967591), # 'distance is wrong!') # # self.failUnless( distance([9,8],[4,2]) == distance([4,2],[9,8]), # 'distance is wrong!') def test_angle(self): from util import angle assert allclose(angle([1.0, 1.0])/pi*180, 45.0) def test_anglediff(self): from util import anglediff assert allclose(anglediff([0.0, 1.], [1.0, 1.0])/pi*180, 45.0) #------------------------------------------------------------- if __name__ == "__main__": suite = unittest.makeSuite(TestCase,'test') runner = unittest.TextTestRunner() runner.run(suite)