1 | """ |
---|
2 | Unit tests for Invertor class |
---|
3 | """ |
---|
4 | # Disable "missing docstring" complaint |
---|
5 | # pylint: disable-msg=C0111 |
---|
6 | # Disable "too many methods" complaint |
---|
7 | # pylint: disable-msg=R0904 |
---|
8 | |
---|
9 | |
---|
10 | import unittest, math, numpy |
---|
11 | from sans.pr.invertor import Invertor |
---|
12 | |
---|
13 | class TestBasicComponent(unittest.TestCase): |
---|
14 | |
---|
15 | def setUp(self): |
---|
16 | self.invertor = Invertor() |
---|
17 | self.invertor.d_max = 100.0 |
---|
18 | |
---|
19 | # Test array |
---|
20 | self.ntest = 5 |
---|
21 | self.x_in = numpy.ones(self.ntest) |
---|
22 | for i in range(self.ntest): |
---|
23 | self.x_in[i] = 1.0*i |
---|
24 | |
---|
25 | |
---|
26 | def testset_dmax(self): |
---|
27 | """ |
---|
28 | Set and read d_max |
---|
29 | """ |
---|
30 | value = 15.0 |
---|
31 | self.invertor.d_max = value |
---|
32 | self.assertEqual(self.invertor.d_max, value) |
---|
33 | |
---|
34 | def testset_x_1(self): |
---|
35 | """ |
---|
36 | Setting and reading the x array the hard way |
---|
37 | """ |
---|
38 | # Set x |
---|
39 | self.invertor.x = self.x_in |
---|
40 | |
---|
41 | # Read it back |
---|
42 | npts = self.invertor.get_nx() |
---|
43 | x_out = numpy.ones(npts) |
---|
44 | |
---|
45 | self.invertor.get_x(x_out) |
---|
46 | |
---|
47 | for i in range(self.ntest): |
---|
48 | self.assertEqual(self.x_in[i], x_out[i]) |
---|
49 | |
---|
50 | def testset_x_2(self): |
---|
51 | """ |
---|
52 | Setting and reading the x array the easy way |
---|
53 | """ |
---|
54 | # Set x |
---|
55 | self.invertor.x = self.x_in |
---|
56 | |
---|
57 | # Read it back |
---|
58 | x_out = self.invertor.x |
---|
59 | |
---|
60 | for i in range(self.ntest): |
---|
61 | self.assertEqual(self.x_in[i], x_out[i]) |
---|
62 | |
---|
63 | def testset_y(self): |
---|
64 | """ |
---|
65 | Setting and reading the y array the easy way |
---|
66 | """ |
---|
67 | # Set y |
---|
68 | self.invertor.y = self.x_in |
---|
69 | |
---|
70 | # Read it back |
---|
71 | y_out = self.invertor.y |
---|
72 | |
---|
73 | for i in range(self.ntest): |
---|
74 | self.assertEqual(self.x_in[i], y_out[i]) |
---|
75 | |
---|
76 | def testset_err(self): |
---|
77 | """ |
---|
78 | Setting and reading the err array the easy way |
---|
79 | """ |
---|
80 | # Set err |
---|
81 | self.invertor.err = self.x_in |
---|
82 | |
---|
83 | # Read it back |
---|
84 | err_out = self.invertor.err |
---|
85 | |
---|
86 | for i in range(self.ntest): |
---|
87 | self.assertEqual(self.x_in[i], err_out[i]) |
---|
88 | |
---|
89 | def test_iq(self): |
---|
90 | """ |
---|
91 | Test iq calculation |
---|
92 | """ |
---|
93 | q = 0.11 |
---|
94 | v1 = 8.0*math.pi**2/q * self.invertor.d_max *math.sin(q*self.invertor.d_max) |
---|
95 | v1 /= ( math.pi**2 - (q*self.invertor.d_max)**2.0 ) |
---|
96 | |
---|
97 | pars = numpy.ones(1) |
---|
98 | self.assertAlmostEqual(self.invertor.iq(pars, q), v1, 2) |
---|
99 | |
---|
100 | def test_pr(self): |
---|
101 | """ |
---|
102 | Test pr calculation |
---|
103 | """ |
---|
104 | r = 10.0 |
---|
105 | v1 = 2.0*r*math.sin(math.pi*r/self.invertor.d_max) |
---|
106 | pars = numpy.ones(1) |
---|
107 | self.assertAlmostEqual(self.invertor.pr(pars, r), v1, 2) |
---|
108 | |
---|
109 | def test_getsetters(self): |
---|
110 | self.invertor.new_data = 1.0 |
---|
111 | self.assertEqual(self.invertor.new_data, 1.0) |
---|
112 | |
---|
113 | self.assertEqual(self.invertor.test_no_data, None) |
---|
114 | |
---|
115 | if __name__ == '__main__': |
---|
116 | unittest.main() |
---|