1 | #!/usr/bin/env python |
---|
2 | |
---|
3 | import unittest |
---|
4 | |
---|
5 | import mesh |
---|
6 | |
---|
7 | |
---|
8 | class TestCase(unittest.TestCase): |
---|
9 | def setUp(self): |
---|
10 | pass |
---|
11 | |
---|
12 | def tearDown(self): |
---|
13 | pass |
---|
14 | |
---|
15 | def test_set_stuff(self): |
---|
16 | """ |
---|
17 | Documentation |
---|
18 | """ |
---|
19 | #making a test mesh |
---|
20 | p0=[0.,2.] |
---|
21 | p1=[1.,2.] |
---|
22 | p2=[0.,1.] |
---|
23 | p3=[1.,1.] |
---|
24 | p4=[0.,0.] |
---|
25 | p5=[2.,0.] |
---|
26 | p6=[-1.,1.] |
---|
27 | point_list = [p0,p1,p2,p3,p4,p5,p6] |
---|
28 | |
---|
29 | a0=[0] |
---|
30 | a1=[0] |
---|
31 | a2=[100] |
---|
32 | a3=[0] |
---|
33 | a4=[0] |
---|
34 | a5=[0] |
---|
35 | a6=[0] |
---|
36 | attribute=[a0,a1,a2,a3,a4,a5,a6] |
---|
37 | |
---|
38 | t0=[0,3,1] |
---|
39 | t1=[0,2,3] |
---|
40 | t2=[2,4,3] |
---|
41 | t3=[4,5,3] |
---|
42 | t4=[1,3,5] |
---|
43 | t5=[2,6,4] |
---|
44 | |
---|
45 | n0=[4,-1,2] |
---|
46 | n1=[2,0,-1] |
---|
47 | n2=[3,1,5] |
---|
48 | n3=[4,2,-1] |
---|
49 | n4=[3,-1,0] |
---|
50 | n5=[-1,2,-1] |
---|
51 | |
---|
52 | tri_list = [t0,t1,t2,t3,t4,t5] |
---|
53 | n_list = [n0,n1,n2,n3,n4,n5] |
---|
54 | for i in range(6): |
---|
55 | for j in (0,1,2): |
---|
56 | a=attribute[tri_list[i][j]] |
---|
57 | tri_list[i][j]=point_list[tri_list[i][j]] |
---|
58 | tri_list[i][j]=mesh.Vertex(tri_list[i][j][0]\ |
---|
59 | ,tri_list[i][j][1],a) |
---|
60 | neighbours=n_list[i] |
---|
61 | tri_list[i]=mesh.Triangle(tri_list[i][0],\ |
---|
62 | tri_list[i][1],tri_list[i][2]\ |
---|
63 | ,neighbors=neighbours) |
---|
64 | |
---|
65 | #testing selectAll |
---|
66 | Mesh = mesh.Mesh() |
---|
67 | Mesh.attributeTitles=['attrib'] |
---|
68 | |
---|
69 | Mesh.meshTriangles=tri_list |
---|
70 | |
---|
71 | Mesh.selectAllTriangles() |
---|
72 | A=Mesh.sets[Mesh.setID['All']] |
---|
73 | assert list_comp(tri_list,A) |
---|
74 | |
---|
75 | #testing threshold |
---|
76 | Mesh = mesh.Mesh() |
---|
77 | Mesh.attributeTitles=['attrib'] |
---|
78 | |
---|
79 | Mesh.meshTriangles=tri_list |
---|
80 | Mesh.selectAllTriangles() |
---|
81 | Mesh.threshold('All',min=30,max=35,attribute_name = 'attrib') |
---|
82 | A = [tri_list[1],tri_list[2],tri_list[5]] |
---|
83 | B = Mesh.sets[Mesh.setID['All']] |
---|
84 | assert list_comp(A,B) |
---|
85 | |
---|
86 | |
---|
87 | A = [tri_list[3],tri_list[2],tri_list[5]] |
---|
88 | assert not list_comp(A,B) |
---|
89 | |
---|
90 | #testing |
---|
91 | |
---|
92 | def test_Discretised_Tuple_Set_rounding(self): |
---|
93 | #This is the hardest bit of DST |
---|
94 | from mesh import Discretised_Tuple_Set |
---|
95 | |
---|
96 | tol = 0.1 |
---|
97 | a=Discretised_Tuple_Set(p_rel=1,t_rel= tol) |
---|
98 | m = 0.541 |
---|
99 | m_up = 0.6 |
---|
100 | m_down = 0.5 |
---|
101 | assert m_up == a.round_up_rel(m) |
---|
102 | assert m_down == a.round_down_rel(m) |
---|
103 | |
---|
104 | tol = 0.1 |
---|
105 | a=Discretised_Tuple_Set(p_rel=1,t_rel = tol) |
---|
106 | m = 0.539 |
---|
107 | m_up = 0.5 |
---|
108 | m_down = 0.5 |
---|
109 | assert m_up == a.round_up_rel(m) |
---|
110 | assert m_down == a.round_down_rel(m) |
---|
111 | |
---|
112 | tol = 0.5 |
---|
113 | a=Discretised_Tuple_Set(p_rel=1,t_rel = tol) |
---|
114 | |
---|
115 | |
---|
116 | m = 0.6 |
---|
117 | m_up = 0.7 |
---|
118 | m_down = 0.5 |
---|
119 | assert m_up == a.round_up_rel(m) |
---|
120 | assert m_down == a.round_down_rel(m) |
---|
121 | |
---|
122 | m = 0.599 |
---|
123 | m_up = 0.6 |
---|
124 | m_down = 0.5 |
---|
125 | assert m_up == a.round_up_rel(m) |
---|
126 | assert m_down == a.round_down_rel(m) |
---|
127 | |
---|
128 | def test_Discretised_Tuple_Set_tuple_rounding(self): |
---|
129 | from mesh import Discretised_Tuple_Set |
---|
130 | |
---|
131 | tol = 0.25 |
---|
132 | a=Discretised_Tuple_Set(p_rel=1,t_rel = tol) |
---|
133 | b = (1.,1.) |
---|
134 | list = [(1.,1.)] |
---|
135 | assert list_comp(list,a.rounded_keys(b)) |
---|
136 | |
---|
137 | b = (1.1,1.) |
---|
138 | list = [(1.2,1.),(1.,1.)] |
---|
139 | assert list_comp(list,a.rounded_keys(b)) |
---|
140 | |
---|
141 | b = (1.1,1.1) |
---|
142 | list = [(1.2,1.),(1.,1.),(1.,1.2),(1.2,1.2)] |
---|
143 | assert list_comp(list,a.rounded_keys(b)) |
---|
144 | |
---|
145 | |
---|
146 | def test_Discretised_Tuple_Set_set(self): |
---|
147 | from mesh import Discretised_Tuple_Set |
---|
148 | tol = 0.25 |
---|
149 | a=Discretised_Tuple_Set(p_rel=1,t_rel = tol) |
---|
150 | b = (1.1,1.1) |
---|
151 | a.append(b) |
---|
152 | list = [(1.2,1.),(1.,1.),(1.,1.2),(1.2,1.2)] |
---|
153 | assert list_comp(list,a.keys()) |
---|
154 | |
---|
155 | def test_Discretised_Tuple_Set_get(self): |
---|
156 | from mesh import Discretised_Tuple_Set |
---|
157 | tol = 0.25 |
---|
158 | a=Discretised_Tuple_Set(p_rel=1,t_rel = tol) |
---|
159 | b = (1.1,1.1) |
---|
160 | a.append(b) |
---|
161 | list = [(1.2,1.),(1.,1.),(1.,1.2),(1.2,1.2)] |
---|
162 | for key in list: |
---|
163 | assert a[key][0]==b |
---|
164 | assert len(a[key])==1 |
---|
165 | |
---|
166 | c = (2.1,1.) |
---|
167 | a.append(c) |
---|
168 | assert a[(2.,1.)][0]==c |
---|
169 | assert a[(2.2,1.)][0]==c |
---|
170 | |
---|
171 | def test_mapped_Discretised_Tuple_Set(self): |
---|
172 | |
---|
173 | from mesh import Mapped_Discretised_Tuple_Set |
---|
174 | from mesh import Discretised_Tuple_Set |
---|
175 | |
---|
176 | def map(sequence): |
---|
177 | return [len(sequence)] |
---|
178 | |
---|
179 | tol = 0.5 |
---|
180 | a=Mapped_Discretised_Tuple_Set(map,p_rel=1,t_rel = tol) |
---|
181 | b = range(20) |
---|
182 | a.append(b) |
---|
183 | assert b in a[range(17)] |
---|
184 | assert b in a[range(22)] |
---|
185 | |
---|
186 | tol = 0.01 |
---|
187 | a=Mapped_Discretised_Tuple_Set(map,p_rel=1,t_rel = tol) |
---|
188 | b = range(20) |
---|
189 | a.append(b) |
---|
190 | assert b in a[range(20)] |
---|
191 | assert b in a[range(19)] |
---|
192 | assert not range(17) in a |
---|
193 | |
---|
194 | |
---|
195 | |
---|
196 | |
---|
197 | """ |
---|
198 | print 'A' |
---|
199 | for triangle in A: |
---|
200 | print tri_list.index(triangle) |
---|
201 | print triangle |
---|
202 | print '' |
---|
203 | print 'B' |
---|
204 | for triangle in B: |
---|
205 | print tri_list.index(triangle) |
---|
206 | print triangle |
---|
207 | print '' |
---|
208 | """ |
---|
209 | |
---|
210 | |
---|
211 | |
---|
212 | def list_comp(A,B): |
---|
213 | yes = len(A)==len(B) |
---|
214 | #print "A" |
---|
215 | #for item in A: |
---|
216 | # print item |
---|
217 | #print "B" |
---|
218 | #for item in B: |
---|
219 | # print item |
---|
220 | for item in A: |
---|
221 | if not item in B: |
---|
222 | yes = False |
---|
223 | return yes |
---|
224 | |
---|
225 | #------------------------------------------------------------- |
---|
226 | if __name__ == "__main__": |
---|
227 | suite = unittest.makeSuite(TestCase,'test') |
---|
228 | runner = unittest.TextTestRunner() |
---|
229 | runner.run(suite) |
---|