Package boxmin :: Package tests :: Module test_eval
[hide private]

Source Code for Module boxmin.tests.test_eval

 1  #!/usr/bin/env python 
 2  # 
 3  # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
 4  #  test_eval.py 
 5  # 
 6  #  This program is in the public domain. 
 7  # 
 8  # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
 9   
10  """Unit tests for function callback via boxmin""" 
11   
12  import unittest 
13  import numpy 
14  import boxmin.tests.cfitfn_example  as cfitfn_example 
15  from boxmin import _boxmin 
16   
17 -def callback(vec):
18 # print "in callback, vec=",vec 19 return vec[0]+vec[1]+vec[2]
20
21 -def badreturn(vec):
22 return (1,2,3)
23
24 -def badsignature():
25 return 6.
26
27 -class TestPyfn(unittest.TestCase):
28 - def setUp(self):
29 self.p = numpy.asarray([1,2,3],'d')
30
31 - def test1(self):
32 """direct call to callback using an array""" 33 self.assertEqual(callback(self.p),6)
34
35 - def test2(self):
36 """direct call to callback using a Tuple""" 37 self.assertEqual(callback((1,2,3)),6)
38
39 - def test3(self):
40 """pyfn call to callback from C""" 41 self.assertEqual(_boxmin.eval(callback,self.p),6)
42
43 - def test4(self):
44 """pyfn call to callback from C with array that is too short""" 45 self.assertRaises(IndexError, _boxmin.eval, callback, self.p[0:1])
46
47 - def test5(self):
48 """pyfn callback with bad return type""" 49 self.assertRaises(TypeError, _boxmin.eval, badreturn, self.p)
50
51 - def test6(self):
52 """pyfn callback with bad signature""" 53 self.assertRaises(TypeError, _boxmin.eval, badsignature, self.p)
54
55 - def test7(self):
56 """pyfn call with insufficient arguments""" 57 self.assertRaises(TypeError, _boxmin.eval, callback)
58
59 - def test8(self):
60 """pyfn call with too many arguments""" 61 self.assertRaises(TypeError, _boxmin.eval, callback, self.p, 1)
62
63 - def test9(self):
64 """pyfn call with wrong tuple instead of callback""" 65 self.assertRaises(TypeError, _boxmin.eval, (callback, self.p))
66
67 - def test10(self):
68 """pyfn call with non-callable function""" 69 self.assertRaises(TypeError, _boxmin.eval, 1, self.p)
70
71 - def test11(self):
72 """pyfn call with list value""" 73 self.assertRaises(TypeError, _boxmin.eval, callback, [1,2,3])
74
75 - def test12(self):
76 """pyfn call with non-numeric value""" 77 self.assertRaises(TypeError, _boxmin.eval, callback, "hello")
78 79 80 81 if __name__ == "__main__": 82 unittest.main() 83